예제 #1
0
    def setUp(self):
        # Graph from Figure 2 in [Bex2006]
        self.tree = Graph(nodes = "abcde")
        self.tree.addnode(StartNode)
        self.tree.addnode(EndNode)
        # StartNode
        self.tree.addedge((StartNode, 'b'))
        self.tree.addedge((StartNode, 'c'))
        # Node `a`
        self.tree.addedge(('a', 'c'))
        # Node `b`
        self.tree.addedge(('b', 'a'))
        # Node `c`
        self.tree.addedge(('c', 'a'))
        self.tree.addedge(('c', 'b'))
        self.tree.addedge(('c', 'd'))
        # Node `d`
        self.tree.addedge(('d', 'a'))
        self.tree.addedge(('d', 'b'))
        self.tree.addedge(('d', 'e'))
        # Node `e`
        self.tree.addedge(('e', EndNode))

        self.cycle = Graph(nodes = "abc")
        self.cycle.addnode(StartNode)
        self.cycle.addnode(EndNode)
        for x in "abc":
            self.cycle.addedge((StartNode, x))
            self.cycle.addedge((x, EndNode))
        self.cycle.addedge(('a', 'b'))
        self.cycle.addedge(('b', 'c'))
        self.cycle.addedge(('c', 'a'))
예제 #2
0
 def prepareGraph(self):
     # Graph from Figure 1 in [Bex2006]
     self.graph = Graph(nodes = "abcde")
     self.graph.addnode(StartNode)
     self.graph.addnode(EndNode)
     # StartNode
     self.graph.addedge((StartNode, 'a'))
     self.graph.addedge((StartNode, 'b'))
     self.graph.addedge((StartNode, 'c'))
     # Node `a`
     self.graph.addedge(('a', 'a'))
     self.graph.addedge(('a', 'b'))
     self.graph.addedge(('a', 'c'))
     self.graph.addedge(('a', 'd'))
     # Node `b`
     self.graph.addedge(('b', 'a'))
     self.graph.addedge(('b', 'c'))
     # Node `c`
     self.graph.addedge(('c', 'a'))
     self.graph.addedge(('c', 'b'))
     self.graph.addedge(('c', 'c'))
     self.graph.addedge(('c', 'd'))
     # Node `d`
     self.graph.addedge(('d', 'b'))
     self.graph.addedge(('d', 'e'))
     self.graph.addedge(('d', 'a'))
     self.graph.addedge(('d', 'c'))
     # Node `e`
     self.graph.addedge(('e', EndNode))
예제 #3
0
 def prepareRandomGraph(self):
     import random
     size = random.randint(10, 20)
     self.rndgraph = Graph(nodes = range(1, size))
     for source in xrange(1, size):
         for target in xrange(source, size):
             if random.random() > 0.5:
                 self.rndgraph.addedge((source, target))
예제 #4
0
 def setUp(self):
     self.graph = Graph(nodes = range(1, 8))
     self.graph.addedge((1, 3))
     self.graph.addedge((2, 3))
     self.graph.addedge((3, 4))
     self.graph.addedge((4, 5))
     self.graph.addedge((5, 6))
     self.graph.addedge((7, 5))
     self.eo4 = eo4 = EmptyObject(4)
     self.eo3 = eo3 = EmptyObject(3)
     self.eo5 = eo5 = EmptyObject(5)
     self.graph.replacenode(4, eo4)
     self.graph.replacenode(3, eo3)
     self.graph.replacenode(5, eo5)
예제 #5
0
def infer_automata(sequences):
    __graph__ = Graph([StartNode, EndNode], [])

    for sequence in sequences:
        last = StartNode
        for item in sequence:
            __graph__.addnode(item)
            __graph__.createedge(last, item)
            last = item

        __graph__.createedge(last, EndNode)

    return __graph__
