Package org.drools.core.common

Examples of org.drools.core.common.InternalAgenda


        final RuleTerminalNodeLeftTuple tuple5 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(5,
                                                                                                     "cheese"),
                                                                               node3,
                                                                               true);

        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        // create the AgendaGroups
        final AgendaGroup agendaGroup1 = new AgendaGroupQueueImpl("agendaGroup1",
                                                                  ruleBase);
        agenda.addAgendaGroup(agendaGroup1);

        final AgendaGroup agendaGroup2 = new AgendaGroupQueueImpl("agendaGroup2",
                                                                  ruleBase);
        agenda.addAgendaGroup(agendaGroup2);

        final AgendaGroup agendaGroup3 = new AgendaGroupQueueImpl("agendaGroup3",
                                                                  ruleBase);
        agenda.addAgendaGroup(agendaGroup3);

        // focus at this point is MAIN
        assertEquals(0,
                     agenda.focusStackSize());

        node0.assertLeftTuple(tuple1,
                              context0,
                              workingMemory);

        agenda.unstageActivations();

        // check focus is main
        final AgendaGroup main = agenda.getAgendaGroup(AgendaGroup.MAIN);
        assertEquals(agenda.getFocus(),
                     main);
        // check main got the tuple
        assertEquals(1,
                     agenda.getFocus().size());
        node2.assertLeftTuple(tuple2,
                              context2,
                              workingMemory);

        agenda.unstageActivations();

        // main is still focus and this tuple went to agendaGroup 2
        assertEquals(1,
                     agenda.getFocus().size());

        // check agendaGroup2 still got the tuple
        assertEquals(1,
                     agendaGroup2.size());

        // make sure total agenda size reflects this
        assertEquals(2,
                     agenda.agendaSize());

        // put another one on agendaGroup 2
        node2.assertLeftTuple(tuple3,
                              context2,
                              workingMemory);

        agenda.unstageActivations();

        // main is still focus so shouldn't have increased
        assertEquals(1,
                     agenda.getFocus().size());

        // check agendaGroup2 still got the tuple
        assertEquals(2,
                     agendaGroup2.size());

        // make sure total agenda size reflects this
        assertEquals(3,
                     agenda.agendaSize());

        // set the focus to agendaGroup1, note agendaGroup1 has no activations
        agenda.setFocus("agendaGroup1");
        // add agendaGroup2 onto the focus stack
        agenda.setFocus("agendaGroup2");
        // finally add agendaGroup3 to the top of the focus stack
        agenda.setFocus("agendaGroup3");

        // agendaGroup3, the current focus, has no activations
        assertEquals(0,
                     agenda.getFocus().size());

        // add to agendaGroup 3
        node3.assertLeftTuple(tuple4,
                              context3,
                              workingMemory);

        agenda.unstageActivations();

        assertEquals(1,
                     agenda.getFocus().size());

        node3.assertLeftTuple(tuple5,
                              context3,
                              workingMemory);

        agenda.unstageActivations();

        // agendaGroup3 now has 2 activations
        assertEquals(2,
                     agenda.getFocus().size());
        // check totalAgendaSize still works
        assertEquals(5,
                     agenda.agendaSize());

        // ok now lets check that stacks work with fireNextItem
        agenda.fireNextItem(null, 0, -1);

        // agendaGroup3 should still be the current agendaGroup
        assertEquals(agenda.getFocus(),
                     agendaGroup3);
        // agendaGroup3 has gone from 2 to one activations
        assertEquals(1,
                     agenda.getFocus().size());
        // check totalAgendaSize has reduced too
        assertEquals(4,
                     agenda.agendaSize());

        // now repeat the process
        agenda.fireNextItem(null, 0, -1);

        // focus is still agendaGroup3, but now its empty
        assertEquals(agenda.getFocus(),
                     agendaGroup3);
        assertEquals(0,
                     agenda.getFocus().size());
        assertEquals(3,
                     agenda.agendaSize());

        // repeat fire again
        agenda.fireNextItem(null, 0, -1);

        // agendaGroup3 is empty so it should be popped from the stack making````````````````````
        // agendaGroup2
        // the current agendaGroup
        assertEquals(agendaGroup2,
                     agenda.getFocus());
        // agendaGroup2 had 2 activations, now it only has 1
        assertEquals(1,
                     agenda.getFocus().size());
        assertEquals(2,
                     agenda.agendaSize());

        // repeat fire again
        agenda.fireNextItem(null, 0, -1);

        assertEquals(agenda.getFocus(),
                     agendaGroup2);
        assertEquals(0,
                     agenda.getFocus().size());
        assertEquals(1,
                     agenda.agendaSize());

        // this last fire is more interesting as it demonstrates that
        // agendaGroup1 on
        // the stack before agendaGroup2 gets skipped as it has no activations
        agenda.fireNextItem(null, 0, -1);

        assertEquals(agenda.getFocus(),
                     main);
        assertEquals(0,
                     agenda.getFocus().size());
        assertEquals(0,
                     agenda.agendaSize());

    }
