Search in sources :

Example 16 with Address

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

the class JpaPersistentStatefulSessionTest method fromNodeWithModifiedCollection.

private void fromNodeWithModifiedCollection(final boolean withOOPath) {
    // DROOLS-376
    final String str = "package org.drools.test\n" + "import org.drools.compiler.Person\n" + "import org.drools.compiler.Address\n" + "rule rule1\n" + "when\n" + (withOOPath ? " $p: Person($list : addresses, /addresses[street == \"y\"])\n" : " $p: Person($list : addresses)\n" + " $a: Address(street == \"y\") from $list\n") + "then\n" + " $list.add( new Address(\"z\") );\n" + " $list.add( new Address(\"w\") );\n" + "end\n";
    final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
    final Person p1 = new Person("John");
    p1.addAddress(new Address("x"));
    p1.addAddress(new Address("y"));
    ksession.insert(p1);
    ksession.fireAllRules();
    assertThat(p1.getAddresses()).hasSize(4);
    ksession.dispose();
// Should not fail here
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person)

Example 17 with Address

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

the class FirstOrderLogicTest method testCollectFromMVELAfterOr.

@Test
public void testCollectFromMVELAfterOr() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CollectFromMVELAfterOr.drl");
    KieSession wm = createKnowledgeSession(kbase);
    List results = new ArrayList();
    wm.setGlobal("results", results);
    Person jill = new Person("jill");
    Person bob = new Person("bob");
    List addresses = new ArrayList();
    addresses.add(new Address("a"));
    addresses.add(new Address("b"));
    addresses.add(new Address("c"));
    bob.setAddresses(addresses);
    wm.insert(jill);
    wm.insert(bob);
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(3, ((Collection) results.get(0)).size());
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 18 with Address

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

the class MVELTest method testArrayAccessorWithStaticFieldAccess.

@Test
public void testArrayAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( addresses[Triangle.ZERO] == new Address('s1'), addresses[Triangle.ZERO].street == new Address('s1').street ) \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.addAddress(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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().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) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 19 with Address

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

the class MVELTest method testArrayAccessorWithoutGenerics.

@Test
public void testArrayAccessorWithoutGenerics() {
    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( addressesNoGenerics[0].street == new Address('s1').street) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    // This should fail as there are no generics for the List
    assertTrue(kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Address(org.drools.compiler.Address) Test(org.junit.Test)

Example 20 with Address

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

the class MVELTest method testArrayAccessorWithGenerics.

@Test
public void testArrayAccessorWithGenerics() {
    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( addresses[0] == new Address('s1'), addresses[0].street == new Address('s1').street ) \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.addAddress(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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1"), ((MvelConstraint) constraint).getField().getValue());
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1").getStreet(), ((MvelConstraint) constraint).getField().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) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) 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