Search in sources :

Example 36 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class PMMLUsageDemoTest method invokePmmlWithTrait.

@Test
public void invokePmmlWithTrait() {
    String extraDrl = "package org.kie.pmml.pmml_4_2.test;" + "" + "import org.drools.core.factmodel.traits.Entity;" + "" + "rule \"Init\" " + "when " + "   $s : String( this == \"trigger\" ) " + "then " + "   System.out.println( \"Trig\" ); " + "   Entity o = new Entity(); " + "   insert( o ); \n" + "" + // both soft and hard fields will be used to feed data into the model
    "" + "   MockColdTrait input = don( o, MockColdTrait.class ); " + "   modify( input ) { " + "       setTemp( 22.0 );" + "   } " + "end " + "" + "" + "rule Log when $x : MockColdTrait() then System.out.println( \"IN \" + $x ); end " + "rule Log2 when $x : Cold() then System.out.println( \"OUT \" + $x ); end ";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newClassPathResource(pmmlSource).setResourceType(ResourceType.PMML));
    kfs.write(ResourceFactory.newByteArrayResource(extraDrl.getBytes()).setSourcePath("rules.drl").setResourceType(ResourceType.DRL));
    Results res = ks.newKieBuilder(kfs).buildAll().getResults();
    if (res.hasMessages(Message.Level.ERROR)) {
        System.out.println(res.getMessages(Message.Level.ERROR));
    }
    assertEquals(0, res.getMessages(Message.Level.ERROR).size());
    KieSession kSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    kSession.insert("trigger");
    kSession.fireAllRules();
    System.out.println(reportWMObjects(kSession));
    QueryResults qrs = kSession.getQueryResults("Cold", "MockCold", Variable.v);
    assertTrue(qrs.iterator().hasNext());
    Object val = qrs.iterator().next().get("$result");
    Assert.assertEquals(0.56, val);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) QueryResults(org.kie.api.runtime.rule.QueryResults) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 37 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class NaiveBayesTest method testNaiveBayesWithGaussianDistr.

@Test
public void testNaiveBayesWithGaussianDistr() throws Exception {
    KieSession kieSession = getModelSession(source2, VERBOSE);
    setKSession(kieSession);
    // init model
    kieSession.fireAllRules();
    kieSession.getEntryPoint("in_Gender").insert("male");
    kieSession.getEntryPoint("in_AgeOfIndividual").insert(24.0);
    kieSession.getEntryPoint("in_NoOfClaims").insert("2");
    kieSession.getEntryPoint("in_AgeOfCar").insert(1.0);
    kieSession.fireAllRules();
    System.out.println(reportWMObjects(kieSession));
    QueryResults q1 = kieSession.getQueryResults("ProbabilityOf1000", "NaiveBayesInsurance", Variable.v);
    assertEquals(1, q1.size());
    Object a1 = q1.iterator().next().get("$result");
    assertTrue(a1 instanceof Double);
    assertEquals(0.112, (Double) a1, 4);
    QueryResults q2 = kieSession.getQueryResults("ChosenClass", "NaiveBayesInsurance", Variable.v);
    assertEquals(1, q2.size());
    Object a2 = q2.iterator().next().get("$result");
    assertTrue(a2 instanceof Integer);
    assertEquals(100, a2);
    checkGeneratedRules();
}
Also used : KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 38 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class NaiveBayesTest method testNaiveBayes.

@Test
public void testNaiveBayes() throws Exception {
    KieSession kieSession = getModelSession(source1, VERBOSE);
    setKSession(kieSession);
    // init model
    kieSession.fireAllRules();
    kieSession.getEntryPoint("in_Gender").insert("male");
    kieSession.getEntryPoint("in_NoOfClaims").insert("2");
    kieSession.getEntryPoint("in_AgeOfCar").insert(1.0);
    kieSession.fireAllRules();
    QueryResults q1 = kieSession.getQueryResults("ProbabilityOf500", "NaiveBayesInsurance", Variable.v);
    assertEquals(1, q1.size());
    Object a1 = q1.iterator().next().get("$result");
    assertTrue(a1 instanceof Double);
    assertEquals(0.034, (Double) a1, 4);
    QueryResults q2 = kieSession.getQueryResults("ChosenClass", "NaiveBayesInsurance", Variable.v);
    assertEquals(1, q2.size());
    Object a2 = q2.iterator().next().get("$result");
    assertTrue(a2 instanceof Integer);
    assertEquals(100, a2);
    checkGeneratedRules();
}
Also used : KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 39 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project jbpm by kiegroup.