View Full Code Here


    //
    @Test
    public void testAutoFocus() throws ConsequenceException {
        final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();
        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        // create the agendaGroup
        final AgendaGroup agendaGroup = new AgendaGroupQueueImpl("agendaGroup",
                                                                 ruleBase);
        agenda.addAgendaGroup(agendaGroup);

        // create the consequence
        final Consequence consequence = new Consequence() {
            private static final long serialVersionUID = 510l;

            public void evaluate(KnowledgeHelper knowledgeHelper,
                                 WorkingMemory workingMemory) {
                // do nothing
            }

            public void readExternal(ObjectInput in) throws IOException,
                    ClassNotFoundException {

            }

            public void writeExternal(ObjectOutput out) throws IOException {

            }

            public String getName() {
                return "default";
            }
        };


        // create a rule for the agendaGroup
        final Rule rule = new Rule("test-rule",
                                   "agendaGroup");
        final RuleTerminalNode node = new RuleTerminalNode(2,
                                                           new MockTupleSource(2),
                                                           rule,
                                                           rule.getLhs(),
                                                           0,
                                                           buildContext);

        final RuleTerminalNodeLeftTuple tuple = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
                                                                                                    "cheese"),
                                                                              node,
                                                                              true);
        rule.setConsequence(consequence);
        final PropagationContext context = pctxFactory.createPropagationContext(0,
                                                                                PropagationContext.INSERTION,
                                                                                rule,
                                                                                null,
                                                                                new DefaultFactHandle());

        // first test that autoFocus=false works. Here the rule should not fire
        // as its agendaGroup does not have focus.
        rule.setAutoFocus(false);

        node.assertLeftTuple(tuple,
                             context,
                             workingMemory);

        agenda.unstageActivations();

        // check activation as added to the agendaGroup
        assertEquals(1,
                     agendaGroup.size());

        // fire next item, agendaGroup should not fire as its not on the focus stack
        // and thus should retain its sinle activation
        agenda.fireNextItem(null, 0, -1);
        assertEquals(1,
                     agendaGroup.size());

        // Clear the agenda we we can test again
        agenda.clearAndCancel();
        assertEquals(0,
                     agendaGroup.size());

        // Now test that autoFocus=true works. Here the rule should fire as its
        // agendaGroup gets the focus when the activation is created.
        rule.setAutoFocus(true);

        node.assertLeftTuple(tuple,
                             context,
                             workingMemory);

        agenda.unstageActivations();

        assertEquals(1,
                     agendaGroup.size());
        agenda.fireNextItem(null, 0, -1);
        assertEquals(0,
                     agendaGroup.size());
    }
