use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class StrictAnnotationTest method testJavaSqlTimestamp.
@Test
public void testJavaSqlTimestamp() {
String str = "package " + Message.class.getPackage().getName() + "\n" + "@Role( Role.Type.EVENT ) @Timestamp( \"startTime\" ) @Duration( \"duration\" )\n" + "declare " + Message.class.getCanonicalName() + "\n" + "end\n";
KieSession ksession = new KieHelper().setKieModuleModel(KieServices.Factory.get().newKieModuleModel().setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())).addContent(str, ResourceType.DRL).build().newKieSession();
Message msg = new Message();
msg.setStartTime(new Timestamp(10000));
msg.setDuration(1000l);
EventFactHandle efh = (EventFactHandle) ksession.insert(msg);
assertEquals(10000, efh.getStartTimestamp());
assertEquals(1000, efh.getDuration());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class StrictAnnotationTest method testWatch.
@Test
public void testWatch() throws Exception {
String str = "package com.sample;\n" + "import " + MyClass.class.getCanonicalName() + ";\n" + "rule R1 when\n" + " @Watch( \"!value\" ) $m : MyClass( value < 10 )\n" + "then \n" + " modify( $m ) { setValue( $m.getValue()+1 ) };\n" + "end\n";
KieSession ksession = new KieHelper().setKieModuleModel(KieServices.Factory.get().newKieModuleModel().setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())).addContent(str, ResourceType.DRL).build().newKieSession();
MyClass myClass = new MyClass("test", 1);
ksession.insert(myClass);
ksession.fireAllRules();
assertEquals(2, myClass.getValue());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class FromTest method testFromSharing.
@Test
public void testFromSharing() {
// Keeping original test as non-property reactive by default, just allowed.
String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + " ListsContainer( $list : list1 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output1.add($s);\n" + "end\n" + "rule R2 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n" + "rule R3 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n";
KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(kbase);
List<String> output1 = new ArrayList<String>();
ksession.setGlobal("output1", output1);
List<String> output2 = new ArrayList<String>();
ksession.setGlobal("output2", output2);
FactHandle fh = ksession.insert(new ListsContainer());
ksession.fireAllRules();
assertEquals("bb", output1.get(0));
assertEquals("22", output2.get(0));
assertEquals("22", output2.get(1));
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
// There is only 1 LIA
assertEquals(1, otn.getObjectSinkPropagator().size());
LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
// There are only 2 FromNodes since R2 and R3 are sharing the second From
LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
assertEquals(2, sinks.length);
// The first from has R1 has sink
assertEquals(1, sinks[0].getSinkPropagator().size());
// The second from has both R2 and R3 as sinks
assertEquals(2, sinks[1].getSinkPropagator().size());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class QueryTest method testQueryWithAccessorAsArgument.
@Test
public void testQueryWithAccessorAsArgument() throws Exception {
// DROOLS-414
String str = "import org.drools.compiler.Person\n" + "global java.util.List persons;\n" + "\n" + "query contains(String $s, String $c)\n" + " $s := String( this.contains( $c ) )\n" + "end\n" + "\n" + "rule R when\n" + " $p : Person()\n" + " contains( $p.name, \"a\"; )\n" + "then\n" + " persons.add( $p );\n" + "end\n";
KieHelper helper = new KieHelper();
helper.addContent(str, ResourceType.DRL);
KieSession ksession = helper.build().newKieSession();
List<Person> personsWithA = new ArrayList<Person>();
ksession.setGlobal("persons", personsWithA);
ksession.insert("Mark");
ksession.insert("Edson");
ksession.insert("Mario");
ksession.insert(new Person("Mark"));
ksession.insert(new Person("Edson"));
ksession.insert(new Person("Mario"));
ksession.fireAllRules();
assertEquals(2, personsWithA.size());
for (Person p : personsWithA) {
assertTrue(p.getName().equals("Mark") || p.getName().equals("Mario"));
}
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class QueryTest method testQueryWithSyntaxError.
@Test
public void testQueryWithSyntaxError() {
String drl = "global java.util.List list; " + "" + "query foo( Integer $i ) end " + "" + "rule React \n" + "when\n" + " $i : Integer() " + // missing ";" should result in 1 compilation error
" foo( $i ) " + "then\n" + "end";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Results results = helper.verify();
assertTrue(results.hasMessages(Message.Level.ERROR));
assertEquals(1, results.getMessages(Message.Level.ERROR).size());
}
Aggregations