Types.jrag 8.46 KB
Newer Older
1
2
/* -*-Java-*- */

3
/* 
4
 * Copyright (C) 2006,2010  Anders Nilsson <anders.nilsson@control.lth.se>
5
6
 *
 * This file is part of OntologyCompiler.
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * OntologyCompiler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * OntologyCompiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with OntologyCompiler.  If not, see <http://www.gnu.org/licenses/>.
20
21
 */

22
23
import java.util.ArrayList;

24
aspect Types {
Anders Nilsson's avatar
Update    
Anders Nilsson committed
25
26
27
28
29
30
    syn lazy Declaration Declaration.getSuperClass() = null;
	eq ClassDeclaration.getSuperClass() {
		return goStart().getSuperClass(name());
	}

	syn Declaration ASTNode.getSuperClass(String name) = null;
Anders Nilsson's avatar
Anders Nilsson committed
31
	eq Start.getSuperClass(String name) =
Anders Nilsson's avatar
Update    
Anders Nilsson committed
32
		getSpecification().getSuperClass(name);
Anders Nilsson's avatar
Anders Nilsson committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	eq Specification.getSuperClass(String name) {
		Declaration decl = getOntoDeclaration().getSuperClass(name);
		if (decl == null) {
			decl = findDecl("Thing");
			if (decl == null) {
				decl = new ClassDeclaration();
				Clazz c = new Clazz();
				c.addAttribute(new IRI(new Value("Thing")));
				decl.addElement(c);
				getOntoDeclaration().addElement(decl);
			}
		}
		return decl;
	}
Anders Nilsson's avatar
Update    
Anders Nilsson committed
47
	eq ComplexElement.getSuperClass(String name) {
Anders Nilsson's avatar
Anders Nilsson committed
48
		for (Element e : getElements()) {
Anders Nilsson's avatar
Update    
Anders Nilsson committed
49
50
51
52
53
54
55
			Declaration d = e.getSuperClass(name);
			if (d != null) {
				return d;
			}
		}
		return null;
	}
Anders Nilsson's avatar
Anders Nilsson committed
56
57
58
59
60
61
	eq SubClassOf.getSuperClass(String name) {
		if (getElement(0).name().equals(name)) {
			return goStart().findDecl(getElement(1).name());
		}
		return null;
	}
Anders Nilsson's avatar
Update    
Anders Nilsson committed
62

Anders Nilsson's avatar
Anders Nilsson committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	syn Declaration ASTNode.findDecl(String name) = null;
	eq Start.findDecl(String name) =
		getSpecification().findDecl(name);
	eq Specification.findDecl(String name) = getOntoDeclaration().findDecl(name);
	eq ComplexElement.findDecl(String name) {
		for (Element e : getElements()) {
			Declaration d = e.findDecl(name);
			if (d != null) {
				return d;
			}
		}
		return null;
	}
	eq ClassDeclaration.findDecl(String name) {
		if (name.equals(name())) {
			return this;
		}
		return null;
	}
Anders Nilsson's avatar
Update    
Anders Nilsson committed
82

83
    syn lazy OClass OClass.getSuperClass();
84
    eq OwlClassDecl.getSuperClass() {
85
86
87
88
89
90
91
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsSubClassOf) {
				RdfsSubClassOf e = (RdfsSubClassOf) getElement(i);
				if (e.getElement(0) instanceof OClass) {
					return ((OClass) e.getElement(0)).decl();
				}
			}
92
		}
93
94
		// No super class found, return an owl Thing instead.
		return new OwlClassDecl(new List(), new List(), "Thing");
95
96
    }
    eq OwlClassUse.getSuperClass() {
97
		return decl().getSuperClass();
98
99
    }

100
    syn lazy OwlClassDecl OClass.decl();
101
102
    eq OwlClassDecl.decl() = this;
    eq OwlClassUse.decl() {
103
104
		ComplexElement top = getTopElement();
		for (int i=0; i<top.getNumElement(); i++) {
105
// 			System.out.println(getId()+" == "+((ComplexElement) top.getElement(i)).getId());
106
107
			if ((top.getElement(i) instanceof ComplexElement) &&
				getId().equals(((ComplexElement) top.getElement(i)).getId())) {
108
109
110
111
112
113
114
115
				return (OwlClassDecl) top.getElement(i);
			}
		}
		// OK, then let's see if this class is declared by w3.org
		String id = getId();
		if (id.endsWith("Thing")) {
			return new OwlClassDecl(new List(), new List(), "Thing");
		}
Anders Nilsson's avatar
Anders Nilsson committed
116
117
118
119
120
		// No decl found, so let's alert user. If the reason for a
		// missing decl is that it is a top level class which Protege
		// for some unknown reason no longer explicitly declares on
		// top level, the right thing to do would be to create a class
		// declaration here. And that we do.
121
        System.out.println("* Couldn't find decl for: "+getId());
Anders Nilsson's avatar
Anders Nilsson committed
122
123
124
		OwlClassDecl nClass = new OwlClassDecl(new List(), new List(), id);
		top.addElement(nClass);
		return nClass;
125
    }
126

