Skip to content

Commit

Permalink
Added test for the parameter query.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRoeder committed Aug 7, 2023
1 parent 3466193 commit fbf85b2
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void main(String[] args) {
Resource experimentResource = ResourceFactory.createResource(experimentIri);

// 1. get parameters from SPARQL endpoint
Model parameterModel = queryParameterModel(endpoint, metaGraph, moduleInsResource);
Model parameterModel = queryParameterModel(endpoint, metaGraph, moduleInstance);
List<Resource> sourceFiles = RdfHelper.getObjectResources(parameterModel, moduleInsResource,
TransformVocab.input);
if (sourceFiles.size() == 0) {
Expand Down Expand Up @@ -186,22 +186,24 @@ protected static void addFile(Resource sourceFile, Model parameterModel, String
*
* @param metaDataEndpoint the SPARQL endpoint hosting the meta data graph
* @param metaDataGraph the graph containing the meta data
* @param moduleInstance the resource representing this instance of the ENEXA
* module
* @param moduleInstance the IRI of the ENEXA module instance
* @return an RDF model containing the queried sub graph
* @throws IllegalStateException in case an error occurs while querying the
* model
*/
protected static Model queryParameterModel(String metaDataEndpoint, String metaDataGraph, Resource moduleInstance) {
protected static Model queryParameterModel(String metaDataEndpoint, String metaDataGraph, String moduleInstance) {
ParameterizedSparqlString query = null;
try (QueryExecutionFactory queryExecFactory = new QueryExecutionFactoryHttp(metaDataEndpoint, metaDataGraph)) {
ParameterizedSparqlString query = SparqlQueryUtils.loadParameterizedQuery(null, "getParameters.query",
StandardCharsets.UTF_8);
query.setParam("instanceIri", moduleInstance);
query = SparqlQueryUtils.loadParameterizedQuery(
EnexaTransformator.class.getClassLoader(), "getParameters.query", StandardCharsets.UTF_8);
query.setIri("?moduleInstance", moduleInstance);
query.setIri("?graph", metaDataGraph);
QueryExecution qe = queryExecFactory.createQueryExecution(query.asQuery());
return qe.execConstruct();
} catch (Exception e) {
LOGGER.error("Error while requesting parameter values.", e);
throw new IllegalStateException("Error while requesting parameter values.", e);
String msg = "Error while requesting parameter values. query: " + ((query == null) ? "no query generated" : query.toString());
LOGGER.error(msg, e);
throw new IllegalStateException(msg, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ CONSTRUCT {
?input <http://w3id.org/dice-research/enexa/ontology#location> ?fileLocation .
?moduleInstance <http://w3id.org/dice-research/enexa/module/transform/parameter/outputMediaType> ?outputMediaType .
} WHERE {
?moduleInstance <http://w3id.org/dice-research/enexa/module/transform/parameter/input> ?input .
?input <http://w3id.org/dice-research/enexa/ontology#location> ?fileLocation .
?moduleInstance <http://w3id.org/dice-research/enexa/module/transform/parameter/outputMediaType> ?outputMediaType .
GRAPH ?graph {
?moduleInstance <http://w3id.org/dice-research/enexa/module/transform/parameter/input> ?input .
OPTIONAL { ?input <http://w3id.org/dice-research/enexa/ontology#location> ?fileLocation . }
?moduleInstance <http://w3id.org/dice-research/enexa/module/transform/parameter/outputMediaType> ?outputMediaType .
}
}
42 changes: 42 additions & 0 deletions src/test/java/org/dice_research/enexa/transform/QueryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.dice_research.enexa.transform;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.riot.Lang;
import org.dice_research.sparql.SparqlQueryUtils;
import org.dice_research.sparql.test.ConstructQueryTest;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class QueryTest extends ConstructQueryTest {

public static final String META_DATA_GRAPH_IRI = "http://example.org/meta-graph";
public static final String SECOND_GRAPH_IRI = "http://example.org/other-graph";

public QueryTest(String storeContentResource, Lang storeContentLang, String expectedResultResource,
Lang expectedResultLang, String query) {
super(META_DATA_GRAPH_IRI, SECOND_GRAPH_IRI, storeContentResource, storeContentLang, expectedResultResource, expectedResultLang, query);
}

@Parameters
public static Collection<Object[]> data() {
final String MODULE_INSTANCE_VARIABLE = "?moduleInstance";
final String MODULE_INSTANCE_IRI = "http://example.org/module/123";

ParameterizedSparqlString query = SparqlQueryUtils.loadParameterizedQuery(QueryTest.class.getClassLoader(),
"org/dice_research/enexa/transform/getParameters.query", StandardCharsets.UTF_8);
query.setIri(MODULE_INSTANCE_VARIABLE, MODULE_INSTANCE_IRI);
query.setIri("?graph", META_DATA_GRAPH_IRI);

List<Object[]> testConfigs = new ArrayList<Object[]>();
testConfigs.add(new Object[] { "org/dice_research/enexa/transform/testDataset.ttl", Lang.TURTLE,
"org/dice_research/enexa/transform/expectedParameters.ttl", Lang.TURTLE, query.toString() });
return testConfigs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix enexa: <http://w3id.org/dice-research/enexa/ontology#> .
@prefix hobbit: <http://w3id.org/hobbit/vocab#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sd: <http://www.w3.org/ns/sparql-service-description#> .
@prefix xsd: <http://www.w3.org/2000/10/XMLSchema#> .

<http://example.org/module/123> <http://w3id.org/dice-research/enexa/module/transform/parameter/input> <http://example.org/experiment1/data/kg/dump.ttl> ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/input> <http://example.org/experiment1/data/kg/dump2.json> ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/outputMediaType> <https://www.iana.org/assignments/media-types/application/n-triples> .

<http://example.org/experiment1/data/kg/dump.ttl> enexa:location "enexa-dir://application1/experiment1/kg/dump.ttl" .

<http://example.org/experiment1/data/kg/dump2.json> enexa:location "enexa-dir://application1/experiment1/kg/dump2.json" .

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix enexa: <http://w3id.org/dice-research/enexa/ontology#> .
@prefix hobbit: <http://w3id.org/hobbit/vocab#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sd: <http://www.w3.org/ns/sparql-service-description#> .
@prefix xsd: <http://www.w3.org/2000/10/XMLSchema#> .

<http://example.org/module/123> a enexa:ModuleInstance, prov:Activity ;
enexa:experiment <http://example.org/experiment1> ;
enexa:instanceOf <http://w3id.org/dice-research/enexa/module/transform/0.0.1> ;
enexa:dnsName "diceTransformer1" ;
enexa:containerId "the ID of the container" ;
prov:startedAtTime "2023-04-01T12:35:58Z"^^xsd:dateTime ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/input> <http://example.org/experiment1/data/kg/dump.ttl> ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/input> <http://example.org/experiment1/data/kg/dump2.json> ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/outputMediaType> <https://www.iana.org/assignments/media-types/application/n-triples> .

<http://example.org/experiment1> a enexa:Experiment ;
enexa:sharedDirectory "enexa-dir://application1/experiment1" ;
enexa:metaDataEndpoint <http://enexa-endpoint/sparql> ;
enexa:metaDataGraph <http://example.org/experiment1/graph> .

<http://example.org/experiment1/modules/fileDownload_1> a prov:Activity ;
enexa:experiment <http://example.org/experiment1> ;
prov:startedAtTime "2023-04-01T12:05:21Z"^^xsd:dateTime ;
prov:endedAtTime "2023-04-01T12:31:29Z"^^xsd:dateTime ;
prov:used <http://some-server.com/data-directory/dump.ttl> .

<http://example.org/experiment1/data/kg/dump.ttl> a prov:Entity ;
enexa:experiment <http://example.org/experiment1> ;
enexa:location "enexa-dir://application1/experiment1/kg/dump.ttl" ;
prov:wasGeneratedBy <http://example.org/experiment1/modules/fileDownload_1> ;
dcat:mediaType <https://www.iana.org/assignments/media-types/text/turtle> ;
dcat:byteSize "5120"^^xsd:nonNegativeInteger .

<http://example.org/experiment1/data/kg/dump2.json> a prov:Entity ;
enexa:experiment <http://example.org/experiment1> ;
enexa:location "enexa-dir://application1/experiment1/kg/dump2.json" ;
dcat:mediaType <https://www.iana.org/assignments/media-types/application/ld+json> .

<http://example.org/module/456> a enexa:ModuleInstance, prov:Activity ;
enexa:experiment <http://example.org/experiment1> ;
enexa:instanceOf <http://w3id.org/dice-research/enexa/module/transform/0.0.1> ;
enexa:dnsName "diceTransformer2" ;
enexa:containerId "the ID of the container" ;
prov:startedAtTime "2023-04-01T12:35:58Z"^^xsd:dateTime ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/input> <http://example.org/experiment1/data/kg/dump3.ttl> ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/input> <http://example.org/experiment1/data/kg/dump4.json> ;
<http://w3id.org/dice-research/enexa/module/transform/parameter/outputMediaType> <https://www.iana.org/assignments/media-types/application/n-triples> .

0 comments on commit fbf85b2

Please sign in to comment.