use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testKieBuilderWithClassLoader.
@Test
public void testKieBuilderWithClassLoader() {
// DROOLS-763
String drl = "import com.billasurf.Person\n" + "\n" + "global java.util.List list\n" + "\n" + "rule R1 when\n" + " $i : Integer()\n" + "then\n" + " Person p = new Person();\n" + " p.setAge($i);\n" + " insert(p);\n" + "end\n" + "\n" + "rule R2 when\n" + " $p : Person()\n" + "then\n" + " list.add($p.getAge());\n" + "end\n";
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { this.getClass().getResource("/billasurf.jar") });
KieSession ksession = new KieHelper().setClassLoader(urlClassLoader).addContent(drl, ResourceType.DRL).build().newKieSession();
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal("list", list);
ksession.insert(18);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(18, (int) list.get(0));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testClassAccumulator.
@Test
public void testClassAccumulator() {
// DROOLS-626
String drl = "global java.util.List list\n" + "declare InitClass\n" + " clazz: Class\n" + "end\n" + "\n" + "rule \"init\" when\n" + "then\n" + " insert( new InitClass( String.class ) );\n" + " insert( new InitClass( Integer.class ) );\n" + "end\n" + "\n" + "rule \"make init classes\"\n" + "when\n" + " accumulate( InitClass( $clazz; ), $classes: collectList( $clazz ) )\n" + "then\n" + " list.addAll($classes);\n" + "end ";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
List<Class> list = new ArrayList<Class>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(2, list.size());
System.out.println(list);
assertTrue(list.containsAll(asList(String.class, Integer.class)));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testNotSubnetwork.
@Test
public void testNotSubnetwork() {
// DROOLS-623
String drl = "import " + TypeCC.class.getCanonicalName() + ";\n" + "import " + TypeDD.class.getCanonicalName() + ";\n" + " \n" + "rule R1 \n" + "when \n" + " $dd : TypeDD( value < 1 )\n" + "then \n" + " System.out.println(\"Rule R1 Fired\");\n" + " modify($dd) { setValue(1); }\n" + "end \n" + " \n" + "rule R2 when \n" + " String( ) \n" + " $cc : TypeCC( value < 1 )\n" + " not( \n" + " $cc_not : TypeCC( ) \n" + " and \n" + " $dd_not : TypeDD( value==0 ) \n" + " ) \n" + "then \n" + " System.out.println(\"Rule R2 Fired\");\n" + " modify($cc) { setValue($cc.getValue()+1); }\n" + "end; ";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.insert("1");
ksession.insert("2");
TypeCC cc = new TypeCC();
ksession.insert(cc);
ksession.insert(new TypeDD());
ksession.fireAllRules();
System.out.println("Rule R2 is fired count - " + cc.getValue());
assertEquals("Rule 2 should be fired once as we have firing rule as one of criteria checking rule only fire once", 1, cc.getValue());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testFromEPDontRequireLeftInput.
@Test
public void testFromEPDontRequireLeftInput() {
// DROOLS-1014
String drl = "rule R when\n" + " $s1 : String() from entry-point \"xxx\"\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
if (String.class == otnType) {
assertEquals(1, otn.getObjectSinkPropagator().size());
} else if (InitialFact.class.isAssignableFrom(otnType)) {
assertEquals(0, otn.getObjectSinkPropagator().size());
} else {
fail("There shouldn't be other OTNs");
}
}
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testSubnetworkAccumulate.
@Test
public void testSubnetworkAccumulate() {
String drl = "import " + StringWrapper.class.getCanonicalName() + ";\n" + "global StringBuilder sb;" + "rule R when\n" + " $s : String()\n" + " Number( $i : intValue ) from accumulate ($sw : StringWrapper( $value : value ) " + " and eval( $sw.contains($s) ), " + " sum($value) )\n" + "then\n" + " sb.append($i);\n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
StringBuilder sb = new StringBuilder();
ksession.setGlobal("sb", sb);
ksession.insert("test");
StringWrapper sw = new StringWrapper();
FactHandle swFH = ksession.insert(sw);
ksession.fireAllRules();
sw.setWrapped("test");
ksession.update(swFH, sw);
ksession.fireAllRules();
sw.setWrapped(null);
ksession.update(swFH, sw);
ksession.fireAllRules();
sw.setWrapped("test");
ksession.update(swFH, sw);
ksession.fireAllRules();
sw.setWrapped(null);
ksession.update(swFH, sw);
ksession.fireAllRules();
sw.setWrapped("test");
ksession.update(swFH, sw);
ksession.fireAllRules();
assertEquals("040404", sb.toString());
}
Aggregations