View Full Code Here

    }

    @Test
    public void testAgendaGroupLockOnActive() {
        final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();
        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        // create the agendaGroup
        final InternalAgendaGroup agendaGroup = new AgendaGroupQueueImpl("agendaGroup",
                                                                         ruleBase);
        agenda.addAgendaGroup(agendaGroup);

        // create a rule for the agendaGroup
        final Rule rule = new Rule("test-rule",
                                   "agendaGroup");
        final RuleTerminalNode node = new RuleTerminalNode(2,
                                                           new MockTupleSource(2),
                                                           rule,
                                                           rule.getLhs(),
                                                           0,
                                                           buildContext);

        final RuleTerminalNodeLeftTuple tuple = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
                                                                                                    "cheese"),
                                                                              node,
                                                                              true);

        final PropagationContext context = pctxFactory.createPropagationContext(0,
                                                                                PropagationContext.INSERTION,
                                                                                rule,
                                                                                null,
                                                                                new DefaultFactHandle());

        // When both the rule is lock-on-active and the agenda group is active, activations should be ignored
        rule.setLockOnActive(true);
        ((InternalRuleFlowGroup) agendaGroup).setAutoDeactivate(false);
        agendaGroup.setActive(true);
        node.assertLeftTuple(tuple,
                             context,
                             workingMemory);
        // activation should be ignored
        assertEquals(0,
                     agendaGroup.size());

        // lock-on-active is now false so activation should propagate
        rule.setLockOnActive(false);
        node.assertLeftTuple(tuple,
                             context,
                             workingMemory);

        agenda.unstageActivations();

        assertEquals(1,
                     agendaGroup.size());

        // even if lock-on-active is true, unless the agenda group is active the activation will still propagate
        rule.setLockOnActive(true);
        agendaGroup.setActive(false);
        node.assertLeftTuple(tuple,
                             context,
                             workingMemory);
        agenda.unstageActivations();
        assertEquals(2,
                     agendaGroup.size());
    }
