-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from meaningfy-ws/feature/TED-206
Feature/ted 206
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
ted_sws/mapping_suite_processor/adapters/yarrrml2rml_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import abc | ||
import pathlib | ||
import subprocess | ||
|
||
|
||
class YARRRML2RMLConverterABC(abc.ABC): | ||
""" | ||
This class is a general interface of a YARRRML to RML converter. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def convert(self, yarrrml_input_file_path: pathlib.Path, rml_output_file_path: pathlib.Path): | ||
""" | ||
This method converts a yarrrml file and writes the result to another rml file. | ||
:param yarrrml_input_file_path: | ||
:param rml_output_file_path: | ||
:return: | ||
""" | ||
|
||
|
||
class YARRRML2RMLConverter(YARRRML2RMLConverterABC): | ||
""" | ||
This class converts YARRRML to RML using an external docker container that performs conversion logic. | ||
""" | ||
def convert(self, yarrrml_input_file_path: pathlib.Path, rml_output_file_path: pathlib.Path): | ||
""" | ||
This method converts a YARRRML file and writes the result to another RML file. | ||
:param yarrrml_input_file_path: | ||
:param rml_output_file_path: | ||
:return: | ||
""" | ||
bash_script = f"(docker run --rm -i -v {yarrrml_input_file_path.parent}:/data rmlio/yarrrml-parser:latest -i /data/{yarrrml_input_file_path.name}) > {rml_output_file_path}" | ||
subprocess.run(bash_script, shell=True, stdout=subprocess.PIPE) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pytest | ||
|
||
from tests import TEST_DATA_PATH | ||
|
||
|
||
@pytest.fixture | ||
def file_system_repository_path(): | ||
return TEST_DATA_PATH / "notice_transformer" / "mapping_suite_processor_repository" | ||
|
||
@pytest.fixture | ||
def yarrrml_file_content(): | ||
return """prefixes: | ||
ex: "http://example.com/" | ||
mappings: | ||
person: | ||
sources: | ||
- ['data.json~jsonpath', '$.persons[*]'] | ||
s: http://example.com/$(firstname) | ||
po: | ||
- [a, foaf:Person] | ||
- [ex:name, $(firstname)] | ||
""" | ||
|
||
@pytest.fixture | ||
def rml_file_result(): | ||
return """@prefix rr: <http://www.w3.org/ns/r2rml#>. | ||
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. | ||
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. | ||
@prefix fnml: <http://semweb.mmlab.be/ns/fnml#>. | ||
@prefix fno: <https://w3id.org/function/ontology#>. | ||
@prefix d2rq: <http://www.wiwiss.fu-berlin.de/suhl/bizer/D2RQ/0.1#>. | ||
@prefix void: <http://rdfs.org/ns/void#>. | ||
@prefix dc: <http://purl.org/dc/terms/>. | ||
@prefix foaf: <http://xmlns.com/foaf/0.1/>. | ||
@prefix rml: <http://semweb.mmlab.be/ns/rml#>. | ||
@prefix ql: <http://semweb.mmlab.be/ns/ql#>. | ||
@prefix : <http://mapping.example.com/>. | ||
@prefix ex: <http://example.com/>. | ||
:rules_000 a void:Dataset; | ||
void:exampleResource :map_person_000. | ||
:map_person_000 rml:logicalSource :source_000. | ||
:source_000 a rml:LogicalSource; | ||
rml:source "data.json"; | ||
rml:iterator "$.persons[*]"; | ||
rml:referenceFormulation ql:JSONPath. | ||
:map_person_000 a rr:TriplesMap; | ||
rdfs:label "person". | ||
:s_000 a rr:SubjectMap. | ||
:map_person_000 rr:subjectMap :s_000. | ||
:s_000 rr:template "http://example.com/{firstname}". | ||
:pom_000 a rr:PredicateObjectMap. | ||
:map_person_000 rr:predicateObjectMap :pom_000. | ||
:pm_000 a rr:PredicateMap. | ||
:pom_000 rr:predicateMap :pm_000. | ||
:pm_000 rr:constant rdf:type. | ||
:pom_000 rr:objectMap :om_000. | ||
:om_000 a rr:ObjectMap; | ||
rr:constant "http://xmlns.com/foaf/0.1/Person"; | ||
rr:termType rr:IRI. | ||
:pom_001 a rr:PredicateObjectMap. | ||
:map_person_000 rr:predicateObjectMap :pom_001. | ||
:pm_001 a rr:PredicateMap. | ||
:pom_001 rr:predicateMap :pm_001. | ||
:pm_001 rr:constant ex:name. | ||
:pom_001 rr:objectMap :om_001. | ||
:om_001 a rr:ObjectMap; | ||
rml:reference "firstname"; | ||
rr:termType rr:Literal. | ||
""" |
20 changes: 20 additions & 0 deletions
20
tests/e2e/mapping_suite_processor/test_yarrrml2rml_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
import pathlib | ||
from tempfile import NamedTemporaryFile | ||
|
||
from ted_sws.mapping_suite_processor.adapters.yarrrml2rml_converter import YARRRML2RMLConverter | ||
|
||
|
||
def test_yarrrml_to_rml_convertor(yarrrml_file_content, rml_file_result): | ||
yarrrml_file = NamedTemporaryFile(mode="w+",suffix=".yaml") | ||
rml_file = NamedTemporaryFile(mode="w+") | ||
yarrrml_file_path = pathlib.Path(yarrrml_file.name) | ||
rml_file_path = pathlib.Path(rml_file.name) | ||
yarrrml_file.write(yarrrml_file_content) | ||
yarrrml_file.seek(0, os.SEEK_SET) | ||
yarrrml2rml_converter = YARRRML2RMLConverter() | ||
yarrrml2rml_converter.convert(yarrrml_input_file_path = yarrrml_file_path, | ||
rml_output_file_path= rml_file_path | ||
) | ||
rml_result = rml_file.read() | ||
assert rml_result == rml_file_result |