Search in sources :

Example 61 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class MarshallingTest method testSerializable.

@Test
public void testSerializable() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Serializable.drl");
    KiePackage kpkg = kpkgs.iterator().next();
    kpkg = SerializationHelper.serializeObject(kpkg);
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    kbase.addPackages(Collections.singleton(kpkg));
    final org.kie.api.definition.rule.Rule[] rules = kbase.getKiePackages().iterator().next().getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
    assertEquals(4, rules.length);
    assertEquals("match Person 1", rules[0].getName());
    assertEquals("match Person 2", rules[1].getName());
    assertEquals("match Person 3", rules[2].getName());
    assertEquals("match Integer", rules[3].getName());
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", new ArrayList());
    final Person bob = new Person("bob");
    ksession.insert(bob);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(1, ksession.getFactCount());
    assertEquals(bob, ksession.getObjects().iterator().next());
    assertEquals(2, ((InternalAgenda) ksession.getAgenda()).getAgendaGroupsManager().agendaSize());
    ksession.fireAllRules();
    List list = (List) ksession.getGlobal("list");
    assertEquals(3, list.size());
    // because of agenda-groups
    assertEquals(new Integer(4), list.get(0));
    // need to create a new collection or otherwise the collection will be identity based
    List<?> objects = new ArrayList<Object>(ksession.getObjects());
    assertEquals(2, objects.size());
    assertTrue(objects.contains(bob));
    assertTrue(objects.contains(new Person("help")));
}
Also used : ArrayList(java.util.ArrayList) InternalAgenda(org.drools.core.common.InternalAgenda) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 62 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class FactHandleMarshallingTest method createEventFactHandle.

private InternalFactHandle createEventFactHandle(StatefulKnowledgeSessionImpl wm, InternalKnowledgeBase kBase) {
    // EntryPointNode
    Rete rete = kBase.getRete();
    NodeFactory nFacotry = CoreComponentFactory.get().getNodeFactoryService();
    RuleBasePartitionId partionId = RuleBasePartitionId.MAIN_PARTITION;
    EntryPointNode entryPointNode = nFacotry.buildEntryPointNode(1, partionId, false, rete, EntryPointId.DEFAULT);
    WorkingMemoryEntryPoint wmEntryPoint = new NamedEntryPoint(EntryPointId.DEFAULT, entryPointNode, wm);
    EventFactHandle factHandle = new EventFactHandle(1, new Person(), 0, (new Date()).getTime(), 0, wmEntryPoint);
    return factHandle;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) Rete(org.drools.core.reteoo.Rete) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) RuleBasePartitionId(org.drools.core.common.RuleBasePartitionId) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) EventFactHandle(org.drools.core.common.EventFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) Person(org.drools.mvel.compiler.Person) Date(java.util.Date)

Example 63 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class MarshallerTest method testAgendaReconciliationAccumulate.

@Test
public void testAgendaReconciliationAccumulate() throws Exception {
    String str = "import " + Person.class.getCanonicalName() + ";" + "rule X when\n" + "  accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + "                $sum : sum($p.getAge())  \n" + "              )                          \n" + "then\n" + "  insert($sum);\n" + "end";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = null;
    try {
        ksession = kbase.newKieSession(null, env);
        ksession.insert(new Person("Mark", 37));
        ksession.insert(new Person("Edson", 35));
        ksession.insert(new Person("Mario", 40));
        assertEquals(1, ksession.fireAllRules());
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        assertEquals(0, ksession.fireAllRules());
    } finally {
        if (ksession != null) {
            ksession.dispose();
        }
    }
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 64 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class MarshallingTest method testExists.

@Test
public void testExists() throws Exception {
    String header = "package org.drools.compiler.test;\n" + "import java.util.List;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "global java.util.List list;\n";
    String rule1 = "rule \"not rule test\"\n" + "salience 10\n" + "when\n" + "    Person()\n" + "    exists Cheese( price >= 5 )\n" + "then\n" + "    list.add( new Integer( 5 ) );\n" + "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(0, 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(0, 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(2, 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, no
    ksession.retract(ksession.getFactHandle(brie));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    // put one cheese back in, with one person should increase by one
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(stilton);
    ksession.fireAllRules();
    assertEquals(5, list.size());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) Cheese(org.drools.mvel.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 65 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class MarshallingTest method testTruthMaintenance.

@Test
public void testTruthMaintenance() throws Exception {
    String header = "package org.drools.compiler.test;\n" + "import java.util.List;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "global Cheese cheese;\n" + "global Person person;\n" + "global java.util.List list;\n";
    String rule1 = "rule \"not person then cheese\"\n" + "when \n" + "    not Person() \n" + "then \n" + "    if (list.size() < 3) { \n" + "        list.add(new Integer(0)); \n" + "        insertLogical( cheese ); \n" + "    }\n" + "    drools.halt();\n" + "end\n";
    String rule2 = "rule \"if cheese then person\"\n" + "when\n" + "    Cheese()\n" + "then\n" + "    if (list.size() < 3) {\n" + "        list.add(new Integer(0));\n" + "        insertLogical( person );\n" + "    }\n" + "    drools.halt();\n" + "end\n";
    KieBase kBase = loadKnowledgeBaseFromString(header + rule1 + rule2);
    KieSession ksession = kBase.newKieSession();
    final List list = new ArrayList();
    final Person person = new Person("person");
    final Cheese cheese = new Cheese("cheese", 0);
    ksession.setGlobal("cheese", cheese);
    ksession.setGlobal("person", person);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    // should not grow any further
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.mvel.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

Person (org.drools.mvel.compiler.Person)196 Test (org.junit.Test)185 KieSession (org.kie.api.runtime.KieSession)178 KieBase (org.kie.api.KieBase)171 ArrayList (java.util.ArrayList)98 List (java.util.List)72 Cheese (org.drools.mvel.compiler.Cheese)46 FactHandle (org.kie.api.runtime.rule.FactHandle)38 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 Address (org.drools.mvel.compiler.Address)33 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)23 KiePackage (org.kie.api.definition.KiePackage)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)16 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)15 HashMap (java.util.HashMap)12 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)12 Collection (java.util.Collection)11 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 AlphaNode (org.drools.core.reteoo.AlphaNode)8