View Full Code Here

    @Test
    public void testActivationGroup() {
        final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();

        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        final List list = new ArrayList();

        // create the consequence
        final Consequence consequence = new Consequence() {
            private static final long serialVersionUID = 510l;

            public void evaluate(KnowledgeHelper knowledgeHelper,
                                 WorkingMemory workingMemory) {
                list.add(knowledgeHelper.getRule());
            }

            public void readExternal(ObjectInput in) throws IOException,
                    ClassNotFoundException {

            }

            public void writeExternal(ObjectOutput out) throws IOException {

            }

            public String getName() {
                return "default";
            }
        };

        // create a rule for each agendaGroup
        final Rule rule0 = new Rule("test-rule0");
        rule0.setActivationGroup("activation-group-0");
        final RuleTerminalNode node0 = new RuleTerminalNode(3,
                                                            new MockTupleSource(2),
                                                            rule0,
                                                            rule0.getLhs(),
                                                            0,
                                                            buildContext);
        rule0.setConsequence(consequence);
        final PropagationContext context0 = pctxFactory.createPropagationContext(0,
                                                                                 PropagationContext.INSERTION,
                                                                                 rule0,
                                                                                 null,
                                                                                 new DefaultFactHandle());

        final Rule rule1 = new Rule("test-rule1");
        rule1.setActivationGroup("activation-group-0");
        rule1.setSalience(new SalienceInteger(10));
        final RuleTerminalNode node1 = new RuleTerminalNode(5,
                                                            new MockTupleSource(4),
                                                            rule1,
                                                            rule1.getLhs(),
                                                            0,
                                                            buildContext);
        rule1.setConsequence(consequence);
        final PropagationContext context1 = pctxFactory.createPropagationContext(0,
                                                                                 PropagationContext.INSERTION,
                                                                                 rule1,
                                                                                 null,
                                                                                 new DefaultFactHandle());

        final Rule rule2 = new Rule("test-rule2");
        rule2.setSalience(new SalienceInteger(-5));
        final RuleTerminalNode node2 = new RuleTerminalNode(7,
                                                            new MockTupleSource(6),
                                                            rule2,
                                                            rule2.getLhs(),
                                                            0,
                                                            buildContext);
        rule2.setConsequence(consequence);
        final PropagationContext context2 = pctxFactory.createPropagationContext(0,
                                                                                 PropagationContext.INSERTION,
                                                                                 rule2,
                                                                                 null,
                                                                                 new DefaultFactHandle());

        final Rule rule3 = new Rule("test-rule3",
                                    "agendaGroup3");
        rule3.setSalience(new SalienceInteger(-10));
        rule3.setActivationGroup("activation-group-3");
        final RuleTerminalNode node3 = new RuleTerminalNode(9,
                                                            new MockTupleSource(8),
                                                            rule3,
                                                            rule3.getLhs(),
                                                            0,
                                                            buildContext);
        rule3.setConsequence(consequence);
        final PropagationContext context3 = pctxFactory.createPropagationContext(0,
                                                                                 PropagationContext.INSERTION,
                                                                                 rule3,
                                                                                 null,
                                                                                 new DefaultFactHandle());

        final RuleTerminalNodeLeftTuple tuple1 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node1, true);
        final RuleTerminalNodeLeftTuple tuple3 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node1, true);
        final RuleTerminalNodeLeftTuple tuple4 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node1, true);
        final RuleTerminalNodeLeftTuple tuple5 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node1, true);
        final RuleTerminalNodeLeftTuple tuple6 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node1, true);
        final RuleTerminalNodeLeftTuple tuple7 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node2, true);
        final RuleTerminalNodeLeftTuple tuple8 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, "cheese"), node3, true);

        // Assert the tuple and check it was added to activation-group-0
        node0.assertLeftTuple(tuple1,
                              context0,
                              workingMemory);
        agenda.unstageActivations();
        final ActivationGroup activationGroup0 = agenda.getActivationGroup("activation-group-0");
        assertEquals(1,
                     activationGroup0.size());

        // Removing a tuple should remove the activation from the activation-group-0 again
        node0.retractLeftTuple(tuple1,
                               context0,
                               workingMemory);
        assertEquals(0,
                     activationGroup0.size());

        // Assert the tuple again and check it was added to activation-group-0
        node0.assertLeftTuple(tuple3,
                              context0,
                              workingMemory);
        agenda.unstageActivations();
        assertEquals(1,
                     activationGroup0.size());

        // Assert another tuple and check it was added to activation-group-0
        node1.assertLeftTuple(tuple4,
                              context1,
                              workingMemory);
        agenda.unstageActivations();
        assertEquals(2,
                     activationGroup0.size());

        // There should now be two potential activations to fire
        assertEquals(2,
                     agenda.focusStackSize());

        // The first tuple should fire, adding itself to the List and clearing and cancelling the other Activations in the activation-group-0
        agenda.fireNextItem(null, 0, -1);

        // Make sure the activation-group-0 is clear
        assertEquals(0,
                     activationGroup0.size());

        // Make sure the Agenda  is  empty
        assertEquals(0,
                     agenda.focusStackSize());

        // List should only have a single item, "rule0"
        assertEquals(1,
                     list.size());
        assertSame(rule1,
                   list.get(0));

        list.clear();

        //-------------------
        // Now try a more complex scenario involving  two Xor Groups and one  rule not in a Group
        node0.assertLeftTuple(tuple5,
                              context0,
                              workingMemory);
        node1.assertLeftTuple(tuple6,
                              context1,
                              workingMemory);
        node2.assertLeftTuple(tuple7,
                              context2,
                              workingMemory);
        node3.assertLeftTuple(tuple8,
                              context3,
                              workingMemory);
        agenda.unstageActivations();

        // activation-group-0 should be populated again
        assertEquals(2,
                     activationGroup0.size());

        // make sure the activation-group-3 is cleared when we can clear the Agenda Group for the activation that is in both
        final ActivationGroup activationGroup3 = agenda.getActivationGroup("activation-group-3");

        assertEquals(4,
                     agenda.agendaSize());
        assertEquals(1,
                     activationGroup3.size());

        agenda.clearAndCancelAgendaGroup("agendaGroup3");
        assertEquals(3,
                     agenda.agendaSize());
        assertEquals(0,
                     activationGroup3.size());

        // Activation for activation-group-0 should be next - the activation in no activation/agenda group should remain on the agenda
        agenda.fireNextItem(null, 0, -1);
        assertEquals(1,
                     agenda.agendaSize());
        assertEquals(0,
                     activationGroup0.size());

        // Fire  the  last activation and  make sure the Agenda Empties
        agenda.fireNextItem(null, 0, -1);
        assertEquals(0,
                     agenda.agendaSize());

        assertEquals(2,
                     list.size());
        assertEquals(rule1,
                     list.get(0));
