Package org.drools.core.reteoo

Examples of org.drools.core.reteoo.MockRightTupleSink


        final ObjectTypeNode objectTypeNode = new ObjectTypeNode(idGenerator.getNextId(),
                                                                 this.entryPoint,
                                                                 new ClassObjectType(String.class),
                                                                 buildContext);

        MockRightTupleSink sink = new MockRightTupleSink();

        final RuleImpl rule1 = new RuleImpl("test-rule1");
        final RuleTerminalNode node = new RuleTerminalNode(idGenerator.getNextId(),
                                                           new MockTupleSource(idGenerator.getNextId()),
                                                           rule1,
                                                           rule1.getLhs(),
                                                           0,
                                                           buildContext);
        StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();

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

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

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

            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) ksession.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,
                              ksession );

        final String logicalString = new String( "logical" );
        InternalFactHandle logicalHandle = (InternalFactHandle) ksession.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 deleted
        node.retractLeftTuple( tuple1,
                               context1,
                               ksession );

        ksession.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,
                              ksession );
        logicalHandle = (InternalFactHandle) ksession.insert( logicalString,
                                                                   null,
                                                                   false,
                                                                   true,
                                                                   rule1,
                                                                   (Activation) tuple1.getObject() );

        new RightTuple( logicalHandle,
                        sink );

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

        node.retractLeftTuple( tuple1,
                               context1,
                               ksession );

        ksession.executeQueuedActions();

        assertLength( 2,
                      sink.getRetracted() );

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

        assertSame( logicalHandle,
                    ((RightTuple) values[0]).getFactHandle() );
    }
View Full Code Here


                                                                  this.entryPoint,
                                                                  new ClassObjectType( String.class ),
                                                                  buildContext );
        objectTypeNode.attach( buildContext );

        MockRightTupleSink sink = new MockRightTupleSink();

        final RuleTerminalNode node = new RuleTerminalNode( idGenerator.getNextId(),
                                                            new MockTupleSource( idGenerator.getNextId() ),
                                                            rule1,
                                                            rule1.getLhs(),
                                                            0,
                                                            buildContext );
        StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();

        final Agenda agenda = ksession.getAgenda();

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

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

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

            }

            public void writeExternal(ObjectOutput out) throws IOException {

            }

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

        // Create first justifier
        rule1.setConsequence( consequence );

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

        final PropagationContext context1 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());
        // get the activation onto the agenda
        node.assertLeftTuple( tuple1,
                              context1,
                              ksession );

        // Create the second justifier
        final RuleImpl rule2 = new RuleImpl( "test-rule2" );
        final RuleTerminalNode node2 = new RuleTerminalNode( idGenerator.getNextId(),
                                                             new MockTupleSource( idGenerator.getNextId() ),
                                                             rule2,
                                                             rule2.getLhs(),
                                                             0,
                                                             buildContext );
        rule2.setConsequence( consequence );

        final DefaultFactHandle handle2 = new DefaultFactHandle( 2,
                                                                 "cheese" );
        final RuleTerminalNodeLeftTuple tuple2 = new RuleTerminalNodeLeftTuple( handle2,
                                                                                node2,
                                                                                true );

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

        // get the activations onto the agenda
        node2.assertLeftTuple( tuple2,
                               context2,
                               ksession );

        // Create the first justifieable relationship
        final String logicalString1 = new String( "logical" );
        final InternalFactHandle logicalHandle1 = (InternalFactHandle) ksession.insert( logicalString1,
                                                                                             "value1",
                                                                                             false,
                                                                                             true,
                                                                                             rule1,
                                                                                             (Activation) tuple1.getObject() );
        new RightTuple( logicalHandle1,
                        sink );

        // Create the second justifieable relationship
        final String logicalString2 = new String( "logical" );
        final InternalFactHandle logicalHandle2 = (InternalFactHandle) ksession.insert( logicalString2,
                                                                                             "value2",
                                                                                             false,
                                                                                             true,
                                                                                             rule2,
                                                                                             (Activation) tuple2.getObject() );

        assertSame( logicalHandle1, logicalHandle2 );

        TruthMaintenanceSystem tms = ((NamedEntryPoint)ksession.getWorkingMemoryEntryPoint( EntryPointId.DEFAULT.getEntryPointId() ) ).getTruthMaintenanceSystem();
       
        // "logical" should only appear once
        assertEquals( 1,
                      getLogicalCount( tms ) );
       
        BeliefSet bs =  ( BeliefSet ) logicalHandle2.getEqualityKey().getBeliefSet();      
        assertEquals( "value1", ((LogicalDependency) ((LinkedListEntry)bs.getFirst()).getObject()).getMode() );
        assertEquals( "value2", ((LogicalDependency) ((LinkedListEntry)bs.getFirst().getNext()).getObject()).getMode() );

        // Now lets cancel the first activation
        node2.retractLeftTuple( tuple2,
                                context2,
                                ksession );

        ksession.executeQueuedActions();

        // because this logical fact has two relationships it shouldn't retract yet
        assertLength( 0,
                      sink.getRetracted() );

        // check "logical" is still in the system
        assertEquals( 1,
                      getLogicalCount( ( tms ) ) );

        // now remove that final justification
        node.retractLeftTuple( tuple1,
                               context1,
                               ksession );

        ksession.executeQueuedActions();

        // Should cause the logical fact to be deleted
        assertLength( 1,
                      sink.getRetracted() );

        // "logical" fact should no longer be in the system
        assertEquals( 0,
                      getLogicalCount( tms ) );
    }
