当前位置: 首页>>代码示例>>Python>>正文


Python DirectedHypergraph.add_node方法代码示例

本文整理汇总了Python中halp.directed_hypergraph.DirectedHypergraph.add_node方法的典型用法代码示例。如果您正苦于以下问题:Python DirectedHypergraph.add_node方法的具体用法?Python DirectedHypergraph.add_node怎么用?Python DirectedHypergraph.add_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在halp.directed_hypergraph.DirectedHypergraph的用法示例。


在下文中一共展示了DirectedHypergraph.add_node方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: from_networkx_digraph

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def from_networkx_digraph(nx_digraph):
    """Returns a DirectedHypergraph object that is the graph equivalent of the
    given NetworkX DiGraph object.

    :param nx_digraph: the NetworkX directed graph object to transform.
    :returns: DirectedHypergraph -- hypergraph object equivalent to the
            NetworkX directed graph.
    :raises: TypeError -- Transformation only applicable to directed
            NetworkX graphs

    """
    import networkx as nx

    if not isinstance(nx_digraph, nx.DiGraph):
        raise TypeError("Transformation only applicable to directed \
                        NetworkX graphs")

    G = DirectedHypergraph()

    for node in nx_digraph.nodes_iter():
        G.add_node(node, copy.copy(nx_digraph.node[node]))

    for edge in nx_digraph.edges_iter():
        tail_node = edge[0]
        head_node = edge[1]
        G.add_hyperedge(tail_node,
                        head_node,
                        copy.copy(nx_digraph[tail_node][head_node]))

    return G
开发者ID:Murali-group,项目名称:halp,代码行数:32,代码来源:directed_graph_transformations.py

示例2: test_get_hyperedge_attributes

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def test_get_hyperedge_attributes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    tail = set([node_a, node_b])
    head = set([node_c, node_d])
    frozen_tail = frozenset(tail)
    frozen_head = frozenset(head)

    attrib = {'weight': 6, 'color': 'black'}

    H = DirectedHypergraph()
    H.add_node(node_a, label=1337)
    hyperedge_name = H.add_hyperedge(tail, head, attrib, weight=5)

    assert H.get_hyperedge_attributes(hyperedge_name) == \
        {'tail': tail, 'head': head, 'weight': 5, 'color': 'black'}

    # Test getting non-existent hyperedge's attributes
    try:
        H.get_hyperedge_attributes("e10")
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:Murali-group,项目名称:halp,代码行数:30,代码来源:test_directed_hypergraph.py

示例3: test_returns_hyperpath_containing_source_if_source_equals_destination

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
 def test_returns_hyperpath_containing_source_if_source_equals_destination(
         self):
     s = '1'
     T = {s: None}
     H = DirectedHypergraph()
     H.add_node(s)
     path = directed_paths.get_hyperpath_from_predecessors(H, T, s, s)
     self.assertTrue(path.has_node(s))
开发者ID:Murali-group,项目名称:halp,代码行数:10,代码来源:test_directed_paths.py

示例4: test_returns_hyperpath_with_single_node_if_source_equals_destination

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
 def test_returns_hyperpath_with_single_node_if_source_equals_destination(
         self):
     s = '1'
     T = {s: None}
     H = DirectedHypergraph()
     H.add_node(s)
     path = directed_paths.get_hyperpath_from_predecessors(H, T, s, s)
     self.assertEqual(len(path.get_node_set()), 1)
     self.assertEqual(len(path.get_hyperedge_id_set()), 0)
开发者ID:Murali-group,项目名称:halp,代码行数:11,代码来源:test_directed_paths.py

示例5: test_returns_empty_list_if_no_s_t_path

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
    def test_returns_empty_list_if_no_s_t_path(self):
        H = DirectedHypergraph()
        H.add_node('s')
        H.add_node('1')
        H.add_node('2')
        H.add_node('t')
        H.add_hyperedge({'s'}, {'1'}, weight=1)
        H.add_hyperedge({'1', '2'}, {'t'}, weight=1)

        output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
        self.assertEqual(output, [])
开发者ID:Murali-group,项目名称:halp,代码行数:13,代码来源:test_k_shortest_hyperpaths.py