예제 #6
0
class IDTDTests(unittest.TestCase):
    def setUp(self):
        # Graph from Figure 2 in [Bex2006]
        self.tree = Graph(nodes = "abcde")
        self.tree.addnode(StartNode)
        self.tree.addnode(EndNode)
        # StartNode
        self.tree.addedge((StartNode, 'b'))
        self.tree.addedge((StartNode, 'c'))
        # Node `a`
        self.tree.addedge(('a', 'c'))
        # Node `b`
        self.tree.addedge(('b', 'a'))
        # Node `c`
        self.tree.addedge(('c', 'a'))
        self.tree.addedge(('c', 'b'))
        self.tree.addedge(('c', 'd'))
        # Node `d`
        self.tree.addedge(('d', 'a'))
        self.tree.addedge(('d', 'b'))
        self.tree.addedge(('d', 'e'))
        # Node `e`
        self.tree.addedge(('e', EndNode))

        self.cycle = Graph(nodes = "abc")
        self.cycle.addnode(StartNode)
        self.cycle.addnode(EndNode)
        for x in "abc":
            self.cycle.addedge((StartNode, x))
            self.cycle.addedge((x, EndNode))
        self.cycle.addedge(('a', 'b'))
        self.cycle.addedge(('b', 'c'))
        self.cycle.addedge(('c', 'a'))

    def testEnableDisjunction(self):
        __enable_disjunction_for_nodes__(self.tree, ['a', 'c'])
        self.assert_(Disjunction(('a', 'c')) in self.tree.nodes)

    def testEnableDisjunctionPreconditionB(self):
        self.assertEqual(__enable_disjunction_case_b__(self.tree), True)
        self.assert_(Disjunction(('a', 'c')) in self.tree.nodes)
        self.assertEqual(__enable_disjunction_case_b__(self.cycle), False)

    def testEnableDisjunctionPreconditionACycle(self):
        self.assertEqual(__enable_disjunction_case_a__(self.cycle), True)
        self.assert_(Disjunction(('a', 'b')) in self.cycle.nodes or
                     Disjunction(('b', 'c')) in self.cycle.nodes or
                     Disjunction(('a', 'c')) in self.cycle.nodes)
        self.assertEqual(__enable_disjunction_case_a__(self.tree), False)

    def testEnableOptionalB(self):
        self.assert_(('c', 'a') in self.tree.edges)
        self.assert_(('d', 'a') in self.tree.edges)
        __enable_optional_for_node__(self.tree, 'b')
        self.assert_(Optional('b') in self.tree.nodes)
        self.assert_((Optional('b'), 'a') in self.tree.edges)
        self.assert_(('c', 'a') not in self.tree.edges)
        self.assert_(('d', 'a') not in self.tree.edges)
        self.assert_((StartNode, 'a') not in self.tree.edges)

    def testEnableOptionalPreconditionA(self):
        self.assertEqual(__enable_optional_case_a__(self.tree), True)
        self.assert_(Optional('b') in self.tree.nodes or
                     Optional('c') in self.tree.nodes or
                     Optional('d') in self.tree.nodes)

    def testEnableOptionalPreconditionB(self):
        self.assertEqual(__enable_optional_case_b__(self.tree), True)
        self.assert_(Optional('e') in self.tree.nodes)
        self.assertEqual(__enable_optional_case_b__(self.tree), False)

    def testIDT(self):
        self.assert_(infer_soa(self.tree))
        self.assert_(infer_soa(self.cycle))
예제 #7
0
class RewriteTests(unittest.TestCase):
    def prepareTree(self):
        # Graph from Figure 2 in [Bex2006]
        self.tree = Graph(nodes = "abcde")
        self.tree.addnode(StartNode)
        self.tree.addnode(EndNode)
        # StartNode
        self.tree.addedge((StartNode, 'b'))
        self.tree.addedge((StartNode, 'c'))
        # Node `a`
        self.tree.addedge(('a', 'c'))
        # Node `b`
        self.tree.addedge(('b', 'a'))
        # Node `c`
        self.tree.addedge(('c', 'a'))
        self.tree.addedge(('c', 'b'))
        self.tree.addedge(('c', 'd'))
        # Node `d`
        self.tree.addedge(('d', 'a'))
        self.tree.addedge(('d', 'b'))
        self.tree.addedge(('d', 'e'))
        # Node `e`
        self.tree.addedge(('e', EndNode))

    def prepareGraph(self):
        # Graph from Figure 1 in [Bex2006]
        self.graph = Graph(nodes = "abcde")
        self.graph.addnode(StartNode)
        self.graph.addnode(EndNode)
        # StartNode
        self.graph.addedge((StartNode, 'a'))
        self.graph.addedge((StartNode, 'b'))
        self.graph.addedge((StartNode, 'c'))
        # Node `a`
        self.graph.addedge(('a', 'a'))
        self.graph.addedge(('a', 'b'))
        self.graph.addedge(('a', 'c'))
        self.graph.addedge(('a', 'd'))
        # Node `b`
        self.graph.addedge(('b', 'a'))
        self.graph.addedge(('b', 'c'))
        # Node `c`
        self.graph.addedge(('c', 'a'))
        self.graph.addedge(('c', 'b'))
        self.graph.addedge(('c', 'c'))
        self.graph.addedge(('c', 'd'))
        # Node `d`
        self.graph.addedge(('d', 'b'))
        self.graph.addedge(('d', 'e'))
        self.graph.addedge(('d', 'a'))
        self.graph.addedge(('d', 'c'))
        # Node `e`
        self.graph.addedge(('e', EndNode))

    def prepareRandomGraph(self):
        import random
        size = random.randint(10, 20)
        self.rndgraph = Graph(nodes = range(1, size))
        for source in xrange(1, size):
            for target in xrange(source, size):
                if random.random() > 0.5:
                    self.rndgraph.addedge((source, target))

    def setUp(self):
        self.prepareTree()
        self.prepareGraph()
        # self.prepareRandomGraph()

    def testSelfLoop(self):
        self.assertEqual(__selflooprule__(self.graph), True)

    def testOptionalRuleApplied(self):
        self.assertEqual(__optionalrule__(self.graph), True)
        self.assert_('b' not in self.graph.nodes)
        self.assert_(Optional('b') in self.graph.nodes)

    def testDisjunctionRuleApplied(self):
        __optionalrule__(self.graph)
        self.assertEqual(__disjunctionrule__(self.graph), True)
        self.assert_(Disjunction(('a', 'c')) in self.graph.nodes)

    def testConjunctionRuleApplied(self):
        __optionalrule__(self.graph)
        __disjunctionrule__(self.graph)
        self.assertEqual(__concatrule__(self.graph), True)
        self.assert_(Conjunction([Optional('b'), Disjunction(['c', 'a'])]) in self.graph.nodes)

    def testNonApplicableConcat(self):
        self.assertEqual(__concatrule__(self.tree), False)
        self.assertEqual(len(self.tree), 7)

    def testNonApplicableDisjunction(self):
        self.assertEqual(__disjunctionrule__(self.tree), False)
        self.assertEqual(len(self.tree), 7)

    def testNonApplicableOptional(self):
        self.assertEqual(__optionalrule__(self.tree), False)
        self.assertEqual(len(self.tree), 7)

    def testNonApplicableSelfLoop(self):
        self.assertEqual(__selflooprule__(self.tree), False)
        self.assertEqual(len(self.tree), 7)

    def testLengthNonAumented(self):
        self.prepareRandomGraph()
        previouslen = len(self.rndgraph)
        rewrite(self.rndgraph)
        self.assert_(previouslen >= len(self.rndgraph))
