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

New module: naming_computer

parent 4d29a82b
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# TENET: Naming Computer
#------------------------------------------------------------------------------
# Class grouping methods to compute names and URI for semantic nets (and their
# attributes)
#==============================================================================
from rdflib import URIRef
#==============================================================================
# Naming Parameter
#==============================================================================
# Property Naming Variants
PROPERTY_NAMING_0 = ''
PROPERTY_NAMING_1 = 'ing'
PROPERTY_NAMING_2 = '-by'
PROPERTY_NAMING_3 = '-of'
#==============================================================================
# Method to compute names
#==============================================================================
def define_composite_naming_1(net_1, net_2, net_3,
property_naming=PROPERTY_NAMING_0):
name_1 = f'{net_1.naming}'
name_2 = f'{net_2.naming}{property_naming}'
name_3 = f'{net_3.naming}'
return f'{name_1}_{name_2}_{name_3}'
#==============================================================================
# Method to compute URI
#==============================================================================
def define_net_uri_1(target_net):
uri = None
if target_net.naming is not None:
uri = URIRef(f'net:{target_net.type_name}_{target_net.naming}')
return uri
\ No newline at end of file
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
# Class to handle semantic nets # Class to handle semantic nets
#============================================================================== #==============================================================================
import rdflib
from rdflib import URIRef
from febTransduction.net import SemanticNetReferenceHandle from febTransduction.net import SemanticNetReferenceHandle
from febTransduction.query_builder import generate_select_query from febTransduction.query_builder import generate_select_query
...@@ -110,9 +113,16 @@ class Net: ...@@ -110,9 +113,16 @@ class Net:
def uri(self): def uri(self):
return self._uri return self._uri
@uri.setter @uri.setter
def uri(self, new_value): def uri(self, new_value):
control = (isinstance(new_value, rdflib.term.URIRef) or isinstance(new_value, str))
assert control, f'new_value does not a string or URIRef ({type(new_value)})'
if isinstance(new_value, rdflib.term.URIRef):
self._uri = new_value self._uri = new_value
else:
self._uri = URIRef(new_value)
......
...@@ -21,6 +21,7 @@ if __name__ == '__main__': ...@@ -21,6 +21,7 @@ if __name__ == '__main__':
import febTransduction as transduction import febTransduction as transduction
from febTransduction.query_builder import generate_select_query from febTransduction.query_builder import generate_select_query
from febTransduction.naming_computer import *
#============================================================================== #==============================================================================
...@@ -100,7 +101,7 @@ def analyze_phenomena_or_1(graph): ...@@ -100,7 +101,7 @@ def analyze_phenomena_or_1(graph):
composite_class_net.compose(class_net, property_net, phenomena_net) composite_class_net.compose(class_net, property_net, phenomena_net)
# -- Data Computation # -- Data Computation
composite_class_net.mother_class = class_net.uri composite_class_net.mother_class_net = class_net.uri
# etc # etc
# -- Restriction Computation # -- Restriction Computation
...@@ -115,6 +116,11 @@ def analyze_phenomena_or_1(graph): ...@@ -115,6 +116,11 @@ def analyze_phenomena_or_1(graph):
# TODO: à voir si on veut d'autres relations # TODO: à voir si on veut d'autres relations
# -- Finalization # -- Finalization
composite_class_net.uri = naming_computer.define_composite_uri_1(
composite_class_net,
class_net, property_net, phenomena_net,
naming_computer.PROPERTY_NAMING_1
)
composite_class_net.finalize() composite_class_net.finalize()
new_triples = composite_class_net.generate_triple_definition() new_triples = composite_class_net.generate_triple_definition()
new_triple_list.append(new_triples) new_triple_list.append(new_triples)
......
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# TENET: Semantic Net Class Test
#------------------------------------------------------------------------------
# Script to test the semantic net classes
#==============================================================================
import subprocess, os
from rdflib import Graph
from rdflib import Namespace
from rdflib.namespace import NamespaceManager
FILE_PATH = f'{os.path.dirname(os.path.abspath(__file__))}'
INPUT_DIR_PATH = f'{FILE_PATH}/input/'
OUTPUT_DIR_PATH = f'{FILE_PATH}/output/'
TEST_GRAPH = f'{INPUT_DIR_PATH}testGraph1.ttl'
from context import tenet
from tenet.febTransduction.net import AtomClassNet
from tenet.febTransduction.net import CompositeClassNet
from tenet.febTransduction.net import AtomPropertyNet
from tenet.febTransduction.net import PhenomenaNet
from tenet.febTransduction import naming_computer
#==============================================================================
# Utilities
#==============================================================================
def test_attribute_access(net, *attr_set):
print(f'\n *** Net attributes ({net.type_id})) ***')
for attr in attr_set:
print(f' ----- {attr}: {eval(attr)}')
#==============================================================================
# Development Test
#==============================================================================
print('\n' + ' *** Development Test ***')
print('\n *** TEST: Naming Computer ***')
print(f'\n -- Test Graph Loading')
graph = Graph()
graph.bind('net', Namespace('https://tenet.tetras-libre.fr/semantic-net#'))
graph.parse(TEST_GRAPH)
print(f" ----- Graph Loaded ({len(graph)})")
print('\n -- Test data: some semantic nets')
net_1 = AtomClassNet(graph, uri='net:atomClass_object_o')
print(f' ----- net_1: {net_1.uri} ({net_1.naming})')
net_2 = AtomPropertyNet(graph, uri='net:atomProperty_orbit_o2')
print(f' ----- net_2: {net_2.uri} ({net_2.naming})')
net_3 = AtomClassNet(graph, uri='net:atomClass_sun_s2')
print(f' ----- net_3: {net_3.uri} ({net_3.naming})')
net_4 = PhenomenaNet(graph, uri='net:phenomena_conjunction-AND_a')
print(f' ----- net_4: {net_4.uri} ({net_4.naming})')
print('\n -- Test data: composite net')
composite_net_1 = CompositeClassNet(graph)
composite_net_1.compose(net_1, net_2, net_3)
print(f' ----- composite_net_1: {composite_net_1.uri} ({composite_net_1.naming})')
print('\n -- URI definition')
naming_0 = naming_computer.define_composite_naming_1(net_1, net_2, net_3)
print(f' ----- Naming (0)): {naming_0}')
naming_1 = naming_computer.define_composite_naming_1(net_1, net_2, net_3,
naming_computer.PROPERTY_NAMING_1)
print(f' ----- Naming (1)): {naming_1}')
naming_2 = naming_computer.define_composite_naming_1(net_1, net_2, net_3,
naming_computer.PROPERTY_NAMING_2)
print(f' ----- Naming (2)): {naming_2}')
naming_3 = naming_computer.define_composite_naming_1(net_1, net_2, net_3,
naming_computer.PROPERTY_NAMING_3)
print(f' ----- Naming (3)): {naming_3}')
print('\n -- Update composite net (with naming_1)')
composite_net_1.naming = naming_1
composite_net_1.uri = naming_computer.define_net_uri_1(composite_net_1)
test_attribute_access(composite_net_1, 'net.uri',
'net.node', 'net.base_node', 'net.structure', 'net.naming',
'net.class_name',
'net.mother_class_net', 'net.restriction', 'net.restriction01')
print('\n -- Advanced Test(s)')
composite_net_2 = CompositeClassNet(graph)
composite_net_2.compose(net_1, net_2, net_4)
composite_net_2.naming = naming_computer.define_composite_naming_1(net_1, net_2, net_4)
composite_net_2.uri = naming_computer.define_net_uri_1(composite_net_2)
test_attribute_access(composite_net_2, 'net.uri',
'net.node', 'net.base_node', 'net.structure', 'net.naming',
'net.class_name',
'net.mother_class_net', 'net.restriction', 'net.restriction01')
print('\n' + ' *** - ***')
\ No newline at end of file
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# TENET: Semantic Net Class Test
#------------------------------------------------------------------------------
# Script to test the semantic net classes
#==============================================================================
import subprocess, os
from rdflib import Graph
from rdflib import Namespace
from rdflib.namespace import NamespaceManager
FILE_PATH = f'{os.path.dirname(os.path.abspath(__file__))}'
INPUT_DIR_PATH = f'{FILE_PATH}/input/'
OUTPUT_DIR_PATH = f'{FILE_PATH}/output/'
TEST_GRAPH = f'{INPUT_DIR_PATH}testGraph1.ttl'
from context import tenet
from tenet.febTransduction.net import Net
from tenet.febTransduction.net import ClassNet
from tenet.febTransduction.net import AtomClassNet
from tenet.febTransduction.net import CompositeClassNet
from tenet.febTransduction.net import PropertyNet
from tenet.febTransduction.net import AtomPropertyNet
from tenet.febTransduction.net import CompositePropertyNet
from tenet.febTransduction.net import IndividualNet
from tenet.febTransduction.net import ValueNet
from tenet.febTransduction.net import PhenomenaNet
from tenet.febTransduction.net import RestrictionNet
#==============================================================================
# Utilities
#==============================================================================
def test_attribute_access(net, *attr_set):
print(f'\n *** Net attributes ({net.type_id})) ***')
for attr in attr_set:
print(f' ----- {attr}: {eval(attr)}')
#==============================================================================
# Development Test
#==============================================================================
print('\n' + ' *** Development Test ***')
print('\n *** TEST: Semantic Net Data Computation ***')
print(f'\n -- Test Graph Loading')
graph = Graph()
graph.bind('net', Namespace('https://tenet.tetras-libre.fr/semantic-net#'))
graph.parse(TEST_GRAPH)
print(f" ----- Graph Loaded ({len(graph)})")
print('\n -- Nets to compose')
net_1 = AtomClassNet(graph, uri='net:atomClass_object_o')
print(f' ----- net_1: {net_1.uri} ({net_1.naming})')
net_2 = PropertyNet(graph, uri='net:atomProperty_orbit_o2')
print(f' ----- net_2: {net_2.uri} ({net_2.naming})')
net_3 = AtomClassNet(graph, uri='net:atomClass_sun_s2')
print(f' ----- net_3: {net_3.uri} ({net_3.naming})')
print('\n -- New Net constructed by composition')
new_net = CompositeClassNet(graph)
new_net.compose(net_1, net_2, net_3)
test_attribute_access(new_net, 'net.uri',
'net.node', 'net.base_node', 'net.structure', 'net.naming',
'net.class_name',
'net.mother_class_net', 'net.restriction', 'net.restriction01')
print('\n -- Data compuation to update a new net')
new_net.mother_class_net = net_1.uri
test_attribute_access(new_net, 'net.uri',
'net.node', 'net.base_node', 'net.structure', 'net.naming',
'net.class_name',
'net.mother_class_net', 'net.restriction', 'net.restriction01')
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