View Full Code Here

     */
    @Test
    public void testRuleFlowGroup() {
        final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();

        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        final List list = new ArrayList();

        // create the consequence
        final Consequence consequence = new Consequence() {
            private static final long serialVersionUID = 510l;

            public void evaluate(KnowledgeHelper knowledgeHelper,
                                 WorkingMemory workingMemory) {
                list.add(knowledgeHelper.getRule());
            }

            public void readExternal(ObjectInput in) throws IOException,
                    ClassNotFoundException {

            }

            public void writeExternal(ObjectOutput out) throws IOException {

            }

            public String getName() {
                return "default";
            }
        };

        // create a rule for each rule flow groups
        final Rule rule0 = new Rule("test-rule0");
        rule0.setAgendaGroup("rule-flow-group-0");
        rule0.setConsequence(consequence);

        final RuleTerminalNode node0 = new RuleTerminalNode(3,
                                                            new MockTupleSource(2),
                                                            rule0,
                                                            rule0.getLhs(),
                                                            0,
                                                            buildContext);

        final Rule rule1 = new Rule("test-rule1");
        rule1.setAgendaGroup("rule-flow-group-1");
        rule1.setConsequence(consequence);

        final RuleTerminalNode node1 = new RuleTerminalNode(4,
                                                            new MockTupleSource(2),
                                                            rule1,
                                                            rule1.getLhs(),
                                                            0,
                                                            buildContext);

        final Rule rule2 = new Rule("test-rule2");
        rule2.setAgendaGroup("rule-flow-group-2");
        rule2.setConsequence(consequence);
        rule2.setSalience(new SalienceInteger(10));

        final RuleTerminalNode node2 = new RuleTerminalNode(5,
                                                            new MockTupleSource(2),
                                                            rule2,
                                                            rule2.getLhs(),
                                                            0,
                                                            buildContext);

        final PropagationContext context0 = pctxFactory.createPropagationContext(0,
                                                                                 PropagationContext.INSERTION,
                                                                                 rule0,
                                                                                 null,
                                                                                 new DefaultFactHandle());

        final RuleFlowGroup ruleFlowGroup0 = agenda.getRuleFlowGroup("rule-flow-group-0");
        final RuleFlowGroup ruleFlowGroup1 = agenda.getRuleFlowGroup("rule-flow-group-1");
        final RuleFlowGroup ruleFlowGroup2 = agenda.getRuleFlowGroup("rule-flow-group-2");

        final RuleTerminalNodeLeftTuple tuple0 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
                                                                                                     "cheese"),
                                                                               node0,
                                                                               true);
        node0.assertLeftTuple(tuple0,
                              context0,
                              workingMemory);

        final RuleTerminalNodeLeftTuple tuple1 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
                                                                                                     "cheese"),
                                                                               node1,
                                                                               true);
        node0.assertLeftTuple(tuple1,
                              context0,
                              workingMemory);

        final RuleTerminalNodeLeftTuple tuple2 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
                                                                                                     "cheese"),
                                                                               node2,
                                                                               true);
        node1.assertLeftTuple(tuple2,
                              context0,
                              workingMemory);

        final RuleTerminalNodeLeftTuple tuple3 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
                                                                                                     "cheese"),
                                                                               node0,
                                                                               true);
        node2.assertLeftTuple(tuple3,
                              context0,
                              workingMemory);
        agenda.unstageActivations();

        assertEquals(2,
                     ruleFlowGroup0.size());
        assertEquals(1,
                     ruleFlowGroup1.size());
        assertEquals(1,
                     ruleFlowGroup2.size());
        assertEquals(4,
                     agenda.agendaSize());

        // Activate the RuleFlowGroup, the nodes stay in the group, but should now also be in the Agenda
        agenda.activateRuleFlowGroup("rule-flow-group-0");
        assertEquals(2,
                     ruleFlowGroup0.size());
        assertEquals(4,
                     agenda.agendaSize());

        // As we fire each rule they are removed from both the Agenda and the RuleFlowGroup
        agenda.fireNextItem(null, 0, -1);
        assertEquals(1,
                     ruleFlowGroup0.size());
        assertEquals(3,
                     agenda.agendaSize());

        // After firing all activations of RuleFlowGroup 0, the agenda is empty
        agenda.fireNextItem(null, 0, -1);
        assertEquals(0,
                     ruleFlowGroup0.size());
        assertEquals(2,
                     agenda.agendaSize());

        // Now we activate two RuleFlowGroups together
        // All their activations should be added to the agenda.
        agenda.activateRuleFlowGroup("rule-flow-group-1");
        agenda.activateRuleFlowGroup("rule-flow-group-2");
        assertEquals(1,
                     ruleFlowGroup1.size());
        assertEquals(1,
                     ruleFlowGroup2.size());
        assertEquals(2,
                     agenda.agendaSize());

        // we set the salience higher on rule2, so it sould fire first and empty ruleFlowGroup2
        agenda.fireNextItem(null, 0, -1);
        assertEquals(1,
                     ruleFlowGroup1.size());
        assertEquals(0,
                     ruleFlowGroup2.size());
        assertEquals(1,
                     agenda.agendaSize());

        // this is the last activation, so everything should be empty after this
        agenda.fireNextItem(null, 0, -1);
        assertEquals(0,
                     ruleFlowGroup0.size());
        assertEquals(0,
                     ruleFlowGroup1.size());
        assertEquals(0,
                     ruleFlowGroup2.size());
        assertEquals(0,
                     agenda.agendaSize());
    }
