def test_NestedNotOrReversion(self):

        lhs = [
            types.NotPatternCE(
                types.OrPatternCE([
                    "C", "D",
                    types.AndPatternCE([
                        "Z",
                        types.NotPatternCE(types.OrPatternCE(["W", "X"]))
                    ])
                ]))
        ]

        lhs = analysis.normalizeLHS(lhs, self.MM)

        self.assertIsInstance(lhs, types.OrPatternCE)

        permutations = []
        for i in range(0, 4):
            self.assertIsInstance(lhs.patterns[i], types.AndPatternCE)
            self.assertIsInstance(lhs.patterns[i].patterns[1],
                                  types.NotPatternCE)
            if isinstance(lhs.patterns[i].patterns[1].pattern,
                          types.AndPatternCE):
                permutations.append("~({0})".format(" ".join([
                    t if not isinstance(t, types.NotPatternCE) else "~" +
                    t.pattern
                    for t in lhs.patterns[i].patterns[1].pattern.patterns
                ])))
            else:
                permutations.append("~" + lhs.patterns[i].patterns[1].pattern)

        permutationExpected = ["~C", "~D", "~(Z ~W)", "~(Z ~X)"]

        self.assertEqual(permutations, permutationExpected)
    def test_NestedNotOrReversion(self):


        lhs = [types.NotPatternCE(
                    types.OrPatternCE(["C", "D",
                        types.AndPatternCE(["Z",
                                types.NotPatternCE(
                                    types.OrPatternCE(["W", "X"])
                                ) 
                            ])
                        ])
            )]

        lhs = analysis.normalizeLHS(lhs,  self.MM)

        self.assertIsInstance(lhs, types.OrPatternCE)
        
        permutations = []
        for i in range(0,4):
            self.assertIsInstance(lhs.patterns[i], types.AndPatternCE)
            self.assertIsInstance(lhs.patterns[i].patterns[1], types.NotPatternCE)
            if isinstance(lhs.patterns[i].patterns[1].pattern, types.AndPatternCE):
                permutations.append("~({0})".format(" ".join([t if not isinstance(t, types.NotPatternCE) else "~"+t.pattern 
                                     for t in lhs.patterns[i].patterns[1].pattern.patterns])))
            else:
                permutations.append("~"+lhs.patterns[i].patterns[1].pattern)
            
        permutationExpected = ["~C", "~D", "~(Z ~W)", "~(Z ~X)"]
        
        self.assertEqual(permutations, permutationExpected)
    def test_NestedAndOrReversion(self):

        lhs = [
            types.AndPatternCE([
                types.OrPatternCE(["A", "B"]),
                types.OrPatternCE([
                    "C", "D",
                    types.AndPatternCE(["Z",
                                        types.OrPatternCE(["W", "X"])])
                ])
            ])
        ]

        lhs = analysis.normalizeLHS(lhs, self.MM)

        self.assertIsInstance(lhs, types.OrPatternCE)
        permutations = []
        for i in range(0, 8):
            self.assertIsInstance(lhs.patterns[i], types.AndPatternCE)
            permutations.append(lhs.patterns[i].patterns)

        permutationExpected = [["A", "C"], ["B", "C"], ["A", "D"], ["B", "D"],
                               ["A", "Z", "W"], ["A", "Z", "X"],
                               ["B", "Z", "W"], ["B", "Z", "X"]]

        self.assertEqual(permutations, permutationExpected)
示例#4
0
    def addRule(self, defrule):
        '''
        Compile a DefRuleConstruct in a network circuit
        sharing nodes if possible
        @param defrule: a DefRule construct which describe the rule
        @type defrule: types.DefRuleConstruct
        @return: the main PNode produced by the rule. If
            the rule containts more than one OR clause,
            other slave-PNodes are linked to the main one
        @rtype: myclips.rete.nodes.PNode
        '''

        if self._rules.has_key("::".join(
            [defrule.scope.moduleName, defrule.defruleName])):
            # before add the new rule, need to remove the old one
            self.removeRule(defrule.defruleName, defrule.scope.moduleName)

        #normalize defule lhs
        defrule.lhs = analysis.normalizeLHS(defrule.lhs, defrule.scope.modules)
        # after normalization:
        #    defrule.lhs is a OrPatternCE with at least a nested AndPatternCe
        firstPNode = None
        for (index, AndInOr) in enumerate(defrule.lhs.patterns):

            variables = {}

            lastNode, _ = self._makeNetwork(None, AndInOr.patterns, 0,
                                            variables)

            # I need to create a PNode (and it must always linked to the first PNode created)
            pNode = PNode(ruleName=defrule.defruleName,
                          leftParent=lastNode,
                          network=self,
                          orClauseCount=index - 1 if index > 0 else None,
                          rhs=defrule.rhs,
                          properties=analysis.normalizeDeclarations(
                              defrule.defruleDeclaration),
                          variables=variables)

            lastNode.prependChild(pNode)
            lastNode.updateChild(pNode)

            self.eventsManager.fire(EventsManager.E_NODE_ADDED, pNode)
            self.eventsManager.fire(EventsManager.E_NODE_LINKED, lastNode,
                                    pNode, -1)

            if firstPNode is None:
                firstPNode = pNode
            else:
                firstPNode.linkOrClause(pNode)

        # store the main PNode
        # inside the rules map

        self._rules[firstPNode.completeMainRuleName()] = firstPNode

        return firstPNode
