Search in sources :

Example 11 with Address

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

the class MVELTest method testNewConstructor.

@Test
public void testNewConstructor() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( address == new Address('s1')) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.setAddress(new Address("s1"));
    ksession.insert(p);
    ksession.fireAllRules();
    assertEquals("r1", list.get(0));
    // Check it was built with MVELReturnValueExpression constraint
    final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (final ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    final AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
        final FieldValue r = ((MvelConstraint) constraint).getField();
        assertEquals(p.getAddress(), r.getValue());
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.core.base.ClassFieldReader) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(org.drools.core.spi.FieldValue) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 12 with Address

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

the class MapConstraintTest method testMapNullConstraint.

@Test
public void testMapNullConstraint() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_mapNullConstraints.drl");
    final KieSession ksession = createKnowledgeSession(kbase);
    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    new WorkingMemoryConsoleLogger((WorkingMemory) ksession);
    final Map addresses = new HashMap();
    addresses.put("home", new Address("home street"));
    final Person bob = new Person("Bob");
    bob.setNamedAddresses(addresses);
    ksession.insert(bob);
    ksession.fireAllRules();
    final ArgumentCaptor<AfterMatchFiredEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(4)).afterMatchFired(arg.capture());
    org.kie.api.event.rule.AfterMatchFiredEvent aaf = arg.getAllValues().get(0);
    assertThat(aaf.getMatch().getRule().getName(), is("1. home != null"));
    aaf = arg.getAllValues().get(1);
    assertThat(aaf.getMatch().getRule().getName(), is("2. not home == null"));
    aaf = arg.getAllValues().get(2);
    assertThat(aaf.getMatch().getRule().getName(), is("7. work == null"));
    aaf = arg.getAllValues().get(3);
    assertThat(aaf.getMatch().getRule().getName(), is("8. not work != null"));
}
Also used : Address(org.drools.compiler.Address) HashMap(java.util.HashMap) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) WorkingMemoryConsoleLogger(org.drools.core.audit.WorkingMemoryConsoleLogger) KieBase(org.kie.api.KieBase) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieSession(org.kie.api.runtime.KieSession) HashMap(java.util.HashMap) Map(java.util.Map) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 13 with Address

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

the class BackwardChainingTest method testQueriesWithNestedAcecssorsMixedArgs.

@Test(timeout = 10000)
public void testQueriesWithNestedAcecssorsMixedArgs() throws Exception {
    String str = "" + "package org.drools.compiler.test  \n" + "import org.drools.compiler.Person \n" + "global java.util.List list\n" + "query peeps( String $name, String $likes, String $street ) \n" + "   Person( $name := name, $likes := likes, $street := address.street ) \n" + "end\n";
    str += "rule x1\n" + "when\n" + "    $s : String()\n" + // output, output,         ,input
    "    ?peeps($name1; $likes1 : $likes, $street : $s )\n" + "then\n" + "   list.add( $name1 + \" : \" + $likes1 + \" : \" +  $s );\n" + "end \n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession ksession = createKnowledgeSession(kbase);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    Person p1 = new Person("darth", "stilton", 100);
    p1.setAddress(new Address("s1"));
    Person p2 = new Person("yoda", "stilton", 300);
    p2.setAddress(new Address("s2"));
    ksession.insert(p1);
    ksession.insert(p2);
    ksession.insert("s1");
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("darth : stilton : s1"));
    list.clear();
    ksession.insert("s2");
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("yoda : stilton : s2"));
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 14 with Address

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

the class BackwardChainingTest method testQueriesWithNestedAcecssorsAllOutputs.

@Test(timeout = 10000)
public void testQueriesWithNestedAcecssorsAllOutputs() throws Exception {
    String str = "" + "package org.drools.compiler.test  \n" + "import org.drools.compiler.Person \n" + "global java.util.List list\n" + "query peeps( String $name, String $likes, String $street ) \n" + "   Person( $name := name, $likes := likes, $street := address.street ) \n" + "end\n";
    str += "rule x1\n" + "when\n" + "    String( this == \"go1\" )\n" + // output, output,         ,output
    "    ?peeps($name1; $likes1 : $likes, $street1 : $street )\n" + "then\n" + "   list.add( $name1 + \" : \" + $likes1 + \" : \" +  $street1 );\n" + "end \n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession ksession = createKnowledgeSession(kbase);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    Person p1 = new Person("darth", "stilton", 100);
    p1.setAddress(new Address("s1"));
    Person p2 = new Person("yoda", "stilton", 300);
    p2.setAddress(new Address("s2"));
    ksession.insert(p1);
    ksession.insert(p2);
    ksession.insert("go1");
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("darth : stilton : s1"));
    assertTrue(list.contains("yoda : stilton : s2"));
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 15 with Address

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

the class CustomOperatorTest method testCustomOperatorOnKieModule.

@Test
public void testCustomOperatorOnKieModule() {
    String str = "import org.drools.compiler.Person\n" + "import org.drools.compiler.Address\n" + "rule R when\n" + "    $alice : Person(name == \"Alice\")\n" + "    $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieSession ksession = new KieHelper().setKieModuleModel(ks.newKieModuleModel().setConfigurationProperty("drools.evaluator.supersetOf", SupersetOfEvaluatorDefinition.class.getName())).addContent(str, ResourceType.DRL).build().newKieSession();
    Person alice = new Person("Alice", 30);
    alice.addAddress(new Address("Large Street", "BigTown", "12345"));
    Person bob = new Person("Bob", 30);
    bob.addAddress(new Address("Large Street", "BigTown", "12345"));
    bob.addAddress(new Address("Long Street", "SmallTown", "54321"));
    ksession.insert(alice);
    ksession.insert(bob);
    assertEquals(1, ksession.fireAllRules());
}
Also used : Address(org.drools.compiler.Address) KieHelper(org.kie.internal.utils.KieHelper) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

Address (org.drools.compiler.Address)32 Person (org.drools.compiler.Person)30 Test (org.junit.Test)30 KieSession (org.kie.api.runtime.KieSession)30 KieBase (org.kie.api.KieBase)22 ArrayList (java.util.ArrayList)16 List (java.util.List)12 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)7 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 MVELObjectClassFieldReader (org.drools.core.base.extractors.MVELObjectClassFieldReader)4 AlphaNode (org.drools.core.reteoo.AlphaNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4 Cheese (org.drools.compiler.Cheese)3 KieHelper (org.kie.internal.utils.KieHelper)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 KiePackage (org.kie.api.definition.KiePackage)2 InputStreamReader (java.io.InputStreamReader)1