From ba4dbe7661673a05d2ce2771600ccc48892ffd53 Mon Sep 17 00:00:00 2001 From: dachafra Date: Tue, 19 Dec 2023 13:17:33 +0100 Subject: [PATCH] solving #68 and #71 --- VERSION | 2 +- src/yatter/mapping.py | 3 +- src/yatter/predicateobject.py | 4 +- src/yatter/subject.py | 2 +- src/yatter/termmap.py | 12 ++- test/rml-fnml/YARRRMLTC-0050b/mapping.ttl | 76 +++++++++++++++++++ test/rml-fnml/YARRRMLTC-0050b/mapping.yml | 28 +++++++ .../YARRRMLTC-0050b/test_yarrrmltc0050b.py | 25 ++++++ 8 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 test/rml-fnml/YARRRMLTC-0050b/mapping.ttl create mode 100644 test/rml-fnml/YARRRMLTC-0050b/mapping.yml create mode 100644 test/rml-fnml/YARRRMLTC-0050b/test_yarrrmltc0050b.py diff --git a/VERSION b/VERSION index 524cb55..45a1b3f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.1 +1.1.2 diff --git a/src/yatter/mapping.py b/src/yatter/mapping.py index 1edb0a9..4a0fa71 100644 --- a/src/yatter/mapping.py +++ b/src/yatter/mapping.py @@ -1,7 +1,6 @@ from .import * prefixes = {} - def add_mapping(mapping, mappings, it): map_template = "<" + mapping + "_" + str(it) + "> a " if mappings[mapping]: @@ -99,7 +98,7 @@ def add_inverse_prefix(rdf_mapping): def get_non_asserted_mappings(yarrrml_data, mapping_format): - mappings = dict.fromkeys(list(yarrrml_data.get(YARRRML_MAPPINGS).keys())) + mappings = dict.fromkeys(list(yarrrml_data.get(YARRRML_MAPPINGS).keys())) for mapping in yarrrml_data.get(YARRRML_MAPPINGS): keys = yarrrml_data.get(YARRRML_MAPPINGS).get(mapping).keys() for key in keys: diff --git a/src/yatter/predicateobject.py b/src/yatter/predicateobject.py index d8fa5e8..c2d9358 100644 --- a/src/yatter/predicateobject.py +++ b/src/yatter/predicateobject.py @@ -150,7 +150,7 @@ def add_predicate_object(data, mapping, predicate_object, mapping_format=RML_URI execution = True template += generate_rml_termmap(R2RML_PREDICATE, R2RML_PREDICATE_CLASS, pm_value,"\t\t\t") if execution: - template = template.replace(R2RML_CONSTANT + " " + pm_value, RML_EXECUTION + " <" + pm_value + ">") + template = template.replace(R2RML_CONSTANT + " \"" + pm_value + "\"", RML_EXECUTION + " <" + pm_value + ">") if YARRRML_TARGETS in pm: template = template[0:-3] + "\t" + RML_LOGICAL_TARGET + " <" + pm[YARRRML_TARGETS] + ">\n\t\t];\n" @@ -225,7 +225,7 @@ def add_predicate_object(data, mapping, predicate_object, mapping_format=RML_URI object_value, "\t\t\t", mapping_format) elif YARRRML_FUNCTION in om: template += generate_rml_termmap(R2RML_OBJECT, R2RML_OBJECT_CLASS, om[YARRRML_FUNCTION], "\t\t\t", mapping_format) - template = template.replace(R2RML_CONSTANT+" "+om[YARRRML_FUNCTION], RML_EXECUTION + " <" + om.get( + template = template.replace(R2RML_CONSTANT+" \""+om[YARRRML_FUNCTION]+ "\"", RML_EXECUTION + " <" + om.get( YARRRML_FUNCTION) + ">") else: template += generate_rml_termmap(R2RML_OBJECT, R2RML_OBJECT_CLASS, diff --git a/src/yatter/subject.py b/src/yatter/subject.py index d77691d..3b28037 100644 --- a/src/yatter/subject.py +++ b/src/yatter/subject.py @@ -48,7 +48,7 @@ def add_subject(data, mapping, mapping_format): subject_termmap = generate_rml_termmap(R2RML_SUBJECT, R2RML_SUBJECT_CLASS, subject_value, "\t\t", mapping_format) if YARRRML_FUNCTION in individual_subject: - subject_termmap = subject_termmap.replace(R2RML_CONSTANT+" "+subject_value, RML_EXECUTION + " <" + individual_subject.get( + subject_termmap = subject_termmap.replace(R2RML_CONSTANT+" \""+subject_value + "\"", RML_EXECUTION + " <" + individual_subject.get( YARRRML_FUNCTION) + ">") if YARRRML_TARGETS in individual_subject: diff --git a/src/yatter/termmap.py b/src/yatter/termmap.py index ee2334d..8693fe1 100644 --- a/src/yatter/termmap.py +++ b/src/yatter/termmap.py @@ -19,11 +19,16 @@ def get_termmap_type(text, mapping_format): ## Generates a TermMap (subject, predicate, object) based on the property, class and the text def generate_rml_termmap(rml_property, rml_class, text, indentation, mapping_format=RML_URI): - template = indentation[0:-1] + rml_property + " [\n"+indentation+"a " + rml_class + ";\n" + indentation + from .mapping import prefixes + template = indentation[0:-1] + rml_property + " [\n" + indentation + "a " + rml_class + ";\n" + indentation term_map = get_termmap_type(text, mapping_format) if term_map == R2RML_TEMPLATE: text = generate_rml_template(text) text = text.replace('"', r'\"') + if ":" in text: + text_prefix_split = text.split(":") + if text_prefix_split[0] in prefixes: + text = prefixes[text_prefix_split[0]] + text_prefix_split[1] elif term_map == RML_REFERENCE or term_map == R2RML_COLUMN: text = text.replace("$(", "").replace(")", "") text = text.replace('"', r'\"') @@ -41,7 +46,10 @@ def generate_rml_termmap(rml_property, rml_class, text, indentation, mapping_for if text.startswith("http"): template += term_map + " <" + text + ">;\n" + indentation[0:-1] + "];\n" else: - template += term_map + " " + text + ";\n"+indentation[0:-1]+"];\n" + if ":" in text or "<" in text: + template += term_map + " " + text + ";\n"+indentation[0:-1]+"];\n" + else: + template += term_map + " \"" + text + "\";\n" + indentation[0:-1] + "];\n" return template diff --git a/test/rml-fnml/YARRRMLTC-0050b/mapping.ttl b/test/rml-fnml/YARRRMLTC-0050b/mapping.ttl new file mode 100644 index 0000000..17504bd --- /dev/null +++ b/test/rml-fnml/YARRRMLTC-0050b/mapping.ttl @@ -0,0 +1,76 @@ +@prefix ex: . +@prefix ontology: . +@prefix schema: . +@prefix idlab-fn: . +@prefix grel: . +@prefix owl: . +@prefix rdf: . +@prefix rdfs: . +@prefix xsd: . +@prefix rr: . +@prefix rml: . +@prefix ql: . +@prefix d2rq: . +@prefix foaf: . +@prefix schema: . +@prefix formats: . +@prefix comp: . +@prefix void: . +@prefix fnml: . +@base . + + + a fnml:Execution; + fnml:function grel:string_split ; + fnml:input + [ + a fnml:Input; + fnml:parameter grel:p_string_sep; + fnml:valueMap [ + a fnml:ValueMap; + rr:constant ", "; + ]; + ], + [ + a fnml:Input; + fnml:parameter grel:valueParameter; + fnml:valueMap [ + a fnml:ValueMap; + rml:reference "directors"; + ]; + ]. + + a rr:TriplesMap; + + rml:logicalSource [ + a rml:LogicalSource; + rml:source "imdb_video_game_rating_modified.csv"; + rml:referenceFormulation ql:CSV; + + ]; + rr:subjectMap [ + a rr:SubjectMap; + rr:template "http://www.semanticweb.org/tijnd/Person/{title}"; + ]; + rr:predicateObjectMap [ + rr:predicateMap [ + a rr:PredicateMap; + rr:constant rdf:type; + ]; + rr:objectMap [ + a rr:ObjectMap; + rr:constant schema:Person; + ]; + ]; + rr:predicateObjectMap [ + rr:predicateMap [ + a rr:PredicateMap; + rr:constant schema:name; + ]; + rr:objectMap [ + a rr:ObjectMap; + fnml:execution ; + ]; + ]. + + diff --git a/test/rml-fnml/YARRRMLTC-0050b/mapping.yml b/test/rml-fnml/YARRRMLTC-0050b/mapping.yml new file mode 100644 index 0000000..95db5f6 --- /dev/null +++ b/test/rml-fnml/YARRRMLTC-0050b/mapping.yml @@ -0,0 +1,28 @@ +prefixes: + ex: "http://www.semanticweb.org/tijnd/" + ontology: "http://www.semanticweb.org/tijnd/ontology/" + schema: "https://schema.org/" + idlab-fn: "http://example.com/idlab/function/" + grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#" + owl: "http://www.w3.org/2002/07/owl#" + rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#" + rdfs: "http://www.w3.org/2000/01/rdf-schema#" + xsd: "http://www.w3.org/2001/XMLSchema#" +sources: + imdb: + access: imdb_video_game_rating_modified.csv + referenceFormulation: csv + +mappings: + director: + sources: + - imdb + s: ex:Person/$(title) #Not really the correct subject, only used to simply test it + po: + - [a, schema:Person] + - p: schema:name + o: + function: grel:string_split + parameters: + - [grel:p_string_sep, ", "] + - [grel:valueParameter, $(directors)] \ No newline at end of file diff --git a/test/rml-fnml/YARRRMLTC-0050b/test_yarrrmltc0050b.py b/test/rml-fnml/YARRRMLTC-0050b/test_yarrrmltc0050b.py new file mode 100644 index 0000000..903ecd5 --- /dev/null +++ b/test/rml-fnml/YARRRMLTC-0050b/test_yarrrmltc0050b.py @@ -0,0 +1,25 @@ +__author__ = "David Chaves-Fraga" +__credits__ = ["David Chaves-Fraga"] + +__license__ = "Apache-2.0" +__maintainer__ = "David Chaves-Fraga" +__email__ = "david.chaves@upm.es" + + +import os +from ruamel.yaml import YAML +import yatter +from rdflib.graph import Graph +from rdflib import compare + + +def test_yarrrmltc0050b(): + expected_mapping = Graph() + expected_mapping.parse(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.ttl'), format="ttl") + + translated_mapping = Graph() + yaml = YAML(typ='safe', pure=True) + mapping_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.yml') + translated_mapping.parse(data=yatter.translate(yaml.load(open(mapping_path))), format="ttl") + + assert compare.isomorphic(expected_mapping, translated_mapping) \ No newline at end of file