Search in sources :

Example 6 with InternalKnowledgeBase

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

the class AlphaNodeTest method testUpdateSinkWithoutMemory.

@Test
public void testUpdateSinkWithoutMemory() {
    // An AlphaNode should try and repropagate from its source
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), constraint, source, // no memory
    buildContext);
    alphaNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    alphaNode.addObjectSink(sink1);
    // Assert a single fact which should be in the AlphaNode memory and also
    // propagated to the
    // the tuple sink
    final Cheese cheese = new Cheese("cheddar", 0);
    final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese);
    // adding handle to the mock source
    source.addFact(handle1);
    alphaNode.assertObject(handle1, context, ksession);
    // Create a fact that should not be propagated, since the alpha node restriction will filter it out
    final Cheese stilton = new Cheese("stilton", 10);
    final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton);
    // adding handle to the mock source
    source.addFact(handle2);
    alphaNode.assertObject(handle2, context, ksession);
    assertLength(1, sink1.getAsserted());
    // Attach a new tuple sink
    final MockObjectSink sink2 = new MockObjectSink();
    // Tell the alphanode to update the new node. Make sure the first sink1
    // is not updated
    // likewise the source should not do anything
    alphaNode.updateSink(sink2, context, ksession);
    assertLength(1, sink1.getAsserted());
    assertLength(1, sink2.getAsserted());
    Assert.assertEquals(1, source.getUdated());
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) PropagationContext(org.drools.core.spi.PropagationContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.mvel.model.Cheese) MockObjectSource(org.drools.mvel.model.MockObjectSource) MockObjectSink(org.drools.core.reteoo.MockObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with InternalKnowledgeBase

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

the class CommonTestMethodBase method loadKnowledgeBaseFromString.

protected KieBase loadKnowledgeBaseFromString(KnowledgeBuilderConfiguration config, KieBaseConfiguration kBaseConfig, String... drlContentStrings) {
    KnowledgeBuilder kbuilder = config == null ? KnowledgeBuilderFactory.newKnowledgeBuilder() : KnowledgeBuilderFactory.newKnowledgeBuilder(config);
    for (String drlContentString : drlContentStrings) {
        kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
    }
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    if (kBaseConfig == null) {
        kBaseConfig = RuleBaseFactory.newKnowledgeBaseConfiguration();
    }
    InternalKnowledgeBase kbase = kBaseConfig == null ? KnowledgeBaseFactory.newKnowledgeBase() : KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 8 with InternalKnowledgeBase

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

the class AlphaNodeTest method testReturnValueConstraintAssertObject.

/*
     *  This just test AlphaNode With a different Constraint type.
     */
@Test
public void testReturnValueConstraintAssertObject() throws Exception {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), constraint, source, buildContext);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
    assertLength(0, sink.getAsserted());
    // object should assert as it passes text
    alphaNode.assertObject(f0, context, ksession);
    assertLength(1, sink.getAsserted());
    final Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
    final Cheese stilton = new Cheese("stilton", 6);
    f0.setObject(stilton);
    sink.getAsserted().clear();
    // object should not assert as it does not pass text
    alphaNode.assertObject(f0, context, ksession);
    assertLength(0, sink.getAsserted());
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.mvel.model.Cheese) MockObjectSource(org.drools.mvel.model.MockObjectSource) MockObjectSink(org.drools.core.reteoo.MockObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 9 with InternalKnowledgeBase

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

the class RemoveRuleTest method testRemoveAccumulateRule.

@Test
public void testRemoveAccumulateRule() {
    // DROOLS-4864
    final String str = "package org.drools.compiler.test\n" + "\n" + "rule Acc no-loop\n" + "when\n" + "    accumulate(\n" + "            String( $l : length, this == \"test\" );\n" + "            $max : max( $l ))\n" + "then end\n";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = KieServices.get().newReleaseId("org.kie", "test-remove-acc-rule", "1.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, str);
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) kc.getKieBase();
    KieSession ksession = kbase.newKieSession();
    ksession.insert("xxx");
    final ReleaseId releaseId2 = KieServices.get().newReleaseId("org.kie", "test-remove-acc-rule", "1.1");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration);
    kc.updateToVersion(releaseId2);
    final EntryPointNode epn = kbase.getRete().getEntryPointNodes().values().iterator().next();
    for (final ObjectTypeNode otn : epn.getObjectTypeNodes().values()) {
        final ObjectSink[] sinks = otn.getObjectSinkPropagator().getSinks();
        if (sinks.length > 0) {
            fail(otn + " has sinks " + Arrays.toString(sinks));
        }
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ObjectSink(org.drools.core.reteoo.ObjectSink) ReleaseId(org.kie.api.builder.ReleaseId) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 10 with InternalKnowledgeBase

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

the class DescrBuilderTest method testNamedConsequence.

@Test
public void testNamedConsequence() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("test").lhs().pattern("Cheese").constraint("type == \"stilton\"").end().namedConsequence().name("c1").end().pattern("Cheese").constraint("type == \"cheddar\"").end().end().rhs("// do something").namedRhs("c1", "// do something else").end().getDescr();
    assertEquals(1, pkg.getRules().size());
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    Cheese stilton = new Cheese("stilton", 5);
    Cheese cheddar = new Cheese("cheddar", 7);
    Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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