class NetworkPlotterTest(MyClipsBaseTest):


    def setUp(self):
        self.MM = ModulesManager()
        self.MM.addMainScope()
        self.network = Network(EventsManager())
        NetworkPlotter().install(self.network.eventsManager)

    def tearDown(self):
        self.network.eventsManager.unregisterObserver()

    def test_NetworkPlotting_AlphaAndDummyJoinOnly(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY, self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN, self.network)

    def test_NetworkPlotting_TemplateAlphaAndDummyJoinOnly(self):
        
        types.DefTemplateConstruct("aTemplate", self.MM, None, [
                types.SingleSlotDefinition("A"),
                types.SingleSlotDefinition("B"),
                types.SingleSlotDefinition("C")
            ])

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.TemplatePatternCE("aTemplate", self.MM, [
                        types.SingleFieldLhsSlot("A", types.Symbol("A")),
                        types.SingleFieldLhsSlot("B", types.Symbol("B")),
                        types.SingleFieldLhsSlot("C", types.Symbol("C"))
                    ])
            ]))
        
        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY, self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN, self.network)

    def test_NetworkPlotting_WithVarBindings(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("C"),
                    ], self.MM),
                types.OrderedPatternCE([
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("A"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY, self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN, self.network)


    def test_NetworkPlotting_DefRuleWithOrClause(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrPatternCE([
                    types.OrderedPatternCE([
                            types.Symbol("A"),
                            types.SingleFieldVariable(types.Symbol("varA")),
                            types.Symbol("C"),
                        ], self.MM),
                    types.OrderedPatternCE([
                            types.SingleFieldVariable(types.Symbol("varA")),
                            types.Symbol("A"),
                            types.Symbol("C"),
                        ], self.MM)
                ])
            ]))
        
        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY, self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN, self.network)
Example #2
0
class NetworkTest(MyClipsBaseTest):


    def setUp(self):
        self.MM = ModulesManager()
        self.MM.addMainScope()
        self.network = Network(eventsManager=EventsManager(),modulesManager=self.MM)
        
