-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from moves-rwth/54-refactor
54 refactor
- Loading branch information
Showing
9 changed files
with
197 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from stormvogel.rdict import rget, rset, merge_dict | ||
|
||
|
||
def test_rget(): | ||
# Empty | ||
d = {} | ||
assert rget(d, []) == {} | ||
# Simple | ||
d = {"a": 1} | ||
assert rget(d, ["a"]) == 1 | ||
# Nested | ||
d = {"a": {"b": {"c": 3, "b": 5}, "c": 2}, "c": 1} | ||
assert rget(d, ["a", "b", "c"]) == 3 | ||
|
||
|
||
def test_rset(): | ||
# Empty path | ||
d = {} | ||
assert rset(d, [], 1) == {} | ||
# Simple | ||
d = {} | ||
assert rset(d, ["a"], 1) == {"a": 1} | ||
# Existing value | ||
d = {"a": 0} | ||
assert rset(d, ["a"], 1) == {"a": 1} | ||
# Nested | ||
d = {"a": {"b": 8}} | ||
assert rset(d, ["a", "b"], {"c": 3}) == {"a": {"b": {"c": 3}}} | ||
|
||
|
||
def test_merge_dict(): | ||
# Test priority for second dict. | ||
d1 = {"a": 1, "b": 1, "c": 1} | ||
d2 = {"b": 2} | ||
assert {"a": 1, "b": 2, "c": 1} == merge_dict(d1, d2) | ||
# Test conservation of elements in both dicts | ||
d1 = {"a": 1, "b": 1} | ||
d2 = {"c": 2, "d": 2} | ||
assert {"a": 1, "b": 1, "c": 2, "d": 2} == merge_dict(d1, d2) | ||
# Test nested | ||
d1 = {"a": {"b": {"c": 1, "d": 1}}, "e": 1} | ||
d2 = {"a": {"b": {"c": 2}}} | ||
assert {"a": {"b": {"c": 2, "d": 1}}, "e": 1} == merge_dict(d1, d2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from stormvogel.visualization import Visualization | ||
from stormvogel.model import Model, ModelType | ||
from stormvogel.result import Result, Scheduler | ||
|
||
|
||
def boilerplate(mocker): | ||
class MockNetwork: | ||
def __init__(self, *args, **kwargs): | ||
self.init(*args, **kwargs) | ||
|
||
init = mocker.stub(name="init_stub") | ||
add_node = mocker.stub(name="add_node_stub") | ||
add_edge = mocker.stub(name="add_edge_stub") | ||
set_options = mocker.stub(name="set_options_stub") | ||
show = mocker.stub(name="show_stub") | ||
|
||
mocker.patch("stormvogel.visjs.Network", MockNetwork) | ||
return MockNetwork | ||
|
||
|
||
def simple_model(): | ||
model = Model("simple", ModelType.DTMC) | ||
one = model.new_state("one") | ||
init = model.get_initial_state() | ||
model.set_transitions(init, [(1, one)]) | ||
return model, one, init | ||
|
||
|
||
def test_show(mocker): | ||
MockNetwork = boilerplate(mocker) | ||
model, one, init = simple_model() | ||
vis = Visualization(model) | ||
vis.show() | ||
MockNetwork.init.assert_called_once_with( | ||
name=vis.name, | ||
width=vis.layout.layout["misc"]["width"], | ||
height=vis.layout.layout["misc"]["height"], | ||
output=vis.output, | ||
debug_output=vis.debug_output, | ||
do_display=False, | ||
do_init_server=vis.do_init_server, | ||
) | ||
MockNetwork.add_node.assert_any_call( | ||
0, label="init", group="states", position_dict={} | ||
) # type: ignore | ||
MockNetwork.add_node.assert_any_call( | ||
1, label="one", group="states", position_dict={} | ||
) # type: ignore | ||
assert MockNetwork.add_node.call_count == 2 | ||
MockNetwork.add_edge.assert_any_call(0, 1, label="1") | ||
assert MockNetwork.add_edge.call_count == 1 | ||
|
||
|
||
def test_rewards(mocker): | ||
MockNetwork = boilerplate(mocker) | ||
model, one, init = simple_model() | ||
model.set_transitions(init, [(1, one)]) | ||
model.add_rewards("LOL") | ||
model.get_rewards("LOL").set(one, 37) | ||
model.add_rewards("HIHI") | ||
model.get_rewards("HIHI").set(one, 42) | ||
vis = Visualization(model=model) | ||
vis.show() | ||
MockNetwork.add_node.assert_any_call( | ||
0, label="init", group="states", position_dict={} | ||
) # type: ignore | ||
MockNetwork.add_node.assert_any_call( | ||
1, label="one\nLOL: 37\nHIHI: 42", group="states", position_dict={} | ||
) # type: ignore | ||
assert MockNetwork.add_node.call_count == 2 | ||
MockNetwork.add_edge.assert_any_call(0, 1, label="1") | ||
assert MockNetwork.add_edge.call_count == 1 | ||
|
||
|
||
def test_results_count(mocker): | ||
MockNetwork = boilerplate(mocker) | ||
model, one, init = simple_model() | ||
result = Result(model, [69, 12]) | ||
|
||
vis = Visualization(model=model, result=result) | ||
vis.show() | ||
RES_SYM = vis.layout.layout["results_and_rewards"]["resultSymbol"] | ||
MockNetwork.add_node.assert_any_call( | ||
0, label=f"init\n{RES_SYM} 69", group="states", position_dict={} | ||
) # type: ignore | ||
MockNetwork.add_node.assert_any_call( | ||
1, label=f"one\n{RES_SYM} 12", group="states", position_dict={} | ||
) # type: ignore | ||
|
||
assert result.values == {0: 69, 1: 12} | ||
assert MockNetwork.add_node.call_count == 2 | ||
MockNetwork.add_edge.assert_any_call(0, 1, label="1") | ||
assert MockNetwork.add_edge.call_count == 1 | ||
|
||
|
||
def test_results_scheduler(mocker): | ||
MockNetwork = boilerplate(mocker) | ||
model = Model("mdp", model_type=ModelType.MDP) | ||
init = model.get_initial_state() | ||
good = model.new_action("good", frozenset(["GOOD"])) | ||
bad = model.new_action("bad", frozenset(["BAD"])) | ||
end = model.new_state("end") | ||
model.set_transitions(init, [(good, end), (bad, end)]) | ||
scheduler = Scheduler(model, {0: good}) | ||
result = Result(model, [1, 2], scheduler) | ||
vis = Visualization(model=model, result=result) | ||
vis.show() | ||
MockNetwork.add_node.assert_any_call( | ||
id=10000000001, label="bad", group="actions", position_dict={} | ||
) | ||
MockNetwork.add_node.assert_any_call( | ||
id=10000000000, label="good", group="scheduled_actions", position_dict={} | ||
) | ||
|
||
assert MockNetwork.add_node.call_count == 4 | ||
assert MockNetwork.add_edge.call_count == 4 |