View Full Code Here

        final ObjectTypeNode objectTypeNode = new ObjectTypeNode(idGenerator.getNextId(),
                                                                 this.entryPoint,
                                                                 new ClassObjectType(String.class),
                                                                 buildContext);

        MockRightTupleSink sink = new MockRightTupleSink();

        final Rule rule1 = new Rule("test-rule1");
        final RuleTerminalNode node = new RuleTerminalNode(idGenerator.getNextId(),
                                                           new MockTupleSource(idGenerator.getNextId()),
                                                           rule1,
                                                           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 );

        workingMemory.executeQueuedActions();

        assertLength( 2,
                      sink.getRetracted() );

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

        assertSame( logicalHandle,
                    ((RightTuple) values[0]).getFactHandle() );
    }
View Full Code Here

                                                                  this.entryPoint,
                                                                  new ClassObjectType( String.class ),
                                                                  buildContext );
        objectTypeNode.attach( buildContext );

        MockRightTupleSink sink = new MockRightTupleSink();

        final RuleTerminalNode node = new RuleTerminalNode( idGenerator.getNextId(),
                                                            new MockTupleSource( idGenerator.getNextId() ),
                                                            rule1,
                                                            rule1.getLhs(),
                                                            0,
                                                            buildContext );
        final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();

        final Agenda agenda = 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";
            }
        };

        // Create first justifier
        rule1.setConsequence( consequence );

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

        final PropagationContext context1 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());
        // get the activation onto the agenda
        node.assertLeftTuple( tuple1,
                              context1,
                              workingMemory );

        // Create the second justifier
        final Rule rule2 = new Rule( "test-rule2" );
        final RuleTerminalNode node2 = new RuleTerminalNode( idGenerator.getNextId(),
                                                             new MockTupleSource( idGenerator.getNextId() ),
                                                             rule2,
                                                             rule2.getLhs(),
                                                             0,
                                                             buildContext );
        rule2.setConsequence( consequence );

        final DefaultFactHandle handle2 = new DefaultFactHandle( 2,
                                                                 "cheese" );
        final RuleTerminalNodeLeftTuple tuple2 = new RuleTerminalNodeLeftTuple( handle2,
                                                                                node2,
                                                                                true );

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

        // get the activations onto the agenda
        node2.assertLeftTuple( tuple2,
                               context2,
                               workingMemory );

        // Create the first justifieable relationship
        final String logicalString1 = new String( "logical" );
        final InternalFactHandle logicalHandle1 = (InternalFactHandle) workingMemory.insert( logicalString1,
                                                                                             "value1",
                                                                                             false,
                                                                                             true,
                                                                                             rule1,
                                                                                             (Activation) tuple1.getObject() );
        new RightTuple( logicalHandle1,
                        sink );

        // Create the second justifieable relationship
        final String logicalString2 = new String( "logical" );
        final InternalFactHandle logicalHandle2 = (InternalFactHandle) workingMemory.insert( logicalString2,
                                                                                             "value2",
                                                                                             false,
                                                                                             true,
                                                                                             rule2,
                                                                                             (Activation) tuple2.getObject() );

        assertSame( logicalHandle1, logicalHandle2 );

        TruthMaintenanceSystem tms = ((NamedEntryPoint)workingMemory.getWorkingMemoryEntryPoint( EntryPointId.DEFAULT.getEntryPointId() ) ).getTruthMaintenanceSystem();
       
        // "logical" should only appear once
        assertEquals( 1,
                      getLogicalCount( tms ) );
       
        BeliefSet bs =  ( BeliefSet ) logicalHandle2.getEqualityKey().getBeliefSet();      
        assertEquals( "value1", ((LogicalDependency) ((LinkedListEntry)bs.getFirst()).getObject()).getValue() );
        assertEquals( "value2", ((LogicalDependency) ((LinkedListEntry)bs.getFirst().getNext()).getObject()).getValue() );

        // Now lets cancel the first activation
        node2.retractLeftTuple( tuple2,
                                context2,
                                workingMemory );

        workingMemory.executeQueuedActions();

        // because this logical fact has two relationships it shouldn't retract yet
        assertLength( 0,
                      sink.getRetracted() );

        // check "logical" is still in the system
        assertEquals( 1,
                      getLogicalCount( ( tms ) ) );

        // now remove that final justification
        node.retractLeftTuple( tuple1,
                               context1,
                               workingMemory );

        workingMemory.executeQueuedActions();

        // Should cause the logical fact to be retracted
        assertLength( 1,
                      sink.getRetracted() );

        // "logical" fact should no longer be in the system
        assertEquals( 0,
                      getLogicalCount( tms ) );
    }
