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);
}
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();
}
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();
}
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");
}
}
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());
}
Aggregations