示例6: test_returns_disconnected_nodes_on_graph_with_two_nodes

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
 def test_returns_disconnected_nodes_on_graph_with_two_nodes(self):
     H = DirectedHypergraph()
     s, t = 's', 't'
     H.add_node(s)
     H.add_node(t)
     e1 = H.add_hyperedge({s}, {t})
     predecessor = {s: None, t: e1}
     ordering = [s, t]
     branch = ksh._branching_step(H, predecessor, ordering)[0]
     self.assertEqual(branch.get_hyperedge_id_set(), set([]))
     self.assertEqual(branch.get_node_set(), {'s', 't'})
开发者ID:Murali-group,项目名称:halp,代码行数:13,代码来源:test_k_shortest_hyperpaths.py

示例7: test_add_hyperedge

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def test_add_hyperedge():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    tail = set([node_a, node_b])
    head = set([node_c, node_d])
    frozen_tail = frozenset(tail)
    frozen_head = frozenset(head)

    attrib = {'weight': 6, 'color': 'black'}

    H = DirectedHypergraph()
    H.add_node(node_a, label=1337)
    hyperedge_name = H.add_hyperedge(tail, head, attrib, weight=5)

    assert hyperedge_name == 'e1'

    # Test that all hyperedge attributes are correct
    assert H._hyperedge_attributes[hyperedge_name]['tail'] == tail
    assert H._hyperedge_attributes[hyperedge_name]['head'] == head
    assert H._hyperedge_attributes[hyperedge_name]['weight'] == 5
    assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'

    # Test that successor list contains the correct info
    assert frozen_head in H._successors[frozen_tail]
    assert hyperedge_name in H._successors[frozen_tail][frozen_head]

    # Test that the precessor list contains the correct info
    assert frozen_tail in H._predecessors[frozen_head]
    assert hyperedge_name in H._predecessors[frozen_head][frozen_tail]

    # Test that forward-stars and backward-stars contain the correct info
    for node in frozen_tail:
        assert hyperedge_name in H._forward_star[node]
    for node in frozen_head:
        assert hyperedge_name in H._backward_star[node]

    # Test that adding same hyperedge will only update attributes
    new_attrib = {'weight': 10}
    H.add_hyperedge(tail, head, new_attrib)
    assert H._hyperedge_attributes[hyperedge_name]['weight'] == 10
    assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'

    try:
        H.add_hyperedge(set(), set())
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:Murali-group,项目名称:halp,代码行数:54,代码来源:test_directed_hypergraph.py

示例8: test_get_node_attributes

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def test_get_node_attributes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}

    # Test adding unadded nodes with various attribute settings
    H = DirectedHypergraph()
    H.add_node(node_a)
    H.add_node(node_b, source=True)
    H.add_node(node_c, attrib_c)
    H.add_node(node_d, attrib_d, sink=False)

    assert H.get_node_attributes(node_a) == {}
    assert H.get_node_attributes(node_d) == {'label': 'black', 'sink': False}

    # Test getting non-existent node's attributes
    try:
        H.get_node_attributes("X")
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:Murali-group,项目名称:halp,代码行数:28,代码来源:test_directed_hypergraph.py

示例9: test_add_node

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def test_add_node():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}

    # Test adding unadded nodes with various attribute settings
    H = DirectedHypergraph()
    H.add_node(node_a)
    H.add_node(node_b, source=True)
    H.add_node(node_c, attrib_c)
    H.add_node(node_d, attrib_d, sink=False)

    assert node_a in H._node_attributes
    assert H._node_attributes[node_a] == {}

    assert node_b in H._node_attributes
    assert H._node_attributes[node_b]['source'] is True

    assert node_c in H._node_attributes
    assert H._node_attributes[node_c]['alt_name'] == 1337

    assert node_d in H._node_attributes
    assert H._node_attributes[node_d]['label'] == 'black'
    assert H._node_attributes[node_d]['sink'] is False

    # Test adding a node that has already been added
    H.add_nodes(node_a, common=False)
    assert H._node_attributes[node_a]['common'] is False

    # Pass in bad (non-dict) attribute
    try:
        H.add_node(node_a, ["label", "black"])
        assert False
    except AttributeError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:Murali-group,项目名称:halp,代码行数:42,代码来源:test_directed_hypergraph.py

