Search in sources :

Example 1 with Cat

use of org.drools.compiler.Cat in project drools by kiegroup.

the class AlphaNetworkModifyTest method testModifyWithLiaToEval.

@Test
public void testModifyWithLiaToEval() {
    String str = "";
    str += "package org.simple \n";
    str += "import " + Person.class.getCanonicalName() + "\n";
    str += "import " + Cheese.class.getCanonicalName() + "\n";
    str += "import " + Cat.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule x1 \n";
    str += "when \n";
    str += "    $pe : Person() \n";
    str += "    $ch : Cheese() \n";
    str += "    $ca : Cat() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x2 \n";
    str += "when \n";
    str += "    $ch : Cheese() \n";
    str += "    $ca : Cat() \n";
    str += "    $pe : Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x3 \n";
    str += "when \n";
    str += "    $ch : Cheese() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x4 \n";
    str += "when \n";
    str += "    $ch : Cheese() \n";
    str += "    eval( $ch != null ) \n";
    str += "then \n";
    str += "end  \n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession wm = kbase.newKieSession();
    ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
    ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
    ObjectTypeNode otnCat = getObjectTypeNode(kbase, "Cat");
    assertEquals(0, otnPerson.getOtnIdCounter());
    assertEquals(0, otnCheese.getOtnIdCounter());
    assertEquals(0, otnCat.getOtnIdCounter());
    wm.insert(new Person());
    wm.insert(new Cat("yyy"));
    wm.insert(new Cheese());
    wm.fireAllRules();
    assertEquals(2, otnPerson.getOtnIdCounter());
    assertEquals(4, otnCheese.getOtnIdCounter());
    assertEquals(2, otnCat.getOtnIdCounter());
}
Also used : KieBase(org.kie.api.KieBase) Cat(org.drools.compiler.Cat) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 2 with Cat

use of org.drools.compiler.Cat in project drools by kiegroup.

the class FieldAccessTest method testAccessFieldsFromSubClass.

@Test
public // unless typesafe=false is used
void testAccessFieldsFromSubClass() throws Exception {
    String rule = "";
    rule += "package org.drools.compiler;\n";
    rule += "import org.drools.compiler.Person;\n";
    rule += "import org.drools.compiler.Pet;\n";
    rule += "import org.drools.compiler.Cat;\n";
    rule += "declare Person @typesafe(false) end\n";
    rule += "rule \"Test Rule\"\n";
    rule += "when\n";
    rule += "    Person(\n";
    rule += "      pet.breed == \"Siamise\"\n";
    rule += "    )\n";
    rule += "then\n";
    rule += "    System.out.println(\"hello person\");\n";
    rule += "end";
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(rule));
    final KieSession session = createKnowledgeSession(kbase);
    final Person person = new Person();
    person.setPet(new Cat("Mittens"));
    session.insert(person);
    session.fireAllRules();
}
Also used : KieBase(org.kie.api.KieBase) Cat(org.drools.compiler.Cat) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

Cat (org.drools.compiler.Cat)2 Person (org.drools.compiler.Person)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieSession (org.kie.api.runtime.KieSession)2 Cheese (org.drools.compiler.Cheese)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1