use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class FieldConstraintTest method testPrimitiveLiteralConstraint.
/**
* <pre>
*
* Cheese( price == 5 )
*
* </pre>
*/
@Test
public void testPrimitiveLiteralConstraint() {
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
;
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final ClassFieldReader extractor = store.getReader(Cheese.class, "price");
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheesePriceEqualsConstraint(extractor, 5, useLambdaConstraint);
final Cheese cheddar = new Cheese("cheddar", 5);
final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
// check constraint
assertTrue(constraint.isAllowed(cheddarHandle, ksession));
final Cheese stilton = new Cheese("stilton", 10);
final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
// check constraint
assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class MergePackageTest method testMergingDifferentPackages2.
@Test
public void testMergingDifferentPackages2() throws Exception {
// using different builders
try {
Collection<KiePackage> kpkgs1 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_RuleNameClashes1.drl").getKiePackages();
KiePackage kpkg1 = kpkgs1.stream().filter(pkg -> pkg.getName().equals("org.drools.package1")).findFirst().get();
assertEquals(1, kpkg1.getRules().size());
Collection<KiePackage> kpkgs2 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_RuleNameClashes2.drl").getKiePackages();
KiePackage kpkg2 = kpkgs2.stream().filter(pkg -> pkg.getName().equals("org.drools.package2")).findFirst().get();
assertEquals(1, kpkg2.getRules().size());
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration);
kbase.addPackages(kpkgs1);
kbase.addPackages(kpkgs2);
KieSession ksession = kbase.newKieSession();
final List results = new ArrayList();
ksession.setGlobal("results", results);
ksession.insert(new Cheese("stilton", 10));
ksession.insert(new Cheese("brie", 5));
ksession.fireAllRules();
assertEquals(results.toString(), 2, results.size());
assertTrue(results.contains("p1.r1"));
assertTrue(results.contains("p2.r1"));
} catch (final Exception e) {
e.printStackTrace();
fail("unexpected exception: " + e.getMessage());
}
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class MergePackageTest method testMergePackageWithSameRuleNames.
@Test
public void testMergePackageWithSameRuleNames() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_MergePackageWithSameRuleNames1.drl");
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_MergePackageWithSameRuleNames2.drl").getKiePackages();
kbase.addPackages(kpkgs);
KieSession ksession = kbase.newKieSession();
final List results = new ArrayList();
ksession.setGlobal("results", results);
ksession.fireAllRules();
assertEquals(1, results.size());
assertEquals("rule1 for the package2", results.get(0));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class LargeRuleBase method bigBlobCompile.
private static void bigBlobCompile() throws DroolsParserException, IOException, Exception {
StringBuilder buf = new StringBuilder();
buf.append(getHeader());
for (int i = 0; i < 1; i++) {
String name = "x" + i;
int status = i;
String r = getTemplate1(name, status);
buf.append(r);
}
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(buf.toString().getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
File f = new File("foo.rulebase");
if (f.exists())
f.delete();
ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(f));
out.writeObject(kbase);
out.flush();
out.close();
ObjectInputStream in = new DroolsObjectInputStream(new FileInputStream(f));
KieBase rb_ = (KieBase) in.readObject();
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class KnowledgeBuilderTest method testDifferentPackages.
@Test
public void testDifferentPackages() throws Exception {
String rule = "package org.drools.mvel.compiler.test.rule\n" + "import org.drools.mvel.compiler.testA.FactA\n" + "import org.drools.mvel.compiler.testB.FactB\n" + "rule R1 when\n" + " $fieldA : FactA( $fieldB : fieldB, bigint == 1 )\n" + " FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + " list.add(\"OK\");" + "end";
String declarationA = "package org.drools.mvel.compiler.testA\n" + "import org.drools.mvel.compiler.testB.FactB\n" + "import java.math.BigInteger\n" + "declare FactA\n" + " fieldB: FactB\n" + " bigint: BigInteger\n" + "end\n";
String declarationB = "package org.drools.mvel.compiler.testB\n" + "import org.drools.mvel.compiler.testA.FactA\n" + "global java.util.List list\n" + "declare FactB\n" + " fieldA: FactA\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.batch().type(ResourceType.DRL).add(ResourceFactory.newByteArrayResource(rule.getBytes())).add(ResourceFactory.newByteArrayResource(declarationA.getBytes())).add(ResourceFactory.newByteArrayResource(declarationB.getBytes())).build();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
String declarationC = "package org.drools.mvel.compiler.testA\n" + "declare FactC\n" + " field : UnknownClass\n" + "end\n";
kbuilder.add(ResourceFactory.newByteArrayResource(declarationC.getBytes()), ResourceType.DRL);
assertTrue(kbuilder.hasErrors());
kbuilder.undo();
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
FactType aType = kbase.getFactType("org.drools.mvel.compiler.testA", "FactA");
Object a = aType.newInstance();
FactType bType = kbase.getFactType("org.drools.mvel.compiler.testB", "FactB");
Object b = bType.newInstance();
aType.set(a, "fieldB", b);
aType.set(a, "bigint", new BigInteger("1"));
bType.set(b, "fieldA", a);
ksession.insert(a);
ksession.insert(b);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
assertEquals("OK", list.get(0));
}
Aggregations