Search in sources :

Example 1 with PersonHolder

use of org.drools.compiler.PersonHolder 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)

Aggregations

ArrayList (java.util.ArrayList)1 Address (org.drools.compiler.Address)1 Person (org.drools.compiler.Person)1 PersonHolder (org.drools.compiler.PersonHolder)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 Test (org.junit.Test)1 KiePackage (org.kie.api.definition.KiePackage)1 KieSession (org.kie.api.runtime.KieSession)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1