Search in sources :

Example 1 with Car

use of com.salaboy.model.Car in project jBPM5-Developer-Guide by Salaboy.

the class GoodOldIntegrationPatterns method statelessGatewayCallTest.

@Test
public void statelessGatewayCallTest() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("good_old_patterns/process-stateless-rule-evaluation-rules-gateway.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.getWorkItemManager().registerWorkItemHandler("Rank Car", new RankCarWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Define Car Price", new DefineCarPriceWorkItemHandler());
    Car car = new Car("AUDI 78", new Date(), 5, "manual", "gas", 285, 25000);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("car", car);
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.stateless-rules-decoration", params);
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    ksession.startProcessInstance(processInstance.getId());
    assertEquals(new Double(17500), new Double(car.getCurrentPrice()));
    assertEquals(6, car.getRanking());
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) DefineCarPriceWorkItemHandler(com.salaboy.jbpm5.DefineCarPriceWorkItemHandler) ClassPathResource(org.drools.io.impl.ClassPathResource) Date(java.util.Date) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) Car(com.salaboy.model.Car) ProcessInstance(org.drools.runtime.process.ProcessInstance) RankCarWorkItemHandler(com.salaboy.jbpm5.RankCarWorkItemHandler)

Example 2 with Car

use of com.salaboy.model.Car in project jBPM5-Developer-Guide by Salaboy.

the class GoodOldIntegrationPatterns method statelessDecisionTest.

@Test
public void statelessDecisionTest() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("good_old_patterns/process-stateless-rule-evaluation-java-gateway.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.getWorkItemManager().registerWorkItemHandler("Rank Car", new RankCarWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Define Car Price", new DefineCarPriceWorkItemHandler());
    Car car = new Car("AUDI 78", new Date(), 5, "manual", "gas", 285, 25000);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("car", car);
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.stateless-rules-decoration", params);
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    ksession.startProcessInstance(processInstance.getId());
    System.out.println("Car : " + car);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) DefineCarPriceWorkItemHandler(com.salaboy.jbpm5.DefineCarPriceWorkItemHandler) ClassPathResource(org.drools.io.impl.ClassPathResource) Date(java.util.Date) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) Car(com.salaboy.model.Car) ProcessInstance(org.drools.runtime.process.ProcessInstance) RankCarWorkItemHandler(com.salaboy.jbpm5.RankCarWorkItemHandler)

Example 3 with Car

use of com.salaboy.model.Car in project jBPM5-Developer-Guide by Salaboy.

the class DefineCarPriceWorkItemHandler method executeWorkItem.

public void executeWorkItem(WorkItem wi, WorkItemManager wim) {
    Car car = (Car) wi.getParameter("carInput");
    System.out.println("Defining Car Price" + car);
    StatelessKnowledgeSession session = createStatelessSession();
    session.execute(car);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("carOutput", car);
    wim.completeWorkItem(wi.getId(), params);
}
Also used : Car(com.salaboy.model.Car) HashMap(java.util.HashMap) StatelessKnowledgeSession(org.drools.runtime.StatelessKnowledgeSession)

Example 4 with Car

use of com.salaboy.model.Car in project jBPM5-Developer-Guide by Salaboy.

the class RankCarWorkItemHandler method executeWorkItem.

public void executeWorkItem(WorkItem wi, WorkItemManager wim) {
    Car car = (Car) wi.getParameter("carInput");
    System.out.println("Ranking Car" + car);
    StatelessKnowledgeSession session = createStatelessSession();
    session.execute(car);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("carOutput", car);
    wim.completeWorkItem(wi.getId(), params);
}
Also used : Car(com.salaboy.model.Car) HashMap(java.util.HashMap) StatelessKnowledgeSession(org.drools.runtime.StatelessKnowledgeSession)

Example 5 with Car

use of com.salaboy.model.Car in project jBPM5-Developer-Guide by Salaboy.

the class GoodOldIntegrationPatterns method theRuleEngineWay.

@Test
public void theRuleEngineWay() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("good_old_patterns/car-evaluations.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    //KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    Car car = new Car("AUDI 78", new Date(), 5, "manual", "gas", 285, 25000);
    MarketMetric metric = new MarketMetric(0.8);
    ksession.insert(metric);
    ksession.insert(car);
    ksession.fireAllRules();
    assertEquals(new Double(17500), new Double(car.getCurrentPrice()));
    assertEquals(6, car.getRanking());
    assertTrue(metric.getResult());
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) Car(com.salaboy.model.Car) MarketMetric(com.salaboy.model.MarketMetric) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ClassPathResource(org.drools.io.impl.ClassPathResource) Date(java.util.Date)

Aggregations

Car (com.salaboy.model.Car)6 HashMap (java.util.HashMap)5 Date (java.util.Date)4 KnowledgeBase (org.drools.KnowledgeBase)4 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)4 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)4 ClassPathResource (org.drools.io.impl.ClassPathResource)4 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)4 DefineCarPriceWorkItemHandler (com.salaboy.jbpm5.DefineCarPriceWorkItemHandler)3 RankCarWorkItemHandler (com.salaboy.jbpm5.RankCarWorkItemHandler)3 ProcessInstance (org.drools.runtime.process.ProcessInstance)3 StatelessKnowledgeSession (org.drools.runtime.StatelessKnowledgeSession)2 MarketMetric (com.salaboy.model.MarketMetric)1