예제 #8
0
 def setUp(self):
     self.graph = Graph(nodes = range(1, 10), edges = [(1,2)])
예제 #9
0
class EdgesTests(unittest.TestCase):
    def setUp(self):
        self.graph = Graph(nodes = range(1, 10), edges = [(1,2)])

    def testLenWhenInitEdges(self):
        self.assertEqual(len(self.graph.edges), 1)

    def testInitNonEdges(self):
        self.assertRaises(AssertionError, Graph,
                          nodes = range(1, 10), edges = [(1,22)])

    def testLenWhenAddedEdge(self):
        previouslen = len(self.graph.edges)
        self.graph.addedge((2, 3))
        self.assertEqual(len(self.graph.edges), previouslen + 1)

    def testLenWhenNonAddedEdge(self):
        previouslen = len(self.graph.edges)
        self.graph.addedge((1, 2))
        self.assertEqual(len(self.graph.edges), previouslen)

    def testAddedEdge(self):
        self.graph.addedge((2, 3))
        self.assert_((2, 3) in self.graph.edges)

    def testErrorWhenAddingEdgeWithInvalidNode(self):
        self.assertRaises(AssertionError, self.graph.addedge, (1, 45))

    def testRemovedEdge(self):
        self.graph.removeedge((1, 2))
        self.assert_((1, 2) not in self.graph.edges)

    def testRemoveInvalidEdge(self):
        self.graph.removeedge((2, 3))
        self.assert_((2, 3) not in self.graph.edges)

    def testRemoveEdgeWithInvalidNode(self):
        self.graph.removeedge((2, 33))
        self.assert_((2, 33) not in self.graph.edges)
예제 #10
0
 def setUp(self):
     self.graph = Graph(nodes = range(1, 10))
예제 #11
0
class NodesTests(unittest.TestCase):
    def setUp(self):
        self.graph = Graph(nodes = range(1, 10))

    def testLenWhenInitNodeArgument(self):
        self.assertEqual(len(self.graph), len(range(1, 10)))

    def testLenWhenAddedNode(self):
        previouslen = len(self.graph)
        self.graph.addnode(1000)
        self.assertEqual(len(self.graph), previouslen + 1)

    def testLenWhenNonAddedNode(self):
        previouslen = len(self.graph)
        self.graph.addnode(1)
        self.assertEqual(len(self.graph), previouslen)

    def testLenWhenRemovedNode(self):
        previouslen = len(self.graph)
        self.graph.removenode(1)
        self.assertEqual(len(self.graph), previouslen - 1)

    def testLenWhenNonRemovedNode(self):
        previouslen = len(self.graph)
        self.graph.removenode(1000)
        self.assertEqual(len(self.graph), previouslen)

    def testAddedNode(self):
        self.graph.addnode(1)
        self.assert_(1 in self.graph.nodes)

    def testRemovedNode(self):
        self.graph.removenode(5)
        self.assert_(5 not in self.graph.nodes)
