Search in sources :

Example 6 with Address

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

the class NullSafeDereferencingTest method testMixedNullSafes.

@Test
public void testMixedNullSafes() {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " $p : Person( " + " address!.street!.length > 0 && ( address!.street!.length < 15 || > 20 && < 30 ) " + " && address!.zipCode!.length > 0 && address.zipCode == \"12345\" " + " ) \n" + "then\n" + " System.out.println( $p ); \n" + "end";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person("Mario", 38));
    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street", null, "12345"));
    ksession.insert(mark);
    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);
    Person alex = new Person("Alex", 34);
    alex.setAddress(new Address("The Main Verrry Long Street"));
    ksession.insert(alex);
    Person frank = new Person("Frank", 24);
    frank.setAddress(new Address("Long Street number 21", null, "12345"));
    ksession.insert(frank);
    assertEquals(2, ksession.fireAllRules());
    ksession.dispose();
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 7 with Address

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

the class PropertyReactivityTest method testModifyWithGetter.

@Test
public void testModifyWithGetter() {
    final String rule1 = "package foo.bar\n" + "import " + Person.class.getName() + "\n" + "declare Person @propertyReactive end\n" + "rule x\n" + "    when\n" + "       $p : Person( address != null ) @watch(!address) \n" + "    then\n" + "       modify($p){getAddress().setStreet(\"foo\");}\n" + "end";
    final KieHelper helper = new KieHelper();
    helper.addContent(rule1, ResourceType.DRL);
    final KieSession ksession = helper.build().newKieSession();
    final Person p = new Person();
    p.setAddress(new Address());
    ksession.insert(p);
    final int fired = ksession.fireAllRules(10);
    assertEquals(1, fired);
    assertEquals("foo", p.getAddress().getStreet());
}
Also used : Address(org.drools.compiler.Address) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 8 with Address

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

the class FirstOrderLogicTest method testFromWithOr.

@Test
public void testFromWithOr() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_FromWithOr.drl");
    KieSession session = createKnowledgeSession(kbase);
    final List<Address> results = new ArrayList<Address>();
    session.setGlobal("results", results);
    Address a1 = new Address();
    a1.setZipCode("12345");
    Address a2 = new Address();
    a2.setZipCode("54321");
    Address a3 = new Address();
    a3.setZipCode("99999");
    Person p = new Person();
    p.addAddress(a1);
    p.addAddress(a2);
    p.addAddress(a3);
    session.insert(p);
    session.fireAllRules();
    assertEquals(2, results.size());
    assertTrue(results.contains(a1));
    assertTrue(results.contains(a2));
}
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 9 with Address

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

the class Misc2Test method testMergeMVELDialect.

@Test
public void testMergeMVELDialect() {
    // DROOLS-1751
    String drl1 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule1 \n" + "    when\n" + "        (PersonHolder($addresses : person.addresses))\n" + "            &&\n" + "        (Address (street == \"AAA\") from $addresses)\n" + "    then\n" + "end";
    KnowledgeBuilder kbuilder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder1.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages1 = kbuilder1.getKnowledgePackages();
    String drl2 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule2 \n" + "    when\n" + "        PersonHolder()\n" + "    then\n" + "end";
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages2 = kbuilder2.getKnowledgePackages();
    InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> combinedPackages1 = new ArrayList<KiePackage>();
    combinedPackages1.addAll(knowledgePackages1);
    combinedPackages1.addAll(knowledgePackages2);
    // Add once to make inUse=true
    kbase1.addPackages(combinedPackages1);
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> combinedPackages2 = new ArrayList<KiePackage>();
    combinedPackages2.addAll(knowledgePackages1);
    combinedPackages2.addAll(knowledgePackages2);
    // this will cause package deepClone
    kbase2.addPackages(combinedPackages2);
    KieSession ksession = kbase2.newKieSession();
    PersonHolder personHolder = new PersonHolder();
    Person person = new Person("John");
    Address address = new Address("AAA", "BBB", "111");
    person.addAddress(address);
    personHolder.setPerson(person);
    ksession.insert(personHolder);
    int fired = ksession.fireAllRules();
    assertEquals(2, fired);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Address(org.drools.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) PersonHolder(org.drools.compiler.PersonHolder) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 10 with Address

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

the class MVELTest method testMapAccessorWithStaticFieldAccess.

@Test
public void testMapAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].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.getNamedAddresses().put(TestEnum.ONE, 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) TestEnum(org.drools.compiler.TestEnum) 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