Search in sources :

Example 46 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class MarshallingTest method testMarshallWithTimedRule.

@Test
public void testMarshallWithTimedRule() {
    // DROOLS-795
    String drl = "rule \"Rule A Timeout\"\n" + "when\n" + "    String( this == \"ATrigger\" )\n" + "then\n" + "   insert (new String( \"A-Timer\") );\n" + "end\n" + "\n" + "rule \"Timer For rule A Timeout\"\n" + "    timer ( int: 5s )\n" + "when\n" + "   String( this == \"A-Timer\")\n" + "then\n" + "   delete ( \"A-Timer\" );\n" + "   delete ( \"ATrigger\" );\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(EqualityBehaviorOption.EQUALITY, DeclarativeAgendaOption.ENABLED, EventProcessingOption.STREAM);
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ksession.insert("ATrigger");
    assertEquals(1, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(4, TimeUnit.SECONDS);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    ksession = marshallAndUnmarshall(kbase, ksession, sessionConfig);
    clock = ksession.getSessionClock();
    clock.advanceTime(4, TimeUnit.SECONDS);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(0, ksession.getFactCount());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 47 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class MarshallingTest method testAccumulateSessionSerialization.

@Test
public void testAccumulateSessionSerialization() throws Exception {
    KieBase kbase = loadKnowledgeBase("../test_AccumulateSerialization.drl");
    KieSession ksession = kbase.newKieSession();
    final List<Number> results = new ArrayList<Number>();
    ksession.setGlobal("results", results);
    ksession.insert(new Cheese("stilton", 10));
    ksession.insert(new Cheese("brie", 5));
    ksession.insert(new Cheese("provolone", 150));
    ksession.insert(new Cheese("brie", 20));
    ksession.insert(new Person("Bob", "brie"));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(25, results.get(0).intValue());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 48 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class MarshallingTest method testNot.

@Test
public void testNot() throws Exception {
    String header = "package org.drools.compiler.test;\n";
    header += "import java.util.List;\n";
    header += "import org.drools.compiler.Person\n";
    header += "import org.drools.compiler.Cheese\n";
    header += "global java.util.List list;\n";
    String rule1 = "rule \"not rule test\"\n";
    rule1 += "salience 10\n";
    rule1 += "when\n";
    rule1 += "    Person()\n";
    rule1 += "    not Cheese( price >= 5 )\n";
    rule1 += "then\n";
    rule1 += "    list.add( new Integer( 5 ) );\n";
    rule1 += "end\n";
    KieBase kBase = loadKnowledgeBaseFromString(header + rule1);
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    KieSession ksession = kBase.newKieSession(null, env);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    // add a person, no cheese
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    Person bobba = new Person("bobba fet", 50);
    ksession.insert(bobba);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    // add another person, no cheese
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    Person darth = new Person("darth vadar", 200);
    ksession.insert(darth);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    // add cheese
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    Cheese stilton = new Cheese("stilton", 5);
    ksession.insert(stilton);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    // remove cheese
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.retract(ksession.getFactHandle(stilton));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    // put 2 cheeses back in
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(stilton);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    Cheese brie = new Cheese("brie", 18);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    // now remove a cheese, should be no change
    ksession.retract(ksession.getFactHandle(stilton));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    // now remove a person, should be no change
    ksession.retract(ksession.getFactHandle(bobba));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    // removal remaining cheese, should increase by one, as one person left
    ksession.retract(ksession.getFactHandle(brie));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(5, list.size());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 49 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class MarshallingTest method testSingleRuleSingleJoinNodePattern.

@Test
public void testSingleRuleSingleJoinNodePattern() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import org.drools.compiler.Person\n";
    rule += "import org.drools.compiler.Cheese\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "    $c : Cheese( ) \n";
    rule += "    $p : Person( cheese == $c ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(5, nodes.size());
    assertEquals("Cheese", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(5)).getObjectType()).getClassType().getSimpleName());
    assertTrue("Should end with JoinNode", nodes.get(6).getClass().getSimpleName().endsWith("JoinNode"));
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(7)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Cheese stilton = new Cheese("stilton", 25);
    Cheese brie = new Cheese("brie", 49);
    Person bobba = new Person("bobba fet", 32);
    bobba.setCheese(stilton);
    Person vadar = new Person("darth vadar", 32);
    session.insert(stilton);
    session.insert(bobba);
    session.insert(vadar);
    session.insert(brie);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals(bobba, ((List) session.getGlobal("list")).get(0));
    Person c3po = new Person("c3p0", 32);
    c3po.setCheese(stilton);
    session.insert(c3po);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("list")).size());
    assertEquals(c3po, ((List) session.getGlobal("list")).get(1));
    Person r2d2 = new Person("r2d2", 32);
    r2d2.setCheese(brie);
    session.insert(r2d2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(3, ((List) session.getGlobal("list")).size());
    assertEquals(r2d2, ((List) session.getGlobal("list")).get(2));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 50 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class MarshallingTest method testEmptyRule.

@Test
public void testEmptyRule() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "then\n";
    rule += "    list.add( \"fired\" );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(2, nodes.size());
    assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals("fired", ((List) session.getGlobal("list")).get(0));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

KieBase (org.kie.api.KieBase)1272 Test (org.junit.Test)1191 KieSession (org.kie.api.runtime.KieSession)1011 ArrayList (java.util.ArrayList)592 List (java.util.List)392 Person (org.drools.compiler.Person)214 FactHandle (org.kie.api.runtime.rule.FactHandle)176 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)168 KieHelper (org.kie.internal.utils.KieHelper)156 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)154 Cheese (org.drools.compiler.Cheese)139 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)99 Arrays.asList (java.util.Arrays.asList)87 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)86 QueryResults (org.kie.api.runtime.rule.QueryResults)78 KieServices (org.kie.api.KieServices)74 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)67 Model (org.drools.model.Model)64 Rule (org.drools.model.Rule)64 ModelImpl (org.drools.model.impl.ModelImpl)64