Rewrites.jrag 7.44 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
aspect MiscUtilities {
    boolean Element.isTopLevel() {
24
25
26
27
28
		if ( getParent() != null ) {
			return getParent().getParent() instanceof RdfDeclaration;
		} else {
			return  false;
		}
29
30
    }

31
    syn lazy String Attribute.trim() {
32
33
34
35
36
37
38
39
		String s = getValue().getSTRING_LITERAL();
		if (s.indexOf('"') == 0) {
			s = s.substring(1,s.length()-1).trim();
		} 
		if (s.indexOf('#') > -1) {
			s = s.substring(s.indexOf('#')+1,s.length()).trim();
		} 
		return s.trim();
40
41
42
    }

    eq RdfResource.trim() {
43
44
45
46
47
48
49
50
51
52
53
		String s = getValue().getSTRING_LITERAL();
		if (s.indexOf('"') == 0) {
			s = s.substring(1,s.length()-1).trim();
		} 
		if (s.indexOf('#') > -1) {
			s = s.substring(s.indexOf('#')+1,s.length()).trim();
		} 
		if (s.equals("string")) {
			s = "String";
		}
		return s.trim();
54
55
56
    }

    boolean ComplexElement.hasRdfId() {
57
58
59
60
61
62
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfId) {
				return true;
			}
		}
		return false;
Anders Nilsson's avatar
Update    
Anders Nilsson committed
63

64
65
66
    }

    boolean ComplexElement.hasRdfAbout() {
67
68
69
70
71
72
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfAbout) {
				return true;
			}
		}
		return false;
73
74
    }

75
    boolean ComplexElement.hasRdfResource() {
76
77
78
79
80
81
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfResource) {
				return true;
			}
		}
		return false;
82
83
    }

84
    syn lazy String Element.getId() {
85
		return new String("Element has no ID");
86
87
88
    }

    eq ComplexElement.getId() {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		if (hasRdfId()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfId) {
					return getAttribute(i).trim();
				}
			}
		} else if (hasRdfAbout()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfAbout) {
					return getAttribute(i).trim();
				}
			}
		} else if (hasRdfResource()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfResource) {
					return getAttribute(i).trim();
				}
			}
		}
		// Fall through. Could not find Id, so let's return something
		// well known
		return "_Unknown_";
    }

	eq OwlValuesFrom.getId() {
		if (hasRdfAbout()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfAbout) {
					return getAttribute(i).trim();
				}
			}
		} else if (hasRdfResource()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfResource) {
					return getAttribute(i).trim();
				}
			}
		} else {
			for (int i=0; i<getNumElement(); i++) {
				ComplexElement e = (ComplexElement) getElement(i);
				if (e instanceof OClass) {
					return e.getId();
				}
			}
		}
		// Fall through. Could not find Id, so let's return something
		// well known
		return "_Unknown_";		
	}

	eq OwlProperty.getId() {
		if (hasRdfId()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfId) {
					return getAttribute(i).trim();
				}
			}
		} else if (hasRdfAbout()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfAbout) {
					return getAttribute(i).trim();
				}
			}
		} else if (hasRdfResource()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfResource) {
					return getAttribute(i).trim();
				}
			}
		} else {
			for (int i=0; i<getNumElement(); i++) {
				ComplexElement e = (ComplexElement) getElement(i);
				if (e instanceof OwlProperty) {
					return e.getId();
				}
			}
		}
		// Fall through. Could not find Id, so let's return something
		// well known
		return "_Unknown_";		
169
	}
170

171
172
173
    syn lazy String Element.type() = null;

    eq RdfsRange.type() {
174
175
176
177
178
179
		if (hasRdfResource()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfResource) {
					return getAttribute(i).trim();
				}
			}
180
		}
181
182
183
184
185
186
187
188
189
		for (int i=0; i<getNumElement(); i++) {
			String s = getElement(i).type();
			if (s != null) {
				return s;
			}
		}
		// Fall through. Could not find Id, so let's return something
		// well known
		return "_Unknown_";
