use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class FirstOrderLogicTest method testCollectAfterOrCE.
@Test
public void testCollectAfterOrCE() throws Exception {
Collection<KiePackage> pkgs = loadKnowledgePackages("test_OrCEFollowedByCollect.drl");
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
KieSession session = createKnowledgeSession(kbase);
// Set up facts
final Cheesery bonFromage = new Cheesery();
bonFromage.addCheese(new Cheese("cheddar"));
bonFromage.addCheese(new Cheese("cheddar"));
session.insert(bonFromage);
int rules = session.fireAllRules();
assertEquals(2, rules);
// Serialize and test again
pkgs = SerializationHelper.serializeObject(pkgs);
kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
session = createKnowledgeSession(kbase);
session.insert(bonFromage);
rules = session.fireAllRules();
assertEquals(2, rules);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class I18nTest method readDrlInEncodingUtf8.
@Test
@Ignore("Fails because of JBRULES-3435. But the JBRULES-2853 part works fine. Support for i18n properties must be fixed in mvel")
public void readDrlInEncodingUtf8() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_I18nPerson_utf8.drl", "UTF-8", getClass()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
I18nPerson i18nPerson = new I18nPerson();
i18nPerson.setGarçon("Value 1");
i18nPerson.setÉlève("Value 2");
i18nPerson.setИмя("Value 3");
i18nPerson.set名称("Value 4");
ksession.insert(i18nPerson);
ksession.fireAllRules();
assertTrue(list.contains("garçon"));
assertTrue(list.contains("élève"));
assertTrue(list.contains("имя"));
assertTrue(list.contains("名称"));
ksession.dispose();
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class I18nTest method testNewClassPathResource.
@Test
public void testNewClassPathResource() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
// newClassPathResource without specifying encoding
kbuilder.add(ResourceFactory.newClassPathResource("test_I18nPerson_utf8_forTestNewClassPathResource.drl", getClass()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
I18nPerson i18nPerson = new I18nPerson();
i18nPerson.set名称("山田花子");
ksession.insert(i18nPerson);
ksession.fireAllRules();
assertTrue(list.contains("名称は山田花子です"));
ksession.dispose();
}
use of org.drools.core.impl.InternalKnowledgeBase 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);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class LinkingTest method testNotNodeDoesNotUnlinksWithConstriants.
@Test
public void testNotNodeDoesNotUnlinksWithConstriants() {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " $a : A() \n";
str += " not B( value == $a.value ) \n";
str += " $c : C() \n";
str += "then \n";
str += " list.add( 'x' ); \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
NotNode bNode = (NotNode) aNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
SegmentUtilities.createSegmentMemory(cNode, wm);
LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
// Only NotNode is linked in
assertEquals(2, amem.getSegmentMemory().getLinkedNodeMask());
FactHandle fha = wm.insert(new A());
FactHandle fhb = wm.insert(new B(1));
FactHandle fhc = wm.insert(new C());
wm.flushPropagations();
// All nodes are linked in
assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
// NotNode does not unlink, due to variable constraint
wm.retract(fhb);
wm.flushPropagations();
assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
}
Aggregations