Search in sources :

Example 36 with KieHelper

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());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 37 with KieHelper

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());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 38 with KieHelper

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());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 39 with KieHelper

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"));
    }
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 40 with KieHelper

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());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Results(org.kie.api.builder.Results) KieHelper(org.kie.internal.utils.KieHelper) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18