Exemple #1
0
 def __init__(self, aInterpreter=None, aCompleter=None, aNetwork=None):
     self._network = aNetwork or Network()
     self._parser = self._network.getParser()
     self._aMainItem = None
     self._completer = aCompleter or Completer(
         myclips.functions.SystemFunctionBroker.definitions().keys())
     self._readlineOn = False
     self._interpreter = aInterpreter or Interpreter()
     self._interpreter.setShell(self)
     self._interpreter.setNetwork(self._network)
Exemple #2
0
class BaseCircuitTest(MyClipsBaseTest):
    def setUp(self):
        self.network = Network()
        self.interpreter = Interpreter(self.network, None)
        MyClipsBaseTest.setUp(self)

    def _turnOnLogger(self):
        myclips.logger.disabled = False

    def _turnOffLogger(self):
        myclips.logger.disabled = True

    def forCircuits(self, *circuits):

        for s in circuits:
            self.interpreter.evaluate(s)

        aTrap = EventTrap().install(self.network.eventsManager)
        cExpect = CircuitExpect(aTrap)

        self.network.run()

        return cExpect
class BaseCircuitTest(MyClipsBaseTest):

    def setUp(self):
        self.network = Network()
        self.interpreter = Interpreter(self.network, None)
        MyClipsBaseTest.setUp(self)
        
    def _turnOnLogger(self):
        myclips.logger.disabled = False

    def _turnOffLogger(self):
        myclips.logger.disabled = True

    def forCircuits(self, *circuits):
        
        for s in circuits:
            self.interpreter.evaluate(s)
        
        aTrap = EventTrap().install(self.network.eventsManager)
        cExpect = CircuitExpect(aTrap)
        
        self.network.run()
        
        return cExpect
 def setUp(self):
     self.MM = ModulesManager()
     self.MM.addMainScope()
     self.network = Network(EventsManager())
     NetworkPlotter().install(self.network.eventsManager)
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)
Exemple #6
0
 def _functionSetup(self, theClass):
     network = Network()
     self.theEnv = FunctionEnv({}, network, network.modulesManager,
                               network.resources)
     self.theFunc = theClass()
     return self.theFunc
 def setUp(self):
     self.network = Network()
     self.interpreter = Interpreter(self.network, None)
     MyClipsBaseTest.setUp(self)
Exemple #8
0
    def getNetwork(self, aSessionToken):
        '''
        Return the myclips.rete.Network object for the session.
        If no Network has been registered before, a new one will
        be created 
        
        @param aSessionToken: a token for a valid session
        @type aSessionToken: string
        @return: a Network object for this session
        @rtype: myclips.rete.Network 
        '''

        theSessionsService = self._broker.Sessions
        theNetwork = theSessionsService.getProperty(aSessionToken,
                                                    'Engine_MyClips.network',
                                                    None)
        if theNetwork is None:

            #---- Prepare the stdout/stdin redirect ----------#
            #---- for the client through ClientIO service ----#

            resources = {}

            # prepare a stream suppressor to use for an unspecified stream
            suppressor = SuppressStream()

            try:
                # get the service
                theClientIOService = self._broker.ClientIO
            except:
                # no service available, use suppressor for stdin/stdout
                resources['stdout'] = suppressor
                resources['stdin'] = suppressor
            else:
                # resolve each resource to a stream in ClientIO service
                # streamMap items format:
                #    #0: theResourceName is the resource name for the stream
                #    #1[0]: theStreamName is the name of the stream to search for in the ClientIO
                #    #1[1]: theAlternative is the resource name of a resource (already binded) to use as alternative
                streamMap = [('stdout', ['stdout', None]),
                             ('stdin', ['stdin', None]), ('t', ['t',
                                                                'stdout']),
                             ('wclips', ['wclips', 'stdout']),
                             ('wdialog', ['wdialog', 'stdout']),
                             ('wdisplay', ['wdisplay', 'stdout']),
                             ('wwarning', ['wwarning', 'stdout']),
                             ('werror', ['werror', 'stdout']),
                             ('wtrace', ['wtrace', 'stdout'])]

                for resourceName, (streamName,
                                   otherResourceAlternative) in streamMap:
                    try:
                        # search a stream for the resource
                        resources[resourceName] = theClientIOService.getStream(
                            aSessionToken, streamName)
                    except:
                        # ... but it isn't found
                        try:
                            # ... i no alternative is possible, just use the suppressor
                            if otherResourceAlternative is None:
                                raise Exception()
                            # ... otherwise try to bind to a previous bound resource
                            resources[resourceName] = resources[
                                otherResourceAlternative]
                        except:
                            # the previous bound resource is not availalble, use the suppressor
                            resources[resourceName] = suppressor

            #---- Create the instance of Network for the client ----#
            #---- and store it in the session ----------------------#

            myclips_server.logger.debug(
                "New Engine:\n\tSession: %s\n\tResources: %s", aSessionToken,
                resources)

            # use the map of resources in the Network initialization
            theNetwork = Network(resources=resources)
            theSessionsService.setProperty(aSessionToken,
                                           'Engine_MyClips.network',
                                           theNetwork)

        return theNetwork
 def setUp(self):
     self.MM = ModulesManager()
     self.MM.addMainScope()
     self.network = Network(EventsManager())
     NetworkPlotter().install(self.network.eventsManager)
 def setUp(self):
     self.MM = ModulesManager()
     self.MM.addMainScope()
     self.network = Network(eventsManager=EventsManager(),modulesManager=self.MM)
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)
Exemple #12
0
 def setUp(self):
     self.network = Network()
     self.interpreter = Interpreter(self.network, None)
     MyClipsBaseTest.setUp(self)
Exemple #13
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)
Exemple #14
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)
            
        
 def setUp(self):
     self.MM = Network().modulesManager
Exemple #16
0
            return types.Symbol("*** READ ERROR ***")


Read.DEFINITION = FunctionDefinition(
    "?SYSTEM?",
    "read",
    Read(), (types.Symbol, types.String, types.Lexeme, types.Integer,
             types.Float, types.Number),
    Read.do, [
        Constraint_MaxArgsLength(1),
        Constraint_ArgType(types.Symbol, 0, failIfMissing=False)
    ],
    forward=False)

if __name__ == '__main__':

    import os
    from myclips.functions import FunctionEnv
    from myclips.rete.Network import Network
    n = Network()
    fr = open(os.path.dirname(__file__) + "/read-test.txt", "rU")
    theEnv = FunctionEnv({}, n, n.modulesManager, {"t": sys.stdout, "fr": fr})
    r = Read()
    while True:
        s = r.do(theEnv, types.Symbol("fr"))
        print repr(s)
        if s.pyEqual("EOF"):
            break

    print repr(r.do(theEnv, types.Symbol("t")))
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)