示例10: test_returns_only_one_hyperpath_for_k_equals_one

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
    def test_returns_only_one_hyperpath_for_k_equals_one(self):
        H = DirectedHypergraph()
        H.add_node('s')
        H.add_node('1')
        H.add_node('2')
        H.add_node('3')
        H.add_node('t')
        H.add_hyperedge({'s'}, {'1'}, weight=1)
        H.add_hyperedge({'s'}, {'2'}, weight=1)
        H.add_hyperedge({'s'}, {'3'}, weight=1)
        H.add_hyperedge({'1'}, {'t'}, weight=1)
        H.add_hyperedge({'2', '3'}, {'t'}, weight=1)

        output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
        self.assertEqual(len(output), 1)
开发者ID:Murali-group,项目名称:halp,代码行数:17,代码来源:test_k_shortest_hyperpaths.py

示例11: TestComputeLowerBound

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
class TestComputeLowerBound(unittest.TestCase):

    def setUp(self):
        self.nielsenGraph = DirectedHypergraph()
        self.nielsenGraph.add_node('s')
        self.nielsenGraph.add_node('1')
        self.nielsenGraph.add_node('2')
        self.nielsenGraph.add_node('3')
        self.nielsenGraph.add_node('4')
        self.nielsenGraph.add_node('t')
        self.nielsenGraph.add_hyperedge({'s'}, {'1'}, weight=1)
        self.nielsenGraph.add_hyperedge({'s'}, {'2'}, weight=1)
        self.nielsenGraph.add_hyperedge({'s'}, {'3'}, weight=1)
        self.nielsenGraph.add_hyperedge({'1'}, {'2'}, weight=1)
        self.nielsenGraph.add_hyperedge({'2'}, {'3'}, weight=1)
        self.nielsenGraph.add_hyperedge({'1', '2'}, {'t'}, weight=1)
        self.nielsenGraph.add_hyperedge({'4'}, {'t'}, weight=1)
        self.nielsenGraph.add_hyperedge({'2', '3'}, {'4'}, weight=1)
        self.nielsenGraph.add_hyperedge({'4'}, {'1'}, weight=1)

    def test_returns_12_for_lower_bound_for_nielsen_H_21(self):
        '''
            Test graph is discussed in Section 3.2 example 2 of
            Nielsen et al.\
        '''
        H_2 = self.nielsenGraph.copy()
        e1 = H_2.get_hyperedge_id({'s'}, {'1'})
        e2 = H_2.get_hyperedge_id({'1'}, {'2'})
        e3 = H_2.get_hyperedge_id({'1', '2'}, {'t'})
        H_2.remove_hyperedge(H_2.get_hyperedge_id({'s'}, {'2'}))
        H_2.remove_hyperedge(H_2.get_hyperedge_id({'4'}, {'t'}))

        # weight vector
        W = {'s': 0, '1': 1, '2': 2, '3': 1, '4': 4, 't': 4}
        # predecessor function
        pred = {'s': None, '1': e1, '2': e2, 't': e3}
        # ordering
        ordering = ['s', '1', '2', 't']

        # branch of H_2 for the test
        H_2_1 = H_2.copy()
        H_2_1.remove_hyperedge(
            H_2_1.get_hyperedge_id({'s'}, {'1'}))

        self.assertEqual(ksh._compute_lower_bound(
            H_2_1, 0, pred, ordering, W, 't'), 12)
开发者ID:Murali-group,项目名称:halp,代码行数:48,代码来源:test_k_shortest_hyperpaths.py

示例12: test_returns_shortest_hyperpath_for_k_equals_one

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
    def test_returns_shortest_hyperpath_for_k_equals_one(self):
        H = DirectedHypergraph()
        H.add_node('s')
        H.add_node('1')
        H.add_node('2')
        H.add_node('3')
        H.add_node('t')
        H.add_hyperedge({'s'}, {'1'}, weight=1)
        H.add_hyperedge({'s'}, {'2'}, weight=1)
        H.add_hyperedge({'s'}, {'3'}, weight=1)
        H.add_hyperedge({'1'}, {'t'}, weight=1)
        H.add_hyperedge({'2', '3'}, {'t'}, weight=1)

        output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
        hyperpath = output[0]
        self.assertEqual(hyperpath.get_node_set(), {'s', '1', 't'})
        self.assertEqual(len(hyperpath.get_hyperedge_id_set()), 2)
        self.assertTrue(hyperpath.get_hyperedge_id({'s'}, {'1'}))
        self.assertTrue(hyperpath.get_hyperedge_id({'1'}, {'t'}))