#        import sys
#        from myclips.listeners.NetworkBuildPrinter import NetworkBuildPrinter
#        NetworkBuildPrinter(sys.stderr).install(self.network.eventsManager)
        
        
    def tearDown(self):
        unittest.TestCase.tearDown(self)
        #self.printer.uninstall()
        self.network.eventsManager.unregisterObserver()


    def test_AlphaCircuitCompilationOrdered(self):

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root, RootNode)
        self.assertIsInstance(self.network._root.children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory, AlphaMemory)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0], JoinNode)


    def test_AlphaCircuitCompilationTemplate(self):

        types.DefTemplateConstruct("aTemplate", self.MM, None, [
                types.SingleSlotDefinition("A"),
                types.SingleSlotDefinition("B"),
                types.SingleSlotDefinition("C")
            ])

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.TemplatePatternCE("aTemplate", self.MM, [
                        types.SingleFieldLhsSlot("A", types.Symbol("A")),
                        types.SingleFieldLhsSlot("B", types.Symbol("B")),
                        types.SingleFieldLhsSlot("C", types.Symbol("C"))
                    ])
            ]))
        
        self.assertIsInstance(self.network._root, RootNode)
        self.assertIsInstance(self.network._root.children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0], PropertyTestNode)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory, AlphaMemory)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0], JoinNode)
    

    def test_DummyJoinNodeCreationAfterFirstPatternCE(self):

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0], JoinNode)
        self.assertTrue(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0].isLeftRoot())



    def test_AssertFact(self):

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))

        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))

        self.assertEqual(len(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.items), 1)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.items[0], WME)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.items[0].fact, theFactClasses)
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.items[0].fact[0], types.Symbol)

    def test_RetractFact(self):
        
        self.network.retractFact(self.network.getWmeFromId(0))
        
        self.assertEqual(len(self.network.facts), 0)


    def test_RetractUsedFact(self):

        prevLen = len(self.network.facts)

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        wme, _ = self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.network.retractFact(self.network.getWmeFromId(wme.factId))
        
        self.assertEqual(len(self.network.facts), prevLen)


    def test_RetractUsedFact0(self):

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[]))
        self.network.retractFact(self.network.getWmeFromId(0))
        self.assertEqual(len(self.network.facts), 0)
        

    def test_DummyJoinNodePropagation(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0], JoinNode)
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0].appendChild(trap)
        
        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertTrue(trap.leftCatch)
        
        
    def test_AlphaMemoryPropagationOrdered(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0], JoinNode)
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].children[0].children[0].memory.appendChild(trap)
        
        
        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertTrue(trap.rightCatch)
        

    def test_AlphaMemoryPropagationTemplate(self):
        
        types.DefTemplateConstruct("aTemplate", self.MM, None, [
                types.SingleSlotDefinition("A"),
                types.SingleSlotDefinition("B"),
                types.SingleSlotDefinition("C")
            ])

        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.TemplatePatternCE("aTemplate", self.MM, [
                        types.SingleFieldLhsSlot("A", types.Symbol("A")),
                        types.SingleFieldLhsSlot("B", types.Symbol("B")),
                        types.SingleFieldLhsSlot("C", types.Symbol("C"))
                    ])
            ]))
                
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].children[0].memory.children[0], JoinNode)
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].children[0].children[0].memory.appendChild(trap)
        
        
        self.network.assertFact(fact({"A": types.Symbol("A"), "B": types.Symbol("B"), "C": types.Symbol("C")}, "aTemplate"))
        
        self.assertTrue(trap.rightCatch)
        

    def test_JoinNodeVarBindingOnOrderedPatternCE(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("C"),
                    ], self.MM),
                types.OrderedPatternCE([
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("A"),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].children[0].memory.children[0], JoinNode)
        
        self.assertFalse(self.network._root.children[0].children[0].children[0].children[0].memory.children[0].isLeftRoot())
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].children[0].memory.children[0].appendChild(trap)
        
        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))

        f = fact([types.Symbol("B"), types.Symbol("A"), types.Symbol("C")])        
        self.network.assertFact(f)

       
        self.assertTrue(trap.leftCatch)
        

    def test_JoinNodeVarBindingOnOrderedPatternCEWithNegativeTermOverVariable(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("C"),
                    ], self.MM),
                types.OrderedPatternCE([
                        types.NegativeTerm(types.SingleFieldVariable(types.Symbol("varA")), True),
                        types.UnnamedSingleFieldVariable(),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].memory.children[0], JoinNode)
        
        self.assertFalse(self.network._root.children[0].children[0].children[0].memory.children[0].isLeftRoot())
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].memory.children[0].appendChild(trap)
        
        f = fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")])
                
        self.network.assertFact(f)
       
        self.assertTrue(trap.leftCatch)
        

    def test_JoinNodeIntraElementVarBindingTestInBetaNetwork(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].memory.children[0], JoinNode)
        
        self.assertTrue(self.network._root.children[0].children[0].children[0].memory.children[0].isLeftRoot())
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].memory.children[0].appendChild(trap)
        
        f = fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")])
        self.network.assertFact(f)
        
        self.assertFalse(trap.leftCatch)

        f = fact([types.Symbol("A"), types.Symbol("A"), types.Symbol("C")])
        self.network.assertFact(f)
        
        self.assertTrue(trap.leftCatch)
        
        
    def test_AlphaCircuitWithMultifield(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.MultiFieldVariable(types.Symbol("varB")),
                        types.Symbol("C"),
                    ], self.MM)
            ]))
        
        self.assertIsInstance(self.network._root.children[0].children[0].children[0].memory, AlphaMemory)
        
        #self.assertTrue(self.network._root.children[0].children[0].memory.children[0].isLeftRoot())
        
        trap = activationCatcher()
        
        self.network._root.children[0].children[0].children[0].memory.prependChild(trap)
        
        f = fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("B2"), types.Symbol("C")])
        self.network.assertFact(f)
       
        self.assertNotEqual(len(self.network._root.children[0].children[0].children[0].memory.items), 0)
        
        self.assertTrue(trap.rightCatch)
        

    def test_NegativeJoinBetaCircuitCompilation(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.OrderedPatternCE([
                            types.Symbol("C"),
                            types.Symbol("B"),
                            types.Symbol("A"),
                        ], self.MM))
            ]))

        self.assertIsInstance(self.network._root
                                .children[0] #MAIN
                                .children[0] #C
                                .children[0] #B
                                .children[0] #A
                                .children[0] #LEN
                                .memory #AM
                                .children[0], NegativeJoinNode)

    def test_NegativeJoinBetaCircuitTrueCondition(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.OrderedPatternCE([
                            types.Symbol("C"),
                            types.Symbol("B"),
                            types.Symbol("A"),
                        ], self.MM))
            ]))
        
        trap = activationCatcher()
        
        (self.network._root.children[0] #MAIN
                        .children[0]    #C
                        .children[0]    #B
                        .children[0]    #A
                        .children[0]    #LEN
                        .memory         #AM
                        .children[0]).prependChild(trap)

        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertTrue(trap.leftCatch)


        
    def test_NegativeJoinBetaCircuitFalseCondition(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.OrderedPatternCE([
                            types.Symbol("C"),
                            types.Symbol("B"),
                            types.Symbol("A"),
                        ], self.MM))
            ]))

        
        trap = activationCatcher()
        
        (self.network._root.children[0] #MAIN
                        .children[0]    #C
                        .children[0]    #B
                        .children[0]    #A
                        .children[0]    #LEN
                        .memory         #AM
                        .children[0]).prependChild(trap)

        self.network.assertFact(fact([types.Symbol("C"), types.Symbol("B"), types.Symbol("A")]))
        
        self.assertFalse(trap.leftCatch)
        
        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertFalse(trap.leftCatch)



    def test_NccBetaCircuitCompilation(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.AndPatternCE([
                        types.OrderedPatternCE([
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                            ], self.MM),
                        types.OrderedPatternCE([
                                types.Symbol("W"),
                                types.Symbol("W"),
                                types.Symbol("W"),
                            ], self.MM)
                        ]))
            ]))

        # check main branch
        self.assertIsInstance(self.network._root
                                .children[0] #MAIN
                                .children[-1] #A
                                .children[-1] #B
                                .children[-1] #C
                                .children[-1] #LEN 3
                                .memory #AM
                                .children[-1] #DUMMYJOIN
                                .children[-1] #NCC
                                , NccNode)        

        # check partner branch
        self.assertIsInstance(self.network._root
                                .children[0] #MAIN
                                .children[-2] #Z
                                .children[-1] #Z
                                .children[-1] #Z
                                .children[-1] #LEN 3
                                .memory #AM
                                .children[-1] #DUMMYJOIN
                                .children[-1] #BETA
                                .children[-1] #JOIN
                                .children[-1]
                                , NccPartnerNode)        

        # check if nccPartner is linked to nccNode
        self.assertEqual(self.network._root
                                .children[0] #MAIN
                                .children[-2] #Z
                                .children[-1] #Z
                                .children[-1] #Z
                                .children[-1] #LEN 3
                                .memory #AM
                                .children[-1] #DUMMYJOIN
                                .children[-1] #BETA
                                .children[-1] #JOIN
                                .children[-1].nccNode
                                ,
                        self.network._root
                                .children[0] #MAIN
                                .children[-1] #A
                                .children[-1] #B
                                .children[-1] #C
                                .children[-1] #LEN 3
                                .memory #AM
                                .children[-1] #DUMMYJOIN
                                .children[-1] #NCC
                        )        

        # check if NccNode is linked to NccPartner
        self.assertEqual(self.network._root
                                .children[0] #MAIN
                                .children[-2] #Z
                                .children[-1] #Z
                                .children[-1] #Z
                                .children[-1] #LEN 3
                                .memory #AM
                                .children[-1] #DUMMYJOIN
                                .children[-1] #BETA
                                .children[-1] #JOIN
                                .children[-1]
                                ,
                        self.network._root
                                .children[0] #MAIN
                                .children[-1] #A
                                .children[-1] #B
                                .children[-1] #C
                                .children[-1] #LEN 3
                                .memory #AM
                                .children[-1] #DUMMYJOIN
                                .children[-1].partner
                        )        


    def test_NccBetaCircuitPropagationBothSubMissing(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.AndPatternCE([
                        types.OrderedPatternCE([
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                            ], self.MM),
                        types.OrderedPatternCE([
                                types.Symbol("W"),
                                types.Symbol("W"),
                                types.Symbol("W"),
                            ], self.MM)
                        ]))
            ]))

        trap = activationCatcher()
        
        (self.network._root.children[0] #MAIN
                            .children[-1] #A
                            .children[-1] #B
                            .children[-1] #C
                            .children[-1] #LEN 3
                            .memory #AM
                            .children[-1] #DUMMYJOIN
                            .children[-1] #NCC
                            ).prependChild(trap)

        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertTrue(trap.leftCatch)
        

    def test_NccBetaCircuitPropagationSecondSubMissing(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.AndPatternCE([
                        types.OrderedPatternCE([
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                            ], self.MM),
                        types.OrderedPatternCE([
                                types.Symbol("W"),
                                types.Symbol("W"),
                                types.Symbol("W"),
                            ], self.MM)
                        ]))
            ]))

        trap = activationCatcher()
        
        (self.network._root.children[0] #MAIN
                            .children[-1] #A
                            .children[-1] #B
                            .children[-1] #C
                            .children[-1] #LEN 3
                            .memory #AM
                            .children[-1] #DUMMYJOIN
                            .children[-1] #NCC
                            ).prependChild(trap)

        self.network.assertFact(fact([types.Symbol("Z"), types.Symbol("Z"), types.Symbol("Z")]))

        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertTrue(trap.leftCatch)
        
    def test_NccBetaCircuitPropagationFirstSubMissing(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.AndPatternCE([
                        types.OrderedPatternCE([
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                            ], self.MM),
                        types.OrderedPatternCE([
                                types.Symbol("W"),
                                types.Symbol("W"),
                                types.Symbol("W"),
                            ], self.MM)
                        ]))
            ]))

        trap = activationCatcher()
        
        (self.network._root.children[0] #MAIN
                            .children[-1] #A
                            .children[-1] #B
                            .children[-1] #C
                            .children[-1] #LEN 3
                            .memory #AM
                            .children[-1] #DUMMYJOIN
                            .children[-1] #NCC
                            ).prependChild(trap)

        self.network.assertFact(fact([types.Symbol("W"), types.Symbol("W"), types.Symbol("W")]))

        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertTrue(trap.leftCatch)
        
    def test_NccBetaCircuitNotPropagation(self):
        
        self.network.addRule(types.DefRuleConstruct("A", self.MM, lhs=[
                types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.Symbol("B"),
                        types.Symbol("C"),
                    ], self.MM),
                types.NotPatternCE(
                    types.AndPatternCE([
                        types.OrderedPatternCE([
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                                types.Symbol("Z"),
                            ], self.MM),
                        types.OrderedPatternCE([
                                types.Symbol("W"),
                                types.Symbol("W"),
                                types.Symbol("W"),
                            ], self.MM)
                        ]))
            ]))

        trap = activationCatcher()
        
        (self.network._root.children[0] #MAIN
                            .children[-1] #A
                            .children[-1] #B
                            .children[-1] #C
                            .children[-1] #LEN 3
                            .memory #AM
                            .children[-1] #DUMMYJOIN
                            .children[-1] #NCC
                            ).prependChild(trap)

        self.network.assertFact(fact([types.Symbol("Z"), types.Symbol("Z"), types.Symbol("Z")]))

        self.network.assertFact(fact([types.Symbol("W"), types.Symbol("W"), types.Symbol("W")]))

        self.network.assertFact(fact([types.Symbol("A"), types.Symbol("B"), types.Symbol("C")]))
        
        self.assertFalse(trap.leftCatch)
