-
Notifications
You must be signed in to change notification settings - Fork 42
/
airPane.js
1051 lines (870 loc) · 48.8 KB
/
airPane.js
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** AIR (Amord in RDF) Pane
*
* TAMI (Transparent and Accountabe Data Mining) project
* This pane will display the justification trace of a when it encounters
* air reasoner output
* oshani@csail.mit.edu
*
* AIR is a rule language used in the TAMI project. The AIR reasoner
* outputs a proof of its results. This pane displays the proof as a
* human-redable justification of the result.
*/
var UI = require('solid-ui')
// moved to airpane.js from paneutils.js 2014 tbl
var ap_air = UI.rdf.Namespace("http://dig.csail.mit.edu/TAMI/2007/amord/air#");
var ap_tms = UI.rdf.Namespace("http://dig.csail.mit.edu/TAMI/2007/amord/tms#");
var ap_compliant = ap_air('compliant-with');
var ap_nonCompliant = ap_air('non-compliant-with');
var ap_antcExpr = ap_tms('antecedent-expr');
var ap_just = ap_tms('justification');
var ap_subExpr = ap_tms('sub-expr');
var ap_description = ap_tms('description');
var ap_ruleName = ap_tms('rule-name');
var ap_prem = ap_tms('premise');
var ap_instanceOf = ap_air('instanceOf');
var justificationsArr = [];
var airPane = module.exports = {};
airPane.name = 'air';
airPane.icon = UI.icons.originalIconBase + '1pt5a.gif'
airPane.label = function(subject) {
//Flush all the justification statements already found
justificationsArr = [];
//Find all the statements with air:justification in it
var stsJust = UI.store.statementsMatching(undefined, ap_just, undefined, subject);
//This will hold the string to display if the pane appears
var stringToDisplay = null
//Then make a registry of the compliant and non-compliant subjects
//(This algorithm is heavily dependant on the output from the reasoner.
//If the output changes, the parser will break.)
for (var j=0; j<stsJust.length; j++){
//The subject of the statements should be a quouted formula and
//the object should not be tms:premise (this corresponds to the final chunk of the output
//which has {some triples} tms:justification tms:premise)
if (stsJust[j].subject.termType == 'Graph' && stsJust[j].object != ap_prem.toString()){
var sts = stsJust[j].subject.statements;
if (sts.length != 1) throw new Error("There should be only ONE statement indicating some event is (non-)compliant with some policy!")
//Keep track of the subjects of the statements in the global variables above and return "Justify"
//which will be the tool-tip text of the label icon
if (sts[0].predicate.toString() == ap_compliant.toString()){
var compliantString = UI.utils.label(sts[0].subject) + " is compliant with " + UI.utils.label(sts[0].object);
var compliantArr = [];
compliantArr.push(sts[0].object);
compliantArr.push(ap_compliant.toString());
compliantArr.push(compliantString);
justificationsArr.push(compliantArr);
}
if (sts[0].predicate.toString() == ap_nonCompliant.toString()){
var nonCompliantString = UI.utils.label(sts[0].subject) + " is non compliant with " + UI.utils.label(sts[0].object);
var nonCompliantArr = [];
nonCompliantArr.push(sts[0].object);
nonCompliantArr.push(ap_nonCompliant.toString());
nonCompliantArr.push(nonCompliantString);
justificationsArr.push(nonCompliantArr);
}
stringToDisplay = "Justify" //Even with one relevant statement this method should return something
}
}
//Make the subject list we will be exploring in the render function unique
//compliantStrings = UI.widgets.unique(compliantStrings);
//nonCompliantStrings = UI.widgets.unique(nonCompliantStrings);
return stringToDisplay;
}
// View the justification trace in an exploratory manner
airPane.render = function(subject, myDocument) {
//Variables specific to the UI
var statementsAsTables = UI.panes.dataContentPane.statementsAsTables;
var divClass;
var div = myDocument.createElement("div");
//Helpers
var logFileURI = UI.widgets.extractLogURI(myDocument.location.toString());
div.setAttribute('class', 'dataContentPane'); //airPane has the same formatting as the dataContentPane
div.setAttribute('id', 'dataContentPane'); //airPane has the same formatting as the dataContentPane
//If there are multiple justifications show a dropdown box to select the correct one
var selectEl = myDocument.createElement("select");
var divOutcome = myDocument.createElement("div"); //This is div to display the final outcome.
divOutcome.setAttribute('id', 'outcome'); //There can only be one outcome per selection from the drop down box
//Show the selected justification only
airPane.render.showSelected = function(){
//Clear the contents of the outcome div
if (myDocument.getElementById('outcome') != null){
myDocument.getElementById('outcome').innerHTML = '';
}
//Function to hide the natural language description div and the premises div
airPane.render.showSelected.hide = function(){
//Clear the outcome div
if (myDocument.getElementById('outcome') != null){
myDocument.getElementById('outcome').innerHTML = '';
}
//Remove the justification div from the pane
var d = myDocument.getElementById('dataContentPane');
var j = myDocument.getElementById('justification');
var b = myDocument.getElementById('hide');
var m = myDocument.getElementById('more');
var o = myDocument.getElementById('outcome');
var w = myDocument.getElementById('whyButton');
if (d != null && m != null){
d.removeChild(m);
}
if (d != null && j != null && b != null){
d.removeChild(j);
d.removeChild(b);
}
if (d != null && o != null){
d.removeChild(o);
}
if (d != null && w != null){
d.removeChild(w);
}
}
//End of airPane.render.showSelected.hide
//Clear the contents of the justification div
airPane.render.showSelected.hide();
if (this.selectedIndex == 0)
return;
selected = justificationsArr[this.selectedIndex - 1];
var stsJust = UI.store.statementsMatching(undefined, ap_just, undefined, subject);
for (var i=0; i<stsJust.length; i++){
//Find the statement maching the option selected from the drop down box
if (stsJust[i].subject.termType == 'Graph' &&
stsJust[i].object != ap_prem.toString() &&
stsJust[i].subject.statements[0].object.toString() == selected[0].toString()){
var stsFound = stsJust[i].subject.statements[0]; //We have only one statement - so no need to iterate
//@@@@@@ Variables specific to the logic
var ruleNameFound;
//Display red or green depending on the compliant/non-compliant status
if (selected[1].toString() == ap_nonCompliant.toString()){
divOutcome.setAttribute('class', 'nonCompliantPane');
divOutcome.setAttribute('style', 'border-top: solid 1px red; border-left: solid 1px red; border-bottom: solid 1px red; border-right: solid 1px red; padding: 0.5em; background-color: #fbf0f7; margin-top: 0.5em; margin-bottom: 0.5em; border-radius: 1em;');
}
else if (selected[1].toString() == ap_compliant.toString()){
divOutcome.setAttribute('class', 'compliantPane');
divOutcome.setAttribute('style', 'border-top: solid 1px green; border-left: solid 1px green; border-bottom: solid 1px green;border-right: solid 1px green;padding: 0.5em; background-color: #def8e0;margin-top: 0.5em; margin-bottom: 0.5em;border-radius: 1em;');
}
else{
alert("something went terribly wrong");
}
//Create a table and structure the final conclucsion appropriately
var table = myDocument.createElement("table");
var tr = myDocument.createElement("tr");
var td_s = myDocument.createElement("td");
var a_s = myDocument.createElement('a')
a_s.setAttribute('href', stsFound.subject.uri)
a_s.appendChild(myDocument.createTextNode(UI.utils.label(stsFound.subject)));
td_s.appendChild(a_s);
tr.appendChild(td_s);
var td_is = myDocument.createElement("td");
td_is.appendChild(myDocument.createTextNode(' is '));
tr.appendChild(td_is);
var td_p = myDocument.createElement("td");
var a_p = myDocument.createElement('a');
a_p.setAttribute('href', stsFound.predicate.uri)
a_p.appendChild(myDocument.createTextNode(UI.utils.label(stsFound.predicate)));
td_p.appendChild(a_p);
tr.appendChild(td_p);
var td_o = myDocument.createElement("td");
var a_o = myDocument.createElement('a')
a_o.setAttribute('href', stsFound.object.uri)
a_o.appendChild(myDocument.createTextNode(UI.utils.label(stsFound.object)));
td_o.appendChild(a_o);
tr.appendChild(td_o);
table.appendChild(tr);
divOutcome.appendChild(table);
div.appendChild(divOutcome);
//End of the outcome sentences
//Add the initial buttons
airPane.render.showSelected.addInitialButtons = function(){ //Function Call 1
//Create and append the 'Why?' button
//Check if it is visible in the DOM, if not add it.
if (myDocument.getElementById('whyButton') == null){
var becauseButton = myDocument.createElement('input');
becauseButton.setAttribute('type','button');
becauseButton.setAttribute('id','whyButton');
becauseButton.setAttribute('value','Why?');
div.appendChild(becauseButton);
becauseButton.addEventListener('click',airPane.render.showSelected.because,false);
}
div.appendChild(myDocument.createTextNode(' '));//To leave some space between the 2 buttons, any better method?
}
//End of airPane.render.showSelected.addInitialButtons
//The following function is triggered, when the why button is clicked
airPane.render.showSelected.because = function(){ //Function Call 2
//If the reasoner used closed-world-assumption, there are no interesting premises
var cwa = ap_air('closed-world-assumption');
var cwaStatements = UI.store.statementsMatching(undefined, cwa, undefined, subject);
var noPremises = false;
/* if (cwaStatements.length > 0){
noPremises = true;
}
*/
//Disable the 'why' button, otherwise clicking on that will keep adding the divs
var whyButton = myDocument.getElementById('whyButton');
var d = myDocument.getElementById('dataContentPane');
if (d != null && whyButton != null)
d.removeChild(whyButton);
//Function to display the natural language description
airPane.render.showSelected.because.displayDesc = function(obj){
for (var i=0; i<obj.elements.length; i++) {
dump(obj.elements[i]);
dump("\n");
if (obj.elements[i].termType == 'NamedNode') {
var anchor = myDocument.createElement('a');
anchor.setAttribute('href', obj.elements[i].uri);
anchor.appendChild(myDocument.createTextNode(UI.utils.label(obj.elements[i])));
//anchor.appendChild(myDocument.createTextNode(obj.elements[i]));
divDescription.appendChild(anchor);
}
else if (obj.elements[i].termType == 'Literal') {
if (obj.elements[i].value != undefined)
divDescription.appendChild(myDocument.createTextNode(obj.elements[i].value));
}
else if (obj.elements[i].termType == 'Graph') {
//@@ As per Lalana's request to handle formulas within the description
divDescription.appendChild(myDocument.createTextNode(obj.elements[i]));
//@@@ Using statementsAsTables to render the result gives a very ugly result -- urgh!
//divDescription.appendChild(statementsAsTables(obj.elements[i].statements,myDocument));
}
}
}
//End of airPane.render.showSelected.because.displayDesc
//Function to display the inner most stuff from the proof-tree
airPane.render.showSelected.because.moreInfo = function(ruleToFollow){
//Terminating condition:
// if the rule has for example - "pol:MA_Disability_Rule_1 tms:justification tms:premise"
// there are no more information to follow
var terminatingCondition = UI.store.statementsMatching(ruleToFollow, ap_just, ap_prem, subject);
if (terminatingCondition[0] != undefined){
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createTextNode("No more information available from the reasoner!"));
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
}
else{
//Update the description div with the description at the next level
var currentRule = UI.store.statementsMatching(undefined, undefined, ruleToFollow, subject);
//Find the corresponding description matching the currenrRule
var currentRuleDescSts = UI.store.statementsMatching(undefined, undefined, currentRule[0].object);
for (var i=0; i<currentRuleDescSts.length; i++){
if (currentRuleDescSts[i].predicate == ap_instanceOf.toString()){
var currentRuleDesc = UI.store.statementsMatching(currentRuleDescSts[i].subject, undefined, undefined, subject);
for (var j=0; j<currentRuleDesc.length; j++){
if (currentRuleDesc[j].predicate == ap_description.toString() &&
currentRuleDesc[j].object.termType == 'Collection'){
divDescription.appendChild(myDocument.createElement('br'));
airPane.render.showSelected.because.displayDesc(currentRuleDesc[j].object);
divDescription.appendChild(myDocument.createElement('br'));
divDescription.appendChild(myDocument.createElement('br'));
}
}
}
}
//This is a hack to fix the rule appearing instead of the bnode containing the description
correctCurrentRule = "";
for (var i=0; i< currentRule.length; i++){
if (currentRule[i].subject.termType == 'BlankNode'){
correctCurrentRule = currentRule[i].subject;
break;
}
}
var currentRuleSts = UI.store.statementsMatching(correctCurrentRule, ap_just, undefined, subject);
var nextRuleSts = UI.store.statementsMatching(currentRuleSts[0].object, ap_ruleName, undefined, subject);
ruleNameFound = nextRuleSts[0].object;
var currentRuleAntc = UI.store.statementsMatching(currentRuleSts[0].object, ap_antcExpr, undefined, subject);
var currentRuleSubExpr = UI.store.statementsMatching(currentRuleAntc[0].object, ap_subExpr, undefined, subject);
var formulaFound = false;
var bnodeFound = false;
for (var i=0; i<currentRuleSubExpr.length; i++){
if(currentRuleSubExpr[i].object.termType == 'Graph'){
divPremises.appendChild(statementsAsTables(currentRuleSubExpr[i].object.statements, myDocument));
formulaFound = true;
}
else if (currentRuleSubExpr[i].object.termType == 'BlankNode'){
bnodeFound = true;
}
}
if (bnodeFound){
divPremises.appendChild(myDocument.createElement("br"));
divPremises.appendChild(myDocument.createTextNode(" No premises applicable."));
divPremises.appendChild(myDocument.createElement("br"));
divPremises.appendChild(myDocument.createElement("br"));
}
}
}
//End of airPane.render.showSelected.because.moreInfo
//Function to bootstrap the natural language description div and the premises div
airPane.render.showSelected.because.justify = function(){ //Function Call 3
//Clear the contents of the premises div
myDocument.getElementById('premises').innerHTML='';
airPane.render.showSelected.because.moreInfo(ruleNameFound); //@@@@ make sure this rul would be valid at all times!
divJustification.appendChild(divPremises);
div.appendChild(divJustification);
}
//End of airPane.render.showSelected.because.justify
//Add the More Information Button
var justifyButton = myDocument.createElement('input');
justifyButton.setAttribute('type','button');
justifyButton.setAttribute('id','more');
justifyButton.setAttribute('value','More Information');
justifyButton.addEventListener('click',airPane.render.showSelected.because.justify,false);
div.appendChild(justifyButton);
//Add 2 spaces to leave some space between the 2 buttons, any better method?
div.appendChild(myDocument.createTextNode(' '));
div.appendChild(myDocument.createTextNode(' '));
//Add the hide button
var hideButton = myDocument.createElement('input');
hideButton.setAttribute('type','button');
hideButton.setAttribute('id','hide');
hideButton.setAttribute('value','Start Over');
div.appendChild(hideButton);
hideButton.addEventListener('click',airPane.render.showSelected.hide,false);
//This div is the containing div for the natural language description and the premises of any given justification
var divJustification = myDocument.createElement("div");
divJustification.setAttribute('class', 'justification');
divJustification.setAttribute('id', 'justification');
//Leave a gap between the outcome and the justification divs
divJustification.appendChild(myDocument.createElement('br'));
//Div for the natural language description
var divDescription = myDocument.createElement("div");
divDescription.setAttribute('class', 'description');
divDescription.setAttribute('id', 'description');
//Div for the premises
var divPremises = myDocument.createElement("div");
divPremises.setAttribute('class', 'premises');
divPremises.setAttribute('id', 'premises');
//@@@@ what is this for?
var justificationSts;
//Get all the triples with a air:description as the predicate
var stsDesc = UI.store.statementsMatching(undefined, ap_description, undefined, subject);
//You are bound to have more than one such triple,
//so iterates through all of them and figure out which belongs to the one that's referred from the drop down box
for (var j=0; j<stsDesc.length; j++){
if (stsDesc[j].subject.termType == 'Graph' &&
stsDesc[j].object.termType == 'Collection' &&
stsDesc[j].subject.statements[0].object.toString() == selected[0].toString()){
divDescription.appendChild(myDocument.createElement('br'));
airPane.render.showSelected.because.displayDesc(stsDesc[j].object);
divDescription.appendChild(myDocument.createElement('br'));
divDescription.appendChild(myDocument.createElement('br'));
}
divJustification.appendChild(divDescription);
}
//@@@@@@@@@ Why was this here???
//div.appendChild(divJustification);
//Leave spaces
divJustification.appendChild(myDocument.createElement('br'));
divJustification.appendChild(myDocument.createElement('br'));
//Yes, we are showing premises next...
divJustification.appendChild(myDocument.createElement('b').appendChild(myDocument.createTextNode('Premises:')));
//Leave spaces
divJustification.appendChild(myDocument.createElement('br'));
divJustification.appendChild(myDocument.createElement('br'));
//Closed World Assumption
if (noPremises){
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createTextNode("Nothing interesting found in the "));
var a = myDocument.createElement('a')
a.setAttribute("href", unescape(logFileURI));
a.appendChild(myDocument.createTextNode("log file"));
divPremises.appendChild(a);
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
}
for (var j=0; j<stsJust.length; j++){
if (stsJust[j].subject.termType == 'Graph' && stsJust[j].object.termType == 'BlankNode'){
var ruleNameSts = UI.store.statementsMatching(stsJust[j].object, ap_ruleName, undefined, subject);
ruleNameFound = ruleNameSts[0].object; // This would be the initial rule name from the
// statement containing the formula
if (!noPremises){
var t1 = UI.store.statementsMatching(stsJust[j].object, ap_antcExpr, undefined, subject);
for (var k=0; k<t1.length; k++){
var t2 = UI.store.statementsMatching(t1[k].object, undefined, undefined, subject);
for (var l=0; l<t2.length; l++){
if (t2[l].subject.termType == 'BlankNode' && t2[l].object.termType == 'Graph'){
justificationSts = t2;
divPremises.appendChild(myDocument.createElement('br'));
//divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(statementsAsTables(t2[l].object.statements, myDocument));
//@@@@ The following piece of code corresponds to going one level of the justification proof to figure out
// whether there are any premises
//it is commented out, because, the user need not know that at each level there are no premises associated
//that particular step
/*if (t2[l].object.statements.length == 0){
alert("here");
divPremises.appendChild(myDocument.createTextNode("Nothing interesting found in "));
var a = myDocument.createElement('a')
a.setAttribute('href', unescape(logFileURI));
a.appendChild(myDocument.createTextNode("log file"));
divPremises.appendChild(a);
}
else{
divPremises.appendChild(statementsAsTables(t2[l].object.statements, myDocument));
}
*/
//divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
}
}
}
}
}
}
divJustification.appendChild(divPremises);
div.appendChild(divJustification);
}
//End of airPane.render.showSelected.because
airPane.render.showSelected.addInitialButtons(); //Add the "Why Button"
}
}
}
//First add a bogus element
var optionElBogus = myDocument.createElement("option");
var optionTextBogus = myDocument.createTextNode(" ");
optionElBogus.appendChild(optionTextBogus);
selectEl.appendChild(optionElBogus);
//Adds the option of which justification to choose in a drop down list box
for (var i=0; i<justificationsArr.length; i++){
var optionEl = myDocument.createElement("option");
var optionText = myDocument.createTextNode(justificationsArr[i][2]);
optionEl.appendChild(optionText);
selectEl.appendChild(optionEl);
}
div.appendChild(selectEl);
selectEl.addEventListener('change', airPane.render.showSelected, false);
div.appendChild(myDocument.createElement('br'));
div.appendChild(myDocument.createElement('br'));
return div;
};
//^^
airPane.renderReasonsForStatement = function renderReasonsForStatement(st,
divJustification){
var divDescription = myDocument.createElement("div");
divDescription.setAttribute('class', 'description');
//Display the actual English-like description first
//It's no longer English-like, but just property tables
//var stsDesc = kb.statementsMatching(undefined, ap_description, undefined, subject);
//var stsDesc = kb.statementsMatching(st, ap_description);
var stsDesc = UI.store.statementsMatching(st, ap_just);
// {} tms:justification []. (multiple)
if(stsDesc.length > 1){
for (var j=0; j<stsDesc.length; j++){
//Display the header "Reason x:"
var h3 = divJustification.appendChild(document.createElement('h3'));
h3.textContent = "Reason " + String(j+1); + ":";
airPane.render.because.displayDesc(stsDesc[j].object,
divDescription);
divJustification.appendChild(divDescription);
//Make a copy of the orange box
divDescription = divDescription.cloneNode(false); //shallow:true
}
} else{
airPane.render.because.displayDesc(stsDesc[0].object,
divDescription);
divJustification.appendChild(divDescription);
}
};
airPane.renderExplanationForStatement = function renderExplanationForStatement(st){
var subject = undefined; //not restricted to a source, but kb
var div = myDocument.createElement("div"); //the returned div
var ruleNameFound;
var stsCompliant;
var stsNonCompliant;
var stsFound;
var stsJust = UI.store.statementsMatching(st, ap_just);
var divOutcome = myDocument.createElement("div"); //To give the "yes/no" type answer indicating the concise reason
var divClass = 'compliantPane'; //a statement is natively good :)
//stsFound = kb.anyStatementsMatching(st, ap_just);
stsFound = stsJust[0].subject.statements[0];
if (stsFound != undefined){
divOutcome.setAttribute('class', divClass);
divOutcome.setAttribute('id', 'outcome');
var table = myDocument.createElement("table");
var tr = myDocument.createElement("tr");
var td_intro = myDocument.createElement("td");
td_intro.appendChild(myDocument.createTextNode('The reason '));
tr.appendChild(td_intro);
var td_s = myDocument.createElement("td");
var a_s = myDocument.createElement('a')
a_s.setAttribute('href', stsFound.subject.uri)
a_s.appendChild(myDocument.createTextNode(UI.utils.label(stsFound.subject)));
td_s.appendChild(a_s);
tr.appendChild(td_s);
//var td_is = myDocument.createElement("td");
//td_is.appendChild(myDocument.createTextNode(' is '));
//tr.appendChild(td_is);
var td_p = myDocument.createElement("td");
var a_p = myDocument.createElement('a');
a_p.setAttribute('href', stsFound.predicate.uri);
a_p.appendChild(myDocument.createTextNode(UI.utils.label(stsFound.predicate)));
td_p.appendChild(a_p);
tr.appendChild(td_p);
var td_o = myDocument.createElement("td");
var a_o = null;
if (stsFound.object.termType == 'Literal'){
a_o = myDocument.createTextNode(stsFound.object.value);
} else {
var a_o = myDocument.createElement('a');
a_o.setAttribute('href', stsFound.object.uri);
a_o.appendChild(myDocument.createTextNode(UI.utils.label(stsFound.object)));
}
td_o.appendChild(a_o);
tr.appendChild(td_o);
var td_end = myDocument.createElement("td");
td_end.appendChild(myDocument.createTextNode(' is because: '));
tr.appendChild(td_end);
table.appendChild(tr);
divOutcome.appendChild(table);
div.appendChild(divOutcome);
var hideButton = myDocument.createElement('input');
hideButton.setAttribute('type','button');
hideButton.setAttribute('id','hide');
hideButton.setAttribute('value','Start Over');
}
airPane.render.addInitialButtons = function(){ //Function Call 1
//Create and append the 'Why?' button
var becauseButton = myDocument.createElement('input');
becauseButton.setAttribute('type','button');
becauseButton.setAttribute('id','whyButton');
becauseButton.setAttribute('value','Why?');
div.appendChild(becauseButton);
becauseButton.addEventListener('click',airPane.render.because,false);
div.appendChild(myDocument.createTextNode(' '));//To leave some space between the 2 buttons, any better method?
}
airPane.render.hide = function(){
//Remove the justification div from the pane
var d = myDocument.getElementById('dataContentPane');
var j = myDocument.getElementById('justification');
var b = myDocument.getElementById('hide');
var m = myDocument.getElementById('more');
if (d != null && m != null){
d.removeChild(m);
}
if (d != null && j != null && b != null){
d.removeChild(j);
d.removeChild(b);
}
airPane.render.addInitialButtons();
}
airPane.render.because = function(){ //Function Call 2
var cwa = ap_air('closed-world-assumption');
var cwaStatements = UI.store.statementsMatching(undefined, cwa, undefined, subject);
var noPremises = false;
if (cwaStatements.length > 0){
noPremises = true;
}
//Disable the 'why' button, otherwise clicking on that will keep adding the divs
var whyButton = myDocument.getElementById('whyButton');
var d = myDocument.getElementById('dataContentPane');
if (d != null && whyButton != null)
d.removeChild(whyButton);
airPane.render.because.displayDesc = function(obj, divDescription){
//@argument obj: most likely a [] that has
//a tms:antecedent-expr and a tms:rule-name
var aAnd_justification = UI.store.the(obj, ap_antcExpr);
var subExprs = UI.store.each(aAnd_justification, ap_subExpr);
var premiseFormula = null;
if (subExprs[0].termType == 'Graph')
premiseFormula = subExprs[0];
else
premiseFormula = subExprs[1];
divDescription.waitingFor = []; //resources of more information
//this reason is waiting for
divDescription.informationFound = false; //true if an extra
//information is found and we can stop the throbber
function dumpFormula(formula, firstLevel){
for (var i=0;i<formula.statements.length;i++){
var st = formula.statements[i];
var elements_to_display = [st.subject, st.predicate,
st.object];
var p = null; //the paragraph element the description is dumped to
if (firstLevel){
p = myDocument.createElement('p');
//Look up the outermost subject and object for information
if (st.subject.termType == 'NamedNode'){
var doc_uri = Util.uri.docpart(st.subject.uri);
if (divDescription.waitingFor.indexOf(doc_uri) < 0 &&
typeof sf.requested[doc_uri]=="undefined")
divDescription.waitingFor.push(doc_uri);
}
if (st.object.termType == 'NamedNode'){
var doc_uri = Util.uri.docpart(st.object.uri);
if (divDescription.waitingFor.indexOf(doc_uri) < 0 &&
typeof sf.requested[doc_uri]=="undefined")
divDescription.waitingFor.push(doc_uri);
}
}
else{
p = dumpFormula.current_p;
}
for (var j=0; j<3; j++) {
var element = elements_to_display[j];
switch(element.termType) {
//@@ As per Lalana's request to handle formulas within the description
case 'Graph':
p.appendChild(myDocument.createTextNode("{ "));
dumpFormula.current_p = p;
dumpFormula(element, false);
p.appendChild(myDocument.createTextNode(" }"));
break;
case 'NamedNode':
var anchor = myDocument.createElement('a');
anchor.setAttribute('href', element.uri);
anchor.appendChild(myDocument.createTextNode(UI.utils.label(element)));
p.appendChild(anchor);
p.appendChild(myDocument.createTextNode(" "));
break;
case 'Literal':
//if (obj.elements[i].value != undefined)
p.appendChild(myDocument.createTextNode(element.value));
}
}
p.appendChild(myDocument.createTextNode(". "));
if(firstLevel){
divDescription.appendChild(p);
var one_statement_formula = new UI.rdf.IndexedFormula();
one_statement_formula.statements.push(st)
p.AJAR_formula = one_statement_formula;
function make_callback(st, p, divDescription){
return function statement_more_information_callback(uri){
divDescription.waitingFor.remove(uri);
if(UI.store.any(p.AJAR_formula, ap_just)) {
//The would get called twice even if the callback
//is canceled, so check the last child.
//dump("in statement_more_information_callback with st: " +st + "and uri: " + uri + "\n");
divDescription.informationFound = true;
if (p.lastChild.nodeName=="#text"){
var explain_icon = p.appendChild(myDocument.createElement('img'));
explain_icon.src = UI.icons.originalIconBase + "tango/22-help-browser.png";
var click_cb = function(){
airPane.renderReasonsForStatement(
p.AJAR_formula, divJustification);
};
explain_icon.addEventListener('click',
click_cb,
false)
}
if (throbber_p && throbber_callback)
throbber_callback();
return false; //no need to fire this function
}
//Fetch sameAs here. We try to load minimum sources
//so this comes after the above kb.any
for (var h=0;h<2;h++) { //Never use for each!!!
//Array.prototype.remove would
//be one of them!!!
var thing =[st.subject, st.object][h];
var uris = UI.store.uris(thing);
for (var k=0;k<uris.length;k++){
var doc_uri = Util.uri.docpart(uris[k])
if (typeof sf.requested[doc_uri]=="undefined" &&
divDescription.waitingFor.indexOf(doc_uri)<0){
//the second condition holds, for example,
//Util.uri.docpart(thing.uri)
divDescription.waitingFor.push(doc_uri);
sf.lookUpThing(UI.store.sym(doc_uri));
}
}
}
if (divDescription.waitingFor.length == 0){
if (throbber_p && throbber_callback)
throbber_callback();
return false; //the last resource this div is waiting for
}
if (throbber_p && throbber_callback)
throbber_callback();
return true;
};
}
var cb = make_callback(st, p, divDescription)
cb();
//statement_more_information_callback(); //run once for exsiting information
sf.addCallback('done',cb);
sf.addCallback('fail',cb);
}
} //statement loop
} //function dumpFormula
dumpFormula(premiseFormula, true);
//'Looking for more information...
//@correct the background color of the throbber
var throbber_p = myDocument.createElement('p');
throbber_p.setAttribute('class', 'ap_premise_loading')
var throbber = throbber_p.appendChild(myDocument.createElement
('img'));
throbber.src = UI.icons.originalIconBase + "loading.png";
throbber_p.appendChild(myDocument.createTextNode
("Looking for more information..."));
divDescription.appendChild(throbber_p);
function throbber_callback(uri){
divDescription.waitingFor.remove(uri);
if (divDescription.informationFound){
throbber_p.removeChild(throbber_p.firstChild);
throbber_p.textContent = "More information found!";
return false;
} else if (divDescription.waitingFor.length == 0){
//The final call to this function. But the above callbacks
//might not have been fired. So check all. Well...
//It takes time to close the world
//@@This method assumes there's only one thread for this js.
//Maybe not?
var found = false;
// for (var i=0;i<divDescription.childNodes.length;i++){
// var p = divDescription.childNodes[i];
// if(p.AJAR_formula && kb.any(p.AJAR_formula, ap_just)){
// found = true;
// break;
// }
// }
if (found){
throbber_p.removeChild(throbber_p.firstChild);
throbber_p.textContent = "More information found!";
} else {
throbber_p.removeChild(throbber_p.firstChild);
throbber_p.textContent = "No more information.";
}
return false; //no more resource waiting for
} else {
return true;
}
}
throbber_callback();
// if(divDescription.waitingFor.length){
// //we don't need to do call back if there's nothing more to lookup
// sf.addCallback('done',throbber_callback);
// sf.addCallback('fail',throbber_callback);
// }
for (var i=0;i<divDescription.waitingFor.length;i++)
sf.lookUpThing(UI.store.sym(divDescription.waitingFor[i]));
} //function airPane.render.because.displayDesc
airPane.render.because.moreInfo = function(ruleToFollow){
//Terminating condition:
// if the rule has for example - "pol:MA_Disability_Rule_1 tms:justification tms:premise"
// there are no more information to follow
var terminatingCondition = UI.store.statementsMatching(ruleToFollow, ap_just, ap_prem, subject);
if (terminatingCondition[0] != undefined){
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createTextNode("No more information available from the reasoner!"));
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
}
else{
//Update the description div with the description at the next level
var currentRule = UI.store.statementsMatching(undefined, undefined, ruleToFollow);
//Find the corresponding description matching the currenrRule
var currentRuleDescSts = UI.store.statementsMatching(undefined, undefined, currentRule[0].object);
for (var i=0; i<currentRuleDescSts.length; i++){
if (currentRuleDescSts[i].predicate == ap_instanceOf.toString()){
var currentRuleDesc = UI.store.statementsMatching(currentRuleDescSts[i].subject, undefined, undefined, subject);
for (var j=0; j<currentRuleDesc.length; j++){
if (currentRuleDesc[j].predicate == ap_description.toString() &&
currentRuleDesc[j].object.termType == 'Collection'){
divDescription.appendChild(myDocument.createElement('br'));
airPane.render.because.displayDesc(currentRuleDesc[j].object);
divDescription.appendChild(myDocument.createElement('br'));
divDescription.appendChild(myDocument.createElement('br'));
}
}
}
}
var currentRuleSts = UI.store.statementsMatching(currentRule[0].subject, ap_just, undefined);
var nextRuleSts = UI.store.statementsMatching(currentRuleSts[0].object, ap_ruleName, undefined);
ruleNameFound = nextRuleSts[0].object;
var currentRuleAntc = UI.store.statementsMatching(currentRuleSts[0].object, ap_antcExpr, undefined);
var currentRuleSubExpr = UI.store.statementsMatching(currentRuleAntc[0].object, ap_subExpr, undefined);
for (var i=0; i<currentRuleSubExpr.length; i++){
if(currentRuleSubExpr[i].object.termType == 'Graph')
divPremises.appendChild(statementsAsTables(currentRuleSubExpr[i].object.statements, myDocument));
}
}
}
airPane.render.because.justify = function(){ //Function Call 3
//Clear the contents of the div
myDocument.getElementById('premises').innerHTML='';
airPane.render.because.moreInfo(ruleNameFound);
divJustification.appendChild(divPremises);
div.appendChild(divJustification);
}
//Add the More Information Button
/* //disable buttons
var justifyButton = myDocument.createElement('input');
justifyButton.setAttribute('type','button');
justifyButton.setAttribute('id','more');
justifyButton.setAttribute('value','More Information');
justifyButton.addEventListener('click',airPane.render.because.justify,false);
div.appendChild(justifyButton);
div.appendChild(myDocument.createTextNode(' '));//To leave some space between the 2 buttons, any better method?
div.appendChild(myDocument.createTextNode(' '));
div.appendChild(hideButton);
hideButton.addEventListener('click',airPane.render.hide,false);
*/
var divJustification = myDocument.createElement("div");
divJustification.setAttribute('class', 'justification');
divJustification.setAttribute('id', 'justification');
var divDescription = myDocument.createElement("div");
divDescription.setAttribute('class', 'description');
//divDescription.setAttribute('id', 'description');
/*
var divPremises = myDocument.createElement("div");
divPremises.setAttribute('class', 'premises');
divPremises.setAttribute('id', 'premises');
*/
var justificationSts;
airPane.renderReasonsForStatement(st, divJustification);
div.appendChild(divJustification);
/*
divJustification.appendChild(myDocument.createElement('br'));
divJustification.appendChild(myDocument.createElement('br'));
divJustification.appendChild(myDocument.createElement('b').appendChild(myDocument.createTextNode('Premises:')));
divJustification.appendChild(myDocument.createElement('br'));
divJustification.appendChild(myDocument.createElement('br'));
if (noPremises){
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createElement('br'));
divPremises.appendChild(myDocument.createTextNode("Nothing interesting found in the "));
var a = myDocument.createElement('a')
a.setAttribute("href", unescape(logFileURI));
a.appendChild(myDocument.createTextNode("log file"));
divPremises.appendChild(a);
divPremises.appendChild(myDocument.createElement('br'));