예제 #12
0
class PredSuccTests(unittest.TestCase):
    def setUp(self):
        self.graph = Graph(nodes = range(1, 8))
        self.graph.addedge((1, 3))
        self.graph.addedge((2, 3))
        self.graph.addedge((3, 4))
        self.graph.addedge((4, 5))
        self.graph.addedge((5, 6))
        self.graph.addedge((7, 5))
        self.eo4 = eo4 = EmptyObject(4)
        self.eo3 = eo3 = EmptyObject(3)
        self.eo5 = eo5 = EmptyObject(5)
        self.graph.replacenode(4, eo4)
        self.graph.replacenode(3, eo3)
        self.graph.replacenode(5, eo5)

    def testInitialPredSet(self):
        self.assertEqual(self.graph.pred(4), set([]))
        self.assertEqual(self.graph.pred(self.eo4), set([1, 2, self.eo3]))

    def testInitialSuccSet(self):
        self.assertEqual(self.graph.succ(4), set([]))
        self.assertEqual(self.graph.succ(self.eo3), set([self.eo4, self.eo5, 6]))

    def testPredSet(self):
        self.graph.replacenode(self.eo3, 3)
        self.assertEqual(self.graph.pred(self.eo4), set([3]))
        self.graph.replacenode(3, self.eo3)
        self.assertEqual(self.graph.pred(self.eo4), set([1, 2, self.eo3]))

    def testSuccSet(self):
        self.graph.replacenode(self.eo4, 4)
        self.assertEqual(self.graph.succ(self.eo3), set([4]))
        self.graph.replacenode(4, self.eo4)
        self.assertEqual(self.graph.succ(self.eo3), set([self.eo4, self.eo5, 6]))
예제 #13
0
 def setUp(self):
     self.graph = Graph(nodes = range(1, 6))
     self.graph.addedge((1, 2))
     self.graph.addedge((2, 3))
     self.graph.addedge((3, 1))
     self.graph.addedge((4, 5))
예제 #14
0
class ReplacementTests(unittest.TestCase):
    def setUp(self):
        self.graph = Graph(nodes = range(1, 6))
        self.graph.addedge((1, 2))
        self.graph.addedge((2, 3))
        self.graph.addedge((3, 1))
        self.graph.addedge((4, 5))

    def testLenWhenNodeReplacement(self):
        l = len(self.graph)
        self.graph.replacenode(2, 21)
        self.assertEqual(len(self.graph), l)

    def testNodeReplacement(self):
        self.graph.addedge((2, 2))
        self.graph.replacenode(2, 21)
        self.assert_(2 not in self.graph.nodes)
        self.assert_(21 in self.graph.nodes)
        self.assert_((1, 21) in self.graph.edges)
        self.assert_((21, 3) in self.graph.edges)

    def testEdgeReplacement(self):
        self.graph.replaceedge((1, 2), (4, 3))
        self.assert_((1, 2) not in self.graph.edges)
        self.assert_((4, 3) in self.graph.edges)
예제 #15
0
class EdgePropertiesTests(unittest.TestCase):
    def setUp(self):
        self.graph = Graph(nodes = range(1, 10))
        self.graph.addedge((1, 2))
        self.graph.addedge((2, 3))
        self.graph.addedge((3, 1))
        self.graph.addedge((4, 5))

    def testIsolatedNode(self):
        nodes = self.graph.getedgesintonode(6)
        self.assertEqual(nodes, [])
        nodes = self.graph.getedgesoutofnode(6)
        self.assertEqual(nodes, [])

    def testNonIsolatedNode(self):
        nodes = self.graph.getedgesintonode(2)
        self.assertEqual(nodes, [(1, 2)])
        nodes = self.graph.getedgesoutofnode(2)
        self.assertEqual(nodes, [(2, 3)])

    def testAddEdge(self):
        self.graph.addedge((3, 5))
        self.assert_((3, 5) in self.graph.getedgesintonode(5))
        self.assert_((3, 5) in self.graph.getedgesoutofnode(3))
        self.assert_((3, 5) in self.graph.edges)

    def testRemoveEdge(self):
        self.graph.removeedge((1, 2))
        self.assert_((1, 2) not in self.graph.getedgesintonode(2))
        self.assert_((1, 2) not in self.graph.getedgesoutofnode(1))
        self.assert_((1, 2) not in self.graph.edges)