Ejemplo n.º 1
0
def test_portgraph_connect():
    pg = PortGraph()
    vid1 = pg.add_vertex()
    vid2 = pg.add_vertex()

    # invalid ports
    assert_raises(InvalidPort, lambda: pg.connect(0, 1))
    # invalid source port
    pid1 = pg.add_in_port(vid1, "in")
    assert_raises(InvalidPort, lambda: pg.connect(pid1 + 1, pid1))
    # invalid out port
    pid2 = pg.add_out_port(vid2, "out")
    assert_raises(InvalidPort, lambda: pg.connect(pid2, pid1 + pid2 + 1))
    # edge connection from in to out raise error
    assert_raises(InvalidPort, lambda: pg.connect(pid1, pid2))

    eid = pg.connect(pid2, pid1)
    assert pg.source_port(eid) == pid2
    assert pg.target_port(eid) == pid1
    assert tuple(pg.connected_edges(pid1)) == (eid,)
    assert tuple(pg.connected_edges(pid2)) == (eid,)
    assert pid1 in pg.connected_ports(pid2)
    assert pid2 in pg.connected_ports(pid1)

    # can not duplicate edge
    assert_raises(InvalidEdge, lambda: pg.connect(pid2, pid1, eid))
Ejemplo n.º 2
0
def portgraph_nb_connections():
    pg = PortGraph()
    vid1 = pg.add_vertex()
    vid2 = pg.add_vertex()
    vid3 = pg.add_vertex()

    assert_raises(InvalidPort, lambda: pg.nb_connections(0))

    pid1 = pg.add_in_port(vid1, 0)
    pid2 = pg.add_in_port(vid1, 1)
    pid3 = pg.add_out_port(vid2, 0)
    pid4 = pg.add_out_port(vid3, 0)

    for pid in (pid1, pid2, pid3, pid4):
        assert pg.nb_connections(pid) == 0

    pg.connect(pid3, pid1)
    assert pg.nb_connections(pid3) == 1
    assert pg.nb_connections(pid1) == 1
    assert pg.nb_connections(pid2) == 0
    assert pg.nb_connections(pid4) == 0

    pg.connect(pid4, pid1)
    assert pg.nb_connections(pid3) == 1
    assert pg.nb_connections(pid4) == 1
    assert pg.nb_connections(pid1) == 2
    assert pg.nb_connections(pid2) == 0

    pg.connect(pid4, pid2)
    assert pg.nb_connections(pid3) == 1
    assert pg.nb_connections(pid4) == 2
    assert pg.nb_connections(pid1) == 2
    assert pg.nb_connections(pid2) == 1
Ejemplo n.º 3
0
def test_ws_can_not_store_data_on_input_port():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_in_port(0, "in", 0)
    ws = WorkflowState(pg)

    assert_raises(UserWarning, lambda: ws.store(0, "data"))
Ejemplo n.º 4
0
def test_evaluation_do_not_reevaluate_same_node():
    visited = []

    def func():
        visited.append("yes")

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)
    pg.add_actor(n, 1)

    env = EvaluationEnvironment()
    algo = BruteEvaluation(pg)

    ws = WorkflowState(pg)
    algo.eval(env, ws, 0)
    assert len(visited) == 1

    algo.eval(env, ws, 1)
    assert len(visited) == 2

    algo.eval(env, ws, 0)
    assert len(visited) == 2

    env.new_execution()
    algo.eval(env, ws, 0)
    assert len(visited) == 3
Ejemplo n.º 5
0
def test_portgraph_set_actor():
    pg = PortGraph()
    assert_raises(InvalidVertex, lambda: pg.set_actor(0, None))

    actor = Node()
    for key in ("toto", 1, "titi"):
        actor.add_input(key, "descr")
        actor.add_output(key, "descr")

    assert_raises(InvalidVertex, lambda: pg.set_actor(0, actor))

    vid = pg.add_vertex()
    pg.set_actor(vid, None)
    assert pg.actor(vid) is None
    assert_raises(InvalidPort, lambda: pg.set_actor(vid, actor))

    for key in actor.inputs():
        pg.add_in_port(vid, key)

    assert_raises(InvalidPort, lambda: pg.set_actor(vid, actor))

    for key in actor.outputs():
        pg.add_out_port(vid, key)

    pg.set_actor(vid, actor)
    assert pg.actor(vid) == actor

    pg.set_actor(vid, None)
    assert pg.actor(vid) is None
