|
| 1 | +import pyreason as pr |
| 2 | +import networkx as nx |
| 3 | +from pprint import pprint |
| 4 | + |
| 5 | +pr.reset() |
| 6 | +pr.reset_rules() |
| 7 | + |
| 8 | +g = nx.DiGraph() |
| 9 | + |
| 10 | +g.add_nodes_from(['cb_1', 'cb_2', 'l1', 'l2']) |
| 11 | +g.add_edge('cb_1', 'cb_2', stepFrom =1) |
| 12 | +g.add_edge('cb_1', 'l1', hasLabel = 1) |
| 13 | +g.add_edge('cb_2', 'l2', hasLabel = 1) |
| 14 | +#g.add_edge('l1','l2', cond=1) # Adding this edge will satisfy hackerControl(cb) |
| 15 | + |
| 16 | +pr.settings.verbose = True |
| 17 | +pr.settings.atom_trace = True |
| 18 | +pr.settings.inconsistency_check = True |
| 19 | + |
| 20 | +pr.load_graph(g) |
| 21 | + |
| 22 | +# hackerControl is a closed-world pred, meaning that it will be grounded as [0,0] if its bounds are [0,1] (or if it is not in the interpretation dict) |
| 23 | +pr.add_closed_world_predicate('hackerControl') |
| 24 | + |
| 25 | +# Initial fact instantiation |
| 26 | +pr.add_fact(pr.Fact('stepFrom(cb_1, cb_2)', 'step_from_fact', 0, 1)) |
| 27 | +pr.add_fact(pr.Fact('hackerControl(cb_1)', 'hacker_control_initial_fact', 0, 0)) |
| 28 | + |
| 29 | +# Future(Y) will fire for cb_2 |
| 30 | +pr.add_rule(pr.Rule('future(Y) <-1 stepFrom(X,Y), hackerControl(X)')) |
| 31 | + |
| 32 | +#This rule will not fire for cb_2, as cond(cb_1, cb_2) is not grounded |
| 33 | +pr.add_rule(pr.Rule('hackerControl(Y) <-1 hackerControl(X), hasLabel(X,L1), hasLabel(Y,L2), cond(L1, L2), stepFrom(X,Y)', 'hacker-control-rule')) |
| 34 | + |
| 35 | +# At timestep 1, hackerControl(cb_1) and hackerControl(cb_2) have no associated bounds, so they are treated as [0,1]. |
| 36 | +# Because hackerControl is minimized, its bounds are gounded as [0,0]. Future(cb_2) has bounds [1,1], so inconsistent(cb_2) fires. |
| 37 | +pr.add_rule(pr.Rule('inconsistent(Y) <- future(Y), ~hackerControl(Y), ~hackerControl(X)', 'inconsistent_rule')) |
| 38 | + |
| 39 | + |
| 40 | +interpretation = pr.reason(timesteps=2) |
| 41 | +interp_dict = interpretation.get_dict() |
| 42 | + |
| 43 | +pprint(interp_dict) |
| 44 | + |
| 45 | +# Filter and sort nodes based on hackerControl |
| 46 | +dataframes = pr.filter_and_sort_nodes(interpretation, ['hackerControl']) |
| 47 | +for t, df in enumerate(dataframes): |
| 48 | + print(f'TIMESTEP - {t}') |
| 49 | + print(df) |
| 50 | + print() |
| 51 | + |
| 52 | +# Filter and sort edges based on inconsistent |
| 53 | +edge_dataframes = pr.filter_and_sort_nodes(interpretation, ['inconsistent']) |
| 54 | +for t, df in enumerate(edge_dataframes): |
| 55 | + print(f'TIMESTEP - {t} (inconsistent nodes)') |
| 56 | + print(df) |
| 57 | + print() |
0 commit comments