View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
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   * Implementation of OWL MaxCardinality Restriction: it contains all individuals
33   * that are connected by the specified property to at most <code>max</code>
34   * individuals that are instances of the specified class expression.
35   * 
36   * @author Carsten Stockloew
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      /** Standard constructor for exclusive use by serializers. */
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  	// setOnProperty(propURI);
68  	// super.setProperty(PROP_OWL_MAX_QUALIFIED_CARDINALITY, new
69  	// Integer(value));
70      }
71  
72      public String getClassURI() {
73  	return MY_URI;
74      }
75  
76      /** Get the value of this cardinality restriction */
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      /** @see org.universAAL.middleware.owl.TypeExpression#copy() */
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  	// ttl =
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     /** @see org.universAAL.middleware.owl.TypeExpression#isWellFormed() */
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     /** @see org.universAAL.middleware.rdf.Resource#setProperty(String, Object) */
158     public boolean setProperty(String propURI, Object o) {
159 	if (o == null || propURI == null || props.containsKey(propURI))
160 	    return false;
161 
162 	// handle this restriction
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 	// do not handle other restrictions
179 	if (propMap.containsKey(propURI))
180 	    return false;
181 
182 	// for everything else: call super
183 	return super.setProperty(propURI, o);
184     }
185 }