|
1 |
| -from rdflib import Graph, URIRef, Variable |
| 1 | +from rdflib import Variable |
| 2 | +from .constants import * |
2 | 3 |
|
3 |
| -import yatter |
4 |
| -from constants import * |
5 |
| -from ruamel.yaml import YAML |
6 |
| -import argparse |
7 |
| -import pprint |
8 | 4 |
|
9 | 5 |
|
10 | 6 | # ---------------------------------------------------------------------------------------------------------------------------
|
11 | 7 |
|
12 |
| -def add_class(change): |
| 8 | +def add_class(change, change_data, output_mappings): |
13 | 9 | """
|
14 | 10 | Adds a class defined in the change KG into the output_mappings.
|
15 | 11 | If there is a TriplesMap that creates instances of that class, the TriplesMap is not created
|
@@ -48,7 +44,7 @@ def add_class(change):
|
48 | 44 |
|
49 | 45 |
|
50 | 46 | # ---------------------------------------------------------------------------------------------------------------------------
|
51 |
| -def remove_class(change): |
| 47 | +def remove_class(change,change_data, output_mappings, review_mappings, ontology): |
52 | 48 | """
|
53 | 49 | Remove a class defined in the change KG into the output_mappings.
|
54 | 50 | 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):
|
202 | 198 |
|
203 | 199 | # ---------------------------------------------------------------------------------------------------------------------------------
|
204 | 200 |
|
205 |
| -def add_super_class(change): |
| 201 | +def add_super_class(change,change_data, output_mappings): |
206 | 202 | """
|
207 | 203 | Adds a superclass and its properties into the TriplesMap that instantiate the subclass .
|
208 | 204 | Args:
|
@@ -284,7 +280,7 @@ def add_super_class(change):
|
284 | 280 |
|
285 | 281 |
|
286 | 282 | # --------------------------------------------------------------------------------------------------------------
|
287 |
| -def remove_super_class(change): |
| 283 | +def remove_super_class(change,change_data, output_mappings): |
288 | 284 | """
|
289 | 285 | Removes superclass and its properties from the TriplesMap that instantiate the subclass .
|
290 | 286 | Args:
|
@@ -362,7 +358,7 @@ def remove_super_class(change):
|
362 | 358 | output_mappings.update(remove_super_class_pom_query)
|
363 | 359 |
|
364 | 360 |
|
365 |
| -def add_object_property(change): |
| 361 | +def add_object_property(change,change_data, output_mappings): |
366 | 362 | """
|
367 | 363 | 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.
|
368 | 364 | Args:
|
@@ -403,7 +399,7 @@ def add_object_property(change):
|
403 | 399 |
|
404 | 400 |
|
405 | 401 | # --------------------------------------------------------------------------------------------------------------------------------------------------
|
406 |
| -def remove_object_property(change): |
| 402 | +def remove_object_property(change, change_data, output_mappings): |
407 | 403 | """
|
408 | 404 | 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.
|
409 | 405 | Args:
|
@@ -448,7 +444,7 @@ def remove_object_property(change):
|
448 | 444 |
|
449 | 445 |
|
450 | 446 | # -------------------------------------------------------------------------------------------------------------------------
|
451 |
| -def add_data_property(change): |
| 447 | +def add_data_property(change,change_data, output_mappings): |
452 | 448 | """
|
453 | 449 | 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.
|
454 | 450 | Args:
|
@@ -484,7 +480,7 @@ def add_data_property(change):
|
484 | 480 |
|
485 | 481 |
|
486 | 482 | # -----------------------------------------------------------------------------------------------------------------------------------
|
487 |
| -def remove_data_property(change): |
| 483 | +def remove_data_property(change,change_data, output_mappings): |
488 | 484 | """
|
489 | 485 | 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.
|
490 | 486 | Args:
|
@@ -525,70 +521,3 @@ def remove_data_property(change):
|
525 | 521 |
|
526 | 522 |
|
527 | 523 | # -------------------------------------------------------------------------------------------------------------
|
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