Anders Nilsson's avatar
Anders Nilsson committed
127
    syn lazy Properties OwlClassDecl.getProperties() {
128
		List l = new List();
Anders Nilsson's avatar
Anders Nilsson committed
129
130
131
132
133
134
135
136
137
138
139
140
141
		getTopElement().collectProperties(l);
// 		collectProperties(l);
// 		return new Properties(l);
		Properties p = new Properties();
		for (int i=0; i<l.getNumChild(); i++) {
			OwlProperty op = (OwlProperty) l.getChild(i);
			if (op.domainIncludes(this)) {
				p.addProperty(op);
			} else {
// 				System.out.println(op);
			}
		}
		return p;
142
    }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

	syn lazy ArrayList OClass.getSubClasses();
	eq OwlClassUse.getSubClasses() = decl().getSubClasses();
	eq OwlClassDecl.getSubClasses() {
		return goStart().getSubClasses(this,new ArrayList());
	}

	syn ArrayList ASTNode.getSubClasses(OwlClassDecl klass, ArrayList l) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).getSubClasses(klass,l);
		}
		return l;
	}
	eq OwlClassDecl.getSubClasses(OwlClassDecl klass, ArrayList l) {
		if (getSuperClass() == klass) {
			l.add(this);
		}
		return l;
	}
162
163
}

164
165
aspect Restrictions {
  
166
	Restrictions OwlClassDecl.restrictions;
167
	Restrictions OwlClassDecl.ownRestrictions;
168
  
169
170
171
172
173
174
175
176
	syn lazy Restrictions OwlClassDecl.getRestrictions() {
		if (restrictions == null) {
			List l = new List();
			collectRestrictions(l);
			restrictions = new Restrictions(l);
		}
		return restrictions;
	}
177

178
179
180
181
182
	void ASTNode.collectRestrictions(List l) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectRestrictions(l);
		}
	}
183
184
185
186
187
188
189

	void OwlClassDecl.collectRestrictions(List l) {
		if (!getSuperClass().getId().equals("Thing")) {
			getSuperClass().decl().collectRestrictions(l);
		}
		super.collectRestrictions(l);
	}
190
  
191
	void OwlRestriction.collectRestrictions(List l) {
192
193
194
		if (allValuesFrom()) {
			l.add(this);
		}
195
196
197
	}
	
	syn int OwlClassDecl.getNumRestriction() = getRestrictions().getNumOwlRestriction();
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

	syn lazy Restrictions OwlClassDecl.getOwnRestrictions() {
		if (ownRestrictions == null) {
			List l = new List();
			collectOwnRestrictions(l);
			ownRestrictions = new Restrictions(l);
		}
		return ownRestrictions;
	}

	void ASTNode.collectOwnRestrictions(List l) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectOwnRestrictions(l);
		}
	}

	void OwlRestriction.collectOwnRestrictions(List l) {
215
216
217
		if (allValuesFrom()) {
			l.add(this);
		}
218
219
220
221
	}
	
	syn int OwlClassDecl.getNumOwnRestriction() = 
		getOwnRestrictions().getNumOwlRestriction();
222
223
}

224
aspect Properties {
Anders Nilsson's avatar
Anders Nilsson committed
225
    void ASTNode.collectProperties(List l) {
226
227
228
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectProperties(l);
		}
229
230
    }

Anders Nilsson's avatar
Anders Nilsson committed
231
    void OwlProperty.collectProperties(List l) {
232
		l.add(this);
233
234
    }

235
    boolean ASTNode.domainIncludes(OClass clazz) {
236
		return false;
237
238
    }

Anders Nilsson's avatar
Anders Nilsson committed
239
    boolean OwlObjectProperty.domainIncludes(OClass clazz) {
240
241
242
243
244
245
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return ((RdfsDomain) getElement(i)).domainIncludes(clazz);
			}
		}
		return false;
246
    }
247

Anders Nilsson's avatar
Anders Nilsson committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    boolean OwlFunctionalProperty.domainIncludes(OClass clazz) {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return ((RdfsDomain) getElement(i)).domainIncludes(clazz);
			}
		}
		return false;
    }

    boolean OwlDatatypeProperty.domainIncludes(OClass clazz) {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return ((RdfsDomain) getElement(i)).domainIncludes(clazz);
			}
		}
		return false;
    }

266
    boolean RdfsDomain.domainIncludes(OClass clazz) {
267
268
269
270
271
272
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;
273
274
    }

275
    boolean OwlUnionOf.domainIncludes(OClass clazz) {
276
277
278
279
280
281
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;
282
283
    }

284
    boolean OClass.domainIncludes(OClass clazz) {
285
286
287
288
289
290
291
292
293
		if (!getId().equals("_Unknown_")) {
			return getId().equals(clazz.getId());
		}
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;	
294
295
296
297
    }

    syn lazy RdfsRange Element.range() = null;
    
Anders Nilsson's avatar
Anders Nilsson committed
298
    eq OwlObjectProperty.range() {
299
300
301
302
303
304
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
		return null;
305
    }
Anders Nilsson's avatar
Anders Nilsson committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    eq OwlFunctionalProperty.range() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
		return null;
    }
    eq OwlDatatypeProperty.range() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
		return null;
    }
Anders Nilsson's avatar
Anders Nilsson committed
322
}