View Full Code Here

                                                           rule1.getLhs(),
                                                           0,
                                                           buildContext);
        final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();

        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        final Consequence consequence = new Consequence() {
            private static final long serialVersionUID = 510l;

            public void evaluate(KnowledgeHelper knowledgeHelper,
                                 WorkingMemory workingMemory) {
                LinkedList<LogicalDependency> list = ((DefaultKnowledgeHelper) knowledgeHelper).getpreviousJustified();
                if (list != null) {
                    for (SimpleLogicalDependency dep = (SimpleLogicalDependency ) list.getFirst(); dep != null; dep =  ( SimpleLogicalDependency ) dep.getNext() ){
                        knowledgeHelper.insertLogical( dep.getObject(), dep.getValue() );
                    }
                }
            }

            public void readExternal(ObjectInput in) throws IOException,
                                                    ClassNotFoundException {

            }

            public void writeExternal(ObjectOutput out) throws IOException {

            }

            public String getName() {
                return "default";
            }
        };

        rule1.setConsequence( consequence );

        final DefaultFactHandle handle1 = (DefaultFactHandle) workingMemory.insert( "o1" );
        final RuleTerminalNodeLeftTuple tuple1 = new RuleTerminalNodeLeftTuple( handle1,
                                                                                node,
                                                                                true );

        final PropagationContext context1 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());

        // Test single activation for a single logical assertions
        node.assertLeftTuple( tuple1,
                              context1,
                              workingMemory );

        final String logicalString = new String( "logical" );
        InternalFactHandle logicalHandle = (InternalFactHandle) workingMemory.insert( logicalString,
                                                                                      null,
                                                                                      false,
                                                                                      true,
                                                                                      rule1,
                                                                                      (Activation) tuple1.getObject() );
        new RightTuple( logicalHandle,
                        sink );
        context1.setFactHandle( handle1 );
        // Retract the tuple and test the logically asserted fact was also retracted
        node.retractLeftTuple( tuple1,
                               context1,
                               workingMemory );

        workingMemory.executeQueuedActions();

        assertLength( 1,
                      sink.getRetracted() );

        Object[] values = (Object[]) sink.getRetracted().get( 0 );

        assertSame( logicalHandle,
                    ((RightTuple) values[0]).getFactHandle() );

        // Test single activation for a single logical assertions. This also
        // tests that logical assertions live on after the related Activation
        // has fired.
        node.assertLeftTuple( tuple1,
                              context1,
                              workingMemory );
        logicalHandle = (InternalFactHandle) workingMemory.insert( logicalString,
                                                                   null,
                                                                   false,
                                                                   true,
                                                                   rule1,
                                                                   (Activation) tuple1.getObject() );

        new RightTuple( logicalHandle,
                        sink );

        agenda.fireNextItem( null, 0, -1 );

        node.retractLeftTuple( tuple1,
                               context1,
                               workingMemory );