190
191
192
    }

    eq OwlDataRange.type() {
193
194
195
196
197
198
199
		for (int i=0; i<getNumElement(); i++) {
			String s = getElement(i).type();
			if (s != null) {
				return s;
			}
		}	
		return null;
200
201
202
    }

    eq OwlOneOf.type() {
203
204
205
206
207
208
209
		for (int i=0; i<getNumElement(); i++) {
			String s = getElement(i).type();
			if (s != null) {
				return s;
			}
		}	
		return null;
210
211
212
    }

    eq RdfFirst.type() {
213
214
215
216
217
218
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfDatatype) {
				return getAttribute(i).trim();
			}
		}
		return null;
219
220
    }

221
    ComplexElement ComplexElement.getTopElement() {
222
223
224
225
226
		ASTNode e = this;
		while (e != null && !(e instanceof RdfDeclaration)) {
			e = e.getParent();
		}
		return (ComplexElement) e;
227
    }
228
229

	syn boolean Declaration.isClassDecl() = getElement(0).isClass();
Anders Nilsson's avatar
Anders Nilsson committed
230
	eq ClassDeclaration.isClassDecl() = false;
231
232
	eq IndividualDeclaration.isClassDecl() = false;
	syn boolean Declaration.isIndividualDecl() = getElement(0).isIndividual();
Anders Nilsson's avatar
Anders Nilsson committed
233
	eq IndividualDeclaration.isIndividualDecl() = false;
234
235
236
237
238
239
240
241
	eq ClassDeclaration.isIndividualDecl() = false;

	syn boolean Element.isClass() = false;
	eq Clazz.isClass() = true;

	syn boolean Element.isIndividual() = false;
	eq NamedIndividual.isIndividual() = true;

242
243
}

244
aspect RewriteClasses {
Anders Nilsson's avatar
Update    
Anders Nilsson committed
245
246
247
248
249
250
251
252

	rewrite Declaration {
		when (isClassDecl())
			to ClassDeclaration {
			ClassDeclaration c = new ClassDeclaration();
			c.setElementList(getElementList());
			return c;
		}
Anders Nilsson's avatar
Anders Nilsson committed
253
254
255
256
257
258
		// when (isIndividualDecl())
		// 	to IndividualDeclaration {
		// 	IndividualDeclaration c = new IndividualDeclaration();
		// 	c.setElementList(getElementList());
		// 	return c;
		// }
Anders Nilsson's avatar
Update    
Anders Nilsson committed
259
260
	}

261
    rewrite OwlClass {
262
263
264
265
		when (isTopLevel())
			to OwlClassDecl {
			return new OwlClassDecl(new List(), getElementList(), getId());
		}
266
267
268
269
// 		when (hasRdfId())
// 			to OwlClassDecl {
// 			return new OwlClassDecl(new List(), getElementList(), getId());
// 		}
270
271
272
273
		when (!isTopLevel())
			to OwlClassUse {
			return new OwlClassUse(new List(), getElementList(), getId());
		}
274
    }
275
276

    rewrite RdfsSubClassOf {
277
278
279
280
281
282
283
284
		when (getNumAttribute()>0 && getAttribute(0) instanceof RdfResource)
			to RdfsSubClassOf {
			Value value = getAttribute(0).getValue();
			OwlClass oc = new OwlClass(new List().add(new RdfAbout(value)),
									   new List());
			return new RdfsSubClassOf(new List(), 
									  new List().add(oc));
		}
285
    }
286
287

    rewrite RdfsDomain {
288
289
290
291
292
293
294
295
		when (getNumAttribute()>0 && getAttribute(0) instanceof RdfResource)
			to RdfsDomain {
			Value value = getAttribute(0).getValue();
			OwlClass oc = new OwlClass(new List().add(new RdfAbout(value)),
									   new List());
			return new RdfsDomain(new List(), 
								  new List().add(oc));
		}
296
    }
297
298
299
300
301
302
303
304

	rewrite OwlClassDecl {
		when (getId().contains("-"))
			to OwlClassDecl {
			setId(getId().replaceAll("-","_"));
			return this;
		}
	}
305
}