Search in sources :

Example 91 with InternalKnowledgeBase

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));
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Cheese(org.drools.mvel.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 92 with InternalKnowledgeBase

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());
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 93 with InternalKnowledgeBase

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));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 94 with InternalKnowledgeBase

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();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ObjectOutput(java.io.ObjectOutput) FileInputStream(java.io.FileInputStream) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) FileOutputStream(java.io.FileOutputStream) File(java.io.File) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream)

Example 95 with InternalKnowledgeBase

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));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17