View Full Code Here

        ksession.setGlobal( "list", list );

        // AgendaGroup "group1" is not active, so should receive activation
        final Cheese brie12 = new Cheese( "brie", 12 );
        ksession.insert( brie12 );
        InternalAgenda agenda = ((AgendaImpl) ksession.getAgenda()).getAgenda();
        final AgendaGroup group1 = agenda.getAgendaGroup( "group1" );
        assertEquals( 1, group1.size() );

        ksession.getAgenda().getAgendaGroup("group1").setFocus( );
        // AgendaqGroup "group1" is now active, so should not receive activations
        final Cheese brie10 = new Cheese( "brie", 10 );
        ksession.insert( brie10 );
        assertEquals( 1, group1.size() );

        final Cheese cheddar20 = new Cheese( "cheddar", 20 );
        ksession.insert( cheddar20 );
        final AgendaGroup group2 = agenda.getAgendaGroup( "group1" );
        assertEquals( 1, group2.size() );

        agenda.setFocus(group2);
        final Cheese cheddar17 = new Cheese( "cheddar", 17 );
        ksession.insert( cheddar17 );
        assertEquals( 1, group2.size() );
    }
View Full Code Here

        ksession.insert( mic );
        ksession.insert( mark );

        InternalWorkingMemory wm = ( InternalWorkingMemory )((StatefulKnowledgeSessionImpl) ksession).getInternalWorkingMemory();

        final InternalAgenda agenda = (InternalAgenda) ((AgendaImpl)ksession.getAgenda()).getAgenda();
        final AgendaGroup group1 = agenda.getAgendaGroup( "group1" );
        if ( phreak == RuleEngineOption.RETEOO ) {
            agenda.setFocus( group1 );
            assertEquals( 3, group1.size() );
            agenda.fireNextItem( null, 0, 0 );
            assertEquals( 2, group1.size() );
            ksession.update( brieHandle, brie );
            assertEquals( 2, group1.size() );

            AgendaGroup group2 = agenda.getAgendaGroup( "group2" );
            assertEquals( 3, group2.size() );
            agenda.setFocus( group2 );

            agenda.activateRuleFlowGroup( "ruleflow2" );
            agenda.fireNextItem( null, 0, 0 );
            assertEquals( 2, group2.size() );
            ksession.update( brieHandle, brie );
            assertEquals( 2, group2.size() );
        } else {
            agenda.setFocus( group1 );
            assertEquals( 1, group1.size() );
            RuleAgendaItem ruleItem1 = (RuleAgendaItem) group1.getActivations()[0];
            ruleItem1.getRuleExecutor().evaluateNetwork(wm);
            assertEquals(3, ruleItem1.getRuleExecutor().getLeftTupleList().size());

            agenda.fireNextItem( null, 0, 0 );
            assertEquals( 1, group1.size() );
            assertEquals( 2, ruleItem1.getRuleExecutor().getLeftTupleList().size() );

            ksession.update( brieHandle, brie );
            assertEquals( 1, group1.size() );
            ruleItem1.getRuleExecutor().evaluateNetwork(wm);
            assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());

            AgendaGroup group2 = agenda.getAgendaGroup( "group2" );
            agenda.setFocus( group2);
            assertEquals( 1, group2.size() );
            RuleAgendaItem ruleItem2 = (RuleAgendaItem) group2.getActivations()[0];
            ruleItem2.getRuleExecutor().evaluateNetwork(wm);
            assertEquals(3, ruleItem2.getRuleExecutor().getLeftTupleList().size());

            agenda.fireNextItem( null, 0, 0 );
            assertEquals( 1, group2.size() );
            assertEquals( 2, ruleItem2.getRuleExecutor().getLeftTupleList().size() );

            ksession.update( brieHandle, brie );
            assertEquals( 1, group2.size() );
View Full Code Here

            entry = tmp;
        }
       
        if ( wasBlocked ) {
            RuleAgendaItem ruleAgendaItem = targetMatch.getRuleAgendaItem();
            InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
            agenda.stageLeftTuple(ruleAgendaItem, targetMatch);
        }
    }
View Full Code Here

            final AgendaFilter filter,
            int fireCount,
            int fireLimit) {
        LinkedList<StackEntry> outerStack = new LinkedList<StackEntry>();

        InternalAgenda agenda = (InternalAgenda) wm.getAgenda();
        boolean fireUntilHalt = agenda.isFireUntilHalt();

        reEvaluateNetwork(wm, outerStack, fireUntilHalt);
        wm.executeQueuedActions();

        return fire(wm, filter, fireCount, fireLimit, outerStack, agenda, fireUntilHalt);
View Full Code Here

TOP

Related Classes of org.drools.core.common.InternalAgenda

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.