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

3
/* 
4
 * Copyright (C) 2006-2008  Anders Nilsson <anders.nilsson@cs.lth.se>
5
6
7
8
 *
 * This file is part of OntologyCompiler.
 */

9
10
import java.util.ArrayList;

11
aspect Types {
12
    syn lazy OClass OClass.getSuperClass();
13
    eq OwlClassDecl.getSuperClass() {
14
15
16
17
18
19
20
		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();
				}
			}
21
		}
22
23
		// No super class found, return an owl Thing instead.
		return new OwlClassDecl(new List(), new List(), "Thing");
24
25
    }
    eq OwlClassUse.getSuperClass() {
26
		return decl().getSuperClass();
27
28
    }

29
    syn lazy OwlClassDecl OClass.decl();
30
31
    eq OwlClassDecl.decl() = this;
    eq OwlClassUse.decl() {
32
33
		ComplexElement top = getTopElement();
		for (int i=0; i<top.getNumElement(); i++) {
34
// 			System.out.println(getId()+" == "+((ComplexElement) top.getElement(i)).getId());
35
36
			if ((top.getElement(i) instanceof ComplexElement) &&
				getId().equals(((ComplexElement) top.getElement(i)).getId())) {
37
38
39
40
41
42
43
44
45
46
				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");
		}
		// No decl found, so let's return null. Not sure if that's
		// that the right thing to do though.
47
        System.out.println("* Couldn't find decl for: "+getId());
48
		return new OwlClassDecl(new List(), new List(), "_Unknown_");
49
    }
50

Anders Nilsson's avatar
Anders Nilsson committed
51
    syn lazy Properties OwlClassDecl.getProperties() {
52
53
54
55
56
57
58
59
60
61
62
63
		List l = new List();
		// 	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);
		// 	    }
		// 	}
		// 	return p;
64
    }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

	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;
	}
84
85
}

86
87
aspect Restrictions {
  
88
	Restrictions OwlClassDecl.restrictions;
89
	Restrictions OwlClassDecl.ownRestrictions;
90
  
91
92
93
94
95
96
97
98
	syn lazy Restrictions OwlClassDecl.getRestrictions() {
		if (restrictions == null) {
			List l = new List();
			collectRestrictions(l);
			restrictions = new Restrictions(l);
		}
		return restrictions;
	}
99

100
101
102
103
104
	void ASTNode.collectRestrictions(List l) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectRestrictions(l);
		}
	}
105
106
107
108
109
110
111

	void OwlClassDecl.collectRestrictions(List l) {
		if (!getSuperClass().getId().equals("Thing")) {
			getSuperClass().decl().collectRestrictions(l);
		}
		super.collectRestrictions(l);
	}
112
  
113
	void OwlRestriction.collectRestrictions(List l) {
114
115
116
		if (allValuesFrom()) {
			l.add(this);
		}
117
118
119
	}
	
	syn int OwlClassDecl.getNumRestriction() = getRestrictions().getNumOwlRestriction();
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

	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) {
137
138
139
		if (allValuesFrom()) {
			l.add(this);
		}
140
141
142
143
	}
	
	syn int OwlClassDecl.getNumOwnRestriction() = 
		getOwnRestrictions().getNumOwlRestriction();
144
145
}

146
aspect Properties {
Anders Nilsson's avatar
Anders Nilsson committed
147
    void ASTNode.collectProperties(List l) {
148
149
150
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).collectProperties(l);
		}
151
152
    }

Anders Nilsson's avatar
Anders Nilsson committed
153
    void OwlProperty.collectProperties(List l) {
154
		l.add(this);
155
156
    }

157
    boolean ASTNode.domainIncludes(OClass clazz) {
158
		return false;
159
160
    }

Anders Nilsson's avatar
Anders Nilsson committed
161
    boolean OwlObjectProperty.domainIncludes(OClass clazz) {
162
163
164
165
166
167
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsDomain) {
				return ((RdfsDomain) getElement(i)).domainIncludes(clazz);
			}
		}
		return false;
168
    }
169
170

    boolean RdfsDomain.domainIncludes(OClass clazz) {
171
172
173
174
175
176
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;
177
178
    }

179
    boolean OwlUnionOf.domainIncludes(OClass clazz) {
180
181
182
183
184
185
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i).domainIncludes(clazz)) {
				return true;
			}
		}
		return false;
186
187
    }

188
    boolean OClass.domainIncludes(OClass clazz) {
189
190
191
192
193
194
195
196
197
		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;	
198
199
200
201
    }

    syn lazy RdfsRange Element.range() = null;
    
Anders Nilsson's avatar
Anders Nilsson committed
202
    eq OwlObjectProperty.range() {
203
204
205
206
207
208
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof RdfsRange) {
				return (RdfsRange) getElement(i);
			}
		}
		return null;
209
    }
Anders Nilsson's avatar
Anders Nilsson committed
210
}