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.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  
26  import org.universAAL.middleware.rdf.Resource;
27  import org.universAAL.middleware.util.MatchLogEntry;
28  
29  /**
30   * Implementation of OWL AllValuesFrom Restriction: it contains all individuals
31   * that are connected by the specified property to individuals that are
32   * instances of the specified class expression.
33   * 
34   * @author Carsten Stockloew
35   */
36  public final class AllValuesFromRestriction extends PropertyRestriction {
37  
38      public static final String MY_URI = uAAL_VOCABULARY_NAMESPACE
39  	    + "AllValuesFromRestriction";
40  
41      public static final String PROP_OWL_ALL_VALUES_FROM = OWL_NAMESPACE
42  	    + "allValuesFrom";;
43  
44      /** Standard constructor for exclusive use by serializers. */
45      AllValuesFromRestriction() {
46      }
47  
48      public AllValuesFromRestriction(String propURI, TypeExpression expr) {
49  	if (propURI == null || expr == null)
50  	    throw new NullPointerException();
51  	setOnProperty(propURI);
52  	super.setProperty(PROP_OWL_ALL_VALUES_FROM, expr);
53      }
54  
55      public AllValuesFromRestriction(String propURI, String typeURI) {
56  	this(propURI, TypeURI.asTypeURI(typeURI));
57      }
58  
59      public String getClassURI() {
60  	return MY_URI;
61      }
62  
63      public Object getConstraint() {
64  	return getProperty(PROP_OWL_ALL_VALUES_FROM);
65      }
66  
67      public TypeExpression copy() {
68  	return copyTo(new AllValuesFromRestriction());
69      }
70  
71      public boolean hasMember(Object member, HashMap context, int ttl,
72  	    List<MatchLogEntry> log) {
73  	ttl = checkTTL(ttl);
74  	if (!(member instanceof Resource))
75  	    return member == null;
76  
77  	Object o = ((Resource) member).getProperty(getOnProperty());
78  	if (o == null)
79  	    return true;
80  	if (!(o instanceof List)) {
81  	    List aux = new ArrayList(1);
82  	    aux.add(o);
83  	    o = aux;
84  	}
85  	int size = ((List) o).size();
86  
87  	HashMap cloned = (context == null) ? null : (HashMap) context.clone();
88  	TypeExpression from = (TypeExpression) props
89  		.get(PROP_OWL_ALL_VALUES_FROM);
90  	if (from != null)
91  	    for (int i = 0; i < size; i++)
92  		if (!from.hasMember(((List) o).get(i), cloned, ttl, log))
93  		    return false;
94  
95  	synchronize(context, cloned);
96  	return true;
97      }
98  
99      public boolean isDisjointWith(TypeExpression other, HashMap context,
100 	    int ttl, List<MatchLogEntry> log) {
101 	ttl = checkTTL(ttl);
102 	if (!(other instanceof PropertyRestriction))
103 	    return other.isDisjointWith(this, context, ttl, log);
104 
105 	PropertyRestriction r = (PropertyRestriction) other;
106 	Object o = getOnProperty();
107 	if (o == null || !o.equals(r.getOnProperty()))
108 	    return false;
109 
110 	HashMap cloned = (context == null) ? null : (HashMap) context.clone();
111 
112 	TypeExpression myValues = (TypeExpression) getProperty(PROP_OWL_ALL_VALUES_FROM);
113 	if (myValues != null
114 		&& myValues.isDisjointWith(
115 			(TypeExpression) getProperty(PROP_OWL_ALL_VALUES_FROM),
116 			cloned, ttl, log)) {
117 	    synchronize(context, cloned);
118 	    return true;
119 	}
120 
121 	return false;
122     }
123 
124     public boolean isWellFormed() {
125 	return getOnProperty() != null
126 		&& (hasProperty(PROP_OWL_ALL_VALUES_FROM));
127     }
128 
129     public boolean matches(TypeExpression subset, HashMap context, int ttl,
130 	    List<MatchLogEntry> log) {
131 	Object noRes = matchesNonRestriction(subset, context, ttl, log);
132 	if (noRes instanceof Boolean)
133 	    return ((Boolean) noRes).booleanValue();
134 
135 	PropertyRestriction otherRes = (PropertyRestriction) noRes;
136 
137 	if (otherRes instanceof AllValuesFromRestriction) {
138 	    HashMap cloned = (context == null) ? null : (HashMap) context
139 		    .clone();
140 	    TypeExpression my = (TypeExpression) getProperty(PROP_OWL_ALL_VALUES_FROM);
141 	    TypeExpression other = (TypeExpression) ((AllValuesFromRestriction) otherRes)
142 		    .getProperty(PROP_OWL_ALL_VALUES_FROM);
143 	    if (my != null && other != null) {
144 		if (my.matches(other, cloned, ttl, log)) {
145 		    synchronize(context, cloned);
146 		    return true;
147 		}
148 	    }
149 	}
150 
151 	return false;
152     }
153 
154     public boolean setProperty(String propURI, Object o) {
155 	if (o == null || propURI == null || props.containsKey(propURI))
156 	    return false;
157 
158 	// handle this restriction
159 	if (PROP_OWL_ALL_VALUES_FROM.equals(propURI)) {
160 	    TypeExpression all = (TypeExpression) getProperty(PROP_OWL_ALL_VALUES_FROM);
161 	    if (all != null)
162 		return false;
163 	    Object tmp = TypeURI.asTypeURI(o);
164 	    if (tmp != null)
165 		o = tmp;
166 
167 	    if (!(o instanceof TypeExpression))
168 		return false;
169 	    return super.setProperty(PROP_OWL_ALL_VALUES_FROM, o);
170 	}
171 
172 	// do not handle other restrictions
173 	if (propMap.containsKey(propURI))
174 	    return false;
175 
176 	// for everything else: call super
177 	return super.setProperty(propURI, o);
178     }
179 }