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();
}
}
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();
}
}
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();
}
}
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());
}
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());
}
Aggregations