Ejemplo n.º 6
0
def test_evaluation_propagated_upstream():
    visited = []

    def func(txt):
        visited.append(txt)
        return txt

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)
    pg.add_actor(n, 1)
    pg.connect(pg.out_port(0, 'txt'), pg.in_port(1, 'txt'))

    algo = BruteEvaluation(pg)

    ws = WorkflowState(pg)
    env = EvaluationEnvironment()
    ws.store_param(pg.in_port(0, 'txt'), "txt", 0)

    algo.eval(env, ws, 0)
    assert len(visited) == 1
    algo.eval(env, ws, 1)
    assert len(visited) == 2

    env.new_execution()
    algo.eval(env, ws, 1)
    assert len(visited) == 4
Ejemplo n.º 7
0
def test_ws_retrieve_data_on_input_ports():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_out_port(0, "out", 0)
    pg.add_vertex(1)
    pg.add_in_port(1, "in", 1)

    # non existing port
    ws = WorkflowState(pg)
    assert_raises(KeyError, lambda: ws.get(10))

    # lonely input port without data
    ws = WorkflowState(pg)
    assert_raises(KeyError, lambda: ws.get(1))

    # lonely input port with data
    ws.store_param(1, "param", 0)
    assert ws.get(1) == "param"

    # connected input port
    pg.connect(0, 1)
    ws = WorkflowState(pg)

    ws.store(0, "data")
    assert ws.get(1) == "data"
Ejemplo n.º 8
0
def test_portgraph_connected_ports():
    pg = PortGraph()
    vid1 = pg.add_vertex()
    vid2 = pg.add_vertex()
    vid3 = pg.add_vertex()

    assert_raises(InvalidPort, lambda: tuple(pg.connected_ports(0)))

    pid1 = pg.add_in_port(vid1, 0)
    pid2 = pg.add_in_port(vid1, 1)
    pid3 = pg.add_out_port(vid2, 0)
    pid4 = pg.add_out_port(vid3, 0)

    for pid in (pid1, pid2, pid3, pid4):
        assert len(tuple(pg.connected_ports(pid))) == 0

    pg.connect(pid3, pid1)
    assert tuple(pg.connected_ports(pid3)) == (pid1,)
    assert tuple(pg.connected_ports(pid1)) == (pid3,)
    for pid in (pid2, pid4):
        assert len(tuple(pg.connected_ports(pid))) == 0

    pg.connect(pid4, pid1)
    assert tuple(pg.connected_ports(pid3)) == (pid1,)
    assert tuple(pg.connected_ports(pid4)) == (pid1,)
    assert sorted(pg.connected_ports(pid1)) == sorted((pid3, pid4))
    assert len(tuple(pg.connected_ports(pid2))) == 0

    pg.connect(pid4, pid2)
    assert tuple(pg.connected_ports(pid3)) == (pid1,)
    assert sorted(pg.connected_ports(pid4)) == sorted((pid1, pid2))
    assert sorted(pg.connected_ports(pid1)) == sorted((pid3, pid4))
    assert tuple(pg.connected_ports(pid2)) == (pid4,)
Ejemplo n.º 9
0
def test_portgraph_add_actor():
    pg = PortGraph()
    vid1 = pg.add_vertex()
    actor = Node()
    keys = {"toto", 1, "titi"}
    for key in keys:
        actor.add_input(key, "descr")
        actor.add_output(key, "descr")

    # bad actor
    assert_raises(AttributeError, lambda: pg.add_actor(None))
    assert len(pg) == 1
    assert_raises(AttributeError, lambda: pg.add_actor(None, vid1 + 1))
    assert len(pg) == 1
    # vertex id already used
    assert_raises(InvalidVertex, lambda: pg.add_actor(actor, vid1))
    assert len(pg) == 1

    for key in actor.inputs():
        pg.add_in_port(vid1, key)

    for key in actor.outputs():
        pg.add_out_port(vid1, key)

    pg.set_actor(vid1, actor)
    # vertex id already used
    assert_raises(InvalidVertex, lambda: pg.add_actor(actor, vid1))
    assert len(pg) == 1

    vid2 = pg.add_actor(actor)
    assert pg.actor(vid2) == actor
    assert set(actor.inputs()) == keys
    assert set(pg.local_id(pid) for pid in pg.in_ports(vid2)) == keys
    assert set(actor.outputs()) == keys
    assert set(pg.local_id(pid) for pid in pg.out_ports(vid2)) == keys
Ejemplo n.º 10
0
def test_ws_when_raise_key_error_for_unset_param():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_in_port(0, "in", 0)

    ws = WorkflowState(pg)
    assert_raises(KeyError, lambda: ws.when(0))