开发者ID:Murali-group,项目名称:halp,代码行数:21,代码来源:test_k_shortest_hyperpaths.py

示例13: get_hyperpath_from_predecessors

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def get_hyperpath_from_predecessors(H, Pv, source_node, destination_node,
                                    node_weights=None, attr_name="weight"):
    """Gives the hyperpath (DirectedHypergraph) representing the shortest
    B-hyperpath from the source to the destination, given a predecessor
    function and source and destination nodes.

    :note: The IDs of the hyperedges in the subhypergraph returned may be
        different than those in the original hypergraph (even though the
        tail and head sets are identical).

    :param H: the hypergraph which the path algorithm was executed on.
    :param Pv: dictionary mapping each node to the ID of the hyperedge that
            preceeded it in the path.
    :param source_node: the source node of the path.
    :param destination_node: the destination node of the path.
    :returns: DirectedHypergraph -- shortest B-hyperpath from source_node to
            destination_node.
    :raises: TypeError -- Algorithm only applicable to directed hypergraphs
    :raises: KeyError -- Node key in predecessor is not in H
    :raises: KeyError -- Hyperedge key in predecessor is not in H
    :raises: ValueError -- Multiple nodes without predecessor
    :raises: ValueError -- Hypertree does not have source node

    """
    if not isinstance(H, DirectedHypergraph):
        raise TypeError("Algorithm only applicable to directed hypergraphs")

    # Check that Pv is a valid predecessor function:
    # - keys must be nodes in H mapping to hyperedges in H
    # - exactly one node must map to None (i.e., only one node
    #   without predecessor)
    nodes_without_predecessor = 0
    for node, hyperedge_id in Pv.items():
        if not H.has_node(node):
            raise KeyError(
                "Node key %s in predecessor is not in H" % node)

        if hyperedge_id is None:
            nodes_without_predecessor += 1
        elif not H.has_hyperedge_id(hyperedge_id):
            raise KeyError(
                "Hyperedge key %s in predecessor is not in H" % hyperedge_id)

    if nodes_without_predecessor > 1:
        raise ValueError(
            "Multiple nodes without predecessor. %s received" % Pv)
    elif nodes_without_predecessor == 0:
        raise ValueError(
            "Hypertree does not have source node. %s received" % Pv)

    path = DirectedHypergraph()

    # keep track of which nodes are or have been processed
    processedOrInQueue = {n: False for n in Pv}
    nodesToProcess = [destination_node]
    processedOrInQueue[destination_node] = True
    while nodesToProcess:
        node = nodesToProcess.pop(0)
        hyperedge_id = Pv[node]
        if hyperedge_id:
            for n in H.get_hyperedge_tail(hyperedge_id):
                if not processedOrInQueue[n]:
                    nodesToProcess.append(n)
                    processedOrInQueue[n] = True
            path.add_hyperedge(H.get_hyperedge_tail(hyperedge_id),
                               H.get_hyperedge_head(hyperedge_id),
                               weight=H.get_hyperedge_weight(
                               hyperedge_id))
        elif not path.has_node(node):
            path.add_node(node)

    return path
开发者ID:Murali-group,项目名称:halp,代码行数:74,代码来源:directed_paths.py

