Types.jrag 11.6 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 {
25 26 27 28 29 30 31 32 33 34 35
	syn Clazz Element.clazz() = null;
	eq ComplexElement.clazz() {
		for (Element e: getElements()) {
			if (e.clazz() != null) {
				return e.clazz();
			}
		}
		return null;
	}
	eq Clazz.clazz() = this;

Anders Nilsson's avatar
Anders Nilsson committed
36 37 38 39 40 41
    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
42
	eq Start.getSuperClass(String name) =
Anders Nilsson's avatar
Anders Nilsson committed
43
		getSpecification().getSuperClass(name);
Anders Nilsson's avatar
Anders Nilsson committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
	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
Anders Nilsson committed
58
	eq ComplexElement.getSuperClass(String name) {
Anders Nilsson's avatar
Anders Nilsson committed
59
		for (Element e : getElements()) {
Anders Nilsson's avatar
Anders Nilsson committed
60 61 62 63 64 65 66
			Declaration d = e.getSuperClass(name);
			if (d != null) {
				return d;
			}
		}
		return null;
	}
Anders Nilsson's avatar
Anders Nilsson committed
67 68 69 70 71 72
	eq SubClassOf.getSuperClass(String name) {
		if (getElement(0).name().equals(name)) {
			return goStart().findDecl(getElement(1).name());
		}
		return null;
	}
Anders Nilsson's avatar
Anders Nilsson committed
73

Anders Nilsson's avatar
Anders Nilsson committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	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
Anders Nilsson committed
93

94
    syn lazy Declaration OClass.getSuperClass();
95
    eq OwlClassDecl.getSuperClass() {
96 97 98 99 100 101 102
		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();
				}
			}
103
		}
104 105
		// No super class found, return an owl Thing instead.
		return new OwlClassDecl(new List(), new List(), "Thing");
106 107
    }
    eq OwlClassUse.getSuperClass() {
108
		return decl().getSuperClass();
109 110
    }

111
    syn lazy OwlClassDecl Declaration.decl() = null;
112 113
    eq OwlClassDecl.decl() = this;
    eq OwlClassUse.decl() {
114 115
		ComplexElement top = getTopElement();
		for (int i=0; i<top.getNumElement(); i++) {
116
// 			System.out.println(getId()+" == "+((ComplexElement) top.getElement(i)).getId());
117 118
			if ((top.getElement(i) instanceof ComplexElement) &&
				getId().equals(((ComplexElement) top.getElement(i)).getId())) {
119 120 121 122 123 124 125 126
				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
127 128 129 130 131
		// 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.
132
        System.out.println("* Couldn't find decl for: "+getId());
Anders Nilsson's avatar
Anders Nilsson committed
133 134 135
		OwlClassDecl nClass = new OwlClassDecl(new List(), new List(), id);
		top.addElement(nClass);
		return nClass;
136
    }
137

Anders Nilsson's avatar
Anders Nilsson committed
138
    syn lazy Properties OwlClassDecl.getProperties() {
139
		List l = new List();
Anders Nilsson's avatar
Anders Nilsson committed
140 141 142 143 144 145 146 147 148 149 150 151 152
		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;
153
    }
154 155 156 157 158 159 160
    syn lazy Properties ClassDeclaration.getProperties() {
		List l = new List();
		goStart().collectProperties(l);
// 		collectProperties(l);
// 		return new Properties(l);
		Properties p = new Properties();
		for (int i=0; i<l.getNumChild(); i++) {
Anders Nilsson's avatar
Anders Nilsson committed
161
			OwlProperty op = (OwlProperty) l.getChild(i);
162 163 164 165 166 167 168 169
			if (op.domainIncludes(this)) {
				p.addProperty(op);
			} else {
// 				System.out.println(op);
			}
		}
		return p;
    }
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

	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;
	}
189 190
}

191 192
aspect Restrictions {
  
193
	Restrictions OwlClassDecl.restrictions;
194
	Restrictions OwlClassDecl.ownRestrictions;
195
  
196 197 198 199 200 201 202 203
	syn lazy Restrictions OwlClassDecl.getRestrictions() {
		if (restrictions == null) {
			List l = new List();
			collectRestrictions(l);
			restrictions = new Restrictions(l);
		}
		return restrictions;
	}
204

205 206 207 208 209
	void ASTNode.collectRestrictions(List l) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectRestrictions(l);
		}
	}
210 211 212 213 214 215 216

	void OwlClassDecl.collectRestrictions(List l) {
		if (!getSuperClass().getId().equals("Thing")) {
			getSuperClass().decl().collectRestrictions(l);
		}
		super.collectRestrictions(l);
	}
217
  
218
	void OwlRestriction.collectRestrictions(List l) {
219 220 221
		if (allValuesFrom()) {
			l.add(this);
		}
222 223 224
	}
	
	syn int OwlClassDecl.getNumRestriction() = getRestrictions().getNumOwlRestriction();
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

	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) {
242 243 244
		if (allValuesFrom()) {
			l.add(this);
		}
245 246 247 248
	}
	
	syn int OwlClassDecl.getNumOwnRestriction() = 
		getOwnRestrictions().getNumOwlRestriction();
