Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solving #68 and #71 #73

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1
1.1.2
3 changes: 1 addition & 2 deletions src/yatter/mapping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .import *
prefixes = {}


def add_mapping(mapping, mappings, it):
map_template = "<" + mapping + "_" + str(it) + "> a "
if mappings[mapping]:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/yatter/predicateobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/yatter/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 10 additions & 2 deletions src/yatter/termmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'\"')
Expand All @@ -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

Expand Down
76 changes: 76 additions & 0 deletions test/rml-fnml/YARRRMLTC-0050b/mapping.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
@prefix ex: <http://www.semanticweb.org/tijnd/>.
@prefix ontology: <http://www.semanticweb.org/tijnd/ontology/>.
@prefix schema: <https://schema.org/>.
@prefix idlab-fn: <http://example.com/idlab/function/>.
@prefix grel: <http://users.ugent.be/~bjdmeest/function/grel.ttl#>.
@prefix owl: <http://www.w3.org/2002/07/owl#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix rml: <http://semweb.mmlab.be/ns/rml#>.
@prefix ql: <http://semweb.mmlab.be/ns/ql#>.
@prefix d2rq: <http://www.wiwiss.fu-berlin.de/suhl/bizer/D2RQ/0.1#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix schema: <http://schema.org/>.
@prefix formats: <http://www.w3.org/ns/formats/>.
@prefix comp: <http://semweb.mmlab.be/ns/rml-compression#>.
@prefix void: <http://rdfs.org/ns/void#>.
@prefix fnml: <http://semweb.mmlab.be/ns/fnml#>.
@base <http://example.com/ns#>.


<function_director_0> 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";
];
].

<director_0> 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 <function_director_0>;
];
].


28 changes: 28 additions & 0 deletions test/rml-fnml/YARRRMLTC-0050b/mapping.yml
Original file line number Diff line number Diff line change
@@ -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)]
25 changes: 25 additions & 0 deletions test/rml-fnml/YARRRMLTC-0050b/test_yarrrmltc0050b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
__author__ = "David Chaves-Fraga"
__credits__ = ["David Chaves-Fraga"]

__license__ = "Apache-2.0"
__maintainer__ = "David Chaves-Fraga"
__email__ = "[email protected]"


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)