Search in sources :

Example 26 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasFirst.

@Test
public void testRemoveChildLeftTupleThatWasFirst() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE1_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild().getFirstChild();
        assertSame(lt, fh1.getFirstLeftTuple().getFirstChild().getLastChild());
        assertNull(lt.getPeer());
        assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 27 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasLast.

@Test
public void testRemoveChildLeftTupleThatWasLast() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild().getFirstChild();
        assertSame(lt, fh1.getFirstLeftTuple().getFirstChild().getLastChild());
        assertNull(lt.getPeer());
        assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 28 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasFirstWithMultipleData.

@Test
public void testRemoveChildLeftTupleThatWasFirstWithMultipleData() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3, 4, 5);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE1_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final InternalFactHandle fh2 = (InternalFactHandle) kieSession.getFactHandle(4);
        final InternalFactHandle fh3 = (InternalFactHandle) kieSession.getFactHandle(5);
        final LeftTuple lt1 = fh1.getFirstLeftTuple();
        final LeftTuple lt1_1 = lt1.getFirstChild();
        final LeftTuple lt1_2 = lt1_1.getHandleNext();
        final LeftTuple lt1_3 = lt1_2.getHandleNext();
        assertNotNull(lt1_1);
        assertNotNull(lt1_2);
        assertNotNull(lt1_3);
        assertSame(lt1_3, lt1.getLastChild());
        assertSame(lt1_2, lt1_3.getHandlePrevious());
        assertSame(lt1_1, lt1_2.getHandlePrevious());
        assertEquals(1, lt1_1.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(lt1_1.getPeer());
        assertEquals(1, lt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(lt1_2.getPeer());
        assertEquals(1, lt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(lt1_3.getPeer());
        final RightTuple rt1 = fh3.getFirstRightTuple();
        final LeftTuple rt1_1 = rt1.getLastChild();
        assertSame(lt1_1, rt1_1);
        final LeftTuple rt1_2 = rt1_1.getRightParentPrevious();
        final LeftTuple rt1_3 = rt1_2.getRightParentPrevious();
        assertNotNull(rt1_1);
        assertNotNull(rt1_2);
        assertNotNull(rt1_3);
        assertSame(rt1_2, rt1_3.getRightParentNext());
        assertSame(rt1_1, rt1_2.getRightParentNext());
        assertEquals(1, rt1_1.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(rt1_1.getPeer());
        assertEquals(1, rt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(rt1_2.getPeer());
        assertEquals(1, rt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(rt1_3.getPeer());
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Test(org.junit.Test)

Example 29 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class MVELConsequenceBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("pkg1");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(pkgDescr);
    InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    ruleDescr.setNamespace("pkg1");
    ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
    final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
    final GroupElement subrule = new GroupElement(GroupElement.AND);
    subrule.addChild(pattern);
    final Map<String, Declaration> map = new HashMap<String, Declaration>();
    map.put("cheese", pattern.getDeclaration());
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
    builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    kBase.addPackage(pkg);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    f0.removeLeftTuple(tuple);
    final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, tuple != null ? (TerminalNode) tuple.getTupleSink() : null, null), new RuleTerminalNode(0, new CompositeObjectSinkAdapterTest.MockBetaNode(), context.getRule(), subrule, 0, new BuildContext(kBase)), null);
    final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
    kbHelper.setActivation(item);
    ((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    context.getRule().getConsequence().evaluate(kbHelper, ksession);
    assertEquals(5, cheddar.getPrice());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) GroupElement(org.drools.core.rule.GroupElement) Cheese(org.drools.compiler.Cheese) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) AgendaItem(org.drools.core.common.AgendaItem) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) AgendaItemImpl(org.drools.core.common.AgendaItemImpl) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Pattern(org.drools.core.rule.Pattern) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 30 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class MVELConsequenceBuilderTest method testImperativeCodeError.

@Test
public void testImperativeCodeError() throws Exception {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    ruleDescr.setConsequence("if (cheese.price == 10) { cheese.price = 5; }");
    Properties properties = new Properties();
    properties.setProperty("drools.dialect.default", "mvel");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl(properties);
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect(pkgRegistry.getDialect());
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
    final Pattern pattern = new Pattern(0, cheeseObjeectType);
    final PatternExtractor extractor = new PatternExtractor(cheeseObjeectType);
    final Declaration declaration = new Declaration("cheese", extractor, pattern);
    final Map<String, Declaration> map = new HashMap<String, Declaration>();
    map.put("cheese", declaration);
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
    builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    final AgendaItem item = new AgendaItemImpl(0, tuple, 10, null, null, null);
    final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
    kbHelper.setActivation(item);
    try {
        ((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
        context.getRule().getConsequence().evaluate(kbHelper, ksession);
        fail("should throw an exception, as 'if' is not allowed");
    } catch (Exception e) {
    }
    assertEquals(10, cheddar.getPrice());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) HashMap(java.util.HashMap) Cheese(org.drools.compiler.Cheese) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) Properties(java.util.Properties) AgendaItem(org.drools.core.common.AgendaItem) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) AgendaItemImpl(org.drools.core.common.AgendaItemImpl) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) PatternExtractor(org.drools.core.spi.PatternExtractor) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14