示例14: TestKShortestHyperpaths

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
class TestKShortestHyperpaths(unittest.TestCase):

    def setUp(self):
        self.nielsenGraph = DirectedHypergraph()
        self.nielsenGraph.add_node('s')
        self.nielsenGraph.add_node('1')
        self.nielsenGraph.add_node('2')
        self.nielsenGraph.add_node('3')
        self.nielsenGraph.add_node('4')
        self.nielsenGraph.add_node('t')
        self.nielsenGraph.add_hyperedge({'s'}, {'1'}, weight=1)
        self.nielsenGraph.add_hyperedge({'s'}, {'2'}, weight=1)
        self.nielsenGraph.add_hyperedge({'s'}, {'3'}, weight=1)
        self.nielsenGraph.add_hyperedge({'1'}, {'2'}, weight=1)
        self.nielsenGraph.add_hyperedge({'2'}, {'3'}, weight=1)
        self.nielsenGraph.add_hyperedge({'1', '2'}, {'t'}, weight=1)
        self.nielsenGraph.add_hyperedge({'4'}, {'t'}, weight=1)
        self.nielsenGraph.add_hyperedge({'2', '3'}, {'4'}, weight=1)
        self.nielsenGraph.add_hyperedge({'4'}, {'1'}, weight=1)

    # valid input tests
    def test_raises_exception_if_H_not_B_hypegraph(self):
        H = DirectedHypergraph()
        H.add_nodes([1, 2, 3])
        H.add_hyperedge([1], [2, 3])
        source, destination = 1, 2
        self.assertRaises(TypeError,
                          ksh.k_shortest_hyperpaths, H, source, destination, 1)

    def test_raises_exception_if_H_not_hypegraph(self):
        H = "DirectedHypergraph"
        source, destination = 1, 2
        self.assertRaises(TypeError,
                          ksh.k_shortest_hyperpaths, H, source, destination, 1)

    def test_raises_exception_if_source_not_in_graph(self):
        H = DirectedHypergraph()
        source, destination = 1, 2
        H.add_nodes([source, destination])
        self.assertRaises(ValueError,
                          ksh.k_shortest_hyperpaths, H, 3, destination, 1)

    def test_raises_exception_if_destination_not_in_graph(self):
        H = DirectedHypergraph()
        source, destination = 1, 2
        H.add_nodes([source, destination])
        self.assertRaises(ValueError,
                          ksh.k_shortest_hyperpaths, H, source, 3, 1)

    def test_raises_exception_if_k_not_integer(self):
        H = DirectedHypergraph()
        source, destination = 1, 2
        H.add_nodes([source, destination])
        self.assertRaises(TypeError,
                          ksh.k_shortest_hyperpaths,
                          H, source, destination, 0.1)

    def test_raises_exception_if_k_not_positive(self):
        H = DirectedHypergraph()
        source, destination = 1, 2
        H.add_nodes([source, destination])
        self.assertRaises(ValueError,
                          ksh.k_shortest_hyperpaths,
                          H, source, destination, -4)
        self.assertRaises(ValueError,
                          ksh.k_shortest_hyperpaths,
                          H, source, destination, 0)

    # various cases
    def test_returns_only_one_hyperpath_for_k_equals_one(self):
        H = DirectedHypergraph()
        H.add_node('s')
        H.add_node('1')
        H.add_node('2')
        H.add_node('3')
        H.add_node('t')
        H.add_hyperedge({'s'}, {'1'}, weight=1)
        H.add_hyperedge({'s'}, {'2'}, weight=1)
        H.add_hyperedge({'s'}, {'3'}, weight=1)
        H.add_hyperedge({'1'}, {'t'}, weight=1)
        H.add_hyperedge({'2', '3'}, {'t'}, weight=1)

        output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
        self.assertEqual(len(output), 1)

    def test_returns_shortest_hyperpath_for_k_equals_one(self):
        H = DirectedHypergraph()
        H.add_node('s')
        H.add_node('1')
        H.add_node('2')
        H.add_node('3')
        H.add_node('t')
        H.add_hyperedge({'s'}, {'1'}, weight=1)
        H.add_hyperedge({'s'}, {'2'}, weight=1)
        H.add_hyperedge({'s'}, {'3'}, weight=1)
        H.add_hyperedge({'1'}, {'t'}, weight=1)
        H.add_hyperedge({'2', '3'}, {'t'}, weight=1)

        output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
        hyperpath = output[0]
#.........这里部分代码省略.........
开发者ID:Murali-group,项目名称:halp,代码行数:103,代码来源:test_k_shortest_hyperpaths.py

示例15: convert_pnet_to_hypergraph

# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_node [as 别名]
def convert_pnet_to_hypergraph(pnet):
    """ pre-process pnet to number tau-split and tau-join transitions"""
    tau_pre_processing_pnet(pnet)
    """ Convert a Petri net (in pnml format) into a hypergraph (Halp format) """
    hg = DirectedHypergraph()
    transitions = get_transitions(pnet)
    places = get_places(pnet)
    """STEP 1: Pre-process places to find xor places (splits and joints)
    If input/output of a transitions is 2 or more places, then mark those places as "X" and put in hypergraph"""
    for place in places:
        inc_arcs = get_incoming_arcs(place,pnet)
        out_arcs = get_outgoing_arcs(place,pnet)
        isSink = False
        isSource = False
        if len(inc_arcs) > 1:
            #create node for place in hypergraph
            node_id = get_id(place)
            #check if join is end event (sink)
            if (len(out_arcs) == 0):
                isSink = True
            logger.debug("STEP 1 - Creating xor-join node -- {0}".format(node_id))
            hg.add_node(node_id, source = isSource, sink = isSink, type = 'xor-join', name = " ")
            head = []
            head.append(node_id)
            isSink = False
            isSource = False
            #create node for all source of incoming arcs
            for arc in inc_arcs:
                node_id2 = get_id(get_element(get_arc_source(arc), pnet))
                node_name = get_transition_name(get_element(get_arc_source(arc), pnet))
                logger.debug("STEP 1 - Creating transition node -- {0} -- {1}".format(node_id, node_name))
                hg.add_node(node_name, source = isSource, sink = isSink, type = 'transition', name = node_name)
                tail = []
                tail.append(node_name)
                #create hyperedge
                logger.debug("STEP 1 - Creating hyperedge from {0} to {1}".format(str(tail), str(head)))
                hg.add_hyperedge(tail, head, name = " ", phero = 0.5)
        if len(out_arcs) > 1:
            node_id = get_id(place)
            #create node for place in hypergraph (if it does not exist already)
            tail = []
            tail.append(node_id)
            if(not hg.has_node(node_id)):
                #check if source (start event)
                if (len(inc_arcs) == 0):
                    isSource = True
                logger.debug("STEP 1 - Creating xor-split node -- {0}".format(node_id))
                hg.add_node(node_id, source = isSource, sink = isSink, type = 'xor-split', name = " ")
                #create node for all targets of outgoing arcs
                isSink = False
                isSource = False
                for arc in out_arcs:
                    node_id2 = get_id(get_element(get_arc_target(arc), pnet))
                    node_name = get_transition_name(get_element(get_arc_target(arc),pnet))
                    if(not hg.has_node(node_id2)):
                        logger.debug("STEP 1 - Creating transition node -- {0} -- {1}".format(node_id, node_name))
                        hg.add_node(node_name, source = isSource, sink = isSink, type = 'transition', name = node_name)
                    head = []
                    head.append(node_name)
                    #create hyperedge
                    logger.debug("STEP 1 - Creating hyperedge from {0} to {1}".format(str(tail), str(head)))
                    hg.add_hyperedge(tail, head, name = " ", phero = 0.5)
    """ STEP2 : Process each transition """
    for transition in transitions:
        logger.debug("######## Processing transition {0}".format(get_transition_name(transition)))
        isSink = False
        isSource = False
        #check if transition is not a node in hg and add if needed
        #if (not hg.has_node(get_transition_name(transition))):
        #check if transition is start
        inc_arcs = get_incoming_arcs(transition,pnet)
        for inc_arc in inc_arcs:
            source_place = get_element(get_arc_source(inc_arc),pnet)
            place_inc = get_incoming_arcs(source_place,pnet)
            if not place_inc:
                isSource = True
                logger.debug("Transition is START: {0}".format(get_transition_name(transition)))
        #check if trsnasition is end event
        out_arcs = get_outgoing_arcs(transition,pnet)
        for out_arc in out_arcs:
            sink_place = get_element(get_arc_target(out_arc),pnet)
            place_out = get_outgoing_arcs(sink_place,pnet)
            if not place_out:
                isSink = True
                logger.debug("Transition is END: {0}".format(get_transition_name(transition)))
        #create node in hypergraph
        logger.debug("STEP 2 - Creating transition node")
        hg.add_node(get_transition_name(transition), source = isSource, sink = isSink, type = 'transition', name = get_transition_name(transition))
        #look BACKWARD 
        if not isSource:
            inc_arcs = get_incoming_arcs(transition,pnet)
            tail = []
            x_head = [get_transition_name(transition)]
            xplace_list = []
            otherp_list = []
            xplace_tail = []
            for inc_arc in inc_arcs:
                place = get_element(get_arc_source(inc_arc),pnet)
                #separate xor places from other forward places of this transition
                if(hg.has_node(get_id(place))):
#.........这里部分代码省略.........
开发者ID:emettelatripla,项目名称:HypergraphBPM,代码行数:103,代码来源:pnet_to_hypergraph.py


注:本文中的halp.directed_hypergraph.DirectedHypergraph.add_node方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。