Skip to content

Commit

Permalink
Added unit test for 4 types of anyburl rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaikrishnap98 committed Jun 18, 2024
1 parent 94136bb commit 8c750bf
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/knowledge_graph_test_subset.graphml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="isConnectedTo" for="edge" attr.name="isConnectedTo" attr.type="long" />
<key id="Amsterdam_Airport_Schiphol" for="node" attr.name="Amsterdam_Airport_Schiphol" attr.type="long" />
<key id="Riga_International_Airport" for="node" attr.name="Riga_International_Airport" attr.type="long" />
<key id="Chișinău_International_Airport" for="node" attr.name="Chișinău_International_Airport" attr.type="long" />
<key id="Düsseldorf_Airport" for="node" attr.name="Düsseldorf_Airport" attr.type="long" />
<key id="Dubrovnik_Airport" for="node" attr.name="Dubrovnik_Airport" attr.type="long" />
<key id="Athens_International_Airport" for="node" attr.name="Athens_International_Airport" attr.type="long" />
<key id="Yali" for="node" attr.name="Yali" attr.type="long" />
<key id="Vnukovo_International_Airport" for="node" attr.name="Vnukovo_International_Airport" attr.type="long" />
<key id="Hévíz-Balaton_Airport" for="node" attr.name="Hévíz-Balaton_Airport" attr.type="long" />
<key id="Pobedilovo_Airport" for="node" attr.name="Pobedilovo_Airport" attr.type="long" />
<graph id="G" edgedefault="directed">
<node id="Amsterdam_Airport_Schiphol">
<data key="Amsterdam_Airport_Schiphol">1</data>
</node>
<node id="Riga_International_Airport">
<data key="Riga_International_Airport">1</data>
</node>
<node id="Chișinău_International_Airport">
<data key="Chișinău_International_Airport">1</data>
</node>
<node id="Yali">
<data key="Yali">1</data>
</node>
<node id="Düsseldorf_Airport">
<data key="Düsseldorf_Airport">1</data>
</node>
<node id="Pobedilovo_Airport">
<data key="Pobedilovo_Airport">1</data>
</node>
<node id="Dubrovnik_Airport">
<data key="Dubrovnik_Airport">1</data>
</node>
<node id="Hévíz-Balaton_Airport">
<data key="Hévíz-Balaton_Airport">1</data>
</node>
<node id="Athens_International_Airport">
<data key="Athens_International_Airport">1</data>
</node>
<node id="Vnukovo_International_Airport">
<data key="Vnukovo_International_Airport">1</data>
</node>
<edge source="Pobedilovo_Airport" target="Vnukovo_International_Airport">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Vnukovo_International_Airport" target="Hévíz-Balaton_Airport">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Düsseldorf_Airport" target="Dubrovnik_Airport">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Dubrovnik_Airport" target="Athens_International_Airport">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Riga_International_Airport" target="Amsterdam_Airport_Schiphol">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Riga_International_Airport" target="Düsseldorf_Airport">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Chișinău_International_Airport" target="Riga_International_Airport">
<data key="isConnectedTo">1</data>
</edge>
<edge source="Amsterdam_Airport_Schiphol" target="Yali">
<data key="isConnectedTo">1</data>
</edge>

</graph>
</graphml>
132 changes: 132 additions & 0 deletions tests/test_anyBurl_infer_edges_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import pyreason as pr

def test_anyBurl_rule_1():
graph_path = 'knowledge_graph_test_subset.graphml'
pr.reset()
pr.reset_rules()
# Modify pyreason settings to make verbose and to save the rule trace to a file
pr.settings.verbose = True
pr.settings.atom_trace = True
pr.settings.memory_profile = False
pr.settings.canonical = True
pr.settings.inconsistency_check = False
pr.settings.static_graph_facts = False
pr.settings.output_to_file = False
pr.settings.store_interpretation_changes = True
pr.settings.save_graph_attributes_to_trace = True
# Load all the files into pyreason
pr.load_graphml(graph_path)
pr.add_rule(pr.Rule('isConnectedTo(A, Y) <-1 isConnectedTo(Y, B), Amsterdam_Airport_Schiphol(B), Vnukovo_International_Airport(A)', 'connected_rule_1', infer_edges=True))

# Run the program for two timesteps to see the diffusion take place
interpretation = pr.reason(timesteps=1)
# pr.save_rule_trace(interpretation)

# Display the changes in the interpretation for each timestep
dataframes = pr.filter_and_sort_edges(interpretation, ['isConnectedTo'])
for t, df in enumerate(dataframes):
print(f'TIMESTEP - {t}')
print(df)
print()
assert len(dataframes) == 2, 'Pyreason should run exactly 2 fixpoint operations'
assert len(dataframes[1]) == 1, 'At t=1 there should be only 1 new isConnectedTo atom'
assert ('Vnukovo_International_Airport', 'Riga_International_Airport') in dataframes[1]['component'].values.tolist() and dataframes[1]['isConnectedTo'].iloc[0] == [1, 1], '(Vnukovo_International_Airport, Riga_International_Airport) should have isConnectedTo bounds [1,1] for t=1 timesteps'