示例#5
0
 def addRule(self, defrule):
     '''
     Compile a DefRuleConstruct in a network circuit
     sharing nodes if possible
     @param defrule: a DefRule construct which describe the rule
     @type defrule: types.DefRuleConstruct
     @return: the main PNode produced by the rule. If
         the rule containts more than one OR clause,
         other slave-PNodes are linked to the main one
     @rtype: myclips.rete.nodes.PNode
     '''
     
     
     if self._rules.has_key("::".join([defrule.scope.moduleName, defrule.defruleName])):
         # before add the new rule, need to remove the old one
         self.removeRule(defrule.defruleName, defrule.scope.moduleName)
         
     
     #normalize defule lhs
     defrule.lhs = analysis.normalizeLHS(defrule.lhs, defrule.scope.modules)
     # after normalization:
     #    defrule.lhs is a OrPatternCE with at least a nested AndPatternCe
     firstPNode = None
     for (index, AndInOr) in enumerate(defrule.lhs.patterns):
         
         variables = {}
         
         lastNode, _ = self._makeNetwork(None, AndInOr.patterns, 0, variables)
         
         # I need to create a PNode (and it must always linked to the first PNode created)
         pNode = PNode(ruleName=defrule.defruleName, 
                       leftParent=lastNode, 
                       network=self, 
                       orClauseCount=index - 1 if index > 0 else None,
                       rhs=defrule.rhs, 
                       properties=analysis.normalizeDeclarations(defrule.defruleDeclaration),
                       variables=variables)
         
         lastNode.prependChild(pNode)
         lastNode.updateChild(pNode)
         
         self.eventsManager.fire(EventsManager.E_NODE_ADDED, pNode)
         self.eventsManager.fire(EventsManager.E_NODE_LINKED, lastNode, pNode, -1)
         
         if firstPNode is None:
             firstPNode = pNode
         else:
             firstPNode.linkOrClause(pNode)
     
     # store the main PNode
     # inside the rules map
     
     self._rules[firstPNode.completeMainRuleName()] = firstPNode
     
     return firstPNode
    def test_SimpleAndOrReversion(self):

        lhs = [types.AndPatternCE([types.OrPatternCE(["A", "B"]), "C"])]

        lhs = analysis.normalizeLHS(lhs, self.MM)

        self.assertIsInstance(lhs, types.OrPatternCE)
        self.assertIsInstance(lhs.patterns[0], types.AndPatternCE)
        self.assertIsInstance(lhs.patterns[1], types.AndPatternCE)
        self.assertEqual(lhs.patterns[0].patterns, ["A", "C"])
        self.assertEqual(lhs.patterns[1].patterns, ["B", "C"])
 def test_SimpleAndOrReversion(self):
     
     lhs = [types.AndPatternCE([
                 types.OrPatternCE(["A", "B"]),
                 "C"
         ])]
     
     lhs = analysis.normalizeLHS(lhs,  self.MM)
     
     self.assertIsInstance(lhs, types.OrPatternCE)
     self.assertIsInstance(lhs.patterns[0], types.AndPatternCE)
     self.assertIsInstance(lhs.patterns[1], types.AndPatternCE)
     self.assertEqual(lhs.patterns[0].patterns, ["A", "C"])
     self.assertEqual(lhs.patterns[1].patterns, ["B", "C"])
    def test_SimpleNotOrReversion(self):

        lhs = [types.NotPatternCE(types.OrPatternCE(["C", "D"]))]

        lhs = analysis.normalizeLHS(lhs, self.MM)

        self.assertIsInstance(lhs, types.OrPatternCE)

        permutations = []
        for i in range(0, 2):
            self.assertIsInstance(lhs.patterns[i], types.AndPatternCE)
            self.assertIsInstance(lhs.patterns[i].patterns[1],
                                  types.NotPatternCE)
            permutations.append(lhs.patterns[i].patterns[1].pattern)

        permutationExpected = ["C", "D"]

        self.assertEqual(permutations, permutationExpected)
    def test_SimpleNotOrReversion(self):


        lhs = [types.NotPatternCE(
                    types.OrPatternCE(["C", "D"])
            )]

        lhs = analysis.normalizeLHS(lhs, self.MM)

        self.assertIsInstance(lhs, types.OrPatternCE)
        
        permutations = []
        for i in range(0,2):
            self.assertIsInstance(lhs.patterns[i], types.AndPatternCE)
            self.assertIsInstance(lhs.patterns[i].patterns[1], types.NotPatternCE)
            permutations.append(lhs.patterns[i].patterns[1].pattern)
            
        permutationExpected = ["C", "D"]
        
        self.assertEqual(permutations, permutationExpected)
示例#10
0
    def test_NestedAndOrReversion(self):
        
        lhs = [types.AndPatternCE([
                    types.OrPatternCE(["A", "B"]),
                    types.OrPatternCE(["C", "D",
                        types.AndPatternCE(["Z",
                                types.OrPatternCE(["W", "X"]) 
                            ])
                        ])
            ])]

        lhs = analysis.normalizeLHS(lhs, self.MM)
        
        self.assertIsInstance(lhs, types.OrPatternCE)
        permutations = []
        for i in range(0,8):
            self.assertIsInstance(lhs.patterns[i], types.AndPatternCE)
            permutations.append(lhs.patterns[i].patterns)
            
        permutationExpected = [["A", "C"], ["B", "C"], ["A", "D"], ["B", "D"],  
                               ["A", "Z", "W"], ["A", "Z", "X"], ["B", "Z", "W"], ["B", "Z", "X"]]
        
        self.assertEqual(permutations, permutationExpected)