Search in sources :

Example 61 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DescrBuilderTest method testRuleRHSComment.

@Test
public void testRuleRHSComment() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().rhs("// some comment").end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 62 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.err.println(knowledgeBuilder.getErrors());
    assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 63 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class ReteooBuilderTest method checkRuleBase.

private void checkRuleBase(final String name) throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_" + name + ".drl")));
    InternalKnowledgePackage pkg = builder.getPackage("org.drools.mvel.compiler.test");
    final InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackage(pkg);
    if (this.writeTree) {
        writeRuleBase(kBase, name);
    }
    final XStream xstream = createNonTrustingXStream();
    final InternalKnowledgeBase goodKBase = (InternalKnowledgeBase) xstream.fromXML(getClass().getResourceAsStream(name));
    nodesEquals(goodKBase.getRete(), kBase.getRete());
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createNonTrustingXStream(org.kie.utll.xml.XStreamUtils.createNonTrustingXStream) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 64 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class Misc2Test method testKieBuilderWithClassLoader.

@Test
public void testKieBuilderWithClassLoader() {
    // DROOLS-763
    String drl = "import com.billasurf.Person\n" + "\n" + "global java.util.List list\n" + "\n" + "rule R1 when\n" + "    $i : Integer()\n" + "then\n" + "    Person p = new Person();\n" + "    p.setAge($i);\n" + "    insert(p);\n" + "end\n" + "\n" + "rule R2 when\n" + "    $p : Person()\n" + "then\n" + "    list.add($p.getAge());\n" + "end\n";
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { this.getClass().getResource("/billasurf.jar") });
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromDrlWithClassLoaderForKieBuilder("test", urlClassLoader, kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    List<Integer> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert(18);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(18, (int) list.get(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 65 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class NodesPartitioningTest method checkDrl.

private void checkDrl(String drl) {
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, drl);
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, MultithreadEvaluationOption.YES);
    Rete rete = kbase.getRete();
    for (EntryPointNode entryPointNode : rete.getEntryPointNodes().values()) {
        traverse(entryPointNode);
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) Rete(org.drools.core.reteoo.Rete) KieModule(org.kie.api.builder.KieModule) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

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