def test_anyBurl_rule_2():
graph_path = 'knowledge_graph_test_subset.graphml'
pr.reset()
pr.reset_rules()
# Modify pyreason settings to make verbose and to save the rule trace to a file
pr.settings.verbose = True
pr.settings.atom_trace = True
pr.settings.memory_profile = False
pr.settings.canonical = True
pr.settings.inconsistency_check = False
pr.settings.static_graph_facts = False
pr.settings.output_to_file = False
pr.settings.store_interpretation_changes = True
pr.settings.save_graph_attributes_to_trace = True
# Load all the files into pyreason
pr.load_graphml(graph_path)

pr.add_rule(pr.Rule('isConnectedTo(Y, A) <-1 isConnectedTo(Y, B), Amsterdam_Airport_Schiphol(B), Vnukovo_International_Airport(A)', 'connected_rule_2', infer_edges=True))

# Run the program for two timesteps to see the diffusion take place
interpretation = pr.reason(timesteps=1)
# pr.save_rule_trace(interpretation)

# Display the changes in the interpretation for each timestep
dataframes = pr.filter_and_sort_edges(interpretation, ['isConnectedTo'])
for t, df in enumerate(dataframes):
print(f'TIMESTEP - {t}')
print(df)
print()
assert len(dataframes) == 2, 'Pyreason should run exactly 2 fixpoint operations'
assert len(dataframes[1]) == 1, 'At t=1 there should be only 1 new isConnectedTo atom'
assert ('Riga_International_Airport', 'Vnukovo_International_Airport') in dataframes[1]['component'].values.tolist() and dataframes[1]['isConnectedTo'].iloc[0] == [1, 1], '(Riga_International_Airport, Vnukovo_International_Airport) should have isConnectedTo bounds [1,1] for t=1 timesteps'

def test_anyBurl_rule_3():
graph_path = 'knowledge_graph_test_subset.graphml'
pr.reset()
pr.reset_rules()
# Modify pyreason settings to make verbose and to save the rule trace to a file
pr.settings.verbose = True
pr.settings.atom_trace = True
pr.settings.memory_profile = False
pr.settings.canonical = True
pr.settings.inconsistency_check = False
pr.settings.static_graph_facts = False
pr.settings.output_to_file = False
pr.settings.store_interpretation_changes = True
pr.settings.save_graph_attributes_to_trace = True
# Load all the files into pyreason
pr.load_graphml(graph_path)

pr.add_rule(pr.Rule('isConnectedTo(A, Y) <-1 isConnectedTo(B, Y), Amsterdam_Airport_Schiphol(B), Vnukovo_International_Airport(A)', 'connected_rule_3', infer_edges=True))

# Run the program for two timesteps to see the diffusion take place
interpretation = pr.reason(timesteps=1)
# pr.save_rule_trace(interpretation)

# Display the changes in the interpretation for each timestep
dataframes = pr.filter_and_sort_edges(interpretation, ['isConnectedTo'])
for t, df in enumerate(dataframes):
print(f'TIMESTEP - {t}')
print(df)
print()
assert len(dataframes) == 2, 'Pyreason should run exactly 1 fixpoint operations'
assert len(dataframes[1]) == 1, 'At t=1 there should be only 1 new isConnectedTo atom'
assert ('Vnukovo_International_Airport', 'Yali') in dataframes[1]['component'].values.tolist() and dataframes[1]['isConnectedTo'].iloc[0] == [1, 1], '(Vnukovo_International_Airport, Yali) should have isConnectedTo bounds [1,1] for t=1 timesteps'

def test_anyBurl_rule_4():
graph_path = 'knowledge_graph_test_subset.graphml'
pr.reset()
pr.reset_rules()
# Modify pyreason settings to make verbose and to save the rule trace to a file
pr.settings.verbose = True
pr.settings.atom_trace = True
pr.settings.memory_profile = False
pr.settings.canonical = True
pr.settings.inconsistency_check = False
pr.settings.static_graph_facts = False
pr.settings.output_to_file = False
pr.settings.store_interpretation_changes = True
pr.settings.save_graph_attributes_to_trace = True
# Load all the files into pyreason
pr.load_graphml(graph_path)

pr.add_rule(pr.Rule('isConnectedTo(Y, A) <-1 isConnectedTo(B, Y), Amsterdam_Airport_Schiphol(B), Vnukovo_International_Airport(A)', 'connected_rule_4', infer_edges=True))

# Run the program for two timesteps to see the diffusion take place
interpretation = pr.reason(timesteps=1)
# pr.save_rule_trace(interpretation)

# Display the changes in the interpretation for each timestep
dataframes = pr.filter_and_sort_edges(interpretation, ['isConnectedTo'])
for t, df in enumerate(dataframes):
print(f'TIMESTEP - {t}')
print(df)
print()
assert len(dataframes) == 2, 'Pyreason should run exactly 1 fixpoint operations'
assert len(dataframes[1]) == 1, 'At t=1 there should be only 1 new isConnectedTo atom'
assert ('Yali', 'Vnukovo_International_Airport') in dataframes[1]['component'].values.tolist() and dataframes[1]['isConnectedTo'].iloc[0] == [1, 1], '(Yali, Vnukovo_International_Airport) should have isConnectedTo bounds [1,1] for t=1 timesteps'

0 comments on commit 8c750bf

Please sign in to comment.