Search in sources :

Example 16 with InternalKnowledgeBase

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

the class DescrBuilderTest method testRule.

@Test
public void testRule() throws InstantiationException, IllegalAccessException {
    PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs("    System.out.println(\"foo\");\n").end();
    PackageDescr pkg = packBuilder.getDescr();
    String drl = new DrlDumper().dump(packBuilder.getDescr());
    System.out.println(drl);
    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(0, rules);
    ksession = kbase.newKieSession();
    ksession.insert(new StockTick(2, "RHT", 150, 1));
    rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) 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) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 17 with InternalKnowledgeBase

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

the class DslTest method testEmptyDSL.

@Test
@Ignore("antlr cannot parse correctly if the file ends with a comment without a further line break")
public void testEmptyDSL() throws Exception {
    // FIXME etirelli / mic_hat not sure what to do with this?
    // gives antlr <EOF> error
    final String DSL = "# This is an empty dsl file.";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_expander.dsl", getClass()), ResourceType.DSL);
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(DSL)), ResourceType.DSLR);
    // trying to expand Cheese() pattern
    assertFalse(kbuilder.hasErrors());
    // Check errors
    final String err = kbuilder.getErrors().toString();
    assertEquals("", err);
    assertEquals(0, kbuilder.getErrors().size());
    // the compiled package
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    assertEquals(0, pkgs.size());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession session = kbase.newKieSession();
    pkgs = SerializationHelper.serializeObject(pkgs);
    assertNull(pkgs);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with InternalKnowledgeBase

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

the class EnumTest method testQueryEnum.

@Test
public void testQueryEnum() {
    final String str = "package org.kie.test;\n" + "\n" + "declare enum Ennumm\n" + "  ONE, TWO;\n" + "end\n" + "\n" + "declare Bean\n" + "  fld : Ennumm\n" + "end\n" + "\n" + "query seeWhat( Ennumm $e, Bean $b )\n" + "  $b := Bean( $e == Ennumm.ONE )\n" + "end\n" + "\n" + "rule rool\n" + "when\n" + "then\n" + "  insert( new Bean( Ennumm.ONE ) );\n" + "end\n" + "\n" + "\n" + "rule rool2\n" + "when\n" + "  seeWhat( $ex, $bx ; )\n" + "then\n" + "  System.out.println( $bx );\n" + "end";
    final String str2 = "package org.drools.mvel.compiler.test2; \n" + "" + "declare Naeb \n" + "   fld : String \n" + "end \n";
    KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, str);
    List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
    assertTrue(errors.toString(), errors.isEmpty());
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBuilder);
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, str2).getKiePackages();
    kbase.addPackages(kpkgs);
    final KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
    ksession.dispose();
}
Also used : Message(org.kie.api.builder.Message) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 19 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)

Example 20 with InternalKnowledgeBase

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

the class PathEndNodeTest method testSubNetworkSharing.

@Test
public void testSubNetworkSharing() throws Exception {
    String str = "package org.test \n" + "rule xxx \n" + "when \n" + "  $s : String()\n" + "  not( Integer() and Long() )\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  $s : String()\n" + "  not( Integer() and Long() )\n" + "then \n" + "end  \n";
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    EntryPointNode epn = kbase.getRete().getEntryPointNode(EntryPointId.DEFAULT);
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Long.class));
    BetaNode beta1 = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
    RightInputAdapterNode rian = (RightInputAdapterNode) beta1.getSinkPropagator().getSinks()[0];
    BetaNode beta2 = (BetaNode) rian.getObjectSinkPropagator().getSinks()[0];
    LeftTupleSink[] sinks = beta2.getSinkPropagator().getSinks();
    RuleTerminalNode rtn1 = (RuleTerminalNode) sinks[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) sinks[1];
    assertEquals(3, rian.getPathEndNodes().length);
    assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(rtn1, rtn2, rian)));
    kbase.removeRule("org.test", "xxx");
    assertEquals(2, rian.getPathEndNodes().length);
    RuleTerminalNode remainingRTN = rtn1.getRule().getName().equals("yyy") ? rtn1 : rtn2;
    assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(remainingRTN, rian)));
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) 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