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

feat (add logical source): improve code structure #6

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .github/gradle.yml → .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ jobs:
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Build with Gradle
run: ./gradlew build
- name: Run tests with Gradle
run: ./gradlew test
28 changes: 13 additions & 15 deletions src/main/java/fi/csc/mscr/tranformation/rml/RMLGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.topbraid.shacl.vocabulary.SH;

import java.io.InputStream;
import java.util.Optional;

//import fi.vm.yti.datamodel.api.v2.dto.MSCR;

Expand All @@ -29,7 +30,7 @@ public String testMe() {
return "test";
}

private String getSourceIteratorForTargetShape(Model model, String targetShapeUri) throws Exception {
private Optional<String> getSourceIteratorForTargetShape(Model model, String targetShapeUri) {

String q = String.format("""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Expand All @@ -54,14 +55,14 @@ private String getSourceIteratorForTargetShape(Model model, String targetShapeUr

if (results.hasNext()) {
QuerySolution res = results.next();
return res.getLiteral("iterator").toString();
return Optional.of(res.getLiteral("iterator").toString());
} else {
throw new Exception("Source iterator could not be found for " + targetShapeUri);
return Optional.empty();
}

}

public Resource addLogicalSource(String logicalSourceURI, Model m) throws Exception {
public Resource addLogicalSource(String logicalSourceURI, Model m, String targetShapeUri) throws Exception {

/*
You can now get the iterator source for a specific shape by querying along the lines:
Expand All @@ -75,24 +76,21 @@ public Resource addLogicalSource(String logicalSourceURI, Model m) throws Except

String iterator = "";
Resource logicalSource = m.createResource(logicalSourceURI);

logicalSource.addProperty(RDF.type, m.createResource(nsRML + "BaseSource"));
Resource referenceFormulation = m.createResource(nsQL + "JSONPath");
logicalSource.addProperty(m.createProperty(nsRML + "referenceFormulation"), referenceFormulation);
logicalSource.addProperty(m.createProperty(nsRML + "source"), m.createLiteral("data/person.json"));

try {
iterator = getSourceIteratorForTargetShape(m, "iterator:https://shacl-play.sparna.fr/shapes/Person");
iterator = this.getSourceIteratorForTargetShape(m, targetShapeUri).orElseThrow(Exception::new);
} catch(Exception e) {
System.out.println(e.toString());
System.out.println("Iterator could not be found");
throw e;
}

logicalSource.addProperty(m.createProperty(nsRML + "iterator"), iterator);

/*logicalSource.addProperty(RDF.type, m.createResource(nsRML + "BaseSource"));
Resource referenceFormulation = m.createResource(nsQL + "JSONPath");
logicalSource.addProperty(m.createProperty(nsRML + "referenceFormulation"), referenceFormulation);
logicalSource.addProperty(m.createProperty(nsRML + "source"), m.createLiteral("data/person.json"));

Resource sourceProperty = inputModel.getResource(sourcePropertyURI);
Literal iteratorPath = sourceProperty.getProperty(MSCR.instancePath).getLiteral();
logicalSource.addProperty(m.createProperty(nsRML + "iterator"), iteratorPath);*/

return logicalSource;
}
}
71 changes: 39 additions & 32 deletions src/test/java/fi/csc/mscr/tranformation/rml/RMLGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,50 @@
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.riot.RDFDataMgr;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;

class RMLGeneratorTest {

Model loadPersonTestData() {
Model model = ModelFactory.createDefaultModel();

String[] inputFileNames = {"src/test/resources/data/person_json2shacl/crosswalk_content.ttl",
"src/test/resources/data/person_json2shacl/crosswalk_metadata.ttl",
"src/test/resources/data/person_json2shacl/source_metadata.ttl",
"src/test/resources/data/person_json2shacl/source_content.ttl",
"src/test/resources/data/person_json2shacl/target_metadata.ttl",
"src/test/resources/data/person_json2shacl/target_content.ttl"
};

for (String inputFileName : inputFileNames) {
model.add(RDFDataMgr.loadModel(inputFileName));
}

return model;
}

@Test
void test() {
RMLGenerator r = new RMLGenerator();
assertEquals("test", r.testMe());
}

@Test
void testAddLogicalSource() throws Exception {
Model model = loadPersonTestData();
RMLGenerator r = new RMLGenerator();
Resource logicalSource = r.addLogicalSource("http://example.com/1", model);

assertEquals("http://example.com/1", logicalSource.getURI());
String[] personJsonTestData = {"src/test/resources/data/person_json2shacl/crosswalk_content.ttl",
"src/test/resources/data/person_json2shacl/crosswalk_metadata.ttl",
"src/test/resources/data/person_json2shacl/source_metadata.ttl",
"src/test/resources/data/person_json2shacl/source_content.ttl",
"src/test/resources/data/person_json2shacl/target_metadata.ttl",
"src/test/resources/data/person_json2shacl/target_content.ttl"
};

Model loadPersonTestData(List<String> inputFileNames) {
Model model = ModelFactory.createDefaultModel();

inputFileNames.forEach(inputFileName -> model.add(RDFDataMgr.loadModel(inputFileName)));

return model;
}

@Test
void test() {
RMLGenerator r = new RMLGenerator();
assertEquals("test", r.testMe());
}

@Test
void testAddLogicalSourceForPersonJSON() throws Exception {
Model model = loadPersonTestData(Arrays.asList(this.personJsonTestData));
RMLGenerator r = new RMLGenerator();
Resource logicalSource = r.addLogicalSource("http://example.com/1", model, "iterator:https://shacl-play.sparna.fr/shapes/Person");

StmtIterator props = logicalSource.listProperties();
props.forEach(
System.out::println
);

assertEquals("http://example.com/1", logicalSource.getURI());
assertEquals("http://semweb.mmlab.be/ns/rml#BaseSource", logicalSource.getProperty(model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")).getObject().toString());
assertEquals("$", logicalSource.getProperty(model.createProperty("http://semweb.mmlab.be/ns/rml#iterator")).getObject().asLiteral().toString());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<http://www.w3.org/2004/02/skos/core#exactMatch> ;
<http://uri.suomi.fi/datamodel/ns/mscr#processing>
[ <http://uri.suomi.fi/datamodel/ns/mscr#id>
"http://uri.suomi.fi/datamodel/ns/mscr#replace2Func" ;
"http://uri.suomi.fi/datamodel/ns/mscr#template" ; # TODO: adapt in MSCR
<http://uri.suomi.fi/datamodel/ns/mscr#processingParams>
[ a <http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>
Expand All @@ -64,9 +64,9 @@
] ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>
[ <http://uri.suomi.fi/datamodel/ns/mscr#key>
"pattern" ;
<http://uri.suomi.fi/datamodel/ns/mscr#fno/param/value> ; # TODO: adapt in MSCR
<http://uri.suomi.fi/datamodel/ns/mscr#value>
"https://example.com/{value_0}{value_1}"
"https://example.com/{firstName}{lastName}" # TODO: adapt in MSCR
]
]
] ;
Expand Down