Ejemplo n.º 11
0
def test_ws_when_is_none_for_non_evaluated_output_port():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_out_port(0, "out", 0)

    ws = WorkflowState(pg)
    assert ws.when(0) is None
Ejemplo n.º 12
0
def test_portgraph_connected_edges():
    pg = PortGraph()
    vid1 = pg.add_vertex()
    vid2 = pg.add_vertex()
    vid3 = pg.add_vertex()

    assert_raises(InvalidPort, lambda: tuple(pg.connected_edges(0)))

    pid1 = pg.add_in_port(vid1, 0)
    pid2 = pg.add_in_port(vid1, 1)
    pid3 = pg.add_out_port(vid2, 0)
    pid4 = pg.add_out_port(vid3, 0)

    # test vertices and ports are created without connections
    for pid in (pid1, pid2, pid3, pid4):
        assert len(tuple(pg.connected_edges(pid))) == 0

    eid1 = pg.connect(pid3, pid1)
    assert tuple(pg.connected_edges(pid3)) == (eid1,)
    assert tuple(pg.connected_edges(pid1)) == (eid1,)
    for pid in (pid2, pid4):
        assert len(tuple(pg.connected_edges(pid))) == 0

    eid2 = pg.connect(pid4, pid1)
    assert tuple(pg.connected_edges(pid3)) == (eid1,)
    assert tuple(pg.connected_edges(pid4)) == (eid2,)
    assert sorted(pg.connected_edges(pid1)) == sorted((eid1, eid2))
    assert len(tuple(pg.connected_edges(pid2))) == 0

    eid3 = pg.connect(pid4, pid2)
    assert tuple(pg.connected_edges(pid3)) == (eid1,)
    assert sorted(pg.connected_edges(pid4)) == sorted((eid2, eid3))
    assert sorted(pg.connected_edges(pid1)) == sorted((eid1, eid2))
    assert tuple(pg.connected_edges(pid2)) == (eid3,)
Ejemplo n.º 13
0
def test_ws_when_set_explicitly_for_params():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_in_port(0, "in", 0)

    ws = WorkflowState(pg)
    ws.store_param(0, "param", 10)
    assert ws.when(0) == 10
Ejemplo n.º 14
0
def test_ws_when_is_last_evaluation_for_output_port():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_out_port(0, "out", 0)

    ws = WorkflowState(pg)
    ws.set_last_evaluation(0, 1)
    assert ws.when(0) == 1
Ejemplo n.º 15
0
def test_ws_when_is_none_on_creation():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_in_port(0, "in", 0)
    pg.add_out_port(0, "out", 1)

    ws = WorkflowState(pg)
    assert_raises(KeyError, lambda: ws.when(10))
Ejemplo n.º 16
0
def test_ws_nodes_not_evaluated_on_creation():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_vertex(1)

    ws = WorkflowState(pg)
    assert ws.last_evaluation(0) is None
    assert ws.last_evaluation(1) is None
Ejemplo n.º 17
0
def test_ws_nodes_last_evaluation():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_vertex(1)

    ws = WorkflowState(pg)
    ws.set_last_evaluation(0, 1)
    assert ws.last_evaluation(0) == 1
    assert ws.last_evaluation(1) is None
Ejemplo n.º 18
0
def test_portgraph_add_vertex():
    pg = PortGraph()

    vid = pg.add_vertex()
    assert pg.nb_vertices() == 1
    assert_raises(InvalidVertex, lambda: pg.add_vertex(vid))

    assert len(tuple(pg.ports(vid))) == 0
    assert pg.actor(vid) is None
Ejemplo n.º 19
0
def test_portgraph_vertex():
    pg = PortGraph()

    assert_raises(InvalidPort, lambda: pg.vertex(0))

    vid = pg.add_vertex()
    pid1 = pg.add_in_port(vid, 0)
    pid2 = pg.add_out_port(vid, 0)
    assert pg.vertex(pid1) == vid
    assert pg.vertex(pid2) == vid
Ejemplo n.º 20
0
def test_evaluation_new_require_evaluation():
    pg = PortGraph()
    pg.add_vertex(0)

    algo = BruteEvaluation(pg)
    assert id(algo.portgraph()) == id(pg)

    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    assert algo.requires_evaluation(env, ws)
Ejemplo n.º 21
0
def test_ws_retrieve_stored_data_on_output_port():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_out_port(0, "out", 0)
    ws = WorkflowState(pg)

    ws.store(0, "data")
    assert_raises(KeyError, lambda: ws.get(1))

    assert ws.get(0) == "data"
