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.HashMap;
23 import java.util.List;
24
25 import org.universAAL.middleware.container.utils.LogUtils;
26 import org.universAAL.middleware.datarep.SharedResources;
27 import org.universAAL.middleware.rdf.Resource;
28 import org.universAAL.middleware.util.MatchLogEntry;
29 import org.universAAL.middleware.xsd.NonNegativeInteger;
30
31
32
33
34
35
36
37
38 public final class MaxCardinalityRestriction extends PropertyRestriction {
39
40 public static final String MY_URI = uAAL_VOCABULARY_NAMESPACE
41 + "MaxCardinalityRestriction";
42
43 public static final String PROP_OWL_MAX_CARDINALITY = OWL_NAMESPACE
44 + "maxCardinality";
45 public static final String PROP_OWL_MAX_QUALIFIED_CARDINALITY = OWL_NAMESPACE
46 + "maxQualifiedCardinality";
47
48
49 MaxCardinalityRestriction() {
50 }
51
52 public MaxCardinalityRestriction(String propURI, int value) {
53 if (propURI == null)
54 throw new NullPointerException();
55 if (value < 0)
56 throw new IllegalArgumentException(
57 "Value of a Max Cardinality Restriction must be non-negative: "
58 + value);
59 setOnProperty(propURI);
60 super.setProperty(PROP_OWL_MAX_CARDINALITY, new NonNegativeInteger(
61 value));
62 }
63
64 public MaxCardinalityRestriction(String propURI, int value,
65 TypeExpression ce) {
66 throw new UnsupportedOperationException("Not yet implemented");
67
68
69
70 }
71
72 public String getClassURI() {
73 return MY_URI;
74 }
75
76
77 public final int getValue() {
78 NonNegativeInteger i = (NonNegativeInteger) props
79 .get(PROP_OWL_MAX_CARDINALITY);
80 if (i == null)
81 return -1;
82 return i.intValue();
83 }
84
85
86 public TypeExpression copy() {
87 return copyTo(new MaxCardinalityRestriction());
88 }
89
90 public boolean hasMember(Object member, HashMap context, int ttl,
91 List<MatchLogEntry> log) {
92
93 checkTTL(ttl);
94 if (member == null)
95 return false;
96
97 Object value = ((Resource) member).getProperty(getOnProperty());
98
99 if (value == null)
100 return true;
101
102 if (!(value instanceof List))
103 return getValue() > 0;
104 else
105 return getValue() >= ((List) value).size();
106 }
107
108 public boolean isDisjointWith(TypeExpression other, HashMap context,
109 int ttl, List<MatchLogEntry> log) {
110 ttl = checkTTL(ttl);
111 if (!(other instanceof PropertyRestriction))
112 return other.isDisjointWith(this, context, ttl, log);
113
114 PropertyRestriction r = (PropertyRestriction) other;
115 Object o = getOnProperty();
116 if (o == null || !o.equals(r.getOnProperty()))
117 return false;
118
119 if (r instanceof MinCardinalityRestriction) {
120 if (getValue() < ((MinCardinalityRestriction) r).getValue())
121 return true;
122 } else if (r instanceof ExactCardinalityRestriction) {
123 if (getValue() < ((ExactCardinalityRestriction) r).getValue())
124 return true;
125 }
126
127 return false;
128 }
129
130
131 public boolean isWellFormed() {
132 return getOnProperty() != null
133 && (hasProperty(PROP_OWL_MAX_CARDINALITY));
134 }
135
136 public boolean matches(TypeExpression subset, HashMap context, int ttl,
137 List<MatchLogEntry> log) {
138 Object noRes = matchesNonRestriction(subset, context, ttl, log);
139 if (noRes instanceof Boolean)
140 return ((Boolean) noRes).booleanValue();
141
142 PropertyRestriction other = (PropertyRestriction) noRes;
143
144 if (other instanceof MaxCardinalityRestriction) {
145 if (getValue() >= ((MaxCardinalityRestriction) other).getValue())
146 return true;
147 } else if (other instanceof ExactCardinalityRestriction) {
148 if (getValue() >= ((ExactCardinalityRestriction) other).getValue())
149 return true;
150 else
151 return false;
152 }
153
154 return false;
155 }
156
157
158 public boolean setProperty(String propURI, Object o) {
159 if (o == null || propURI == null || props.containsKey(propURI))
160 return false;
161
162
163 if (PROP_OWL_MAX_CARDINALITY.equals(propURI)) {
164 if (o instanceof NonNegativeInteger) {
165 return super.setProperty(propURI, o);
166 }
167 LogUtils.logError(
168 SharedResources.moduleContext,
169 MaxCardinalityRestriction.class,
170 "setProperty",
171 new Object[] {
172 "Trying to set the max cardinality with an invalid value: ",
173 o, " of type ", o.getClass().getName(),
174 ". It must be a NonNegativeInteger!" }, null);
175 return false;
176 }
177
178
179 if (propMap.containsKey(propURI))
180 return false;
181
182
183 return super.setProperty(propURI, o);
184 }
185 }