View Full Code Here

        final ObjectTypeNode objectTypeNode = new ObjectTypeNode(idGenerator.getNextId(),
                                                                 this.entryPoint,
                                                                 new ClassObjectType(String.class),
                                                                 buildContext);

        MockRightTupleSink sink = new MockRightTupleSink();

        final RuleImpl rule1 = new RuleImpl("test-rule1");
        final RuleTerminalNode node = new RuleTerminalNode(idGenerator.getNextId(),
                                                           new MockTupleSource(idGenerator.getNextId()),
                                                           rule1,
                                                           rule1.getLhs(),
                                                           0,
                                                           buildContext);
        StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();

        final InternalAgenda agenda = (InternalAgenda) ksession.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) ksession.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,
                              ksession );

        final String logicalString = new String( "logical" );
        InternalFactHandle logicalHandle = (InternalFactHandle) ksession.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 deleted
        node.retractLeftTuple( tuple1,
                               context1,
                               ksession );

        ksession.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,
                              ksession );
        logicalHandle = (InternalFactHandle) ksession.insert( logicalString,
                                                                   null,
                                                                   false,
                                                                   true,
                                                                   rule1,
                                                                   (Activation) tuple1.getObject() );

        new RightTuple( logicalHandle,
                        sink );

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

        node.retractLeftTuple( tuple1,
                               context1,
                               ksession );

        ksession.executeQueuedActions();

        assertLength( 2,
                      sink.getRetracted() );

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

        assertSame( logicalHandle,
                    ((RightTuple) values[0]).getFactHandle() );
    }
