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 * <p>Title: WSMO Studio</p>
38
39 * <p>Description: Semantic Web Service Editor</p>
40
41 * <p>Copyright: Copyright (c) 2004-2006</p>
42
43 * <p>Company: OntoText Lab. / SIRMA </p>
44
45 */
46
47
48
49 package org.wsmostudio.ui.views.navigator;
50
51
52
53 import java.util.*;
54
55
56
57 import org.eclipse.jface.viewers.*;
58
59 import org.eclipse.ui.IEditorInput;
60
61 import org.omwg.ontology.*;
62
63 import org.wsmo.common.*;
64
65 import org.wsmo.common.exception.SynchronisationException;
66
67 import org.wsmo.service.*;
68
69 import org.wsmostudio.runtime.*;
70
71 import org.wsmostudio.ui.Utils;
72
73
74
75 /***
76
77 * A content provider implementation which determines what kind of information
78
79 * will be visualized as content for certain WSMO-API objects in a tree-styled
80
81 * viewer.
82
83 * This content provider is used by the <code>WSMO Navigator</code> view and the
84
85 * various WSMO choosers.
86
87 *
88
89 * @author not attributable
90
91 * @version $Revision: 1.18 $ $Date: 2006/04/10 14:05:30 $
92
93 */
94
95
96
97 public class WSMOContentProvider implements ITreeContentProvider {
98
99
100
101 public static final int CONCEPT_MASK = 1;
102
103 public static final int INSTANCE_MASK = 1 << 1;
104
105 public static final int RELATION_MASK = 1 << 2;
106
107 public static final int RELATION_INSTANCE_MASK = 1 << 3;
108
109 public static final int AXIOM_MASK = 1 << 4;
110
111
112
113 public static final int ATTRIBUTE_MASK = 1 << 5;
114
115
116
117 public static final int ONTOLOGY_MASK = 1 << 10;
118
119 public static final int SERVICE_MASK = 1 << 11;
120
121 public static final int OO_MEDIATOR_MASK = 1 << 12;
122
123 public static final int WW_MEDIATOR_MASK = 1 << 13;
124
125 public static final int WG_MEDIATOR_MASK = 1 << 14;
126
127 public static final int GG_MEDIATOR_MASK = 1 << 15;
128
129 public static final int GOAL_MASK = 1 << 16;
130
131
132
133 public static final int INTERFACE_MASK = 1 << 20;
134
135 public static final int CAPABILITY_MASK = 1 << 21;
136
137
138
139 public static final int CHOR_MASK = 1 << 22;
140
141 public static final int ORCH_MASK = 1 << 23;
142
143
144
145 public static final int MEDIATOR_MASK = OO_MEDIATOR_MASK
146
147 | WW_MEDIATOR_MASK
148
149 | WG_MEDIATOR_MASK
150
151 | GG_MEDIATOR_MASK;
152
153 public static final int TOP_ENTITY_MASK = ONTOLOGY_MASK
154
155 | MEDIATOR_MASK
156
157 | SERVICE_MASK
158
159 | GOAL_MASK;
160
161
162
163
164
165 public static final int NO_FILTER = 0xFFFFFFFF;
166
167
168
169 private int filterMask;
170
171
172
173 public WSMOContentProvider(int mask) {
174
175 filterMask = checkValidMask(mask);
176
177 }
178
179
180
181 private WSMOContentProvider() {}
182
183
184
185 @SuppressWarnings("unchecked")
186
187 public Object[] getChildren(Object parentElement) {
188
189 if (parentElement instanceof Ontology) {
190
191 if (Utils.isAProxy(parentElement)) {
192
193 Identifier id = ((Ontology)parentElement).getIdentifier();
194
195 WSMORuntime.getIOManager().doLoadOntology(id);
196
197 }
198
199 try {
200
201 return createOntologyContent((Ontology)parentElement);
202
203 }
204
205 catch(SynchronisationException synchEx) {
206
207 return null;
208
209 }
210
211 }
212
213 else if (parentElement instanceof Concept) {
214
215 return createConceptContent((Concept)parentElement);
216
217 }
218
219 else if (parentElement instanceof Relation) {
220
221 return createRelationContent((Relation)parentElement);
222
223 }
224
225 else if (parentElement instanceof ServiceDescription) {
226
227 if (Utils.isAProxy(parentElement)) {
228
229 Identifier id = ((Ontology)parentElement).getIdentifier();
230
231 if (parentElement instanceof Goal) {
232
233 WSMORuntime.getIOManager().doLoadGoal(id);
234
235 }
236
237 else {
238
239 WSMORuntime.getIOManager().doLoadWebService(id);
240
241 }
242
243 }
244
245 ServiceDescription ws = (ServiceDescription)parentElement;
246
247 LinkedList<TopEntity> resultHolder = new LinkedList<TopEntity>();
248
249 if (isShowingCapability()
250
251 && ws.getCapability() != null) {
252
253 resultHolder.add(ws.getCapability());
254
255 }
256
257 if (isShowingInterfaces()) {
258
259 Set<Interface> interfaces = ws.listInterfaces();
260
261 if (interfaces.size() > 0) {
262
263 resultHolder.addAll(interfaces);
264
265 }
266
267 }
268
269 return (resultHolder.size() == 0)
270
271 ? null
272
273 : resultHolder.toArray();
274
275 }
276
277 else if (parentElement instanceof Interface) {
278
279 Interface iface = (Interface)parentElement;
280
281 LinkedList<Entity> result = new LinkedList<Entity>();
282
283 if (isShowingChors()
284
285 && iface.getChoreography() != null) {
286
287 result.add(iface.getChoreography());
288
289 }
290
291 if (isShowingOrch()
292
293 && iface.getOrchestration() != null) {
294
295 result.add(iface.getOrchestration());
296
297 }
298
299 return result.toArray();
300
301 }
302
303 else if (parentElement instanceof Capability) {
304
305 LinkedList<Axiom> axioms = new LinkedList<Axiom>();
306
307 Capability capa = (Capability)parentElement;
308
309 axioms.addAll(capa.listPreConditions());
310
311 axioms.addAll(capa.listAssumptions());
312
313 axioms.addAll(capa.listPostConditions());
314
315 axioms.addAll(capa.listEffects());
316
317 return axioms.toArray();
318
319 }
320
321
322
323 return null;
324
325 }
326
327
328
329
330
331
332
333
334
335 public Object getParent(Object element) {
336
337 return null;
338
339 }
340
341
342
343
344
345
346
347
348
349 public boolean hasChildren(Object element) {
350
351
352
353 if (element instanceof TopEntity) {
354
355 if (Utils.isAProxy(element)) {
356
357 if (element instanceof Capability
358
359 || element instanceof Interface) {
360
361 return false;
362
363 }
364
365 Identifier id = ((Entity)element).getIdentifier();
366
367 return id instanceof IRI
368
369 && isShowingTopEntityContent()
370
371 && WSMORuntime.getCache().getTypeByIRI((IRI)id) != -1;
372
373 }
374
375 }
376
377
378
379 try {
380
381 if (element instanceof Ontology) {
382
383 Ontology ontology = (Ontology)element;
384
385
386
387 return (isShowingConcepts() && ontology.listConcepts().size() > 0)
388
389 || (isShowingRelations() && ontology.listRelations().size() > 0)
390
391 || (isShowingAxioms() && ontology.listAxioms().size() > 0);
392
393 }
394
395 else if (element instanceof Concept) {
396
397 return createConceptContent((Concept)element).length > 0;
398
399 }
400
401 else if (element instanceof Relation) {
402
403 return createRelationContent((Relation)element).length > 0;
404
405 }
406
407 else if (element instanceof ServiceDescription) {
408
409 ServiceDescription ws = (ServiceDescription)element;
410
411 return (isShowingCapability()
412
413 && ws.getCapability() != null)
414
415 || (isShowingInterfaces()
416
417 && ws.listInterfaces().size() > 0);
418
419 }
420
421 else if (element instanceof Interface) {
422
423 Interface iface = (Interface)element;
424
425 return (isShowingChors() && iface.getChoreography() != null)
426
427 || (isShowingOrch() && iface.getOrchestration() != null);
428
429 }
430
431 else if (element instanceof Capability) {
432
433 Capability capa = (Capability)element;
434
435 return capa.listPreConditions().size() > 0
436
437 || capa.listAssumptions().size() > 0
438
439 || capa.listPostConditions().size() > 0
440
441 || capa.listEffects().size() > 0;
442
443 }
444
445
446
447 }
448
449 catch(SynchronisationException synchEx) {
450
451 return false;
452
453 }
454
455 catch(Exception anyEx) {
456
457 LogManager.logError(anyEx);
458
459 }
460
461 return false;
462
463 }
464
465
466
467
468
469
470
471
472
473 public Object[] getElements(Object inputElement) {
474
475 if (inputElement == null) {
476
477 return null;
478
479 }
480
481
482
483 if (inputElement instanceof IEditorInput) {
484
485 Entity object = (Entity)((IEditorInput)inputElement).getAdapter(Entity.class);
486
487 return new Object[] { object };
488
489 }
490
491 try {
492
493 if (inputElement instanceof WSMORuntime) {
494
495 if (filterMask == CAPABILITY_MASK) {
496
497 return WSMORuntime.getCache().getCapabilities().toArray();
498
499 }
500
501 if (filterMask == INTERFACE_MASK) {
502
503 return WSMORuntime.getCache().getInterfaces().toArray();
504
505 }
506
507
508
509 boolean showOntos = (filterMask & ONTOLOGY_MASK) != 0;
510
511 boolean showServices = (filterMask & SERVICE_MASK) != 0;
512
513 boolean showGoals = (filterMask & GOAL_MASK) != 0;
514
515 boolean showMediators = (filterMask & MEDIATOR_MASK) != 0;
516
517 int totalLength = 0;
518
519
520
521 Collection ontos = null;
522
523 Collection services = null;
524
525 Collection goals = null;
526
527 Object[] mediators = null;
528
529
530
531 if (showOntos) {
532
533 ontos = WSMORuntime.getCache().getOntologies();
534
535 totalLength += ontos.size();
536
537 }
538
539 if (showServices) {
540
541 services = WSMORuntime.getCache().getWebServices();
542
543 totalLength += services.size();
544
545 }
546
547 if (showGoals) {
548
549 goals = WSMORuntime.getCache().getGoals();
550
551 totalLength += goals.size();
552
553 }
554
555 if (showMediators) {
556
557 mediators = findAppropMediators();
558
559 totalLength += mediators.length;
560
561 }
562
563
564
565 Object[] result = new Object[totalLength];
566
567 int offs = 0;
568
569 if (showOntos) {
570
571 System.arraycopy(ontos.toArray(), 0, result, offs, ontos.size());
572
573 offs += ontos.size();
574
575 }
576
577 if (showGoals) {
578
579 System.arraycopy(goals.toArray(), 0, result, offs, goals.size());
580
581 offs += goals.size();
582
583 }
584
585 if (showServices) {
586
587 System.arraycopy(services.toArray(), 0, result, offs, services.size());
588
589 offs += services.size();
590
591 }
592
593 if (showMediators) {
594
595 System.arraycopy(mediators, 0, result, offs, mediators.length);
596
597 offs += mediators.length;
598
599 }
600
601
602
603 return result;
604
605 }
606
607
608
609 if (inputElement instanceof Ontology) {
610
611 return createOntologyContent((Ontology)inputElement);
612
613 }
614
615 if (inputElement instanceof ServiceDescription) {
616
617 return new Object[] { inputElement };
618
619 }
620
621
622
623 if (inputElement instanceof Concept) {
624
625 return getChildren(inputElement);
626
627 }
628
629 }
630
631 catch(SynchronisationException synchEx) {
632
633 return null;
634
635 }
636
637 catch(Exception anyEx) {
638
639 LogManager.logError(anyEx);
640
641 }
642
643 return null;
644
645 }
646
647
648
649
650
651
652
653
654
655 public void dispose() {
656
657 }
658
659
660
661
662
663
664
665
666
667 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
668
669 }
670
671
672
673 @SuppressWarnings("unchecked")
674
675 private Object[] findTopConcepts(Ontology ref) {
676
677 Set<Concept> allConcepts = ref.listConcepts();
678
679 Set<Concept> topConcepts = new HashSet<Concept>();
680
681
682
683 for (Concept concept : allConcepts) {
684
685 if (filterExternalSuperEntities(ref, concept.listSuperConcepts()).size() == 0) {
686
687 topConcepts.add(concept);
688
689 }
690
691 }
692
693 return topConcepts.toArray();
694
695 }
696
697
698
699 private Object[] createConceptContent(Concept superConcept) {
700
701 boolean showInstances = (filterMask & INSTANCE_MASK) != 0;
702
703
704
705 Object[] subConcepts = superConcept.listSubConcepts().toArray();
706
707 Object[] instances = null;
708
709 int resultLength = subConcepts.length;
710
711 if (showInstances) {
712
713 instances = superConcept.listInstances().toArray();
714
715 resultLength+= instances.length;
716
717 }
718
719
720
721 Object[] result = new Object[resultLength];
722
723
724
725 if (subConcepts.length > 0) {
726
727 System.arraycopy(subConcepts, 0, result, 0, subConcepts.length);
728
729 }
730
731 if (showInstances && instances.length > 0) {
732
733 System.arraycopy(instances, 0, result, subConcepts.length, instances.length);
734
735 }
736
737
738
739 return result;
740
741 }
742
743
744
745 private Object[] createRelationContent(Relation parentRel) {
746
747 Object[] subRels = parentRel.listSubRelations().toArray();
748
749 if (!isShowingRelationInstances()) {
750
751 return subRels;
752
753 }
754
755 Object[] relInsts = parentRel.listRelationInstances().toArray();
756
757 if (relInsts.length == 0) {
758
759 return subRels;
760
761 }
762
763
764
765 Object[] result = new Object[subRels.length + relInsts.length];
766
767
768
769 if (subRels.length > 0) {
770
771 System.arraycopy(subRels, 0, result, 0, subRels.length);
772
773 }
774
775 System.arraycopy(relInsts, 0, result, subRels.length, relInsts.length);
776
777 return result;
778
779 }
780
781
782
783 @SuppressWarnings("unchecked")
784
785 private Object[] findTopRelations(Ontology ref) {
786
787 Set<Relation> allRelations = ref.listRelations();
788
789 Set<Relation> topRelations = new HashSet<Relation>();
790
791
792
793 for (Relation rel : allRelations) {
794
795 if (filterExternalSuperEntities(ref, rel.listSuperRelations()).size() == 0) {
796
797 topRelations.add(rel);
798
799 }
800
801 }
802
803 return topRelations.toArray();
804
805 }
806
807
808
809 @SuppressWarnings("unchecked")
810
811 private Object[] findUnboundInstances(Ontology ref) {
812
813 Set allInstances = ref.listInstances();
814
815 Set result = new HashSet();
816
817 for(Iterator it = allInstances.iterator(); it.hasNext();) {
818
819 Instance inst = (Instance)it.next();
820
821 if (filterExternalSuperEntities(ref, inst.listConcepts()).size() == 0) {
822
823 result.add(inst);
824
825 }
826
827 }
828
829 return result.toArray();
830
831 }
832
833
834
835 public static Set filterExternalSuperEntities(Ontology onto, Set all) {
836
837 Set<OntologyElement> result = new HashSet<OntologyElement>();
838
839 for(Iterator it = all.iterator(); it.hasNext(); ) {
840
841 OntologyElement el = (OntologyElement)it.next();
842
843 if (false == Utils.isAProxy(el)
844
845 && el.getOntology() != null
846
847 && el.getOntology().equals(onto)) {
848
849 result.add(el);
850
851 }
852
853 }
854
855
856
857 return result;
858
859 }
860
861
862
863 private Object[] createOntologyContent(Ontology owner) {
864
865 Object[] concepts = null;
866
867 Object[] rels = null;
868
869 Object[] instances = null;
870
871 Set axioms = null;
872
873 int resultLength = 0;
874
875 int offset = 0;
876
877 boolean showConcepts = (filterMask & CONCEPT_MASK) != 0;
878
879 boolean showRelations = (filterMask & RELATION_MASK) != 0;
880
881 boolean showAxioms = (filterMask & AXIOM_MASK) != 0;
882
883 boolean showInstances = (filterMask & INSTANCE_MASK) != 0;
884
885
886
887 if (showConcepts) {
888
889 concepts = findTopConcepts(owner);
890
891 resultLength += concepts.length;
892
893 }
894
895 if (showInstances) {
896
897 instances = findUnboundInstances(owner);
898
899 resultLength += instances.length;
900
901 }
902
903 if (showRelations) {
904
905 rels = findTopRelations(owner);
906
907 resultLength += rels.length;
908
909 }
910
911 if (showAxioms) {
912
913 axioms = owner.listAxioms();
914
915 resultLength += axioms.size();
916
917 }
918
919
920
921 Object[] result = new Object[resultLength];
922
923 if (showConcepts && concepts.length > 0) {
924
925 System.arraycopy(concepts, 0, result, 0, concepts.length);
926
927 offset += concepts.length;
928
929 }
930
931 if (showInstances && instances.length > 0) {
932
933 System.arraycopy(instances, 0, result, offset, instances.length);
934
935 offset += instances.length;
936
937 }
938
939 if (showRelations && rels.length > 0) {
940
941 System.arraycopy(rels, 0, result, offset, rels.length);
942
943 offset += rels.length;
944
945 }
946
947 if (showAxioms && axioms.size() > 0) {
948
949 System.arraycopy(axioms.toArray(), 0, result, offset, axioms.size());
950
951 }
952
953 return result;
954
955 }
956
957
958
959 private boolean isShowingTopEntityContent() {
960
961 return isShowingAxioms()
962
963 || isShowingConcepts()
964
965 || isShowingRelations()
966
967 || isShowingRelationInstances()
968
969 || isShowingInterfaces()
970
971 || isShowingCapability();
972
973 }
974
975
976
977 public boolean isShowingConcepts() {
978
979 return (filterMask & CONCEPT_MASK) != 0;
980
981 }
982
983
984
985 public boolean isShowingRelations() {
986
987 return (filterMask & RELATION_MASK) != 0;
988
989 }
990
991
992
993 public boolean isShowingAxioms() {
994
995 return (filterMask & AXIOM_MASK) != 0;
996
997 }
998
999
1000
1001 public boolean isShowingRelationInstances() {
1002
1003 return (filterMask & RELATION_INSTANCE_MASK) != 0;
1004
1005 }
1006
1007
1008
1009 public boolean isShowingCapability() {
1010
1011 return (filterMask & CAPABILITY_MASK) != 0;
1012
1013 }
1014
1015
1016
1017 public boolean isShowingInterfaces() {
1018
1019 return (filterMask & INTERFACE_MASK) != 0;
1020
1021 }
1022
1023
1024
1025 public boolean isShowingChors() {
1026
1027 return (filterMask & CHOR_MASK) != 0;
1028
1029 }
1030
1031
1032
1033 public boolean isShowingOrch() {
1034
1035 return (filterMask & ORCH_MASK) != 0;
1036
1037 }
1038
1039
1040
1041
1042
1043 private int checkValidMask(int mask) {
1044
1045 if (mask == NO_FILTER) {
1046
1047 return mask;
1048
1049 }
1050
1051
1052
1053 if ((mask & INSTANCE_MASK) != 0) {
1054
1055 mask = mask | CONCEPT_MASK;
1056
1057 }
1058
1059
1060
1061 if ((mask & RELATION_INSTANCE_MASK) != 0) {
1062
1063 mask = mask | RELATION_MASK;
1064
1065 }
1066
1067
1068
1069 if (((mask & CONCEPT_MASK) != 0)
1070
1071 || ((mask & INSTANCE_MASK) != 0)
1072
1073 || ((mask & RELATION_MASK) != 0)
1074
1075 || ((mask & AXIOM_MASK) != 0)
1076
1077 || ((mask & RELATION_INSTANCE_MASK) != 0)) {
1078
1079 mask = mask | ONTOLOGY_MASK;
1080
1081 }
1082
1083
1084
1085 return mask;
1086
1087 }
1088
1089
1090
1091 private Object[] findAppropMediators() {
1092
1093 LinkedList<Entity> result = new LinkedList<Entity>();
1094
1095 if ((filterMask & OO_MEDIATOR_MASK) != 0) {
1096
1097 result.addAll(WSMORuntime.getCache().getOOMediators());
1098
1099 }
1100
1101 if ((filterMask & WW_MEDIATOR_MASK) != 0) {
1102
1103 result.addAll(WSMORuntime.getCache().getWWMediators());
1104
1105 }
1106
1107 if ((filterMask & WG_MEDIATOR_MASK) != 0) {
1108
1109 result.addAll(WSMORuntime.getCache().getWGMediators());
1110
1111 }
1112
1113 if ((filterMask & GG_MEDIATOR_MASK) != 0) {
1114
1115 result.addAll(WSMORuntime.getCache().getGGMediators());
1116
1117 }
1118
1119 return result.toArray();
1120
1121 }
1122
1123 }
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174