1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.universAAL.middleware.owl;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.universAAL.middleware.container.utils.LogUtils;
29 import org.universAAL.middleware.datarep.SharedResources;
30 import org.universAAL.middleware.rdf.Resource;
31 import org.universAAL.middleware.rdf.TypeMapper;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public final class MergedRestriction extends Intersection {
54
55 public static final String MY_URI = uAAL_VOCABULARY_NAMESPACE
56 + "MergedRestriction";
57
58
59 private class SafeIterator implements Iterator {
60 private Iterator it;
61
62 SafeIterator(Iterator it) {
63 this.it = it;
64 }
65
66 public boolean hasNext() {
67 return it.hasNext();
68 }
69
70 public Object next() {
71 return it.next();
72 }
73
74 public void remove() {
75 }
76 }
77
78
79 public static final int MIN_UNDEFINED = 0;
80
81
82 public static final int MAX_UNDEFINED = -1;
83
84
85
86
87
88 public static final int allValuesFromID = 0;
89
90
91
92
93
94 public static final int someValuesFromID = 1;
95
96
97
98
99
100 public static final int hasValueID = 2;
101
102
103
104
105
106 public static final int minCardinalityID = 3;
107
108
109
110
111
112 public static final int maxCardinalityID = 4;
113
114
115
116
117
118 public static final int exactCardinalityID = 5;
119
120
121
122
123
124
125
126
127
128 private int index[] = new int[6];
129
130
131 private String onProperty = null;
132
133
134 public MergedRestriction() {
135 super(new String[] { MY_URI });
136 for (int i = 0; i < 6; i++)
137 index[i] = -1;
138 }
139
140
141
142
143
144
145
146
147
148 public MergedRestriction(String onProperty) {
149 this();
150 this.onProperty = onProperty;
151 }
152
153
154
155
156
157
158
159
160
161
162 public MergedRestriction(String onProperty, ArrayList restrictions) {
163 this();
164 setRestrictions(onProperty, restrictions);
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public static final MergedRestriction getPropertyBanningRestriction(
186 String propURI) {
187 if (propURI == null)
188 return null;
189 MergedRestriction m = new MergedRestriction(propURI);
190 m.addRestriction(new MaxCardinalityRestriction(propURI, 0));
191 return m;
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public static final MergedRestriction getFixedValueRestriction(
216 String propURI, Object value) {
217 if (propURI == null || value == null)
218 return null;
219
220 if (value instanceof String && isQualifiedName((String) value))
221 value = new Resource((String) value);
222
223 MergedRestriction m = new MergedRestriction(propURI);
224 m.addRestriction(new HasValueRestriction(propURI, value));
225 return m;
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 public static final MergedRestriction getCardinalityRestriction(
256 String propURI, int min, int max) {
257 if (propURI == null)
258 return null;
259 if (max > -1 && max < min) {
260 LogUtils.logDebug(
261 SharedResources.moduleContext,
262 MergedRestriction.class,
263 "getCardinalityRestriction",
264 new String[] { "Can not create the restriction because of invalid input parameters: "
265 + "the maximum cardinality is smaller than the minimum cardinality; this does not make sense." },
266 null);
267 return null;
268 }
269 if (max < 0 && min < 1) {
270 LogUtils.logDebug(
271 SharedResources.moduleContext,
272 MergedRestriction.class,
273 "getCardinalityRestriction",
274 new String[] { "Can not create the restriction because of invalid input parameters: "
275 + "both maximum cardinality and minimum cardinality are undefined. This is not a restriction because everything is allowed." },
276 null);
277 return null;
278 }
279
280 MergedRestriction ret = new MergedRestriction(propURI);
281
282 if (min > 0 && min == max) {
283 ret.addRestriction(new ExactCardinalityRestriction(propURI, min));
284 } else {
285 if (min > 0)
286 ret.addRestriction(new MinCardinalityRestriction(propURI, min));
287 if (max >= 0)
288 ret.addRestriction(new MaxCardinalityRestriction(propURI, max));
289 }
290
291 return ret;
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 public static final MergedRestriction getAllValuesRestrictionWithCardinality(
326 String propURI, String typeURI, int min, int max) {
327 if (typeURI == null)
328 return null;
329
330 TypeURI type = null;
331
332 if (TypeMapper.isRegisteredDatatypeURI(typeURI))
333 type = new TypeURI(typeURI, true);
334 else if (OntologyManagement.getInstance().isRegisteredClass(typeURI,
335 true))
336 type = new TypeURI(typeURI, false);
337
338 if (type == null) {
339 LogUtils.logDebug(
340 SharedResources.moduleContext,
341 MergedRestriction.class,
342 "getAllValuesRestrictionWithCardinality",
343 new String[] { "Can not create the restriction because of invalid input parameters: "
344 + "the specified type URI ("
345 + typeURI
346 + ") is not registered. It is neither a data type nor a registered ontology class." },
347 null);
348 return null;
349 }
350
351 return getAllValuesRestrictionWithCardinality(propURI, type, min, max);
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 public static final MergedRestriction getAllValuesRestrictionWithCardinality(
386 String propURI, TypeExpression expr, int min, int max) {
387 if (expr == null)
388 return null;
389
390 MergedRestriction ret = new MergedRestriction(propURI);
391 if (min > 0 || max >= 0)
392 ret.addRestriction(MergedRestriction.getCardinalityRestriction(
393 propURI, min, max));
394 ret.addRestriction(new AllValuesFromRestriction(propURI, expr));
395
396 return ret;
397 }
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 public static final MergedRestriction getAllValuesRestriction(
419 String propURI, String typeURI) {
420 if (typeURI == null || propURI == null)
421 return null;
422
423 TypeURI type = null;
424
425 if (TypeMapper.isRegisteredDatatypeURI(typeURI))
426 type = new TypeURI(typeURI, true);
427 else if (OntologyManagement.getInstance().isRegisteredClass(typeURI,
428 true))
429 type = new TypeURI(typeURI, false);
430
431 if (type == null) {
432 LogUtils.logDebug(
433 SharedResources.moduleContext,
434 MergedRestriction.class,
435 "getAllValuesRestriction",
436 new String[] { "Can not create the restriction because of invalid input parameters: "
437 + "the specified type URI ("
438 + typeURI
439 + ") is not registered. It is neither a data type nor a registered ontology class." },
440 null);
441 return null;
442 }
443
444 MergedRestriction ret = new MergedRestriction(propURI);
445 ret.addRestriction(new AllValuesFromRestriction(propURI, type));
446 return ret;
447 }
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468 public static final MergedRestriction getAllValuesRestriction(
469 String propURI, TypeExpression expr) {
470 if (expr == null || propURI == null)
471 return null;
472
473 MergedRestriction ret = new MergedRestriction(propURI);
474 ret.addRestriction(new AllValuesFromRestriction(propURI, expr));
475 return ret;
476 }
477
478
479
480
481
482
483
484
485
486
487
488
489
490 public static ArrayList getFromList(List o) {
491
492
493
494
495
496
497 HashMap map = new HashMap();
498 Object tmp;
499
500
501 for (int i = 0; i < o.size(); i++) {
502 tmp = o.get(i);
503 if (tmp instanceof PropertyRestriction) {
504 PropertyRestriction res = (PropertyRestriction) tmp;
505 ArrayList a = (ArrayList) map.get(res.getOnProperty());
506 if (a == null)
507 map.put(res.getOnProperty(), a = new ArrayList());
508 a.add(res);
509 }
510 }
511
512
513 ArrayList ret = new ArrayList();
514 Iterator it = map.keySet().iterator();
515 while (it.hasNext()) {
516 String prop = (String) it.next();
517 ArrayList a = (ArrayList) map.get(prop);
518 MergedRestriction m = new MergedRestriction(prop, a);
519 ret.add(m);
520 }
521 return ret;
522 }
523
524
525
526
527
528
529
530 private void reset() {
531 types.clear();
532 onProperty = null;
533 for (int i = 0; i < index.length; i++)
534 index[i] = -1;
535 }
536
537
538
539
540
541
542
543
544
545
546 public int getMinCardinality() {
547 if (index[minCardinalityID] != -1)
548 return ((MinCardinalityRestriction) (types
549 .get(index[minCardinalityID]))).getValue();
550 if (index[exactCardinalityID] != -1)
551 return ((ExactCardinalityRestriction) (types
552 .get(index[exactCardinalityID]))).getValue();
553 return MIN_UNDEFINED;
554 }
555
556
557
558
559
560
561
562
563
564
565 public int getMaxCardinality() {
566 if (index[maxCardinalityID] != -1)
567 return ((MaxCardinalityRestriction) (types
568 .get(index[maxCardinalityID]))).getValue();
569 if (index[exactCardinalityID] != -1)
570 return ((ExactCardinalityRestriction) (types
571 .get(index[exactCardinalityID]))).getValue();
572 return MAX_UNDEFINED;
573 }
574
575
576
577
578
579
580
581
582
583
584
585 public Object getConstraint(int id) {
586 if (index[id] != -1)
587 return ((PropertyRestriction) types.get(index[id])).getConstraint();
588 return null;
589 }
590
591
592
593
594
595
596
597
598
599
600
601
602
603 private void setRestrictions(String onProperty, ArrayList restrictions) {
604 if (restrictions == null || onProperty == null)
605 throw new NullPointerException();
606 reset();
607 types.addAll(restrictions);
608 this.onProperty = onProperty;
609 try {
610 analyze();
611 } catch (IllegalArgumentException e) {
612 reset();
613 throw e;
614 }
615 }
616
617
618
619
620
621
622
623
624 public List getRestrictions() {
625 return Collections.unmodifiableList(types);
626 }
627
628
629
630
631
632
633
634 private void removeRestriction(int id) {
635 int index = this.index[id];
636 if (index < 0 || index >= types.size())
637 return;
638
639
640 types.remove(index);
641
642
643 for (int i = 0; i < this.index.length; i++)
644 if (this.index[i] > index)
645 this.index[i]--;
646 }
647
648
649
650
651
652
653
654
655 public PropertyRestriction getRestriction(int id) {
656 if (index[id] == -1)
657 return null;
658 return (PropertyRestriction) types.get(index[id]);
659 }
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677 public MergedRestriction addRestriction(PropertyRestriction res) {
678 addRestrictionCheck(res);
679 return this;
680 }
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698 public boolean addRestrictionCheck(PropertyRestriction res) {
699
700
701
702
703
704 if (res == null)
705 return false;
706
707 if (onProperty == null) {
708 onProperty = res.getOnProperty();
709 } else {
710 if (!onProperty.equals(res.getOnProperty()))
711 throw new IllegalArgumentException(
712 "Trying to add a restriction for a different property. All restrictions of a MergedRestriction must be defined for the same property.");
713 }
714
715 int max = getMaxCardinality();
716 if (max == 0) {
717 LogUtils.logDebug(
718 SharedResources.moduleContext,
719 MergedRestriction.class,
720 "addRestriction",
721 new String[] { "Can not add the PropertyRestriction ("
722 + res.getType()
723 + ") because the maximum cardinality is 0 (no additional restriction is allowed)." },
724 null);
725 return false;
726 }
727
728
729
730 int id = getID(res);
731
732 PropertyRestriction all = getRestriction(allValuesFromID);
733 PropertyRestriction some = getRestriction(someValuesFromID);
734
735 switch (id) {
736 case allValuesFromID:
737 if (all == null
738 && (some == null || (max != 1 && res.matches(some, null,
739 getDefaultMatchmakingTTL(), null)))) {
740 index[allValuesFromID] = types.size();
741 types.add(res);
742 return true;
743 } else {
744 if (all != null)
745 LogUtils.logDebug(
746 SharedResources.moduleContext,
747 MergedRestriction.class,
748 "addRestriction",
749 new String[] { "Can not add the AllValuesFromRestriction because such a restriction is already set." },
750 null);
751 else
752 LogUtils.logDebug(
753 SharedResources.moduleContext,
754 MergedRestriction.class,
755 "addRestriction",
756 new String[] { "Can not add the AllValuesFromRestriction, please check with your SomeValuesFromRestriction." },
757 null);
758 }
759 break;
760 case someValuesFromID:
761 if (some == null
762 && (all == null || ((TypeExpression) all)
763 .matches(res))) {
764 if (all != null && max == 1)
765 removeRestriction(allValuesFromID);
766 index[someValuesFromID] = types.size();
767 types.add(res);
768 return true;
769 }
770 break;
771 case hasValueID:
772 PropertyRestriction has = getRestriction(hasValueID);
773 if (has == null) {
774 if (max != 0) {
775 index[hasValueID] = types.size();
776 types.add(res);
777 return true;
778 } else {
779 LogUtils.logDebug(
780 SharedResources.moduleContext,
781 MergedRestriction.class,
782 "addRestriction",
783 new String[] { "Can not add the HasValueRestriction because the maximum cardinality is set to zero, so no instances are allowed." },
784 null);
785 }
786 } else {
787 LogUtils.logDebug(
788 SharedResources.moduleContext,
789 MergedRestriction.class,
790 "addRestriction",
791 new String[] { "Can not add the HasValueRestriction because such a restriction is already set." },
792 null);
793 }
794 break;
795 case minCardinalityID:
796 int newMin = ((MinCardinalityRestriction) res).getValue();
797 if (getMinCardinality() == MIN_UNDEFINED)
798 if (max == MAX_UNDEFINED || max > newMin) {
799 if (index[minCardinalityID] == -1) {
800 index[minCardinalityID] = types.size();
801 types.add(res);
802 return true;
803 }
804 } else if (max == newMin) {
805 removeRestriction(maxCardinalityID);
806 res = new ExactCardinalityRestriction(onProperty, newMin);
807 if (index[exactCardinalityID] == -1) {
808 index[exactCardinalityID] = types.size();
809 types.add(res);
810 return true;
811 } else {
812 types.set(index[exactCardinalityID], res);
813 return true;
814 }
815 }
816 break;
817 case maxCardinalityID:
818 int newMax = ((MaxCardinalityRestriction) res).getValue();
819 int min = getMinCardinality();
820 if (max == MAX_UNDEFINED
821 && (newMax > 1
822 || (newMax == 1 && (some == null || all == null)) || (newMax == 0
823 && all == null && some == null)))
824 if (min < newMax) {
825 index[maxCardinalityID] = types.size();
826 types.add(res);
827 return true;
828 } else if (min == newMax) {
829 removeRestriction(minCardinalityID);
830 res = new ExactCardinalityRestriction(onProperty, newMax);
831 index[exactCardinalityID] = types.size();
832 types.add(res);
833 return true;
834 }
835 break;
836 case exactCardinalityID:
837 int newExact = ((ExactCardinalityRestriction) res).getValue();
838 if (max == MAX_UNDEFINED
839 && getRestriction(minCardinalityID) == null
840 && (newExact > 1
841 || (newExact == 1 && (some == null || all == null)) || (newExact == 0
842 && all == null && some == null)))
843 if (index[exactCardinalityID] == -1) {
844 index[exactCardinalityID] = types.size();
845 types.add(res);
846 return true;
847 } else {
848 types.set(index[exactCardinalityID], res);
849 return true;
850 }
851 break;
852 }
853 return false;
854 }
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869 public MergedRestriction addRestriction(MergedRestriction r) {
870 ArrayList resList = (ArrayList) r.types;
871 for (int i = 0; i < resList.size(); i++)
872 addRestriction((PropertyRestriction) ((TypeExpression) resList
873 .get(i)).copy());
874 return this;
875 }
876
877
878
879
880
881
882
883
884 private int getID(PropertyRestriction res) {
885 int idx = -1;
886 if (res instanceof AllValuesFromRestriction)
887 idx = allValuesFromID;
888 else if (res instanceof SomeValuesFromRestriction)
889 idx = someValuesFromID;
890 else if (res instanceof HasValueRestriction)
891 idx = hasValueID;
892 else if (res instanceof MinCardinalityRestriction)
893 idx = minCardinalityID;
894 else if (res instanceof MaxCardinalityRestriction)
895 idx = maxCardinalityID;
896 else if (res instanceof ExactCardinalityRestriction)
897 idx = exactCardinalityID;
898 else
899 throw new IllegalArgumentException("Unknown Restriction type: "
900 + res + " (Class URI: " + res.getClassURI() + ")");
901 return idx;
902 }
903
904
905
906
907
908
909
910
911
912 private void analyze() {
913 for (int i = 0; i < types.size(); i++) {
914 if (!(types.get(i) instanceof PropertyRestriction))
915 throw new IllegalArgumentException(
916 "Non-restriction found at index: " + i);
917
918 PropertyRestriction res = (PropertyRestriction) types.get(i);
919 if (!onProperty.equals(res.getOnProperty()))
920 throw new IllegalArgumentException(
921 "Restriction defined for wrong property: "
922 + res.getClassURI() + " " + res.getOnProperty());
923
924 int idx = getID(res);
925 if (idx != -1 && index[idx] != -1)
926 throw new IllegalArgumentException("Duplicate Restriction: "
927 + res.getClassURI());
928 index[idx] = i;
929 }
930 }
931
932
933 public TypeExpression copy() {
934 ArrayList newList = new ArrayList();
935 for (int i = 0; i < types.size(); i++)
936 newList.add(((PropertyRestriction) types.get(i)).copy());
937 return new MergedRestriction(onProperty, newList);
938 }
939
940
941
942
943
944
945
946
947
948
949
950 public MergedRestriction copyWithNewCardinality(int min, int max) {
951 if (max > -1 && max < min)
952 return null;
953 MergedRestriction r = (MergedRestriction) copy();
954 r.removeRestriction(minCardinalityID);
955 r.removeRestriction(maxCardinalityID);
956 r.removeRestriction(exactCardinalityID);
957 r.addRestriction(new MinCardinalityRestriction(getOnProperty(), min));
958 r.addRestriction(new MaxCardinalityRestriction(getOnProperty(), max));
959 return r;
960 }
961
962
963
964
965
966
967
968
969
970
971 public MergedRestriction copyOnNewProperty(String onProp) {
972 MergedRestriction r = (MergedRestriction) copy();
973 for (Iterator i = types.iterator(); i.hasNext();)
974 ((Resource) i.next()).setProperty(
975 PropertyRestriction.PROP_OWL_ON_PROPERTY, onProp);
976 onProperty = onProp;
977 return r;
978 }
979
980
981 public boolean isWellFormed() {
982 for (int i = 0; i < types.size(); i++)
983 if (!((TypeExpression) types.get(i)).isWellFormed())
984 return false;
985 return true;
986 }
987
988
989 public boolean setProperty(String propURI, Object value) {
990 if (Intersection.PROP_OWL_INTERSECTION_OF.equals(propURI)) {
991 List lst = null;
992 if (value instanceof List) {
993 lst = (List) value;
994 } else if (value instanceof PropertyRestriction) {
995
996
997
998 return addRestrictionCheck((PropertyRestriction) value);
999 } else if (value instanceof MergedRestriction) {
1000 lst = ((MergedRestriction) value).getRestrictions();
1001 }
1002
1003 if (lst != null) {
1004 boolean res = true;
1005 boolean tmp;
1006 for (Object el : lst) {
1007 if (el instanceof PropertyRestriction) {
1008 tmp = addRestrictionCheck((PropertyRestriction) el);
1009 res = res && tmp;
1010 }
1011 }
1012 return res;
1013 }
1014 }
1015 return false;
1016 }
1017
1018
1019 public Iterator types() {
1020
1021
1022 return new SafeIterator(types.iterator());
1023 }
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 public String getPropTypeURI() {
1034 TypeExpression all = null;
1035
1036 if (index[allValuesFromID] == -1)
1037 return null;
1038
1039 all = (TypeExpression) ((AllValuesFromRestriction) types
1040 .get(index[allValuesFromID])).getConstraint();
1041
1042 if (all instanceof TypeURI) {
1043 return ((TypeURI) all).getURI();
1044 }
1045
1046 if (all instanceof TypeRestriction) {
1047 return ((TypeRestriction)all).getTypeURI();
1048 }
1049
1050 return null;
1051 }
1052
1053 public String getOnProperty() {
1054 return onProperty;
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085 public MergedRestriction appendTo(MergedRestriction root, String[] path) {
1086
1087
1088
1089 if (path == null || path.length == 0) {
1090 LogUtils.logDebug(
1091 SharedResources.moduleContext,
1092 MergedRestriction.class,
1093 "appendTo",
1094 new String[] {
1095 "Not possible to append a restriction because the specified path is invalid: ",
1096 path == null ? "path is null." : "path is empty." },
1097 null);
1098 return null;
1099 }
1100 if (getOnProperty() == null) {
1101 LogUtils.logDebug(
1102 SharedResources.moduleContext,
1103 MergedRestriction.class,
1104 "appendTo",
1105 new String[] { "Not possible to append a restriction because it does not define a property for which this restriction applies: the onProperty must be set." },
1106 null);
1107 return null;
1108 }
1109 if (!getOnProperty().equals(path[path.length - 1])) {
1110 LogUtils.logDebug(
1111 SharedResources.moduleContext,
1112 MergedRestriction.class,
1113 "appendTo",
1114 new String[] { "Not possible to append a restriction because "
1115 + "the restriction is not defined for the property at the end of the property path: the onProperty value of the restriction must correspond to the last element of the property path." },
1116 null);
1117 return null;
1118 }
1119 if (path.length == 1)
1120 if (root == null) {
1121 return this;
1122 } else {
1123
1124
1125 root.addRestriction(this);
1126 return root;
1127 }
1128 if (root == null) {
1129 root = new MergedRestriction(path[0]);
1130 PropertyRestriction r = new AllValuesFromRestriction();
1131 r.setProperty(PropertyRestriction.PROP_OWL_ON_PROPERTY, path[0]);
1132 root.addRestriction(r);
1133 } else {
1134
1135
1136 if (!root.getOnProperty().equals(path[0])) {
1137 LogUtils.logDebug(
1138 SharedResources.moduleContext,
1139 MergedRestriction.class,
1140 "appendTo",
1141 new String[] { "Not possible to append a new restriction to an existing restriction because "
1142 + "the existing restriction is defined for a different property than what is specified in the property path:\n"
1143 + "the existing restriction restriction is defined for property "
1144 + root.getOnProperty()
1145 + "\nand the property path starts with "
1146 + path[0] }, null);
1147 return null;
1148 }
1149 }
1150
1151
1152 PropertyRestriction tmp = root.getRestriction(allValuesFromID);
1153 if (tmp == null) {
1154
1155
1156 tmp = new AllValuesFromRestriction();
1157 tmp.setOnProperty(path[0]);
1158 root.addRestriction(tmp);
1159 }
1160 tmp = root.getRestriction(allValuesFromID);
1161 if (tmp == null) {
1162 LogUtils.logDebug(
1163 SharedResources.moduleContext,
1164 MergedRestriction.class,
1165 "appendTo",
1166 new String[] { "The root object does not contain an AllValuesFromRestriction and it is not possible to add a new AllValuesFromRestriction. Maybe root already contains a different restriction that prevents the AllValuesFromRestriction from being added (e.g. a SomeValuesFromRestriction or a HasValueRestriction)." },
1167 null);
1168 return null;
1169 }
1170
1171
1172
1173
1174 for (int i = 1; i < path.length - 1; i++)
1175 tmp = tmp.getRestrictionOnProperty(path[i]);
1176
1177
1178
1179
1180
1181
1182 TypeExpression all = (TypeExpression) tmp.getConstraint();
1183 if (all == null && types.size() == 1) {
1184
1185
1186
1187 tmp.changeProperty(
1188 AllValuesFromRestriction.PROP_OWL_ALL_VALUES_FROM,
1189 types.get(0));
1190 } else {
1191
1192
1193
1194 if (!(all instanceof Intersection)) {
1195 Intersection i = new Intersection();
1196 if (all != null)
1197 i.addType(all);
1198 tmp.changeProperty(
1199 AllValuesFromRestriction.PROP_OWL_ALL_VALUES_FROM, i);
1200 all = i;
1201 }
1202
1203
1204
1205 Intersection isec = (Intersection) all;
1206 for (int i = 0; i < types.size(); i++)
1207 isec.addType((TypeExpression) types.get(i));
1208 }
1209 return root;
1210 }
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221 public MergedRestriction getRestrictionOnPath(String[] path) {
1222 if (path == null || path.length == 0
1223 || !getOnProperty().equals(path[0]))
1224 return null;
1225
1226 MergedRestriction tmp = this;
1227 for (int i = 1; i < path.length && tmp != null; i++)
1228 tmp = tmp.getRestrictionOnPathElement(path[i]);
1229 return tmp;
1230 }
1231
1232 private MergedRestriction getRestrictionOnPathElement(String pathElement) {
1233 TypeExpression all = (TypeExpression) getConstraint(allValuesFromID);
1234 if (all instanceof Intersection) {
1235
1236
1237 MergedRestriction m = new MergedRestriction(pathElement);
1238 PropertyRestriction tmpRes;
1239 for (Iterator i = ((Intersection) all).types(); i.hasNext();) {
1240 TypeExpression tmp = (TypeExpression) i.next();
1241 if (tmp instanceof PropertyRestriction) {
1242 tmpRes = (PropertyRestriction) tmp;
1243 if (tmpRes.getOnProperty().equals(pathElement))
1244 m.addRestriction(tmpRes);
1245 }
1246 }
1247 return m;
1248 } else if (all instanceof TypeURI)
1249 return ManagedIndividual.getClassRestrictionsOnProperty(
1250 all.getURI(), pathElement);
1251
1252 if (all instanceof PropertyRestriction) {
1253 PropertyRestriction res = (PropertyRestriction) all;
1254 if (res.getOnProperty().equals(pathElement)) {
1255 MergedRestriction m = new MergedRestriction(pathElement);
1256 m.addRestriction(res);
1257 return m;
1258 }
1259 }
1260
1261 return null;
1262 }
1263
1264
1265
1266
1267
1268
1269 public Object[] getEnumeratedValues() {
1270 int idx;
1271
1272 idx = index[allValuesFromID];
1273 if (idx < 0)
1274 return null;
1275 AllValuesFromRestriction allres = (AllValuesFromRestriction) types
1276 .get(idx);
1277 if (allres != null) {
1278 TypeExpression all = (TypeExpression) allres.getConstraint();
1279 if (all instanceof Enumeration)
1280 return ((Enumeration) all).getUpperEnumeration();
1281 else if (all instanceof TypeURI) {
1282 OntClassInfo info = OntologyManagement.getInstance()
1283 .getOntClassInfo(all.getURI());
1284 return info == null ? null : info.getInstances();
1285 }
1286 }
1287
1288 idx = index[hasValueID];
1289 if (idx < 0)
1290 return null;
1291 HasValueRestriction hasres = (HasValueRestriction) types.get(idx);
1292 if (hasres != null) {
1293 TypeExpression has = (TypeExpression) hasres.getConstraint();
1294 return (has == null) ? null : new Object[] { has };
1295 }
1296
1297 return null;
1298 }
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320 public MergedRestriction merge(MergedRestriction other) {
1321 String onThis = getOnProperty();
1322 if (onThis == null)
1323 return null;
1324
1325 if (other == null)
1326 return this;
1327
1328 String onOther = other.getOnProperty();
1329 if (onOther == null || !onThis.equals(onOther))
1330 return this;
1331
1332 MergedRestriction res = (MergedRestriction) other.copy();
1333 res.addRestriction(this);
1334 return res;
1335 }
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346 public boolean hasMemberIgnoreCardinality(Object o) {
1347 int j = 0;
1348 for (Iterator i = types.iterator(); i.hasNext();) {
1349 if (j != minCardinalityID && j != maxCardinalityID
1350 && j != exactCardinalityID)
1351 if (!((TypeExpression) i.next()).hasMember(o))
1352 return false;
1353 j++;
1354 }
1355 return true;
1356 }
1357 }