-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTrafficLightReports.py
585 lines (561 loc) · 21.1 KB
/
TrafficLightReports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/usr/bin/python
"""
Module defining report templates for Traffic light reports of checklist evaluation.
This module primarily defines data that are used by other modules to create traffic light reports
Report definition structure:
report-defn = { 'report': template-item } // May add more later
template-item = sequence | query-template // Bindings to date are fed down into template-item processing
sequence = [ template-item, ... ]
query-template = { 'query': sparql-query [None], # Probe query for input RDF data
'max': integer [1], # Max numberof query matches to use
'output': python-format-string [None], # Output format string for query results
'report': template-item [None], # Sub-report used with query results
'alt': python-format-string [None], # Alternate string if querry not matched
'altreport': template-item [None], # Alternate sub-report for query not matched
'sep': python-format-string [None], # Separator between query result outputs
}
"""
__author__ = "Graham Klyne ([email protected])"
__copyright__ = "Copyright 2011-2013, University of Oxford"
__license__ = "MIT (http://opensource.org/licenses/MIT)"
import os, os.path
import sys
import logging
import json
import rdflib
from rocommand.ro_namespaces import RDF, DCTERMS, RO, AO, ORE
from rocommand.ro_prefixes import make_sparql_prefixes
sparql_prefixes = make_sparql_prefixes()
log = logging.getLogger(__name__)
def LIT(l): return rdflib.Literal(l)
def REF(u): return rdflib.URIRef(u)
# Report structure used to get evaluation result URI from result graph
# Query idiom adapted from http://lists.w3.org/Archives/Public/public-sparql-dev/2006JulSep/0000.html
#
# Report a inserts one of the following status URIs:
# http://purl.org/minim/minim#fullySatifies
# http://purl.org/minim/minim#nominallySatifies
# http://purl.org/minim/minim#minimallySatifies
# http://purl.org/minim/minim#potentiallySatisfies (actually: did not satisfy)
#
# Assumed incoming bindings:
# result result node being reproted
# minim URI of minim model for which evaluation has been performed
#
# @@TODO:
# Consider refactoring this to follow the report/altreport pattern used for
# generating result label text
#
EvalTargetResultUri = (
{ 'report':
{ 'output': "http://purl.org/minim/minim#fullySatisfies"
, 'query': """ASK { ?result <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
, 'altreport':
{ 'output': "http://purl.org/minim/minim#nominallySatisfies"
, 'query': """ASK { ?result <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
, 'altreport':
{ 'output': "http://purl.org/minim/minim#minimallySatisfies"
, 'query': """ASK { ?result <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
, 'alt': "http://purl.org/minim/minim#potentiallySatisfies"
}
}
}
})
# Report structure used to get evaluation result label from result graph
#
# Report a inserts one of the following strings:
# "fully satisfies" (no item failures)
# "nominally satisfies" (failed MAY items only)
# "minimally satisfies" (failed SHOULD items)
# "does not satisfy" (failed MUST items)
#
# Assumed incoming bindings:
# result result node being reproted
# minim URI of minim model for which evaluation has been performed
#
EvalTargetResultLabel = (
{ 'report':
{ 'output': "fully satisfies"
, 'query': """ASK { ?result <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
, 'altreport':
{ 'output': "nominally satisfies"
, 'query': """ASK { ?result <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
, 'altreport':
{ 'output': "minimally satisfies"
, 'query': """ASK { ?result <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
, 'alt': "does not satisfy"
}
}
}
})
# Report structure used to get evaluation result class list from result graph
# The intent is that these class labels can be used in conjunction with CSS-directed
# styling when generating a web page display of a checklist evaluation result.
#
# Report a inserts one of the following strings:
# ["pass"] (no item failures)
# ["fail" "may"] (failed MAY items only)
# ["fail", "should"] (failed SHOULD items)
# ["fail", "must"] (failed MUST items)
#
# Assumed incoming bindings:
# result result node being reproted
# minim URI of minim model for which evaluation has been performed
#
EvalTargetResultClass = (
{ 'report':
{ 'output': '"pass"'
, 'query': """ASK { ?result <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
, 'altreport':
{ 'output': '"fail", "may"'
, 'query': """ASK { ?result <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
, 'altreport':
{ 'output': '"fail", "should"'
, 'query': """ASK { ?result <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
, 'alt': '"fail", "must"'
}
}
}
})
# Report inserts result of checklist item evaluation item as JSON
#
# Example of output:
#
# { "itemuri": "URI of checklist item"
# , "itemlabel": "Result label text generated from checklist item"
# , "itemlevel": "http://purl.org/minim/minim#hasShouldRequirement"
# , "itemsatisfied": false
# , "itemclass": ["fail", "should"]
# }
#
# Assumed incoming bindings:
# result result node being reproted
# rouri URI of RO being evaluated
# modeluri URI of Minim model defining the evaluated checklist
# itemuri URI or Minim model checklist item whose result is reported
# itemlevel URI of satisfaction level asspociated with this item:
# minim:hasMustRequirement, minim:hasShouldRequirement or minim:hasMayRequirement
#
EvalItemJson = (
{ 'report':
[
{ 'output':
'''\n { "itemuri": "%(itemuri)s"'''+
'''\n , "itemlabel": "%(itemlabel_esc)s"'''+
'''\n , "itemlevel": "%(itemlevel)s" '''
, 'alt': '''\n *** no match for message/label ***'''
, 'query': sparql_prefixes+
"""
SELECT * WHERE
{
?s minim:tryRequirement ?itemuri ;
minim:tryMessage ?itemlabel .
}"""
},
{ 'output':
'''\n , "itemsatisfied": true'''
, 'alt':
'''\n , "itemsatisfied": false'''
, 'query': sparql_prefixes+
"""
SELECT * WHERE
{
?result minim:satisfied [ minim:tryRequirement ?itemuri ]
}
"""
},
{ 'query': sparql_prefixes+"""ASK { ?result minim:satisfied [ minim:tryRequirement ?itemuri ] }"""
, 'output': '''\n , "itemclass": ["pass"]'''
, 'altreport':
{ 'query': sparql_prefixes+"""ASK { ?result minim:missingMay [ minim:tryRequirement ?itemuri ] }"""
, 'output': '''\n , "itemclass": ["fail", "may"]'''
, 'altreport':
{ 'query': sparql_prefixes+"""ASK { ?result minim:missingShould [ minim:tryRequirement ?itemuri ] }"""
, 'output': '''\n , "itemclass": ["fail", "should"]'''
, 'alt': '''\n , "itemclass": ["fail", "must"]'''
}
}
},
{ 'output':
'''\n }'''
},
]
})
# Report generates JSON for traffic-light display
#
# The resulting JSON may be used by some other software (e.g. Javascript) to genrate an
# HTML display of a checklist evaluation result.
#
# Outer query is per evaluated RO, extracting evaluation parameters and overall result
# Inner query is repeated for each checklist item and results (for each RO)
#
# Example output:
#
# { "rouri": "file:///usr/workspace/wf4ever-ro-catalogue/v0.1/simple-requirements/"
# , "roid": "simple-requirements"
# , "checklisturi": "file:///runnable-wf-trafficlight/checklist.rdf#Runnable_model"
# , "checklisttarget": "file:///usr/workspace/wf4ever-ro-catalogue/v0.1/simple-requirements/"
# , "checklisttargetid": "simple-requirements"
# , "checklisttargetlabel": "simple-requirements"
# , "checklistpurpose": "Runnable"
# , "evalresult": "http://purl.org/minim/minim#minimallySatisfies"
# , "evalresultlabel": "minimally satisfies"
# , "evalresultclass": ["fail", "should"]
# , "checklistitems":
# [
# ... (EvalItemJson output, repeated)
# ]
# }
#
# Optional incoming bindings:
# rouri URI of RO evaluated
# modeluri URI of Minim model defining the evaluated checklist
#
EvalChecklistJson = (
{ 'report':
[ { 'output':
'''\n{ "rouri": "%(rouri)s"'''+
'''\n, "roid": "%(roid)s"'''+
'''\n, "title": "%(title_esc)s"'''+
'''\n, "description": "%(description_esc)s"'''+
'''\n, "checklisturi": "%(modeluri)s"'''+
'''\n, "checklistpurpose": "%(purpose)s"'''+
'''\n, "checklisttarget": "%(target)s"'''+
# '''\n, "checklisttargetid": "%(targetid)s"'''+
# '''\n, "checklisttargetlabel": "%(targetlabel_esc)s"'''+
''''''
, 'query':
sparql_prefixes+
"""
SELECT ?result ?rouri ?roid ?title ?description ?modeluri ?target ?targetlabel ?purpose
WHERE
{
?result minim:testedRO ?rouri ;
minim:testedModel ?modeluri ;
minim:testedTarget ?target ;
minim:testedPurpose ?purpose .
?rouri
dcterms:identifier ?roid ;
dcterms:title ?title ;
dcterms:description ?description .
OPTIONAL { ?target rdfs:label ?targetlabel . }
}
LIMIT 1
"""
# OPTIONAL { ?target rdfs:label ?targetlabel }
# OPTIONAL { FILTER( !bound(?targetlabel) ) BIND(str(?target) as ?targetlabel) }
# {
# ?target rdfs:label ?targetlabel
# }
# UNION
# {
# OPTIONAL { ?target rdfs:label ?targethaslabel }
# FILTER(!bound(?targethaslabel))
# BIND(str(?target) as ?targetlabel)
# }
, 'report':
[ { 'output':
'''\n, "checklisttargetid": "%(targetid)s"'''
, 'query':
sparql_prefixes+
"""
SELECT ?targetid WHERE
{
?target
dcterms:identifier ?targetid
}
"""
}
, { 'output':
'''\n, "checklisttargetlabel": "%(targetlabel_esc)s"'''
, 'query':
sparql_prefixes+
"""
SELECT ?targetlabel WHERE
{
?target
rdfs:label ?targetlabel
}
"""
}
, { 'output':
'''\n, "evalresult": "'''
}
, { 'report': EvalTargetResultUri
}
, { 'output':
'''"'''
}
, { 'output':
'''\n, "evalresultlabel": "'''
}
, { 'report': EvalTargetResultLabel
}
, { 'output':
'''"'''
}
, { 'output':
'''\n, "evalresultclass": ['''
}
, { 'report': EvalTargetResultClass
}
, { 'output':
''']'''
}
, { 'output':
'''\n, "checklistitems":\n ['''
}
, { 'report': EvalItemJson
, 'sep': ","
, 'query': sparql_prefixes+
"""
SELECT ?itemuri ?itemlevel ?modeluri WHERE
{ ?modeluri ?itemlevel ?itemuri .
?itemuri a minim:Requirement ;
minim:seq ?itemseq .
}
ORDER BY ?itemseq
"""
}
, { 'output':
'''\n ]\n}'''
}
]
}
]
})
# Report inserts result of checklist item evaluation item as HTML
#
# Example of output:
#
# <tr>
# <td></td>
# <td class="trafficlight small pass must"><div/></td>
# <td>Workflow is present</td>
# </tr>
#
# Assumed incoming bindings:
# result Checklist minim:Result node being displayed
# modeluri URI of Minim model defining the evaluated checklist
# itemuri URI or Minim model checklist item whose result is reported
# itemlevel URI of satisfaction level asspociated with this item:
# minim:hasMustRequirement, minim:hasShouldRequirement or minim:hasMayRequirement
#
EvalItemHtml = (
{ 'report':
[
{ 'output':
'''\n <tr class="sub_result">'''+
'''\n <td></td>'''+
''''''
}
, { 'query': sparql_prefixes+"""ASK { ?result minim:satisfied [ minim:tryRequirement ?itemuri ] }"""
, 'output': '''\n <td class="trafficlight small pass"><div/></td>'''
, 'altreport':
{ 'query': sparql_prefixes+"""ASK { ?result minim:missingMay [ minim:tryRequirement ?itemuri ] }"""
, 'output': '''\n <td class="trafficlight small fail may"><div/></td>'''
, 'altreport':
{ 'query': sparql_prefixes+"""ASK { ?result minim:missingShould [ minim:tryRequirement ?itemuri ] }"""
, 'output': '''\n <td class="trafficlight small fail should"><div/></td>'''
, 'alt': '''\n <td class="trafficlight small fail must"><div/></td>'''
}
}
}
, { 'output':
'''\n <td>%(itemlabel)s</td>'''
, 'alt':
'''\n <td>*** no match for message/label ***</td>'''
, 'query': sparql_prefixes+
"""
SELECT * WHERE
{
?s minim:tryRequirement ?itemuri ;
minim:tryMessage ?itemlabel .
}"""
}
, { 'output':
'''\n </tr>'''+
''''''
}
]
})
# Report generates HTML for traffic-light display
#
# Example output:
#
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
# <html xmlns="http://www.w3.org/1999/xhtml">
#
# <html>
#
# <head>
# <link rel="stylesheet" type="text/css" href="css/checklist.css" />
# <meta http-equiv="content-type" content="text/html; charset=utf-8" />
# <title>Research Object runnable evaluation - simple-requirements</title>
# </head>
#
# <body>
# <div class="Container">
#
# <div class="header">
# Research object <a href="(Research-object-URI)">simple-requirements</a>
# </div>
#
# <div class="body">
# <table>
# <tr>
# <th class="trafficlight large fail should"><div/></th>
# <th colspan="2">Target
# <span class="target">
# <a href="(Research-object-URI)">simple-requirements</a>
# </span>
# <span class="testresult">minimally satisfies</span> checklist for
# <span class="testpurpose">Runnability</span>.
# <p>This Research Object might not be runnable.</p>
# </th>
# </tr>
# :
# (checklist items here)
# :
# </table>
# </div>
#
# <div class="footer">
# <hr/>
# <div><b><a href="http://www.wf4ever-project.org">Wf4Ever project</a></b></div>
# </div>
#
# </div>
# </body>
#
# </html>
#
# Incoming bindings:
# @@TBD css js?
#
# Optional incoming bindings:
# rouri URI of RO evaluated
# modeluri URI of Minim model defining the evaluated checklist
#
EvalChecklistHtml = (
{ 'report':
[ { 'output':
'''\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '''+
'''\n "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'''+
'''\n<html xmlns="http://www.w3.org/1999/xhtml">'''+
'''\n<html>'''+
'''\n <head>'''+
'''\n <link rel="stylesheet" type="text/css" href="css/checklist.css" />'''+
'''\n <meta http-equiv="content-type" content="text/html; charset=utf-8" />'''+
'''\n <title>Research Object %(purpose)s evaluation - %(roid)s</title>'''+
'''\n </head>'''+
''''''
, 'query':
sparql_prefixes+
"""
SELECT ?result ?rouri ?roid ?title ?description ?modeluri ?target ?targetlabel ?purpose
WHERE
{
?result minim:testedRO ?rouri ;
minim:testedModel ?modeluri ;
minim:testedTarget ?target ;
minim:testedPurpose ?purpose .
?rouri
dcterms:identifier ?roid ;
dcterms:title ?title ;
dcterms:description ?description .
OPTIONAL { ?target rdfs:label ?targetlabel . }
}
LIMIT 1
"""
, 'report':
[ { 'output':
'''\n <body>'''+
'''\n <div class="Container">'''+
'''\n <div class="header">'''+
'''\n %(title_esc)s'''+
#'''\n Research object <a href="%(rouri)s">%(roid)s</a>'''+
'''\n </div>'''+
'''\n <div class="content">'''+
'''\n <div class="sub_header">%(description_esc)s</div>'''+
'''\n <div class="body">'''+
'''\n <table>'''+
'''\n <thead>'''+
'''\n <tr class="main_result">'''
}
, { 'report':
{ 'output':
'''\n <th class="trafficlight large pass"><div/></th>'''
, 'query': """ASK { ?result <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
, 'altreport':
{ 'output':
'''\n <th class="trafficlight large fail may"><div/></th>'''
, 'query': """ASK { ?result <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
, 'altreport':
{ 'query': """ASK { ?result <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
, 'output':
'''\n <th class="trafficlight large fail should"><div/></th>'''
, 'alt':
'''\n <th class="trafficlight large fail must"><div/></th>'''
}
}
}
}
, { 'output':
'''\n <th colspan="2">Target <span class="target">'''+
'''\n <a href="%(target)s">%(targetlabel_esc)s</a></span> '''+
''''''
}
, { 'output':
'''\n <span class="testresult">'''
}
, { 'report': EvalTargetResultLabel
}
, { 'output':
'''</span> checklist for '''+
'''\n <span class="testpurpose">%(purpose)s</span>.'''+
'''\n </th>'''+
''''''
}
, { 'output':
'''\n </tr>'''+
'''\n </thead>'''+
''''''
}
, { 'output':
'''\n <tbody class="result_detail">'''+
''''''
}
, { 'report': EvalItemHtml
, 'query': sparql_prefixes+
"""
SELECT ?itemuri ?itemseq ?itemlevel ?modeluri WHERE
{ ?modeluri ?itemlevel ?itemuri .
?itemuri a minim:Requirement ;
minim:seq ?itemseq .
}
ORDER BY ?itemseq
"""
}
, { 'output':
'''\n </tbody>'''+
''''''
}
, { 'output':
'''\n </table>'''+
'''\n </div>'''+
'''\n <div class="footer">'''+
'''\n <div><b><a href="http://www.wf4ever-project.org">Wf4Ever project</a></b></div>'''+
'''\n </div>'''+
'''\n </div>'''+
'''\n </div>'''+
'''\n </body>'''+
'''\n</html>'''+
''''''
}
]
}
]
})
# End.