Example #3
0
    MM = ModulesManager()
    MM.addMainScope()
    
    try:
    
        parsed = Parser(modulesManager=MM, debug=False).parse(s)
    
        [constructs_prettyprint(repr(x)) for x in parsed if isinstance(x, ParsedType)]
            
    except Exception, err:
        try:
            print Parser.ExceptionPPrint(err, s)
        except:
            # raise the original exception,
            # pretty printer failed
            raise err
#    finally:
#        for scopeName in MM.getModulesNames():
#            print MM.getScope(scopeName)
        
    n = Network(modulesManager=MM)
    
    NetworkBuildPrinter(sys.stdout).install(n.eventsManager)
        
    for p in parsed:
        
        if p.__class__.__name__ == 'DefRuleConstruct':
            
            n.addRule(p)
            
        
Example #4
0
    try:

        parsed = Parser(modulesManager=MM, debug=False).parse(s)

        [
            constructs_prettyprint(repr(x)) for x in parsed
            if isinstance(x, ParsedType)
        ]

    except Exception, err:
        try:
            print Parser.ExceptionPPrint(err, s)
        except:
            # raise the original exception,
            # pretty printer failed
            raise err
#    finally:
#        for scopeName in MM.getModulesNames():
#            print MM.getScope(scopeName)

    n = Network(modulesManager=MM)

    NetworkBuildPrinter(sys.stdout).install(n.eventsManager)

    for p in parsed:

        if p.__class__.__name__ == 'DefRuleConstruct':

            n.addRule(p)
