Skip to content
Snippets Groups Projects
Commit 16cca79a authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

Minimal AMR Rule: generation.odrl_rule_generator

parent f49cdecd
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ from transduction.naming_computer import define_composite_naming_2
def __search_pattern_1(graph):
select_data_list = ['?rule_net']
clause_list = [(f'?rule_net a net:Rule_Net.')]
clause_list = [f'?rule_net a net:Rule_Net.']
query_code = generate_select_query(graph, select_data_list, clause_list)
result_set = graph.query(query_code)
return query_code, result_set
......@@ -60,19 +60,30 @@ def __is_property_to_generate(property_net):
#==============================================================================
def __compute_policy_uri(graph, net):
return produce_uriref(graph, f'ext-out:policy_{net.structure}')
structure_name = net.get_attribute_first_value(net.structure)
return produce_uriref(graph, f'ext-out:policy_{structure_name}')
def __compute_odrl_rule_type_uri(graph, net):
odrl_rule_type_uri = f'odrl:permission'
# TODO: odrl:permission, odrl;obligation, odrl:prohibition
naming = net.get_attribute_first_value(net.rule_relation_name)
odrl_rule_type_uri = f'odrl:{naming}'
return produce_uriref(graph, odrl_rule_type_uri)
def __generate_odrl_triple_definition(graph, net):
policy_uri = __compute_policy_uri(graph, net)
odrl_rule_type_uri = __compute_odrl_rule_type_uri(graph, net)
action_definition = produce_literal(graph, 'net:x')
def __compute_odrl_action_code(graph, rule_net):
action_net_uri = rule_net.get_attribute_first_value(rule_net.rule_action_net)
if action_net_uri is not None:
print(action_net_uri)
action_net_uri = produce_uriref(graph, action_net_uri)
print(action_net_uri)
action_net = net.ActionNet(graph, action_net_uri)
return produce_literal(graph, 'net:x')
def __generate_odrl_triple_definition(graph, rule_net):
policy_uri = __compute_policy_uri(graph, rule_net)
odrl_rule_type_uri = __compute_odrl_rule_type_uri(graph, rule_net)
action_definition = __compute_odrl_action_code(graph, rule_net)
# print(f'{policy_uri}, {odrl_rule_type_uri}, {action_definition}')
return [(policy_uri, odrl_rule_type_uri, action_definition)]
......@@ -91,8 +102,9 @@ def generate_odrl_rule(graph):
# -- Get property net listings
_, pattern_set = __search_pattern_1(graph)
# # -- Triple Definition for 'not deprecated property net'
# -- Triple Definition for 'not deprecated property net'
for pattern in pattern_set:
print(pattern.rule_net)
rule_net = net.RuleNet(graph, pattern.rule_net)
if not rule_net.is_deprecated():
rule_triple_list += __generate_odrl_triple_definition(graph, rule_net)
......
- INFO - [TENET] Extraction Processing
- INFO -
=== Process Initialization ===
- INFO - -- Process Setting
- INFO - ----- Corpus source: /home/lamenji/Workspace/Tetras/tenet/tests/input/amrDocuments/dev/asail_odrl_sentences/s03.stog.amr.ttl (amr)
- INFO - ----- Base output dir: /home/lamenji/Workspace/Tetras/tenet/tests/output/aos03-20230413/aos03_factoid.ttl
- INFO - ----- technical dir path: /home/lamenji/Workspace/Tetras/tenet/tests/output/aos03-20230413/technical-data/
- INFO - ----- Ontology target (id): https://tenet.tetras-libre.fr/demo/clara/03/
- DEBUG - handle_close[ddbf9d58da4311edb5fe14abc581ea52]({'header': {'msg_id': '31fe189f-aad39126dac2f71a0a08fe6f_50868_88', 'msg_type': 'comm_close', 'username': 'lamenji', 'session': '31fe189f-aad39126dac2f71a0a08fe6f', 'date': datetime.datetime(2023, 4, 13, 21, 48, 47, 997618, tzinfo=tzutc()), 'version': '5.3'}, 'msg_id': '31fe189f-aad39126dac2f71a0a08fe6f_50868_88', 'msg_type': 'comm_close', 'parent_header': {}, 'metadata': {}, 'content': {'comm_id': 'ddbf9d58da4311edb5fe14abc581ea52', 'data': {}}, 'buffers': []})
)
/tenet.tetras-libre.fr/demo/clara/03/
- INFO - ----- Current path: /home/lamenji/Workspace/Tetras/tenet/tenet
- DEBUG - ----- Config file: /home/lamenji/Workspace/Tetras/tenet/tenet/config.xml
- DEBUG -
......@@ -72,25 +67,25 @@
- DEBUG - ----- Total rule number: 87
- INFO - -- Applying extraction step: preprocessing
- INFO - --- *** November Transduction *** Sequence: amrld-correcting-sequence
- DEBUG - ----- fix-amr-bug-about-system-solar-planet: 0/0 new triple (551, 0:00:00.026522)
- DEBUG - ----- fix-amr-bug-about-system-solar-planet: 0/0 new triple (551, 0:00:00.027755)
- INFO - --- *** November Transduction *** Sequence: amr-reification-sequence
- INFO - ----- reclassify-concept-1: 5/5 new triples (556, 0:00:00.105608)
- DEBUG - ----- reclassify-concept-2: 0/0 new triple (556, 0:00:00.064289)
- INFO - ----- reclassify-concept-3: 4/4 new triples (560, 0:00:00.048410)
- INFO - ----- reclassify-concept-4: 4/4 new triples (564, 0:00:00.058512)
- INFO - ----- reclassify-concept-5: 4/4 new triples (568, 0:00:00.041019)
- DEBUG - ----- reify-roles-as-concept: 0/0 new triple (568, 0:00:00.043750)
- INFO - ----- reclassify-existing-variable: 17/17 new triples (585, 0:00:00.031118)
- DEBUG - ----- add-new-variable-for-reified-concept: 0/0 new triple (585, 0:00:00.042439)
- INFO - ----- add-amr-leaf-for-reclassified-concept: 12/12 new triples (597, 0:00:00.040535)
- DEBUG - ----- add-amr-leaf-for-reified-concept: 0/0 new triple (597, 0:00:00.039820)
- INFO - ----- add-amr-edge-for-core-relation: 9/9 new triples (606, 0:00:00.092282)
- DEBUG - ----- add-amr-edge-for-reified-concept: 0/0 new triple (606, 0:00:00.083474)
- INFO - ----- add-amr-edge-for-name-relation: 5/5 new triples (611, 0:00:00.080671)
- DEBUG - ----- add-value-for-quant-relation: 0/0 new triple (611, 0:00:00.079411)
- INFO - ----- add-amr-edge-for-polarity-relation: 5/5 new triples (616, 0:00:00.081605)
- INFO - ----- update-amr-edge-role-1: 5/5 new triples (621, 0:00:00.050274)
- INFO - ----- add-amr-root: 5/5 new triples (626, 0:00:00.032472)
- INFO - ----- reclassify-concept-1: 5/5 new triples (556, 0:00:00.096391)
- DEBUG - ----- reclassify-concept-2: 0/0 new triple (556, 0:00:00.052021)
- INFO - ----- reclassify-concept-3: 4/4 new triples (560, 0:00:00.046134)
- INFO - ----- reclassify-concept-4: 4/4 new triples (564, 0:00:00.056738)
- INFO - ----- reclassify-concept-5: 4/4 new triples (568, 0:00:00.042466)
- DEBUG - ----- reify-roles-as-concept: 0/0 new triple (568, 0:00:00.039816)
- INFO - ----- reclassify-existing-variable: 17/17 new triples (585, 0:00:00.028319)
- DEBUG - ----- add-new-variable-for-reified-concept: 0/0 new triple (585, 0:00:00.059122)
- INFO - ----- add-amr-leaf-for-reclassified-concept: 12/12 new triples (597, 0:00:00.041588)
- DEBUG - ----- add-amr-leaf-for-reified-concept: 0/0 new triple (597, 0:00:00.034341)
- INFO - ----- add-amr-edge-for-core-relation: 9/9 new triples (606, 0:00:00.095954)
- DEBUG - ----- add-amr-edge-for-reified-concept: 0/0 new triple (606, 0:00:00.079685)
- INFO - ----- add-amr-edge-for-name-relation: 5/5 new triples (611, 0:00:00.101049)
- DEBUG - ----- add-value-for-quant-relation: 0/0 new triple (611, 0:00:00.078799)
- INFO - ----- add-amr-edge-for-polarity-relation: 5/5 new triples (616, 0:00:00.084544)
- INFO - ----- update-amr-edge-role-1: 5/5 new triples (621, 0:00:00.045121)
- INFO - ----- add-amr-root: 5/5 new triples (626, 0:00:00.030758)
- DEBUG - --- Serializing graph to tenet.tetras-libre.fr_demo_clara_03_preprocessing
- DEBUG - ----- step: preprocessing
- DEBUG - ----- id: https://tenet.tetras-libre.fr/demo/clara/03/
......@@ -99,25 +94,25 @@
- INFO - ----- 75 triples extracted during preprocessing step
- INFO - -- Applying extraction step: transduction
- INFO - --- *** February Transduction *** Sequence: atomic extraction sequence
- INFO - ----- extract atom classes: 12/12 new triples (638, 0:00:00.069749)
- INFO - ----- extract atom individuals: 7/7 new triples (645, 0:00:00.050061)
- INFO - ----- extract atomic properties: 13/13 new triples (658, 0:00:00.127397)
- INFO - ----- extract atom values: 10/10 new triples (668, 0:00:00.050948)
- INFO - ----- extract atom phenomena: 7/7 new triples (675, 0:00:00.041708)
- INFO - ----- propagate atom relations: 11/22 new triples (686, 0:00:00.311171)
- INFO - ----- extract atom classes: 12/12 new triples (638, 0:00:00.067803)
- INFO - ----- extract atom individuals: 7/7 new triples (645, 0:00:00.047774)
- INFO - ----- extract atomic properties: 13/13 new triples (658, 0:00:00.049456)
- INFO - ----- extract atom values: 10/10 new triples (668, 0:00:00.062454)
- INFO - ----- extract atom phenomena: 7/7 new triples (675, 0:00:00.038464)
- INFO - ----- propagate atom relations: 11/22 new triples (686, 0:00:00.419121)
- INFO - --- *** February Transduction *** Sequence: phenomena analyze sequence (1)
- DEBUG - ----- analyze "polarity" phenomena (1): 0/0 new triple (686, 0:00:00.009125)
- INFO - ----- analyze "polarity" phenomena (2): 12/14 new triples (698, 0:00:00.076581)
- DEBUG - ----- analyze modifier phenomena (mod): 0/0 new triple (698, 0:00:00.009838)
- DEBUG - ----- analyze "polarity" phenomena (1): 0/0 new triple (686, 0:00:00.009006)
- INFO - ----- analyze "polarity" phenomena (2): 12/14 new triples (698, 0:00:00.052621)
- DEBUG - ----- analyze modifier phenomena (mod): 0/0 new triple (698, 0:00:00.006898)
- INFO - --- *** February Transduction *** Sequence: phenomena analyze sequence (2)
- DEBUG - ----- analyze "or" phenomena (1): 0/0 new triple (698, 0:00:00.010714)
- DEBUG - ----- analyze "or" phenomena (2): 0/0 new triple (698, 0:00:00.014737)
- DEBUG - ----- analyze "or" phenomena (1): 0/0 new triple (698, 0:00:00.009443)
- DEBUG - ----- analyze "or" phenomena (2): 0/0 new triple (698, 0:00:00.014862)
- INFO - --- *** February Transduction *** Sequence: composite class extraction sequence
- INFO - ----- extract composite classes (1): 23/24 new triples (721, 0:00:00.126944)
- DEBUG - ----- extract composite classes (2): 0/0 new triple (721, 0:00:00.025372)
- INFO - ----- extract composite classes (1): 23/24 new triples (721, 0:00:00.138688)
- DEBUG - ----- extract composite classes (2): 0/0 new triple (721, 0:00:00.018134)
- INFO - --- *** February Transduction *** Sequence: ODRL extraction sequence
- INFO - ----- extract ODRL actions: 12/14 new triples (733, 0:00:00.093927)
- INFO - ----- extract ODRL rules: 11/11 new triples (744, 0:00:00.116949)
- INFO - ----- extract ODRL actions: 12/14 new triples (733, 0:00:00.095729)
- INFO - ----- extract ODRL rules: 11/11 new triples (744, 0:00:00.115878)
- DEBUG - --- Serializing graph to tenet.tetras-libre.fr_demo_clara_03_transduction
- DEBUG - ----- step: transduction
- DEBUG - ----- id: https://tenet.tetras-libre.fr/demo/clara/03/
......@@ -126,7 +121,7 @@
- INFO - ----- 118 triples extracted during transduction step
- INFO - -- Applying extraction step: generation
- INFO - --- *** February Transduction *** Sequence: ODRL Rule Generation Sequence
- INFO - ----- generate ODRL rule: 1/1 new triple (745, 0:00:00.016414)
- INFO - ----- generate ODRL rule: 1/1 new triple (745, 0:00:00.039938)
- DEBUG - --- Serializing graph to tenet.tetras-libre.fr_demo_clara_03_generation
- DEBUG - ----- step: generation
- DEBUG - ----- id: https://tenet.tetras-libre.fr/demo/clara/03/
......@@ -149,6 +144,7 @@
- INFO -
*** Execution Time ***
----- Function: create_ontology_from_amrld_file (tenet.main)
----- Total Time: 0:00:02.567053
----- Process Time: 0:00:02.538713
----- Total Time: 0:00:02.715752
----- Process Time: 0:00:02.630108
*** - ***
- DEBUG - handle_close[c12677ced9db11edb5fe14abc581ea52]({'header': {'msg_id': 'ffab3b95-4abbba292202b266c50ed0- DEBUG - handle_close[dd509a52da2511edb5fe14abc581ea52]({'header': {'msg_id': 'd976c9c2-b0931d003eee38a633805abf_50868_28', 'msg_type': 'comm_close', 'username': 'lamenji', 'session': 'd976c9c2-b0931d003eee38a633805abf', 'date': datetime.datetime(2023, 4, 13, 21, 48, 47, 54497, tzinfo=tzutc()), 'version': '5.3'}, 'msg_id': 'd976c9c2-b0931d003eee38a633805abf_50868_28', 'msg_type': 'comm_close', 'parent_header': {}, 'metadata': {}, 'content': {'comm_id': 'dd509a52da2511edb5fe14abc581ea52', 'data': {}}, 'buffers': []})
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# TENET: Extraction Rule Test
#------------------------------------------------------------------------------
# Script to test rules 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 = 'odrl-generation-devGraph-1'
TEST_FILE_NAME_2 = 'odrl-generation-devGraph-2'
TEST_FILE_NAME_3 = 'odrl-generation-devGraph-3'
from context import tenet
from tenet.scheme.amr_clara_rule.odrl_generation import odrl_rule_generator as dev_rule
from tenet.scheme import amr_clara_rule as rule
from tenet.transduction import rdfterm_computer, prefix_handle
from tenet.transduction.rdfterm_computer import __update_uri_with_prefix
#==============================================================================
# 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 define_clause_list(composition_pattern_list):
clause_list = []
for (net_1, relation, net_2) in composition_pattern_list:
clause_list.append(f'{net_1} {relation} {net_2}.')
return clause_list
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_1(graph):
print('\n -- Search patterns (1)')
_, result_set = dev_rule.__search_pattern_1(graph)
print(f'\n ----- number of selection found: {len(result_set)}')
for selection in result_set:
result_str = f'>>> '
result_str += f'{selection.rule_net.n3(graph.namespace_manager)}'
print(result_str)
#==============================================================================
# Unit Test
#==============================================================================
def test_rule_application(test_file_name, graph, rule):
print('\n -- Rule Test')
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 ///////////////////// Extraction Rule 1')
print('\n *** Step Test ***')
print('\n -- Step 1: Search Pattern')
pattern_set = test_search_pattern_1(graph_1)
print('\n \n')
print('\n *** Unit Test ***')
test_rule_application(TEST_FILE_NAME_1, graph_1, rule.generate_odrl_rule)
print('\n \n')
print('\n *** - ***')
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment