Skip to content

Commit 6bb695c

Browse files
Merge pull request #22 from oeg-upm/dev
Dev
2 parents d510302 + e1e4316 commit 6bb695c

File tree

11 files changed

+11449
-205
lines changed

11 files changed

+11449
-205
lines changed

.github/workflows/test.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest, macos-latest, windows-latest]
11+
python-version: ['3.9', '3.12']
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Set up Python
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: ${{ matrix.python-version }}
18+
- name: Install dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install poetry
22+
poetry update
23+
- name: Build ocp2kg
24+
run: |
25+
poetry install --with test
26+
- name: Test with pytest
27+
run: |
28+
poetry run pytest

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ RDF following the [OWL Change Ontology](https://w3id.org/def/och) model.
1212
# How to run it?
1313

1414
```bash
15-
python3 -m pip install -r requirements
16-
python3 evol_kg.py -c path_to_change_kg.nt -m path_to_old_mapping.rml.ttl -o path_to_new_ontology.ttl -n path_output_mappings.rml.ttl
15+
python3 -m pip install ocp2kg
16+
python3 -m ocp2kg -c path_to_change_kg.nt -m path_to_old_mapping.rml.ttl -o path_to_new_ontology.ttl -n path_output_mappings.rml.ttl
1717
```
1818

1919
Options:

src/ocp2kg/__init__.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from rdflib import URIRef, Graph
2+
from .constants import *
3+
from .evol_kg import *
4+
5+
def propagate(change_data, output_mappings, review_mappings, ontology=Graph()):
6+
changes_order = (OCH_ADD_CLASS, OCH_ADD_SUBCLASS, OCH_ADD_OBJECT_PROPERTY, OCH_ADD_DATA_PROPERTY, OCH_REMOVE_CLASS,
7+
OCH_REMOVE_SUBCLASS, OCH_REMOVE_OBJECT_PROPERTY, OCH_REMOVE_DATA_PROPERTY)
8+
9+
# ToDo: removing subclass action needs to be implemented
10+
for change_type in changes_order:
11+
12+
q = f' SELECT DISTINCT ?change WHERE {{ ' \
13+
f' ?change {RDF_TYPE} {URIRef(change_type)} . }}'
14+
15+
for change_result in change_data.query(q):
16+
if URIRef(change_type) == URIRef(OCH_ADD_CLASS):
17+
add_class(change_result["change"], change_data, output_mappings)
18+
elif URIRef(change_type) == URIRef(OCH_REMOVE_CLASS):
19+
remove_class(change_result["change"],change_data, output_mappings, review_mappings, ontology)
20+
elif URIRef(change_type) == URIRef(OCH_ADD_SUBCLASS):
21+
add_super_class(change_result["change"],change_data, output_mappings)
22+
elif URIRef(change_type) == URIRef(OCH_REMOVE_SUBCLASS):
23+
remove_super_class(change_result["change"],change_data, output_mappings)
24+
elif URIRef(change_type) == URIRef(OCH_ADD_OBJECT_PROPERTY):
25+
add_object_property(change_result["change"],change_data, output_mappings)
26+
elif URIRef(change_type) == URIRef(OCH_REMOVE_OBJECT_PROPERTY):
27+
remove_object_property(change_result["change"],change_data, output_mappings)
28+
elif URIRef(change_type) == URIRef(OCH_ADD_DATA_PROPERTY):
29+
add_data_property(change_result["change"],change_data, output_mappings)
30+
elif URIRef(change_type) == URIRef(OCH_REMOVE_DATA_PROPERTY):
31+
remove_data_property(change_result["change"],change_data, output_mappings)
32+
33+
logger.info("Changes propagated over the mapping rules, writing results...")
34+
return output_mappings

src/ocp2kg/__main__.py

+4-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
from rdflib import Graph, URIRef, Variable
3-
3+
from . import propagate
44
import yatter
5-
from ruamel.yaml import YAML
65
import argparse
6+
from ruamel.yaml import YAML
77
from .evol_kg import *
88

99
def define_args():
@@ -33,36 +33,9 @@ def define_args():
3333

3434
review_mappings = Graph()
3535

36-
changes_order = (OCH_ADD_CLASS, OCH_ADD_SUBCLASS, OCH_ADD_OBJECT_PROPERTY, OCH_ADD_DATA_PROPERTY, OCH_REMOVE_CLASS,
37-
OCH_REMOVE_SUBCLASS, OCH_REMOVE_OBJECT_PROPERTY, OCH_REMOVE_DATA_PROPERTY)
38-
39-
# ToDo: removing subclass action needs to be implemented
40-
for change_type in changes_order:
41-
42-
q = f' SELECT DISTINCT ?change WHERE {{ ' \
43-
f' ?change {RDF_TYPE} {URIRef(change_type)} . }}'
44-
45-
for change_result in change_data.query(q):
46-
if URIRef(change_type) == URIRef(OCH_ADD_CLASS):
47-
add_class(change_result["change"], change_data, output_mappings)
48-
elif URIRef(change_type) == URIRef(OCH_REMOVE_CLASS):
49-
remove_class(change_result["change"],change_data, ontology, output_mappings, review_mappings)
50-
elif URIRef(change_type) == URIRef(OCH_ADD_SUBCLASS):
51-
add_super_class(change_result["change"], change_data, output_mappings)
52-
elif URIRef(change_type) == URIRef(OCH_REMOVE_SUBCLASS):
53-
remove_super_class(change_result["change"], change_data, output_mappings)
54-
elif URIRef(change_type) == URIRef(OCH_ADD_OBJECT_PROPERTY):
55-
add_object_property(change_result["change"], change_data, output_mappings)
56-
elif URIRef(change_type) == URIRef(OCH_REMOVE_OBJECT_PROPERTY):
57-
remove_object_property(change_result["change"], change_data, output_mappings)
58-
elif URIRef(change_type) == URIRef(OCH_ADD_DATA_PROPERTY):
59-
add_data_property(change_result["change"], change_data, output_mappings)
60-
elif URIRef(change_type) == URIRef(OCH_REMOVE_DATA_PROPERTY):
61-
remove_data_property(change_result["change"], change_data, output_mappings)
62-
63-
logger.info("Changes propagated over the mapping rules, writing results...")
36+
new_mapping = propagate(change_data, output_mappings, review_mappings,review_mappings, ontology)
6437

65-
output_mappings.serialize(destination=args.new_mappings_path)
38+
new_mapping.serialize(destination=args.new_mappings_path)
6639
review_mappings.serialize(destination="review_mappings.ttl")
6740
yarrrml_content = yatter.inverse_translation(output_mappings)
6841
with open(args.new_mappings_path.replace(".ttl", ".yml"), "wb") as f:

src/ocp2kg/evol_kg.py

+10-81
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
from rdflib import Graph, URIRef, Variable
1+
from rdflib import Variable
2+
from .constants import *
23

3-
import yatter
4-
from constants import *
5-
from ruamel.yaml import YAML
6-
import argparse
7-
import pprint
84

95

106
# ---------------------------------------------------------------------------------------------------------------------------
117

12-
def add_class(change):
8+
def add_class(change, change_data, output_mappings):
139
"""
1410
Adds a class defined in the change KG into the output_mappings.
1511
If there is a TriplesMap that creates instances of that class, the TriplesMap is not created
@@ -48,7 +44,7 @@ def add_class(change):
4844

4945

5046
# ---------------------------------------------------------------------------------------------------------------------------
51-
def remove_class(change):
47+
def remove_class(change,change_data, output_mappings, review_mappings, ontology):
5248
"""
5349
Remove a class defined in the change KG into the output_mappings.
5450
If there is a TriplesMap that creates instances of that class, the TriplesMap and associated POM are removed.
@@ -202,7 +198,7 @@ def remove_class(change):
202198

203199
# ---------------------------------------------------------------------------------------------------------------------------------
204200

205-
def add_super_class(change):
201+
def add_super_class(change,change_data, output_mappings):
206202
"""
207203
Adds a superclass and its properties into the TriplesMap that instantiate the subclass .
208204
Args:
@@ -284,7 +280,7 @@ def add_super_class(change):
284280

285281

286282
# --------------------------------------------------------------------------------------------------------------
287-
def remove_super_class(change):
283+
def remove_super_class(change,change_data, output_mappings):
288284
"""
289285
Removes superclass and its properties from the TriplesMap that instantiate the subclass .
290286
Args:
@@ -362,7 +358,7 @@ def remove_super_class(change):
362358
output_mappings.update(remove_super_class_pom_query)
363359

364360

365-
def add_object_property(change):
361+
def add_object_property(change,change_data, output_mappings):
366362
"""
367363
Adds an object property to the TriplesMap indicated in the domain. For a change in the predicate object map the domain, property and range additions are needed.
368364
Args:
@@ -403,7 +399,7 @@ def add_object_property(change):
403399

404400

405401
# --------------------------------------------------------------------------------------------------------------------------------------------------
406-
def remove_object_property(change):
402+
def remove_object_property(change, change_data, output_mappings):
407403
"""
408404
Removes the object property indicated in the change as property from its domain. For a change in the predicate object map the domain, property and range additions are needed.
409405
Args:
@@ -448,7 +444,7 @@ def remove_object_property(change):
448444

449445

450446
# -------------------------------------------------------------------------------------------------------------------------
451-
def add_data_property(change):
447+
def add_data_property(change,change_data, output_mappings):
452448
"""
453449
Adds a data property to the TriplesMap indicated in the domain. For a change in the predicate object map the domain, property and range additions are needed.
454450
Args:
@@ -484,7 +480,7 @@ def add_data_property(change):
484480

485481

486482
# -----------------------------------------------------------------------------------------------------------------------------------
487-
def remove_data_property(change):
483+
def remove_data_property(change,change_data, output_mappings):
488484
"""
489485
Removes the data property indicated in the change as property from its domain. For a change in the predicate object map the domain, property and range additions are needed.
490486
Args:
@@ -525,70 +521,3 @@ def remove_data_property(change):
525521

526522

527523
# -------------------------------------------------------------------------------------------------------------
528-
529-
530-
def define_args():
531-
parser = argparse.ArgumentParser()
532-
parser.add_argument("-c", "--changes_kg_path", required=True, help="Change KG following the Change Ontology")
533-
parser.add_argument("-m", "--old_mapping_path", required=True, help="Old version of the mappings in RML")
534-
parser.add_argument("-o", "--ontology_path", required=False, help="New version of the ontology")
535-
parser.add_argument("-n", "--new_mappings_path", required=True, help="Output path for the generated mapping")
536-
parser.add_argument("-y", "--yarrrml", nargs=argparse.OPTIONAL, required=False, help="Mappings are also converted into YARRRML")
537-
return parser
538-
539-
540-
if __name__ == "__main__":
541-
logger.info("Starting the propagation of changes over the mapping rules")
542-
args = define_args().parse_args()
543-
change_data = Graph().parse(args.changes_kg_path, format="ttl")
544-
545-
if args.old_mapping_path.endswith(".yml") or args.old_mapping_path.endswith(".yaml"):
546-
logger.info("Loading old mapping rules from YARRRML using YATTER")
547-
output_mappings = Graph()
548-
yaml = YAML(typ='safe', pure=True)
549-
output_mappings.parse(yatter.translate(yaml.load(open(args.old_mapping_path)), RML_URI), format="ttl")
550-
else:
551-
output_mappings = Graph().parse(args.old_mapping_path, format="ttl")
552-
553-
if args.ontology_path:
554-
ontology = Graph().parse(args.ontology_path)
555-
556-
review_mappings = Graph()
557-
558-
changes_order = (OCH_ADD_CLASS, OCH_ADD_SUBCLASS, OCH_ADD_OBJECT_PROPERTY, OCH_ADD_DATA_PROPERTY, OCH_REMOVE_CLASS,
559-
OCH_REMOVE_SUBCLASS, OCH_REMOVE_OBJECT_PROPERTY, OCH_REMOVE_DATA_PROPERTY)
560-
561-
# ToDo: removing subclass action needs to be implemented
562-
for change_type in changes_order:
563-
564-
q = f' SELECT DISTINCT ?change WHERE {{ ' \
565-
f' ?change {RDF_TYPE} {URIRef(change_type)} . }}'
566-
567-
for change_result in change_data.query(q):
568-
if URIRef(change_type) == URIRef(OCH_ADD_CLASS):
569-
add_class(change_result["change"])
570-
elif URIRef(change_type) == URIRef(OCH_REMOVE_CLASS):
571-
remove_class(change_result["change"])
572-
elif URIRef(change_type) == URIRef(OCH_ADD_SUBCLASS):
573-
add_super_class(change_result["change"])
574-
elif URIRef(change_type) == URIRef(OCH_REMOVE_SUBCLASS):
575-
remove_super_class(change_result["change"])
576-
elif URIRef(change_type) == URIRef(OCH_ADD_OBJECT_PROPERTY):
577-
add_object_property(change_result["change"])
578-
elif URIRef(change_type) == URIRef(OCH_REMOVE_OBJECT_PROPERTY):
579-
remove_object_property(change_result["change"])
580-
elif URIRef(change_type) == URIRef(OCH_ADD_DATA_PROPERTY):
581-
add_data_property(change_result["change"])
582-
elif URIRef(change_type) == URIRef(OCH_REMOVE_DATA_PROPERTY):
583-
remove_data_property(change_result["change"])
584-
585-
logger.info("Changes propagated over the mapping rules, writing results...")
586-
587-
output_mappings.serialize(destination=args.new_mappings_path)
588-
review_mappings.serialize(destination="review_mappings.ttl")
589-
yarrrml_content = yatter.inverse_translation(output_mappings)
590-
with open(args.new_mappings_path.replace(".ttl", ".yml"), "wb") as f:
591-
yaml = YAML()
592-
yaml.default_flow_style = False
593-
yaml.width = 3000
594-
yaml.dump(yarrrml_content, f)

0 commit comments

Comments
 (0)