Rewrites.jrag 7.99 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
		String s = getValue().getSTRING_LITERAL();
Anders Nilsson's avatar
Anders Nilsson committed
33
		s = s.replaceAll("-","_");
34 35 36 37 38 39
		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();
		} 
Anders Nilsson's avatar
Anders Nilsson committed
40 41 42
		if (s.indexOf(';') > -1) {
			s = s.substring(s.indexOf(';')+1,s.length()).trim();
		} 
43
		return s.trim();
44 45 46
    }

    eq RdfResource.trim() {
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();
		} 
54 55 56
		if (s.indexOf(';') > -1) {
			s = s.substring(s.indexOf(';')+1,s.length()).trim();
		} 
57 58 59 60
		if (s.equals("string")) {
			s = "String";
		}
		return s.trim();
61 62 63
    }

    boolean ComplexElement.hasRdfId() {
64 65 66 67 68 69
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfId) {
				return true;
			}
		}
		return false;
Anders Nilsson's avatar
Anders Nilsson committed
70

71 72 73
    }

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

82
    boolean ComplexElement.hasRdfResource() {
83 84 85 86 87 88
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfResource) {
				return true;
			}
		}
		return false;
89 90
    }

91
    syn lazy String Element.getId() {
92
		return new String("Element has no ID");
93 94 95
    }

    eq ComplexElement.getId() {
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 169 170 171 172 173 174 175
		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_";		
176
	}
177

178 179 180 181 182
	// eq RdfsDomain.getId() {
	// 	 System.out.println(((OwlClassUse) getElement(0)).getId());
	// return ((OwlClassUse)getElement(0)).getId();
	// }

183 184 185
    syn lazy String Element.type() = null;

    eq RdfsRange.type() {
186 187 188 189 190 191
		if (hasRdfResource()) {
			for (int i=0; i<getNumAttribute(); i++) {
				if (getAttribute(i) instanceof RdfResource) {
					return getAttribute(i).trim();
				}
			}
192
		}
193 194 195 196 197 198 199 200
		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
201
		return "Thing";
202 203 204
    }

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

    eq OwlOneOf.type() {
215 216 217 218 219 220 221
		for (int i=0; i<getNumElement(); i++) {
			String s = getElement(i).type();
			if (s != null) {
				return s;
			}
		}	
		return null;
222 223 224
    }

    eq RdfFirst.type() {
225 226 227 228 229 230
		for (int i=0; i<getNumAttribute(); i++) {
			if (getAttribute(i) instanceof RdfDatatype) {
				return getAttribute(i).trim();
			}
		}
		return null;
231 232
    }

233
    ComplexElement ComplexElement.getTopElement() {
234 235 236 237 238
		ASTNode e = this;
		while (e != null && !(e instanceof RdfDeclaration)) {
			e = e.getParent();
		}
		return (ComplexElement) e;
239
    }
240

Anders Nilsson's avatar
Anders Nilsson committed
241 242 243 244 245
	 syn boolean Declaration.isClassDecl() {
		  return getElement(0).isClass();
	 }
	eq OwlClassDecl.isClassDecl() = true;
	eq OwlClassUse.isClassDecl() = false;
Anders Nilsson's avatar
Anders Nilsson committed
246
	eq ClassDeclaration.isClassDecl() = false;
247 248
	eq IndividualDeclaration.isClassDecl() = false;
	syn boolean Declaration.isIndividualDecl() = getElement(0).isIndividual();
Anders Nilsson's avatar
Anders Nilsson committed
249
	eq IndividualDeclaration.isIndividualDecl() = false;
250 251 252 253 254 255 256 257
	eq ClassDeclaration.isIndividualDecl() = false;

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

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

258 259
}

260
aspect RewriteClasses {
Anders Nilsson's avatar
Anders Nilsson committed
261

Anders Nilsson's avatar
Anders Nilsson committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	// rewrite OClass {
	// 	when (isClassDecl())
	// 		to ClassDeclaration {
	// 		 System.out.println(getId());
	// 		ClassDeclaration c = new ClassDeclaration();
	// 		c.setElementList(getElementList());
	// 		c.setAttributeList(getAttributeList());
	// 		return c;
	// 	}
	// 	// when (isIndividualDecl())
	// 	// 	to IndividualDeclaration {
	// 	// 	IndividualDeclaration c = new IndividualDeclaration();
	// 	// 	c.setElementList(getElementList());
	// 	// 	return c;
	// 	// }
	// }
Anders Nilsson's avatar
Anders Nilsson committed
278

279
    rewrite OwlClass {
280 281 282 283
		when (isTopLevel())
			to OwlClassDecl {
			return new OwlClassDecl(new List(), getElementList(), getId());
		}
284 285 286 287
// 		when (hasRdfId())
// 			to OwlClassDecl {
// 			return new OwlClassDecl(new List(), getElementList(), getId());
// 		}
288 289 290 291
		when (!isTopLevel())
			to OwlClassUse {
			return new OwlClassUse(new List(), getElementList(), getId());
		}
292
    }
293 294

    rewrite RdfsSubClassOf {
295 296 297 298 299 300 301 302
		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));
		}
303
    }
304 305

    rewrite RdfsDomain {
306 307 308 309 310 311 312 313
		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));
		}
314
    }
315 316 317 318 319 320 321 322

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