Search in sources :

Example 1 with ReactiveFromNode

use of org.drools.core.reteoo.ReactiveFromNode in project drools by kiegroup.

the class OOPathReactiveTests method testReactiveDeleteOnLia.

@Test
public void testReactiveDeleteOnLia() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    final KieSession ksession = kbase.newKieSession();
    final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
    final ObjectTypeNode otn = epn.getObjectTypeNodes().values().iterator().next();
    final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
    final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
    final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
    final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 11);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
    final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
    Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
    Iterator<LeftTuple> it = tupleMemory.iterator();
    for (LeftTuple next = it.next(); next != null; next = it.next()) {
        final Object obj = next.getFactHandle().getObject();
        Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
    }
    list.clear();
    debbie.setAge(10);
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(0);
    ;
    Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
    it = tupleMemory.iterator();
    for (LeftTuple next = it.next(); next != null; next = it.next()) {
        final Object obj = next.getFactHandle().getObject();
        Assertions.assertThat(obj == charlie).isTrue();
    }
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 2 with ReactiveFromNode

use of org.drools.core.reteoo.ReactiveFromNode in project drools by kiegroup.

the class ReactiveFromBuilder method build.

public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final From from = (From) rce;
    context.pushRuleComponent(from);
    @SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
    AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
    ReactiveFromNode node = CoreComponentFactory.get().getNodeFactoryService().buildReactiveFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
    context.setTupleSource(utils.attachNode(context, node));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    List<XpathConstraint> xpathConstraints = context.getXpathConstraints();
    for (XpathConstraint xpathConstraint : xpathConstraints) {
        for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
            context.setAlphaConstraints(chunk.getAlphaConstraints());
            context.setBetaconstraints(chunk.getBetaConstraints());
            context.setXpathConstraints(chunk.getXpathConstraints());
            build(context, utils, chunk.asFrom());
        }
    }
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BetaConstraints(org.drools.core.common.BetaConstraints) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) From(org.drools.core.rule.From)

Example 3 with ReactiveFromNode

use of org.drools.core.reteoo.ReactiveFromNode in project drools by kiegroup.

the class ReactiveObjectUtil method notifyModification.

public static void notifyModification(Object object, Collection<Tuple> leftTuples, ModificationType type) {
    for (Tuple leftTuple : leftTuples) {
        if (!((ReactiveFromNodeLeftTuple) leftTuple).updateModificationState(object, type)) {
            continue;
        }
        PropagationContext propagationContext = leftTuple.getPropagationContext();
        ReactiveFromNode node = leftTuple.getTupleSink();
        LeftTupleSinkNode sink = node.getSinkPropagator().getFirstLeftTupleSink();
        ReteEvaluator reteEvaluator = propagationContext.getFactHandle().getReteEvaluator();
        reteEvaluator.addPropagation(new ReactivePropagation(object, (ReactiveFromNodeLeftTuple) leftTuple, propagationContext, node, sink, type));
    }
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) PropagationContext(org.drools.core.spi.PropagationContext) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) LeftTupleSinkNode(org.drools.core.reteoo.LeftTupleSinkNode) ReactiveFromNodeLeftTuple(org.drools.core.reteoo.ReactiveFromNodeLeftTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakFromNode.deleteChildLeftTuple(org.drools.core.phreak.PhreakFromNode.deleteChildLeftTuple) Tuple(org.drools.core.spi.Tuple) ReactiveFromNodeLeftTuple(org.drools.core.reteoo.ReactiveFromNodeLeftTuple)

Example 4 with ReactiveFromNode

use of org.drools.core.reteoo.ReactiveFromNode in project drools by kiegroup.

the class OOPathReactiveTest method testReactiveDeleteOnLia.

@Test
public void testReactiveDeleteOnLia() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
    final ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Man.class));
    final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
    final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
    final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
    final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 11);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
    final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
    Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
    Iterator<LeftTuple> it = tupleMemory.iterator();
    for (LeftTuple next = it.next(); next != null; next = it.next()) {
        final Object obj = next.getFactHandle().getObject();
        Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
    }
    list.clear();
    debbie.setAge(10);
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(0);
    ;
    Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
    it = tupleMemory.iterator();
    for (LeftTuple next = it.next(); next != null; next = it.next()) {
        final Object obj = next.getFactHandle().getObject();
        Assertions.assertThat(obj == charlie).isTrue();
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Child(org.drools.mvel.compiler.oopath.model.Child) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Aggregations

ReactiveFromNode (org.drools.core.reteoo.ReactiveFromNode)4 LeftTuple (org.drools.core.reteoo.LeftTuple)3 ArrayList (java.util.ArrayList)2 BetaMemory (org.drools.core.reteoo.BetaMemory)2 EntryPointNode (org.drools.core.reteoo.EntryPointNode)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 TupleMemory (org.drools.core.reteoo.TupleMemory)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieSession (org.kie.api.runtime.KieSession)2 Child (org.drools.compiler.oopath.model.Child)1 Man (org.drools.compiler.oopath.model.Man)1 Toy (org.drools.compiler.oopath.model.Toy)1 Woman (org.drools.compiler.oopath.model.Woman)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 BetaConstraints (org.drools.core.common.BetaConstraints)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 PhreakFromNode.deleteChildLeftTuple (org.drools.core.phreak.PhreakFromNode.deleteChildLeftTuple)1