Skip to content

Commit

Permalink
adding support for constant literal values in functions (solving #71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dachafra committed Dec 19, 2023
1 parent da6e9c0 commit 1aa7656
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 4 deletions.
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 @@ -218,7 +218,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
5 changes: 4 additions & 1 deletion src/yatter/termmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def generate_rml_termmap(rml_property, rml_class, text, identation, mapping_form
if text.startswith("http"):
template += term_map + " <" + text + ">;\n" + identation[0:-1] + "];\n"
else:
template += term_map + " " + text + ";\n"+identation[0:-1]+"];\n"
if ":" in text or "<" in text:
template += term_map + " " + text + ";\n"+identation[0:-1]+"];\n"
else:
template += term_map + " \"" + text + "\";\n" + identation[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 "ex: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)

0 comments on commit 1aa7656

Please sign in to comment.