249 250
}

251
aspect Properties {
Anders Nilsson's avatar
Anders Nilsson committed
252
    void ASTNode.collectProperties(List l) {
253 254 255
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectProperties(l);
		}
256 257
    }

Anders Nilsson's avatar
Anders Nilsson committed
258
    void OwlProperty.collectProperties(List l) {
259
		l.add(this);
260 261
    }

262
    boolean ASTNode.domainIncludes(Declaration clazz) {
263
		return false;
264 265
    }

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

275
    boolean OwlFunctionalProperty.domainIncludes(Declaration clazz) {
Anders Nilsson's avatar
Anders Nilsson committed
276 277 278 279 280 281 282 283
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return ((RdfsDomain) getElement(i)).domainIncludes(clazz);
			}
		}
		return false;
    }

284
    boolean OwlDatatypeProperty.domainIncludes(Declaration clazz) {
Anders Nilsson's avatar
Anders Nilsson committed
285 286 287 288 289 290 291 292
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return ((RdfsDomain) getElement(i)).domainIncludes(clazz);
			}
		}
		return false;
    }

293
    boolean RdfsDomain.domainIncludes(Declaration clazz) {
294 295 296 297 298 299
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;
300 301
    }

302
    boolean OwlUnionOf.domainIncludes(Declaration clazz) {
303 304 305 306 307 308
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;
309 310
    }

311
    boolean Declaration.domainIncludes(Declaration clazz) {
312 313 314 315 316 317 318 319 320
		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;	
321 322
    }

323 324 325 326 327 328 329 330 331 332
	boolean ObjectPropertyDomain.domainIncludes(Declaration clazz) {
		while (!clazz.name().equals("Thing")) {
			if (clazz().name().equals(clazz.name())) {
				return true;
			}
			clazz = clazz.getSuperClass();
		}
		return false;
	}

Anders Nilsson's avatar
Anders Nilsson committed
333 334 335 336 337 338 339 340 341 342
	boolean DataPropertyDomain.domainIncludes(Declaration clazz) {
		while (!clazz.name().equals("Thing")) {
			if (clazz().name().equals(clazz.name())) {
				return true;
			}
			clazz = clazz.getSuperClass();
		}
		return false;
	}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    syn lazy RdfsDomain Element.domain() = null;

    eq OwlObjectProperty.domain() {
		for (int i=0; i<getNumElement(); i++) {
			 // getElement(i).dumpTree("",System.out);
			 // System.out.println("Checking "+getElement(i));
			if (getElement(i) instanceof RdfsDomain) {
				 // System.out.println("Yes");
				return (RdfsDomain) getElement(i);
			}
		}
		return null;
    }
    eq OwlFunctionalProperty.domain() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return (RdfsDomain) getElement(i);
			}
		}
		return null;
    }
    eq OwlDatatypeProperty.domain() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return (RdfsDomain) getElement(i);
			}
		}
		return null;
    }

373 374
    syn lazy RdfsRange Element.range() = null;
    
375 376 377 378 379 380 381 382 383 384
    eq OwlProperty.range() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
		// Default to Thing
		System.out.println("* Default range to Thing");
		return new RdfsRange(new List().add(new RdfResource(new Value("Thing"))),new List());
    }
Anders Nilsson's avatar
Anders Nilsson committed
385
    eq OwlObjectProperty.range() {
386 387 388 389 390
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
391 392
		// Default to Thing
		return new RdfsRange(new List().add(new RdfResource(new Value("Thing"))),new List());
393
    }
Anders Nilsson's avatar
Anders Nilsson committed
394 395 396 397 398 399
    eq OwlFunctionalProperty.range() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
400 401
		// return null;
		return new RdfsRange(new List().add(new RdfResource(new Value("Thing"))),new List());
Anders Nilsson's avatar
Anders Nilsson committed
402 403 404 405 406 407 408
    }
    eq OwlDatatypeProperty.range() {
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
409 410
		return new RdfsRange(new List().add(new RdfResource(new Value("Thing"))),new List());
		// return null;
Anders Nilsson's avatar
Anders Nilsson committed
411
    }
412 413 414 415 416 417 418 419 420 421 422

	syn ObjectProperty ObjectPropertyDomain.objectProperty() {
		for (Element e : getElements()) {
			if (e.isObjectProperty()) {
				return (ObjectProperty) e;
			}
		}
		System.err.println("ObjectPropertyDomain.objectProperty(): not found");
		return null;
	}

Anders Nilsson's avatar
Anders Nilsson committed
423 424 425 426 427 428 429 430 431 432
	syn DataProperty DataPropertyDomain.dataProperty() {
		for (Element e : getElements()) {
			if (e.isDataProperty()) {
				return (DataProperty) e;
			}
		}
		System.err.println("DataPropertyDomain.dataProperty(): not found");
		return null;
	}

433 434
	syn boolean Element.isObjectProperty() = false;
	eq ObjectProperty.isObjectProperty() = true;
Anders Nilsson's avatar
Anders Nilsson committed
435 436
	syn boolean Element.isDataProperty() = false;
	eq DataProperty.isDataProperty() = true;
437

Anders Nilsson's avatar
Anders Nilsson committed
438
}