Skip to content

Commit 388414d

Browse files
committed
Get label of ontology property and include on page
1 parent c3af261 commit 388414d

File tree

4 files changed

+105
-49
lines changed

4 files changed

+105
-49
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@
9494
<version>2.9.0.pr4</version>
9595
</dependency>
9696
</dependencies>
97+
<dependency>
98+
<groupId>org.apache.jena</groupId>
99+
<artifactId>jena-core</artifactId>
100+
<version>3.3.0</version>
101+
</dependency>
97102

98-
<build>
103+
104+
<build>
99105
<plugins>
100106
<plugin>
101107
<groupId>org.springframework.boot</groupId>

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.fasterxml.jackson.databind.node.TextNode;
2727
import org.apache.commons.io.FileUtils;
2828
import org.apache.commons.io.FilenameUtils;
29+
import org.apache.jena.ontology.OntModelSpec;
2930
import org.apache.jena.query.QuerySolution;
3031
import org.apache.jena.query.ResultSet;
3132
import org.apache.jena.rdf.model.Model;
@@ -187,9 +188,6 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
187188
String packedWorkflowID) throws CWLValidationException {
188189

189190
// Get RDF representation from cwltool
190-
String graphName = String.format("github.com/%s/%s/%s/%s", githubInfo.getOwner(),
191-
githubInfo.getRepoName(), latestCommit, githubInfo.getPath());
192-
193191
String url = String.format("https://cdn.rawgit.com/%s/%s/%s/%s", githubInfo.getOwner(),
194192
githubInfo.getRepoName(), latestCommit, githubInfo.getPath());
195193
if (packedWorkflowID != null) {
@@ -199,21 +197,21 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
199197
url += packedWorkflowID;
200198
}
201199

202-
if (!rdfService.graphExists(graphName)) {
200+
if (!rdfService.graphExists(url)) {
203201
String rdf = cwlTool.getRDF(url);
204202

205203
// Create a workflow model from RDF representation
206204
Model model = ModelFactory.createDefaultModel();
207205
model.read(new ByteArrayInputStream(rdf.getBytes()), null, "TURTLE");
208206

209207
// Store the model
210-
rdfService.storeModel(graphName, model);
208+
rdfService.storeModel(url, model);
211209
}
212210

213211
// Base workflow details
214212
String label = FilenameUtils.getName(githubInfo.getPath());
215213
String doc = null;
216-
ResultSet labelAndDoc = rdfService.getLabelAndDoc(graphName, url);
214+
ResultSet labelAndDoc = rdfService.getLabelAndDoc(url);
217215
if (labelAndDoc.hasNext()) {
218216
QuerySolution labelAndDocSoln = labelAndDoc.nextSolution();
219217
if (labelAndDocSoln.contains("label")) {
@@ -226,7 +224,7 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
226224

227225
// Inputs
228226
Map<String, CWLElement> wfInputs = new HashMap<>();
229-
ResultSet inputs = rdfService.getInputs(graphName, url);
227+
ResultSet inputs = rdfService.getInputs(url);
230228
while (inputs.hasNext()) {
231229
QuerySolution input = inputs.nextSolution();
232230
String inputName = rdfService.stepNameFromURI(url, input.get("name").toString());
@@ -269,7 +267,16 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
269267
}
270268

271269
if (input.contains("format")) {
272-
wfInput.setFormat(input.get("format").toString());
270+
String format = input.get("format").toString();
271+
wfInput.setFormat(format);
272+
273+
if (!rdfService.ontPropertyExists(format)) {
274+
Model ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
275+
ontModel.read(format, null, "RDF/XML");
276+
rdfService.addToOntologies(ontModel);
277+
}
278+
String formatLabel = rdfService.getOntLabel(format);
279+
wfInput.setType(wfInput.getType() + " (" + formatLabel + " format)");
273280
}
274281
if (input.contains("label")) {
275282
wfInput.setLabel(input.get("label").toString());
@@ -282,7 +289,7 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
282289

283290
// Outputs
284291
Map<String, CWLElement> wfOutputs = new HashMap<>();
285-
ResultSet outputs = rdfService.getOutputs(graphName, url);
292+
ResultSet outputs = rdfService.getOutputs(url);
286293
while (outputs.hasNext()) {
287294
QuerySolution output = outputs.nextSolution();
288295
CWLElement wfOutput = new CWLElement();
@@ -330,7 +337,16 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
330337
output.get("src").toString()));
331338
}
332339
if (output.contains("format")) {
333-
wfOutput.setFormat(output.get("format").toString());
340+
String format = output.get("format").toString();
341+
wfOutput.setFormat(format);
342+
343+
if (!rdfService.ontPropertyExists(format)) {
344+
Model ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
345+
ontModel.read(format, null, "RDF/XML");
346+
rdfService.addToOntologies(ontModel);
347+
}
348+
String formatLabel = rdfService.getOntLabel(format);
349+
wfOutput.setType(wfOutput.getType() + " (" + formatLabel + " format)");
334350
}
335351
if (output.contains("label")) {
336352
wfOutput.setLabel(output.get("label").toString());
@@ -344,7 +360,7 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
344360

345361
// Steps
346362
Map<String, CWLStep> wfSteps = new HashMap<>();
347-
ResultSet steps = rdfService.getSteps(graphName, url);
363+
ResultSet steps = rdfService.getSteps(url);
348364
while (steps.hasNext()) {
349365
QuerySolution step = steps.nextSolution();
350366
String uri = rdfService.stepNameFromURI(url, step.get("step").toString());
@@ -396,7 +412,7 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
396412
}
397413

398414
// Docker link
399-
ResultSet dockerResult = rdfService.getDockerLink(graphName, url);
415+
ResultSet dockerResult = rdfService.getDockerLink(url);
400416
String dockerLink = null;
401417
if (dockerResult.hasNext()) {
402418
QuerySolution docker = dockerResult.nextSolution();
@@ -413,7 +429,7 @@ public Workflow parseWorkflowWithCwltool(GithubDetails githubInfo,
413429

414430
// Generate DOT graph
415431
StringWriter graphWriter = new StringWriter();
416-
RDFDotWriter RDFDotWriter = new RDFDotWriter(graphWriter, rdfService, graphName);
432+
RDFDotWriter RDFDotWriter = new RDFDotWriter(graphWriter, rdfService);
417433
try {
418434
RDFDotWriter.writeGraph(url);
419435
workflowModel.setVisualisationDot(graphWriter.toString());

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

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public RDFService(@Value("${sparql.endpoint}") String rdfService) {
5050
this.rdfService = rdfService;
5151
}
5252

53+
/**
54+
* Add to ontologies in the triple store
55+
* @param model The model to be stored
56+
*/
57+
public void addToOntologies(Model model) {
58+
DatasetAccessor accessor = DatasetAccessorFactory.createHTTP(rdfService);
59+
accessor.putModel("ontologies", model);
60+
}
61+
5362
/**
5463
* Store a model with triples in the triple store
5564
* @param graphName The name of the graph to store the model in
@@ -75,39 +84,77 @@ public boolean graphExists(String graphName) {
7584
}
7685
}
7786

87+
/**
88+
* Check if a property of the ontology exists within the triple store
89+
* @param ontUri The URI of the property
90+
* @return Whether the graph exists
91+
*/
92+
public boolean ontPropertyExists(String ontUri) {
93+
ParameterizedSparqlString graphQuery = new ParameterizedSparqlString();
94+
graphQuery.setCommandText("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
95+
"ASK WHERE { GRAPH ?graphName { ?ont rdfs:label ?label } }");
96+
graphQuery.setIri("ont", ontUri);
97+
graphQuery.setIri("graphName", rdfService + "ontologies");
98+
Query query = QueryFactory.create(graphQuery.toString());
99+
try (QueryExecution qexec = QueryExecutionFactory.createServiceRequest(rdfService, query)) {
100+
return qexec.execAsk();
101+
}
102+
}
103+
78104
/**
79105
* Get the label and doc strings for a workflow resource
80-
* @param graphName The graph containing the model
81106
* @param workflowURI The URI of the workflow
82107
* @return Result set with label and doc strings
83108
*/
84-
public ResultSet getLabelAndDoc(String graphName, String workflowURI) {
109+
public ResultSet getLabelAndDoc(String workflowURI) {
85110
ParameterizedSparqlString labelQuery = new ParameterizedSparqlString();
86111
labelQuery.setCommandText(queryCtx +
87112
"SELECT ?label ?doc\n" +
88113
"WHERE {\n" +
89-
" GRAPH ?graphName {" +
114+
" GRAPH ?wf {" +
90115
" OPTIONAL { ?wf sld:label|rdfs:label ?label }\n" +
91116
" OPTIONAL { ?wf sld:doc|rdfs:comment ?doc }\n" +
92117
" }" +
93118
"}");
94119
labelQuery.setIri("wf", workflowURI);
95-
labelQuery.setIri("graphName", rdfService + graphName);
96120
return runQuery(labelQuery);
97121
}
98122

123+
/**
124+
* Get the label for an ontology URL
125+
* TODO: can be merged with getLabelAndDoc when cwltool namespace bug is resolved
126+
* @param ontologyURI The format URI for the ontology
127+
* @return Result set with label and doc strings
128+
*/
129+
public String getOntLabel(String ontologyURI) {
130+
ParameterizedSparqlString labelQuery = new ParameterizedSparqlString();
131+
labelQuery.setCommandText("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
132+
"SELECT ?label\n" +
133+
"WHERE {\n" +
134+
" GRAPH ?graphName {\n" +
135+
" ?ont rdfs:label ?label\n" +
136+
" }\n" +
137+
"}\n");
138+
labelQuery.setIri("ont", ontologyURI);
139+
labelQuery.setIri("graphName", rdfService + "ontologies");
140+
ResultSet result = runQuery(labelQuery);
141+
if (result.hasNext()) {
142+
return result.next().get("label").toString();
143+
}
144+
return null;
145+
}
146+
99147
/**
100148
* Get the inputs for the workflow in the model
101-
* @param graphName The graph containing the model
102149
* @param workflowURI URI of the workflow
103150
* @return The result set of inputs
104151
*/
105-
public ResultSet getInputs(String graphName, String workflowURI) {
152+
public ResultSet getInputs(String workflowURI) {
106153
ParameterizedSparqlString inputsQuery = new ParameterizedSparqlString();
107154
inputsQuery.setCommandText(queryCtx +
108155
"SELECT ?name ?type ?format ?label ?doc\n" +
109156
"WHERE {\n" +
110-
" GRAPH ?graphName {" +
157+
" GRAPH ?wf {" +
111158
" ?wf rdf:type cwl:Workflow .\n" +
112159
" ?wf cwl:inputs ?name .\n" +
113160
" OPTIONAL { ?name sld:type ?type }\n" +
@@ -117,22 +164,20 @@ public ResultSet getInputs(String graphName, String workflowURI) {
117164
" }" +
118165
"}");
119166
inputsQuery.setIri("wf", workflowURI);
120-
inputsQuery.setIri("graphName", rdfService + graphName);
121167
return runQuery(inputsQuery);
122168
}
123169

124170
/**
125171
* Get the outputs for the workflow in the model
126-
* @param graphName The graph containing the model
127172
* @param workflowURI URI of the workflow
128173
* @return The result set of outputs
129174
*/
130-
public ResultSet getOutputs(String graphName, String workflowURI) {
175+
public ResultSet getOutputs(String workflowURI) {
131176
ParameterizedSparqlString outputsQuery = new ParameterizedSparqlString();
132177
outputsQuery.setCommandText(queryCtx +
133178
"SELECT ?name ?type ?format ?label ?doc\n" +
134179
"WHERE {\n" +
135-
" GRAPH ?graphName {" +
180+
" GRAPH ?wf {" +
136181
" ?wf rdf:type cwl:Workflow .\n" +
137182
" ?wf cwl:outputs ?name .\n" +
138183
" OPTIONAL { ?name sld:type ?type }\n" +
@@ -142,17 +187,15 @@ public ResultSet getOutputs(String graphName, String workflowURI) {
142187
" }" +
143188
"}");
144189
outputsQuery.setIri("wf", workflowURI);
145-
outputsQuery.setIri("graphName", rdfService + graphName);
146190
return runQuery(outputsQuery);
147191
}
148192

149193
/**
150194
* Get the steps for the workflow in the model
151-
* @param graphName The graph containing the model
152195
* @param workflowURI URI of the workflow
153196
* @return The result set of steps
154197
*/
155-
public ResultSet getSteps(String graphName, String workflowURI) {
198+
public ResultSet getSteps(String workflowURI) {
156199
ParameterizedSparqlString stepQuery = new ParameterizedSparqlString();
157200
stepQuery.setCommandText(queryCtx +
158201
"SELECT ?step ?run ?runtype ?label ?doc ?stepinput ?default ?src\n" +
@@ -170,74 +213,67 @@ public ResultSet getSteps(String graphName, String workflowURI) {
170213
" }" +
171214
"}");
172215
stepQuery.setIri("wf", workflowURI);
173-
stepQuery.setIri("graphName", rdfService + graphName);
174216
return runQuery(stepQuery);
175217
}
176218

177219
/**
178220
* Get links between steps for the workflow in the model
179-
* @param graphName The graph containing the model
180221
* @param workflowURI URI of the workflow
181222
* @return The result set of step links
182223
*/
183-
public ResultSet getStepLinks(String graphName, String workflowURI) {
224+
public ResultSet getStepLinks(String workflowURI) {
184225
ParameterizedSparqlString linkQuery = new ParameterizedSparqlString();
185226
linkQuery.setCommandText(queryCtx +
186227
"SELECT ?src ?dest ?default\n" +
187228
"WHERE {\n" +
188-
" GRAPH ?graphName {" +
229+
" GRAPH ?wf {" +
189230
" ?wf Workflow:steps ?step .\n" +
190231
" ?step cwl:in ?dest .\n" +
191232
" { ?dest cwl:source ?src } UNION { ?dest cwl:default ?default }\n" +
192233
" }" +
193234
"}");
194235
linkQuery.setIri("wf", workflowURI);
195-
linkQuery.setIri("graphName", rdfService + graphName);
196236
return runQuery(linkQuery);
197237
}
198238

199239
/**
200240
* Get links between steps and outputs for the workflow in the model
201-
* @param graphName The graph containing the model
202241
* @param workflowURI URI of the workflow
203242
* @return The result set of steps
204243
*/
205-
public ResultSet getOutputLinks(String graphName, String workflowURI) {
244+
public ResultSet getOutputLinks(String workflowURI) {
206245
ParameterizedSparqlString linkQuery = new ParameterizedSparqlString();
207246
linkQuery.setCommandText(queryCtx +
208247
"SELECT ?src ?dest\n" +
209248
"WHERE {\n" +
210-
" GRAPH ?graphName {" +
249+
" GRAPH ?wf {" +
211250
" ?wf rdf:type cwl:Workflow .\n" +
212251
" ?wf cwl:outputs ?dest .\n" +
213252
" ?dest cwl:outputSource ?src\n" +
214253
" }" +
215254
"}");
216255
linkQuery.setIri("wf", workflowURI);
217-
linkQuery.setIri("graphName", rdfService + graphName);
218256
return runQuery(linkQuery);
219257
}
220258

221259
/**
222260
* Gets the docker requirement and pull link for a workflow
223-
* @param graphName The graph containing the model
224261
* @param workflowURI URI of the workflow
225262
* @return Result set of docker hint and pull link
226263
*/
227-
public ResultSet getDockerLink(String graphName, String workflowURI) {
264+
public ResultSet getDockerLink(String workflowURI) {
228265
ParameterizedSparqlString dockerQuery = new ParameterizedSparqlString();
229266
dockerQuery.setCommandText(queryCtx +
230267
"SELECT ?docker ?pull\n" +
231268
"WHERE {\n" +
232-
" GRAPH ?graphName {" +
269+
" GRAPH ?wf {" +
233270
" ?wf rdf:type cwl:Workflow .\n" +
234271
" { ?wf cwl:requirements ?docker } UNION { ?wf cwl:hints ?docker} .\n" +
235272
" ?docker rdf:type cwl:DockerRequirement\n" +
236273
" OPTIONAL { ?docker DockerRequirement:dockerPull ?pull }\n" +
237274
" }" +
238275
"}");
239276
dockerQuery.setIri("wf", workflowURI);
240-
dockerQuery.setIri("graphName", rdfService + graphName);
241277
return runQuery(dockerQuery);
242278
}
243279

0 commit comments

Comments
 (0)