the class BusinessRuleAssignmentStrategy method apply.

@SuppressWarnings("unchecked")
@Override
public Assignment apply(Task task, TaskContext context, String excludedUser) {
    if (!active) {
        logger.debug("{} strategy is not active", this);
        return null;
    }
    logger.debug("Using rules to assign actual owner to task {}", task);
    KieSession kieSession = this.kieContainer.newKieSession();
    try {
        context.loadTaskVariables(task);
        kieSession.insert(task);
        kieSession.fireAllRules();
        Set<Assignment> assignments = new TreeSet<>();
        String queryName = System.getProperty("org.jbpm.task.assignment.rules.query");
        if (queryName != null) {
            logger.debug("Query {} is going to be used to retrieve results from working memory", queryName);
            QueryResults results = kieSession.getQueryResults(queryName);
            results.forEach(row -> assignments.add((Assignment) row.get("assignment")));
        } else {
            logger.debug("No query defined, retrieving all facts of type Assignment");
            Collection<Assignment> results = (Collection<Assignment>) kieSession.getObjects(new ClassObjectFilter(Assignment.class));
            assignments.addAll(results);
        }
        logger.debug("Rule evaluation completed with selected assignments of {}", assignments);
        if (assignments.isEmpty()) {
            logger.debug("No assignments found by BusinessRule strategy");
            return null;
        }
        Assignment selected = assignments.iterator().next();
        logger.debug("Selected assignment is {} for task {}", selected, task);
        return selected;
    } finally {
        kieSession.dispose();
        logger.debug("KieSession in BusinessRule disposed");
    }
}
Also used : Assignment(org.kie.internal.task.api.assignment.Assignment) ClassObjectFilter(org.drools.core.ClassObjectFilter) TreeSet(java.util.TreeSet) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults)

Example 40 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class TraitTest method testTypeRefractionOnQueryWithIsA.

@Test(timeout = 10000)
public void testTypeRefractionOnQueryWithIsA() {
    String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.core.factmodel.traits.Thing \n" + "import org.drools.core.factmodel.traits.Traitable \n" + "\n" + "global java.util.List list; \n" + "\n" + "" + "declare trait A @propertyReactive end\n" + "declare trait B extends A @propertyReactive end\n" + "declare trait C extends B @propertyReactive end\n" + "declare trait D extends A @propertyReactive end\n" + "declare trait E extends C, D @propertyReactive end\n" + "declare trait F extends E @propertyReactive end\n" + "" + "declare Kore\n" + "   @Traitable\n" + "end\n" + "" + "rule Init when\n" + "then\n" + "   Kore k = new Kore();\n" + "   don( k, C.class ); \n" + "   don( k, D.class ); \n" + "   don( k, E.class ); \n" + "   don( k, B.class ); \n" + "   don( k, A.class ); \n" + "   don( k, F.class ); \n" + "   shed( k, B.class ); \n" + "end\n" + "" + " \n" + "query queryA\n" + "   $x := Kore( this isA A ) \n" + "end\n" + "";
    KieSession ks = getSessionFromString(source);
    TraitFactory.setMode(mode, ks.getKieBase());
    List list = new ArrayList();
    ks.setGlobal("list", list);
    ks.fireAllRules();
    QueryResults res = ks.getQueryResults("queryA");
    Iterator<QueryResultsRow> iter = res.iterator();
    Object a = iter.next().get("$x");
    assertFalse(iter.hasNext());
    assertEquals(1, res.size());
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)80 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)59 ArrayList (java.util.ArrayList)30 KieBase (org.kie.api.KieBase)25 List (java.util.List)24 Person (org.drools.modelcompiler.domain.Person)19 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 FactHandle (org.kie.api.runtime.rule.FactHandle)15 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)10 Collection (java.util.Collection)9 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 Map (java.util.Map)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 InternationalAddress (org.drools.modelcompiler.oopathdtables.InternationalAddress)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)6 Global (org.drools.model.Global)5