Search in sources :

Example 11 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class KieProjectRuntimeModulesTest method createMultipleJarAndFileResources.

@Test
public void createMultipleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kProjModel1 = createKieModule("jar1", true);
    KieModuleModel kProjModel2 = createKieModule("jar2", true);
    KieModuleModel kProjModel3 = createKieModule("jar3", true);
    KieModuleModel kProjModel4 = createKieModule("fol4", false);
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId("jar1", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId("jar2", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId3 = KieServices.Factory.get().newReleaseId("jar3", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId4 = KieServices.Factory.get().newReleaseId("fol4", "art1", "1.0-SNAPSHOT");
    java.io.File file1 = fileManager.newFile("jar1-1.0-SNAPSHOT.jar");
    java.io.File file2 = fileManager.newFile("jar2-1.0-SNAPSHOT.jar");
    java.io.File file3 = fileManager.newFile("jar3-1.0-SNAPSHOT.jar");
    java.io.File fol4 = fileManager.newFile("fol4-1.0-SNAPSHOT");
    ZipKieModule mod1 = new ZipKieModule(releaseId1, kProjModel1, file1);
    ZipKieModule mod2 = new ZipKieModule(releaseId2, kProjModel2, file2);
    ZipKieModule mod3 = new ZipKieModule(releaseId3, kProjModel3, file3);
    FileKieModule mod4 = new FileKieModule(releaseId4, kProjModel4, fol4);
    mod1.addKieDependency(mod2);
    mod1.addKieDependency(mod3);
    mod1.addKieDependency(mod4);
    KieModuleKieProject kProject = new KieModuleKieProject(mod1);
    KieContainer kContainer = new KieContainerImpl(kProject, null);
    KieBase kBase = kContainer.getKieBase("jar1.KBase1");
    ClassLoader cl = ((RuleBase) kBase).getRootClassLoader();
    Class cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar1");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar2");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar3");
    assertNotNull(cls);
    testEntry(new KProjectTestClassImpl("jar1", kContainer), "jar1");
    testEntry(new KProjectTestClassImpl("jar2", kContainer), "jar2");
    testEntry(new KProjectTestClassImpl("jar3", kContainer), "jar3");
    testEntry(new KProjectTestClassImpl("fol4", kContainer), "fol4");
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) RuleBase(org.drools.core.impl.RuleBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 12 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class DeclaredTypesTest method testFactType.

@Test
public void testFactType() throws Exception {
    // DROOLS-4784
    String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + "declare Name\n" + "    VALUE : String\n" + "end\n" + "rule R when\n" + "    Name($v : VALUE == \"Mario\")\n" + "then\n" + "    insert($v);" + "end";
    KieSession ksession = getKieSession(str);
    FactType nameType = ksession.getKieBase().getFactType("org.test", "Name");
    Object name = nameType.newInstance();
    nameType.set(name, "VALUE", "Mario");
    ksession.insert(name);
    ksession.fireAllRules();
    assertEquals("Mario", nameType.get(name, "VALUE"));
    Collection<String> results = getObjectsIntoList(ksession, String.class);
    assertEquals(1, results.size());
    assertEquals("Mario", results.iterator().next());
    EntryPointNode epn = ((RuleBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
    Iterator<ObjectTypeNode> otns = epn.getObjectTypeNodes().values().iterator();
    ObjectTypeNode otn = otns.next();
    if (otn.toString().contains("InitialFact")) {
        otn = otns.next();
    }
    AlphaNode alpha = (AlphaNode) otn.getSinks()[0];
    AlphaNodeFieldConstraint constraint = alpha.getConstraint();
    int index = ((IndexableConstraint) constraint).getFieldExtractor().getIndex();
    assertTrue(index >= 0);
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieSession(org.kie.api.runtime.KieSession) AlphaNode(org.drools.core.reteoo.AlphaNode) RuleBase(org.drools.core.impl.RuleBase) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 13 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class CepEspTest method testEventExpiration.

@Test(timeout = 10000)
public void testEventExpiration() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "declare StockTick \n" + "    @role( event )\n" + "    @timestamp( dateTimestamp )\n" + "    @expires( 1h30m )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\" )\n" + "then\n" + "    results.add( $st );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    // read in the source
    final TypeDeclaration factType = ((RuleBase) kbase).getTypeDeclaration(StockTick.class);
    assertEquals(TimeIntervalParser.parse("1h30m")[0], factType.getExpirationOffset());
}
Also used : KieBase(org.kie.api.KieBase) TypeDeclaration(org.drools.core.rule.TypeDeclaration) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 14 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class MemoryLeakTest method testStagedLeftTupleLeak.

@Test
public void testStagedLeftTupleLeak() throws Exception {
    // BZ-1058874
    String str = "rule R1 when\n" + "    String( this == \"this\" )\n" + "    String( this == \"that\" )\n" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
    for (int i = 0; i < 10; i++) {
        FactHandle fh = ksession.insert("this");
        ksession.fireAllRules();
        ksession.delete(fh);
        ksession.fireAllRules();
    }
    Rete rete = ((RuleBase) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (String.class == otn.getObjectType().getValueType().getClassType()) {
            AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
            liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    assertNotNull(liaNode);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory(liaNode);
    TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
    assertNull(stagedLeftTuples.getDeleteFirst());
    assertNull(stagedLeftTuples.getInsertFirst());
}
Also used : Rete(org.drools.core.reteoo.Rete) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) RuleBase(org.drools.core.impl.RuleBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 15 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class Misc2Test method testFromEPDontRequireLeftInput.

@Test
public void testFromEPDontRequireLeftInput() {
    // DROOLS-1014
    String drl = "rule R when\n" + "    $s1 : String() from entry-point \"xxx\"\n" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    Rete rete = ((RuleBase) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (String.class == otnType) {
            assertEquals(1, otn.getObjectSinkPropagator().size());
        } else if (InitialFact.class.isAssignableFrom(otnType)) {
            assertEquals(0, otn.getObjectSinkPropagator().size());
        } else {
            fail("There shouldn't be other OTNs");
        }
    }
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) RuleBase(org.drools.core.impl.RuleBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

RuleBase (org.drools.core.impl.RuleBase)21 Test (org.junit.Test)16 KieBase (org.kie.api.KieBase)9 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 KieSession (org.kie.api.runtime.KieSession)6 Rete (org.drools.core.reteoo.Rete)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 IndexableConstraint (org.drools.core.rule.IndexableConstraint)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 ReleaseId (org.kie.api.builder.ReleaseId)2 KiePackage (org.kie.api.definition.KiePackage)2