Select Git revision
jest-puppeteer.config.js
test_rule_deducer_relation.py 5.26 KiB
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# RELATION DEDUCER Test
#------------------------------------------------------------------------------
# Script to test the relation deducer under development
#==============================================================================
import subprocess, os
from rdflib import Graph, Namespace
from rdflib.namespace import NamespaceManager, FOAF, RDF
from rdflib import URIRef, Literal, BNode
FILE_PATH = f'{os.path.dirname(os.path.abspath(__file__))}'
INPUT_DIR_PATH = f'{FILE_PATH}/test_data/'
OUTPUT_DIR_PATH = f'{FILE_PATH}/test_data/'
TEST_FILE_NAME_1 = 'deducer-devGraph-1'
TEST_FILE_NAME_2 = 'deducer-devGraph-2'
TEST_FILE_NAME_3 = 'deducer-devGraph-3'
from context import tenet
from tenet.scheme.amr_master_rule.transduction.heuristic_deducer import relation_deducer_1 as test_rule_1
from tenet.scheme import amr_master_rule
from tenet.transduction.rdfterm_computer import __update_uri_with_prefix
from tenet.transduction import rdfterm_computer, prefix_handle
from tenet.transduction import net
#==============================================================================
# Useful Methods
#==============================================================================
def load_test_graph(test_file_name):
print(f'\n -- Test Graph Loading')
graph = Graph()
prefix_handle.update_graph_namespacemanager(graph)
graph_path = f'{INPUT_DIR_PATH}{test_file_name}.ttl'
graph.parse(graph_path)
print(f" ----- Graph Loaded ({len(graph)})")
return graph
def print_triple(graph, triple, num=-1):
num_str = f'[{num}]' if num > -1 else '[-]'
(s, p, o) = triple
s = __update_uri_with_prefix(graph, s)
p = __update_uri_with_prefix(graph, p)
o = __update_uri_with_prefix(graph, o)
print(f' {num_str} {s} {p} {o}')
def add_triples_in_graph(test_file_name, graph, triple_list):
print(f'\n -- Adding triple(s) in graph')
print(f" ----- Graph length before update: {len(graph)}")
print(f" ----- Number of triples to add: {len(triple_list)}")
print(f" ----- Added triples:")
n = 0
graph_length = len(graph)
for triple in triple_list:
graph.add(triple)
if graph_length < len(graph):
n += 1
graph_length = len(graph)
print_triple(graph, triple, num=n)
print(f" ----- Graph length after update: {len(graph)}")
output_graph_path = f'{OUTPUT_DIR_PATH}{test_file_name}.result.ttl'
output_graph_uri = f'https://amr.tetras-libre.fr/rdf/{test_file_name}/result'
print(f'\n -- Serialize test graph to {output_graph_path}')
graph.serialize(destination=output_graph_path,
format='turtle',
base=output_graph_uri)
#==============================================================================
# Development Test
#==============================================================================
def test_search_pattern_deducer(graph):
_, pattern_set = test_rule_1.__search_pattern(graph)
print(f'\n ----- number of selection found: {len(pattern_set)}')
for row in pattern_set:
result_str = f'>>> '
result_str += f'{row.subject_class_net.n3(graph.namespace_manager)}'
result_str += f' {row.predicate_property_net.n3(graph.namespace_manager)}'
result_str += f' {row.object_net.n3(graph.namespace_manager)}'
print(result_str)
return pattern_set
#==============================================================================
# Unit Test
#==============================================================================
def test_rule_application_deducer(test_file_name, graph, rule):
print('\n -- Rule Test for Relation Deduction')
rule_label, new_triple_list = rule(graph)
print(f' ----- label: {rule_label}')
add_triples_in_graph(test_file_name, graph, new_triple_list)
#==============================================================================
# Test Script
#==============================================================================
if __name__ == '__main__':
print('\n *** Test Preparation ***')
graph_1 = load_test_graph(TEST_FILE_NAME_1)
graph_2 = load_test_graph(TEST_FILE_NAME_2)
graph_3 = load_test_graph(TEST_FILE_NAME_3)
print('\n \n')
print('\n ///////////////////// Relation Deduction Rule')
print('\n *** Step Test ***')
print('\n -- Step 1: Search Pattern for Deducer')
pattern_set = test_search_pattern_deducer(graph_1)
pattern_set = test_search_pattern_deducer(graph_2)
pattern_set = test_search_pattern_deducer(graph_3)
print('\n \n')
print('\n *** Unit Test ***')
# test_rule_application_deducer(
# TEST_FILE_NAME_1, graph_1, test_rule_1.deduce_individual_and_relation_from_restriction)
test_rule_application_deducer(
TEST_FILE_NAME_2, graph_2, test_rule_1.deduce_individual_and_relation_from_restriction)
test_rule_application_deducer(
TEST_FILE_NAME_3, graph_3, test_rule_1.deduce_individual_and_relation_from_restriction)
# test_rule_application_deducer(TEST_FILE_NAME_1, graph_1, test_rule_1.deduce_individual_and_relation_from_restriction_Recursively)
print('\n \n')
print('\n *** - ***')