Ejemplo n.º 22
0
def test_portgraph_local_id():
    pg = PortGraph()
    vid = pg.add_vertex()

    assert_raises(InvalidPort, lambda: pg.local_id(0))

    for lpid in (0, 1, "a", None):
        pid = pg.add_in_port(vid, lpid)
        assert pg.local_id(pid) == lpid
        pid = pg.add_out_port(vid, lpid)
        assert pg.local_id(pid) == lpid
Ejemplo n.º 23
0
def test_ws_is_created_empty():
    pg = PortGraph()
    ws = WorkflowState(pg)
    assert len(tuple(ws.items())) == 0
    assert id(ws.portgraph()) == id(pg)

    pg.add_vertex(0)
    pg.add_in_port(0, "in", 0)
    pg.add_out_port(0, "out", 1)

    ws = WorkflowState(pg)
    assert len(tuple(ws.items())) == 0
Ejemplo n.º 24
0
def test_evaluation_needs_ready_to_evaluate_state():
    def func(a, b):
        c = a + b
        return c

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)

    algo = BruteEvaluation(pg)

    ws = WorkflowState(pg)
    assert_raises(EvaluationError, lambda: algo.eval(None, ws))
Ejemplo n.º 25
0
def test_ws_store_param_only_on_lonely_input_ports():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_in_port(0, "in", 0)
    pg.add_out_port(0, "out", 1)
    pg.add_vertex(1)
    pg.add_in_port(1, "in", 2)
    pg.connect(1, 2)

    ws = WorkflowState(pg)

    assert_raises(UserWarning, lambda: ws.store_param(1, "param", 0))
    assert_raises(UserWarning, lambda: ws.store_param(2, "param", 0))
Ejemplo n.º 26
0
def test_portgraph_is_out_port():
    pg = PortGraph()
    vid = pg.add_vertex()

    assert_raises(InvalidPort, lambda: pg.is_out_port(None))
    assert_raises(InvalidPort, lambda: pg.is_out_port(0))

    pid = pg.add_out_port(vid, "out")
    assert_raises(InvalidPort, lambda: pg.is_out_port(pid + 1))
    assert pg.is_out_port(pid)

    pid = pg.add_in_port(vid, "in")
    assert not pg.is_out_port(pid)
Ejemplo n.º 27
0
def test_ws_sub_ready_for_evaluation_if_no_input_port():
    pg = PortGraph()
    pg.add_vertex(0)
    pg.add_out_port(0, "out", 0)
    pg.add_vertex(1)
    pg.add_in_port(1, "in", 1)
    pg.connect(0, 1)

    ws = WorkflowState(pg)
    assert ws.is_ready_for_evaluation()

    subpg = get_upstream_subportgraph(pg, 1)
    subws = WorkflowState(subpg)
    assert subws.is_ready_for_evaluation()
Ejemplo n.º 28
0
def test_portgraph_out_port():
    pg = PortGraph()
    assert_raises(InvalidVertex, lambda: pg.out_port(0, None))

    vid = pg.add_vertex()
    assert_raises(InvalidPort, lambda: pg.out_port(vid, None))

    pg.add_in_port(vid, "toto")
    assert_raises(InvalidPort, lambda: pg.out_port(vid, None))

    for lpid in [0, 1, "a", None]:
        assert_raises(InvalidPort, lambda: pg.out_port(vid, "toto"))
        pid = pg.add_out_port(vid, lpid)
        assert pg.out_port(vid, lpid) == pid
Ejemplo n.º 29
0
def test_portgraph_clear():
    pg = PortGraph()
    vid1 = pg.add_vertex()
    vid2 = pg.add_vertex()

    pid1 = pg.add_in_port(vid1, "in")
    pid2 = pg.add_out_port(vid2, "out")

    pg.connect(pid2, pid1)

    pg.clear()

    assert pg.nb_vertices() == 0
    assert pg.nb_edges() == 0
    assert len(tuple(pg.ports())) == 0
Ejemplo n.º 30
0
def test_portgraph_add_out_port():
    pg = PortGraph()
    assert_raises(InvalidVertex, lambda: pg.add_out_port(0, "toto"))

    vid = pg.add_vertex()
    pid = pg.add_out_port(vid, "port")

    # raise error if reuse same global port id
    assert_raises(IndexError, lambda: pg.add_out_port(vid, "toto", pid))
    # raise error if reuse same local port id
    assert_raises(InvalidPort, lambda: pg.add_out_port(vid, "port"))

    assert tuple(pg.out_ports(vid)) == (pid,)
    assert pg.local_id(pid) == "port"
    assert pg.out_port(vid, "port") == pid