View Full Code Here

                                                                  this.entryPoint,
                                                                  new ClassObjectType( String.class ),
                                                                  buildContext );
        objectTypeNode.attach( buildContext );

        MockRightTupleSink sink = new MockRightTupleSink();

        final RuleTerminalNode node = new RuleTerminalNode( idGenerator.getNextId(),
                                                            new MockTupleSource( idGenerator.getNextId() ),
                                                            rule1,
                                                            rule1.getLhs(),
                                                            0,
                                                            buildContext );
        StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();

        final Agenda agenda = ksession.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";
            }
        };

        // Create first justifier
        rule1.setConsequence( consequence );

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

        final PropagationContext context1 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());
        // get the activation onto the agenda
        node.assertLeftTuple( tuple1,
                              context1,
                              ksession );

        // Create the second justifier
        final RuleImpl rule2 = new RuleImpl( "test-rule2" );
        final RuleTerminalNode node2 = new RuleTerminalNode( idGenerator.getNextId(),
                                                             new MockTupleSource( idGenerator.getNextId() ),
                                                             rule2,
                                                             rule2.getLhs(),
                                                             0,
                                                             buildContext );
        rule2.setConsequence( consequence );

        final DefaultFactHandle handle2 = new DefaultFactHandle( 2,
                                                                 "cheese" );
        final RuleTerminalNodeLeftTuple tuple2 = new RuleTerminalNodeLeftTuple( handle2,
                                                                                node2,
                                                                                true );

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

        // get the activations onto the agenda
        node2.assertLeftTuple( tuple2,
                               context2,
                               ksession );

        // Create the first justifieable relationship
        final String logicalString1 = new String( "logical" );
        final InternalFactHandle logicalHandle1 = (InternalFactHandle) ksession.insert( logicalString1,
                                                                                             "value1",
                                                                                             false,
                                                                                             true,
                                                                                             rule1,
                                                                                             (Activation) tuple1.getObject() );
        new RightTuple( logicalHandle1,
                        sink );

        // Create the second justifieable relationship
        final String logicalString2 = new String( "logical" );
        final InternalFactHandle logicalHandle2 = (InternalFactHandle) ksession.insert( logicalString2,
                                                                                             "value2",
                                                                                             false,
                                                                                             true,
                                                                                             rule2,
                                                                                             (Activation) tuple2.getObject() );

        assertSame( logicalHandle1, logicalHandle2 );

        TruthMaintenanceSystem tms = ((NamedEntryPoint)ksession.getWorkingMemoryEntryPoint( EntryPointId.DEFAULT.getEntryPointId() ) ).getTruthMaintenanceSystem();
       
        // "logical" should only appear once
        assertEquals( 1,
                      getLogicalCount( tms ) );
       
        BeliefSet bs =  ( BeliefSet ) logicalHandle2.getEqualityKey().getBeliefSet();      
        assertEquals( "value1", ((LogicalDependency) ((LinkedListEntry)bs.getFirst()).getObject()).getValue() );
        assertEquals( "value2", ((LogicalDependency) ((LinkedListEntry)bs.getFirst().getNext()).getObject()).getValue() );

        // Now lets cancel the first activation
        node2.retractLeftTuple( tuple2,
                                context2,
                                ksession );

        ksession.executeQueuedActions();

        // because this logical fact has two relationships it shouldn't retract yet
        assertLength( 0,
                      sink.getRetracted() );

        // check "logical" is still in the system
        assertEquals( 1,
                      getLogicalCount( ( tms ) ) );

        // now remove that final justification
        node.retractLeftTuple( tuple1,
                               context1,
                               ksession );

        ksession.executeQueuedActions();

        // Should cause the logical fact to be deleted
        assertLength( 1,
                      sink.getRetracted() );

        // "logical" fact should no longer be in the system
        assertEquals( 0,
                      getLogicalCount( tms ) );
    }
View Full Code Here

TOP

Related Classes of org.drools.core.reteoo.MockRightTupleSink

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.