From 8c750bff34b0005ea9eba9071724724b47c94e04 Mon Sep 17 00:00:00 2001 From: jpatil14 Date: Tue, 18 Jun 2024 00:23:50 -0700 Subject: [PATCH 1/3] Added unit test for 4 types of anyburl rules. --- tests/knowledge_graph_test_subset.graphml | 71 ++++++++++++ tests/test_anyBurl_infer_edges_rules.py | 132 ++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 tests/knowledge_graph_test_subset.graphml create mode 100644 tests/test_anyBurl_infer_edges_rules.py diff --git a/tests/knowledge_graph_test_subset.graphml b/tests/knowledge_graph_test_subset.graphml new file mode 100644 index 00000000..72e5c23b --- /dev/null +++ b/tests/knowledge_graph_test_subset.graphml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + + \ No newline at end of file diff --git a/tests/test_anyBurl_infer_edges_rules.py b/tests/test_anyBurl_infer_edges_rules.py new file mode 100644 index 00000000..7b3ae35f --- /dev/null +++ b/tests/test_anyBurl_infer_edges_rules.py @@ -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' \ No newline at end of file From 2a23a8ba87b20b22ef127d1952f5f687f5df6e12 Mon Sep 17 00:00:00 2001 From: jpatil14 Date: Tue, 18 Jun 2024 00:45:03 -0700 Subject: [PATCH 2/3] Added unit test for 4 types of anyburl rules: Updated graphml path --- tests/test_anyBurl_infer_edges_rules.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_anyBurl_infer_edges_rules.py b/tests/test_anyBurl_infer_edges_rules.py index 7b3ae35f..d151de78 100644 --- a/tests/test_anyBurl_infer_edges_rules.py +++ b/tests/test_anyBurl_infer_edges_rules.py @@ -1,7 +1,7 @@ import pyreason as pr def test_anyBurl_rule_1(): - graph_path = 'knowledge_graph_test_subset.graphml' + graph_path = './tests/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 @@ -33,7 +33,7 @@ def test_anyBurl_rule_1(): 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' + graph_path = './tests/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 @@ -66,7 +66,7 @@ def test_anyBurl_rule_2(): 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' + graph_path = './tests/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 @@ -99,7 +99,7 @@ def test_anyBurl_rule_3(): 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' + graph_path = './tests/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 From bc2a67c3d12e36b2d479e24970939abd1c263990 Mon Sep 17 00:00:00 2001 From: Dyuman Aditya Date: Sat, 27 Jul 2024 13:04:16 +0200 Subject: [PATCH 3/3] Merged v3.0.0 into this tests branch --- tests/test_anyBurl_infer_edges_rules.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_anyBurl_infer_edges_rules.py b/tests/test_anyBurl_infer_edges_rules.py index d151de78..6aa3af5f 100644 --- a/tests/test_anyBurl_infer_edges_rules.py +++ b/tests/test_anyBurl_infer_edges_rules.py @@ -1,5 +1,6 @@ import pyreason as pr + def test_anyBurl_rule_1(): graph_path = './tests/knowledge_graph_test_subset.graphml' pr.reset() @@ -32,6 +33,7 @@ def test_anyBurl_rule_1(): 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 = './tests/knowledge_graph_test_subset.graphml' pr.reset() @@ -65,6 +67,7 @@ def test_anyBurl_rule_2(): 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 = './tests/knowledge_graph_test_subset.graphml' pr.reset() @@ -98,6 +101,7 @@ def test_anyBurl_rule_3(): 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 = './tests/knowledge_graph_test_subset.graphml' pr.reset() @@ -129,4 +133,4 @@ def test_anyBurl_rule_4(): 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' \ No newline at end of file + 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'