Search in sources :

Example 6 with StatefulKnowledgeSessionImpl

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

the class TruthMaintenanceTest method testStatedWithShadowAndDeleteException.

@Test(timeout = 10000)
public void testStatedWithShadowAndDeleteException() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go1");
    session.fireAllRules();
    // TMS is now enabled
    assertNotNull(fh1.getEqualityKey());
    assertNotNull(fh2.getEqualityKey());
    // EqualtyKey shows both are stated
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertEquals(EqualityKey.STATED, fh2.getEqualityKey().getStatus());
    // Only fh1 has a logical
    assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
    assertNull(fh2.getEqualityKey().getBeliefSet());
    // Get the logical Handle too
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    EqualityKey key = jfh1.getEqualityKey();
    assertSame(fh1.getEqualityKey(), key);
    assertNotSame(fh1, jfh1);
    assertEquals(2, key.size());
    assertSame(jfh1, key.getLogicalFactHandle());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 7 with StatefulKnowledgeSessionImpl

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

the class TruthMaintenanceTest method testStatedShadowLogicalThenLogicalOnly.

@Test(timeout = 10000)
public void testStatedShadowLogicalThenLogicalOnly() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + "    String( this == 'go2' ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    FactHandle g1 = session.insert("go1");
    session.fireAllRules();
    // This removes the stated position, but it should still be logical now and exist
    session.delete(fh1, FactHandle.State.STATED);
    session.insert("go2");
    session.fireAllRules();
    // fh1 is invalid and no longer in the WM. However it's now reverted to it's Justified version and will still be there
    assertFalse(fh1.isValid());
    // Make sure f1 is still there, but logical only now
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
    InternalFactHandle jfh1 = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().get("f1").getLogicalFactHandle();
    assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
    assertSame(jfh1, session.getFactHandle("f1"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 8 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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 9 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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)

Example 10 with StatefulKnowledgeSessionImpl

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

the class MVELEvalBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    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 InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
    final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
    final Declaration declaration = new Declaration("a", extractor, pattern);
    final Map map = new HashMap();
    map.put("a", declaration);
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final EvalDescr evalDescr = new EvalDescr();
    evalDescr.setContent("a == 10");
    final MVELEvalBuilder builder = new MVELEvalBuilder();
    final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
    ((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    MockLeftTupleSink sink = new MockLeftTupleSink();
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    f0.removeLeftTuple(tuple);
    Object evalContext = eval.createContext();
    assertTrue(eval.isAllowed(tuple, ksession, evalContext));
    cheddar.setPrice(9);
    ksession.update(f0, cheddar);
    assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) HashMap(java.util.HashMap) MockLeftTupleSink(org.drools.compiler.reteoo.MockLeftTupleSink) Cheese(org.drools.compiler.Cheese) EvalCondition(org.drools.core.rule.EvalCondition) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) 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) MVELEvalExpression(org.drools.core.base.mvel.MVELEvalExpression) 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) HashMap(java.util.HashMap) Map(java.util.Map) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)79 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 KieSession (org.kie.api.runtime.KieSession)22 List (java.util.List)20 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)17 ClassObjectType (org.drools.core.base.ClassObjectType)14 FactHandle (org.kie.api.runtime.rule.FactHandle)14 KieBase (org.kie.api.KieBase)13 HashMap (java.util.HashMap)9 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 Cheese (org.drools.core.test.model.Cheese)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6