001 package nl.tudelft.tbm.eeni.owl2java.model.jmodel;
002
003 import nl.tudelft.tbm.eeni.owl2java.model.jmodel.utils.LogUtils;
004 import nl.tudelft.tbm.eeni.owl2java.utils.IReporting;
005 import org.apache.commons.logging.Log;
006 import org.apache.commons.logging.LogFactory;
007
008
009 public class JPropertyRepresentation implements IReporting {
010
011 // Also used to capture default representation of a property. In this
012 // case all restrictions are NULL
013
014 private static final String deprecated = "@Deprecated";
015 private static final String suffix = "As";
016
017 @SuppressWarnings("unused")
018 private static Log log = LogFactory.getLog(JPropertyRepresentation.class);
019
020 private JProperty onProperty;
021
022 private boolean hasDefaultPropertyRange = true;
023 private boolean isDefaultDeprecated = false;
024
025 private JAllValuesRestriction allValuesRestriction;
026 private JOtherRestriction otherRestriction;
027 private JCardinalityRestriction cardinalityRestriction;
028
029 public JPropertyRepresentation(JProperty onProperty) {
030 this.onProperty = onProperty;
031 }
032
033 public JPropertyRepresentation clone() {
034 JPropertyRepresentation pr = new JPropertyRepresentation(onProperty);
035 pr.hasDefaultPropertyRange = hasDefaultPropertyRange;
036 pr.isDefaultDeprecated = isDefaultDeprecated;
037 if (allValuesRestriction != null)
038 pr.allValuesRestriction = allValuesRestriction.clone();
039 if (otherRestriction != null)
040 pr.otherRestriction = otherRestriction.clone();
041 if (cardinalityRestriction != null)
042 pr.cardinalityRestriction = cardinalityRestriction.clone();
043 return pr;
044 }
045
046 public boolean equals(Object obj) {
047 if (!equalsIgnoreDeprecated(obj))
048 return false;
049 JPropertyRepresentation pr = (JPropertyRepresentation) obj;
050 if (!(isDefaultDeprecated == pr.isDefaultDeprecated))
051 return false;
052 return true;
053 }
054
055 public boolean equalsIgnoreDeprecated(Object obj) {
056 if (!(obj instanceof JPropertyRepresentation))
057 return false;
058 JPropertyRepresentation pr = (JPropertyRepresentation) obj;
059
060 if (!(onProperty.getMapUri().equals(pr.getOnProperty().getMapUri())))
061 return false;
062
063 // if (!(isDefaultDeprecated == pr.isDefaultDeprecated))
064 // return false;
065
066 if (!(hasDefaultPropertyRange == pr.hasDefaultPropertyRange))
067 return false;
068
069 if (otherRestriction != null) {
070 if (!otherRestriction.equals(pr.getOtherRestriction()))
071 return false;
072 } else {
073 if (pr.getOtherRestriction() != null)
074 return false;
075 }
076
077 if (cardinalityRestriction != null) {
078 if (!(cardinalityRestriction.equalsIgnoreDeprecated(pr.getCardinalityRestriction())))
079 return false;
080 } else {
081 if (pr.getCardinalityRestriction() != null)
082 return false;
083 }
084
085 if (allValuesRestriction != null) {
086 if (!(allValuesRestriction.equals(pr.getAllValuesRestriction())))
087 return false;
088 } else {
089 if (pr.getAllValuesRestriction() != null)
090 return false;
091 }
092 return true;
093 }
094
095 public void setDeprecated(boolean deprecated) {
096 if (cardinalityRestriction == null)
097 isDefaultDeprecated = deprecated;
098 else {
099 cardinalityRestriction.setMultipleDeprecated(true);
100 cardinalityRestriction.setSingleDeprecated(true);
101 }
102
103 }
104
105 public String getJModelReport() {
106 String ret = new String();
107
108 ret += "Property Representation for " + LogUtils.toLogName(onProperty) + "\n";
109 ret += " default range: " + hasDefaultPropertyRange + ", default deprecated: " + isDefaultDeprecated + "\n";
110 if (allValuesRestriction != null)
111 ret += allValuesRestriction.getJModelReport() + "\n";
112 if (otherRestriction != null)
113 ret += otherRestriction.getJModelReport() + "\n";
114 if (cardinalityRestriction != null)
115 ret += cardinalityRestriction.getJModelReport() + "\n";
116 return ret;
117 }
118
119 public JProperty getOnProperty() {
120 return onProperty;
121 }
122
123 public void setOnProperty(JProperty onProperty) {
124 this.onProperty = onProperty;
125 }
126
127 public JAllValuesRestriction getAllValuesRestriction() {
128 return allValuesRestriction;
129 }
130
131 public void setAllValuesRestriction(JAllValuesRestriction allValuesRestriction) {
132 hasDefaultPropertyRange = false;
133 this.allValuesRestriction = allValuesRestriction;
134 }
135
136 public JOtherRestriction getOtherRestriction() {
137 return otherRestriction;
138 }
139
140 public void setOtherRestriction(JOtherRestriction otherRestriction) {
141 hasDefaultPropertyRange = false;
142 this.otherRestriction = otherRestriction;
143 }
144
145 public JCardinalityRestriction getCardinalityRestriction() {
146 return cardinalityRestriction;
147 }
148
149 public void setCardinalityRestriction(JCardinalityRestriction cardinalityRestriction) {
150 hasDefaultPropertyRange = false;
151 this.cardinalityRestriction = cardinalityRestriction;
152 }
153
154 public boolean isMultipleDeprecated() {
155 if (cardinalityRestriction == null) {
156 if (!onProperty.isFunctional())
157 return true;
158 else
159 return isDefaultDeprecated;
160 }
161 return cardinalityRestriction.isMultipleDeprecated();
162 }
163
164 public String getMultipleDeprecated() {
165 if (isMultipleDeprecated())
166 return deprecated;
167 return "";
168 }
169
170 public String getSingleDeprecated() {
171 if (isSingleDeprecated())
172 return deprecated;
173 return "";
174 }
175
176 public boolean isMultipleEnabled() {
177 if (cardinalityRestriction == null || cardinalityRestriction.isEmpty == true)
178 return (!onProperty.isFunctional());
179 return cardinalityRestriction.isMultipleEnabled();
180 }
181
182 public boolean isSingleDeprecated() {
183 if (cardinalityRestriction == null)
184 if (onProperty.isFunctional())
185 return true;
186 else
187 return isDefaultDeprecated;
188 else
189 return cardinalityRestriction.isSingleDeprecated();
190 }
191
192 public boolean isSingleEnabled() {
193 if (cardinalityRestriction == null || cardinalityRestriction.isEmpty == true)
194 return (onProperty.isFunctional());
195 return cardinalityRestriction.isSingleEnabled();
196 }
197
198 public String getJavaMethodSuffix() {
199 if (allValuesRestriction == null || allValuesRestriction.isEmpty)
200 return "";
201
202 if (allValuesRestriction.hasAllValues()) {
203 JClass allValues = allValuesRestriction.getAllValues();
204 return suffix + allValues.getJavaClassName();
205 }
206
207 return "";
208
209
210 }
211
212 public String getRangeUri() {
213 if (allValuesRestriction == null) {
214 return onProperty.getRangeUri();
215 }
216
217 if (allValuesRestriction.hasAllValues()) {
218 // object property
219 return "";
220 }
221 return onProperty.getRangeUri();
222 }
223
224 public String getRangeJava() {
225 if (allValuesRestriction == null)
226 return onProperty.getRangeJava();
227
228 if (allValuesRestriction.hasAllValues()) {
229 // return the range as defined by allValues
230 JClass allValues = allValuesRestriction.getAllValues();
231 return allValues.getJavaClassName();
232 }
233 // return the default range of the property
234 return onProperty.getRangeJava();
235 }
236
237 public String getRangeJavaFull() {
238 if (allValuesRestriction == null)
239 return onProperty.getRangeJavaFull();
240
241 if (allValuesRestriction.hasAllValues()) {
242 // return the range as defined by allValues
243 JClass allValues = allValuesRestriction.getAllValues();
244 return allValues.getJavaClassFullName();
245 }
246 // return the default range of the property
247 return onProperty.getRangeJavaFull();
248 }
249
250 public String getInterfaceRangeJava() {
251 if (allValuesRestriction == null)
252 return onProperty.getRangeInterfaceJava();
253
254 if (allValuesRestriction.hasAllValues()) {
255 // return the range as defined by allValues
256 JClass allValues = allValuesRestriction.getAllValues();
257 return allValues.getJavaInterfaceName();
258 }
259 // return the default range of the property
260 return onProperty.getRangeInterfaceJava();
261 }
262
263 public String getRangeInterfaceJavaFull() {
264 if (allValuesRestriction == null)
265 return onProperty.getRangeInterfaceJavaFull();
266
267 if (allValuesRestriction.hasAllValues()) {
268 // return the range as defined by allValues
269 JClass allValues = allValuesRestriction.getAllValues();
270 return allValues.getJavaInterfaceFullName();
271 }
272 // return the default range of the property
273 return onProperty.getRangeInterfaceJavaFull();
274 }
275
276 public String getPropertyType() {
277 return onProperty.getPropertyType();
278 }
279
280 public boolean hasDefaultPropertyRange() {
281 return hasDefaultPropertyRange;
282 }
283
284 public void setHasDefaultPropertyRange(boolean hasDefaultPropertyRange) {
285 this.hasDefaultPropertyRange = hasDefaultPropertyRange;
286 }
287
288 }