class NetworkPlotterTest(MyClipsBaseTest):
    def setUp(self):
        self.MM = ModulesManager()
        self.MM.addMainScope()
        self.network = Network(EventsManager())
        NetworkPlotter().install(self.network.eventsManager)

    def tearDown(self):
        self.network.eventsManager.unregisterObserver()

    def test_NetworkPlotting_AlphaAndDummyJoinOnly(self):

        self.network.addRule(
            types.DefRuleConstruct("A",
                                   self.MM,
                                   lhs=[
                                       types.OrderedPatternCE([
                                           types.Symbol("A"),
                                           types.Symbol("B"),
                                           types.Symbol("C"),
                                       ], self.MM)
                                   ]))

        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY,
                                        self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN,
                                        self.network)

    def test_NetworkPlotting_TemplateAlphaAndDummyJoinOnly(self):

        types.DefTemplateConstruct("aTemplate", self.MM, None, [
            types.SingleSlotDefinition("A"),
            types.SingleSlotDefinition("B"),
            types.SingleSlotDefinition("C")
        ])

        self.network.addRule(
            types.DefRuleConstruct(
                "A",
                self.MM,
                lhs=[
                    types.TemplatePatternCE("aTemplate", self.MM, [
                        types.SingleFieldLhsSlot("A", types.Symbol("A")),
                        types.SingleFieldLhsSlot("B", types.Symbol("B")),
                        types.SingleFieldLhsSlot("C", types.Symbol("C"))
                    ])
                ]))

        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY,
                                        self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN,
                                        self.network)

    def test_NetworkPlotting_WithVarBindings(self):

        self.network.addRule(
            types.DefRuleConstruct(
                "A",
                self.MM,
                lhs=[
                    types.OrderedPatternCE([
                        types.Symbol("A"),
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("C"),
                    ], self.MM),
                    types.OrderedPatternCE([
                        types.SingleFieldVariable(types.Symbol("varA")),
                        types.Symbol("A"),
                        types.Symbol("C"),
                    ], self.MM)
                ]))

        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY,
                                        self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN,
                                        self.network)

    def test_NetworkPlotting_DefRuleWithOrClause(self):

        self.network.addRule(
            types.DefRuleConstruct(
                "A",
                self.MM,
                lhs=[
                    types.OrPatternCE([
                        types.OrderedPatternCE([
                            types.Symbol("A"),
                            types.SingleFieldVariable(types.Symbol("varA")),
                            types.Symbol("C"),
                        ], self.MM),
                        types.OrderedPatternCE([
                            types.SingleFieldVariable(types.Symbol("varA")),
                            types.Symbol("A"),
                            types.Symbol("C"),
                        ], self.MM)
                    ])
                ]))

        # manually fire the network ready event
        self.network.eventsManager.fire(EventsManager.E_NETWORK_READY,
                                        self.network)
        self.network.eventsManager.fire(EventsManager.E_NETWORK_SHUTDOWN,
                                        self.network)