Skip to content

Commit 5afde1e

Browse files
committed
Simplify SPARQL queries
1 parent bebabcf commit 5afde1e

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed

src/main/java/org/commonwl/view/cwl/CWLService.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
275275
// Base workflow details
276276
String label = FilenameUtils.getName(url);
277277
String doc = null;
278-
ResultSet labelAndDoc = rdfService.getLabelAndDoc(gitPath, url);
278+
ResultSet labelAndDoc = rdfService.getLabelAndDoc(url);
279279
if (labelAndDoc.hasNext()) {
280280
QuerySolution labelAndDocSoln = labelAndDoc.nextSolution();
281281
if (labelAndDocSoln.contains("label")) {
@@ -288,7 +288,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
288288

289289
// Inputs
290290
Map<String, CWLElement> wfInputs = new HashMap<>();
291-
ResultSet inputs = rdfService.getInputs(gitPath, url);
291+
ResultSet inputs = rdfService.getInputs(url);
292292
while (inputs.hasNext()) {
293293
QuerySolution input = inputs.nextSolution();
294294
String inputName = rdfService.stepNameFromURI(gitPath, input.get("name").toString());
@@ -321,7 +321,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
321321

322322
// Outputs
323323
Map<String, CWLElement> wfOutputs = new HashMap<>();
324-
ResultSet outputs = rdfService.getOutputs(gitPath, url);
324+
ResultSet outputs = rdfService.getOutputs(url);
325325
while (outputs.hasNext()) {
326326
QuerySolution output = outputs.nextSolution();
327327
CWLElement wfOutput = new CWLElement();
@@ -360,7 +360,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
360360

361361
// Steps
362362
Map<String, CWLStep> wfSteps = new HashMap<>();
363-
ResultSet steps = rdfService.getSteps(gitPath, url);
363+
ResultSet steps = rdfService.getSteps(url);
364364
while (steps.hasNext()) {
365365
QuerySolution step = steps.nextSolution();
366366
String uri = rdfService.stepNameFromURI(gitPath, step.get("step").toString());
@@ -381,9 +381,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
381381
// Add new step
382382
CWLStep wfStep = new CWLStep();
383383

384-
IRI wfStepUri = iriFactory.construct(step.get("wf").asResource().getURI());
385-
IRI workflowPath = wfStepUri.resolve("./");
386-
384+
IRI workflowPath = iriFactory.construct(url).resolve("./");
387385
IRI runPath = iriFactory.construct(step.get("run").asResource().getURI());
388386
wfStep.setRun(workflowPath.relativize(runPath).toString());
389387
wfStep.setRunType(rdfService.strToRuntype(step.get("runtype").toString()));
@@ -414,7 +412,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
414412
}
415413

416414
// Docker link
417-
ResultSet dockerResult = rdfService.getDockerLink(gitPath, url);
415+
ResultSet dockerResult = rdfService.getDockerLink(url);
418416
String dockerLink = null;
419417
if (dockerResult.hasNext()) {
420418
QuerySolution docker = dockerResult.nextSolution();

src/main/java/org/commonwl/view/cwl/RDFService.java

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,21 @@ public boolean ontPropertyExists(String ontUri) {
119119

120120
/**
121121
* Get the label and doc strings for a workflow resource
122-
* @param path The path within the Git repository to the workflow
123122
* @param workflowURI The URI of the workflow
124123
* @return Result set with label and doc strings
125124
*/
126-
public ResultSet getLabelAndDoc(String path, String workflowURI) {
125+
public ResultSet getLabelAndDoc(String workflowURI) {
127126
ParameterizedSparqlString labelQuery = new ParameterizedSparqlString();
128127
labelQuery.setCommandText(queryCtx +
129128
"SELECT ?label ?doc\n" +
130129
"WHERE {\n" +
131-
" GRAPH ?graphName {" +
130+
" GRAPH ?wf {" +
132131
" ?wf rdf:type ?type .\n" +
133132
" OPTIONAL { ?wf sld:label|rdfs:label ?label }\n" +
134133
" OPTIONAL { ?wf sld:doc|rdfs:comment ?doc }\n" +
135-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
136134
" }" +
137135
"}");
138-
labelQuery.setLiteral("wfFilter", path + "$");
139-
labelQuery.setIri("graphName", workflowURI);
136+
labelQuery.setIri("wf", workflowURI);
140137
return runQuery(labelQuery);
141138
}
142139

@@ -166,16 +163,15 @@ public String getOntLabel(String ontologyURI) {
166163

167164
/**
168165
* Get the inputs for the workflow in the model
169-
* @param path The path within the Git repository to the workflow
170166
* @param workflowURI URI of the workflow
171167
* @return The result set of inputs
172168
*/
173-
public ResultSet getInputs(String path, String workflowURI) {
169+
public ResultSet getInputs(String workflowURI) {
174170
ParameterizedSparqlString inputsQuery = new ParameterizedSparqlString();
175171
inputsQuery.setCommandText(queryCtx +
176172
"SELECT ?name ?type ?items ?null ?format ?label ?doc\n" +
177173
"WHERE {\n" +
178-
" GRAPH ?graphName {" +
174+
" GRAPH ?wf {" +
179175
" ?wf rdf:type cwl:Workflow .\n" +
180176
" ?wf cwl:inputs ?name .\n" +
181177
" OPTIONAL {\n" +
@@ -196,26 +192,23 @@ public ResultSet getInputs(String path, String workflowURI) {
196192
" OPTIONAL { ?name cwl:format ?format }\n" +
197193
" OPTIONAL { ?name sld:label|rdfs:label ?label }\n" +
198194
" OPTIONAL { ?name sld:doc|rdfs:comment ?doc }" +
199-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
200195
" }" +
201196
"}");
202-
inputsQuery.setLiteral("wfFilter", path + "$");
203-
inputsQuery.setIri("graphName", workflowURI);
197+
inputsQuery.setIri("wf", workflowURI);
204198
return runQuery(inputsQuery);
205199
}
206200

207201
/**
208202
* Get the outputs for the workflow in the model
209-
* @param path The path within the Git repository to the workflow
210203
* @param workflowURI URI of the workflow
211204
* @return The result set of outputs
212205
*/
213-
public ResultSet getOutputs(String path, String workflowURI) {
206+
public ResultSet getOutputs(String workflowURI) {
214207
ParameterizedSparqlString outputsQuery = new ParameterizedSparqlString();
215208
outputsQuery.setCommandText(queryCtx +
216209
"SELECT ?name ?type ?items ?null ?format ?label ?doc\n" +
217210
"WHERE {\n" +
218-
" GRAPH ?graphName {" +
211+
" GRAPH ?wf {" +
219212
" ?wf rdf:type cwl:Workflow .\n" +
220213
" ?wf cwl:outputs ?name .\n" +
221214
" OPTIONAL {\n" +
@@ -236,26 +229,23 @@ public ResultSet getOutputs(String path, String workflowURI) {
236229
" OPTIONAL { ?name cwl:format ?format }\n" +
237230
" OPTIONAL { ?name sld:label|rdfs:label ?label }\n" +
238231
" OPTIONAL { ?name sld:doc|rdfs:comment ?doc }" +
239-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
240232
" }" +
241233
"}");
242-
outputsQuery.setLiteral("wfFilter", path + "$");
243-
outputsQuery.setIri("graphName", workflowURI);
234+
outputsQuery.setIri("wf", workflowURI);
244235
return runQuery(outputsQuery);
245236
}
246237

247238
/**
248239
* Get the steps for the workflow in the model
249-
* @param path The path within the Git repository to the workflow
250240
* @param workflowURI URI of the workflow
251241
* @return The result set of steps
252242
*/
253-
public ResultSet getSteps(String path, String workflowURI) {
243+
public ResultSet getSteps(String workflowURI) {
254244
ParameterizedSparqlString stepQuery = new ParameterizedSparqlString();
255245
stepQuery.setCommandText(queryCtx +
256-
"SELECT ?wf ?step ?run ?runtype ?label ?doc ?stepinput ?default ?src\n" +
246+
"SELECT ?step ?run ?runtype ?label ?doc ?stepinput ?default ?src\n" +
257247
"WHERE {\n" +
258-
" GRAPH ?graphName {" +
248+
" GRAPH ?wf {" +
259249
" ?wf Workflow:steps ?step .\n" +
260250
" ?step cwl:run ?run .\n" +
261251
" ?run rdf:type ?runtype .\n" +
@@ -265,11 +255,9 @@ public ResultSet getSteps(String path, String workflowURI) {
265255
" }\n" +
266256
" OPTIONAL { ?run sld:label|rdfs:label ?label }\n" +
267257
" OPTIONAL { ?run sld:doc|rdfs:comment ?doc }\n" +
268-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
269258
" }" +
270259
"}");
271-
stepQuery.setLiteral("wfFilter", path + "$");
272-
stepQuery.setIri("graphName", workflowURI);
260+
stepQuery.setIri("wf", workflowURI);
273261
return runQuery(stepQuery);
274262
}
275263

@@ -278,20 +266,18 @@ public ResultSet getSteps(String path, String workflowURI) {
278266
* @param workflowURI URI of the workflow
279267
* @return The result set of step links
280268
*/
281-
public ResultSet getStepLinks(String path, String workflowURI) {
269+
public ResultSet getStepLinks(String workflowURI) {
282270
ParameterizedSparqlString linkQuery = new ParameterizedSparqlString();
283271
linkQuery.setCommandText(queryCtx +
284-
"SELECT ?wf ?src ?dest ?default\n" +
272+
"SELECT ?src ?dest ?default\n" +
285273
"WHERE {\n" +
286-
" GRAPH ?graphName {" +
274+
" GRAPH ?wf {" +
287275
" ?wf Workflow:steps ?step .\n" +
288276
" ?step cwl:in ?dest .\n" +
289277
" { ?dest cwl:source ?src } UNION { ?dest cwl:default ?default }\n" +
290-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
291278
" }" +
292279
"}");
293-
linkQuery.setLiteral("wfFilter", path + "$");
294-
linkQuery.setIri("graphName", workflowURI);
280+
linkQuery.setIri("wf", workflowURI);
295281
return runQuery(linkQuery);
296282
}
297283

@@ -300,44 +286,39 @@ public ResultSet getStepLinks(String path, String workflowURI) {
300286
* @param workflowURI URI of the workflow
301287
* @return The result set of steps
302288
*/
303-
public ResultSet getOutputLinks(String path, String workflowURI) {
289+
public ResultSet getOutputLinks(String workflowURI) {
304290
ParameterizedSparqlString linkQuery = new ParameterizedSparqlString();
305291
linkQuery.setCommandText(queryCtx +
306292
"SELECT ?src ?dest\n" +
307293
"WHERE {\n" +
308-
" GRAPH ?graphName {" +
294+
" GRAPH ?wf {" +
309295
" ?wf rdf:type cwl:Workflow .\n" +
310296
" ?wf cwl:outputs ?dest .\n" +
311297
" ?dest cwl:outputSource ?src\n" +
312-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
313298
" }" +
314299
"}");
315-
linkQuery.setLiteral("wfFilter", path + "$");
316-
linkQuery.setIri("graphName", workflowURI);
300+
linkQuery.setIri("wf", workflowURI);
317301
return runQuery(linkQuery);
318302
}
319303

320304
/**
321305
* Gets the docker requirement and pull link for a workflow
322-
* @param path The path within the Git repository to the workflow
323306
* @param workflowURI URI of the workflow
324307
* @return Result set of docker hint and pull link
325308
*/
326-
public ResultSet getDockerLink(String path, String workflowURI) {
309+
public ResultSet getDockerLink(String workflowURI) {
327310
ParameterizedSparqlString dockerQuery = new ParameterizedSparqlString();
328311
dockerQuery.setCommandText(queryCtx +
329312
"SELECT ?docker ?pull\n" +
330313
"WHERE {\n" +
331-
" GRAPH ?graphName {" +
314+
" GRAPH ?wf {" +
332315
" ?wf rdf:type cwl:Workflow .\n" +
333316
" { ?wf cwl:requirements ?docker } UNION { ?wf cwl:hints ?docker} .\n" +
334317
" ?docker rdf:type cwl:DockerRequirement\n" +
335318
" OPTIONAL { ?docker DockerRequirement:dockerPull ?pull }\n" +
336-
" FILTER(regex(str(?wf), ?wfFilter, \"i\" ))" +
337319
" }" +
338320
"}");
339-
dockerQuery.setLiteral("wfFilter", path + "$");
340-
dockerQuery.setIri("graphName", workflowURI);
321+
dockerQuery.setIri("wf", workflowURI);
341322
return runQuery(dockerQuery);
342323
}
343324

src/main/java/org/commonwl/view/graphviz/RDFDotWriter.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919

2020
package org.commonwl.view.graphviz;
2121

22+
import org.apache.jena.iri.IRI;
23+
import org.apache.jena.iri.IRIFactory;
2224
import org.apache.jena.query.QuerySolution;
2325
import org.apache.jena.query.ResultSet;
2426
import org.commonwl.view.cwl.CWLProcess;
2527
import org.commonwl.view.cwl.RDFService;
2628

2729
import java.io.IOException;
2830
import java.io.Writer;
29-
import java.nio.file.Path;
30-
import java.nio.file.Paths;
3131
import java.util.ArrayList;
3232
import java.util.HashSet;
3333
import java.util.List;
@@ -38,6 +38,8 @@
3838
*/
3939
public class RDFDotWriter extends DotWriter {
4040

41+
private final IRIFactory iriFactory = IRIFactory.iriImplementation();
42+
4143
private RDFService rdfService;
4244
private String gitPath;
4345

@@ -75,7 +77,7 @@ private void writeInputs(String workflowUri) throws IOException {
7577
writeLine(" label = \"Workflow Inputs\";");
7678

7779
// Write each of the inputs as a node
78-
ResultSet inputs = rdfService.getInputs(gitPath, workflowUri);
80+
ResultSet inputs = rdfService.getInputs(workflowUri);
7981
while (inputs.hasNext()) {
8082
QuerySolution input = inputs.nextSolution();
8183
writeInputOutput(input);
@@ -99,7 +101,7 @@ private void writeOutputs(String workflowUri) throws IOException {
99101
writeLine(" label = \"Workflow Outputs\";");
100102

101103
// Write each of the outputs as a node
102-
ResultSet outputs = rdfService.getOutputs(gitPath, workflowUri);
104+
ResultSet outputs = rdfService.getOutputs(workflowUri);
103105
while (outputs.hasNext()) {
104106
QuerySolution output = outputs.nextSolution();
105107
writeInputOutput(output);
@@ -117,7 +119,7 @@ private void writeOutputs(String workflowUri) throws IOException {
117119
*/
118120
private void writeSteps(String workflowUri, boolean subworkflow) throws IOException {
119121

120-
ResultSet steps = rdfService.getSteps(gitPath, workflowUri);
122+
ResultSet steps = rdfService.getSteps(workflowUri);
121123
Set<String> addedSteps = new HashSet<>();
122124
while (steps.hasNext()) {
123125
QuerySolution step = steps.nextSolution();
@@ -153,7 +155,7 @@ private void writeSteps(String workflowUri, boolean subworkflow) throws IOExcept
153155
*/
154156
private void writeStepLinks(String workflowUri) throws IOException {
155157
// Write links between steps
156-
ResultSet stepLinks = rdfService.getStepLinks(gitPath, workflowUri);
158+
ResultSet stepLinks = rdfService.getStepLinks(workflowUri);
157159
int defaultCount = 1;
158160
while (stepLinks.hasNext()) {
159161
QuerySolution stepLink = stepLinks.nextSolution();
@@ -171,8 +173,8 @@ private void writeStepLinks(String workflowUri) throws IOException {
171173
if (stepLink.get("default").isLiteral()) {
172174
label = rdfService.formatDefault(stepLink.get("default").toString());
173175
} else if (stepLink.get("default").isURIResource()) {
174-
Path workflowPath = Paths.get(stepLink.get("wf").toString()).getParent();
175-
Path resourcePath = Paths.get(stepLink.get("default").toString());
176+
IRI workflowPath = iriFactory.construct(workflowUri).resolve("./");
177+
IRI resourcePath = iriFactory.construct(stepLink.get("default").asResource().getURI());
176178
label = workflowPath.relativize(resourcePath).toString();
177179
} else {
178180
label = "[Complex Object]";
@@ -190,7 +192,7 @@ private void writeStepLinks(String workflowUri) throws IOException {
190192
}
191193

192194
// Write links between steps and outputs
193-
ResultSet outputLinks = rdfService.getOutputLinks(gitPath, workflowUri);
195+
ResultSet outputLinks = rdfService.getOutputLinks(workflowUri);
194196
while (outputLinks.hasNext()) {
195197
QuerySolution outputLink = outputLinks.nextSolution();
196198
String sourceID = nodeIDFromUri(outputLink.get("src").toString());

0 commit comments

Comments
 (0)