Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
def _swapNotOr(Not, NotParent, NotIndex):
    # not arg is a single subpattern
    # it could be another and/or/not or an
    # ordered/template
    # (not (or must be converted to (or (not (not
    # (not (and (or must be converted to (or (not (and
    changed = False
    if isinstance(Not.pattern, types.OrPatternCE):
        # need to start a new browseOr here
        # before make reversions
        while _browseOr(Not.pattern):
            changed = True
            
        # then reverse (not (or with (or (not
        reversedOrArguments = []
        for inOrPattern in Not.pattern.patterns:
            reversedOrArguments.append(types.NotPatternCE(inOrPattern))
        # then replace the main Not arg with the new Or ([Not, Not,..])
        NotParent[NotIndex] = types.OrPatternCE(reversedOrArguments)
        changed = True
        
    elif isinstance(Not.pattern, types.AndPatternCE):
        # if found an (not (and (???
        # status, i need to try to reverse 
        # all (and (or in the  
        changed = _swapAndOr(Not.pattern, Not, None) or changed
        
        
    return changed
Exemplo n.º 4
0
def _existsToNotNot(Or):
    changed = False
    for (index, inOrPattern) in enumerate(Or.patterns):
        if isinstance(inOrPattern, (types.AndPatternCE, types.OrPatternCE)):
            changed = _existsToNotNot(inOrPattern) or changed
        if isinstance(inOrPattern, types.NotPatternCE):
            if isinstance(inOrPattern.pattern, (types.AndPatternCE, types.OrPatternCE)):
                changed = _existsToNotNot(inOrPattern.pattern) or changed
            elif isinstance(inOrPattern.pattern, types.ExistsPatternCE):
                inOrPattern.pattern = types.NotPatternCE(types.NotPatternCE(inOrPattern.pattern.pattern))
                changed = True
        if isinstance(inOrPattern, types.ExistsPatternCE):
            Or.patterns[index] = types.NotPatternCE(types.NotPatternCE(inOrPattern.pattern))
            changed = True
            
    return changed
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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
                        )