Search in sources :

Example 1 with MarketMetric

use of com.salaboy.model.MarketMetric 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)

Example 2 with MarketMetric

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

the class RulesHelper method evaluate.

public static boolean evaluate(Car car) {
    if (cachedResults.containsKey(car.getName())) {
        return cachedResults.get(car.getName());
    }
    StatelessKnowledgeSession session = createStatelessSession("good_old_patterns/car-sell-or-drop-complex-decision.drl");
    List list = new ArrayList();
    list.add(car);
    // Get external information that you don't want to keep in the process, for example:
    //   - Call an external service to get information about the stock market
    //   - Check the average price for this type of cars in ebay
    //   - Check for the competitors prices in the last two months
    // We can get a metric about the current status of the market
    // when the process is being executed and use that to influence our decision
    MarketMetric metric = new MarketMetric(0.8);
    list.add(metric);
    session.execute(list);
    cachedResults.put(car.getName(), metric.getResult());
    return metric.getResult();
}
Also used : MarketMetric(com.salaboy.model.MarketMetric) ArrayList(java.util.ArrayList) StatelessKnowledgeSession(org.drools.runtime.StatelessKnowledgeSession) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

MarketMetric (com.salaboy.model.MarketMetric)2 Car (com.salaboy.model.Car)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 KnowledgeBase (org.drools.KnowledgeBase)1 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)1 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)1 ClassPathResource (org.drools.io.impl.ClassPathResource)1 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)1 StatelessKnowledgeSession (org.drools.runtime.StatelessKnowledgeSession)1