-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHierarchyExample.java
More file actions
261 lines (222 loc) · 9.42 KB
/
SimpleHierarchyExample.java
File metadata and controls
261 lines (222 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package owlaccessor;
//
import java.io.File;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Set;
import java.util.TreeSet;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import gnu.getopt.LongOpt;
import gnu.getopt.Getopt;
import org.semanticweb.HermiT.Reasoner;
/*
* Copyright (C) 2007, University of Manchester
*
* Modifications to the initial code base are copyright of their
* respective authors, or their employers as appropriate. Authorship
* of the modifications may be determined from the ChangeLog placed at
* the end of this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* <p>Simple example. Read an ontology, and display the class hierarchy. May use a
* reasoner to calculate the hierarchy.</p>
*
* Author: Sean Bechhofer<br>
* The University Of Manchester<br>
* Information Management Group<br>
* Date: 17-03-2007<br>
* <br>
*/
/*public class Distance {
OWLClass node;
int distance=0;
Distance d;
}*/
public class SimpleHierarchyExample {
private static int INDENT = 4;
private OWLReasonerFactory reasonerFactory;
private OWLOntology ontology;
private PrintStream out;
Hashtable<OWLClass,Integer> distance = new Hashtable<OWLClass,Integer>();
private static OWLOntologyManager manager;
public SimpleHierarchyExample(OWLOntologyManager manager, OWLReasonerFactory reasonerFactory)
throws OWLException, MalformedURLException {
this.reasonerFactory = reasonerFactory;
out = System.out;
}
/**
* Print the class hierarchy for the given ontology from this class down, assuming this class is at
* the given level. Makes no attempt to deal sensibly with multiple
* inheritance.
*/
public void printHierarchy(OWLOntology ontology, OWLClass clazz) throws OWLException {
OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ontology);
this.ontology = ontology;
printHierarchy(reasoner, clazz, 0 );
/* Now print out any unsatisfiable classes */
for (OWLClass cl: ontology.getClassesInSignature()) {
if (!reasoner.isSatisfiable(cl)) {
out.println("XXX: " + labelFor(cl));
}
}
reasoner.dispose();
}
private String labelFor( OWLClass clazz) {
/*
* Use a visitor to extract label annotations
*/
LabelExtractor le = new LabelExtractor();
Set<OWLAnnotation> annotations = clazz.getAnnotations(ontology);
for (OWLAnnotation anno : annotations) {
anno.accept(le);
}
/* Print out the label if there is one. If not, just use the class URI */
if (le.getResult() != null) {
return le.getResult().toString();
} else {
return clazz.getIRI().toString();
}
}
/**
* Print the class hierarchy from this class down, assuming this class is at
* the given level. Makes no attempt to deal sensibly with multiple
* inheritance.
*/
public void printHierarchy(OWLReasoner reasoner, OWLClass clazz, int level)
throws OWLException {
TreeSet<OWLClass> list2 = new TreeSet<OWLClass>();
Set<OWLClass> list1;
int flag=1;
// OWLClass nothing = manager.getOWLDataFactory().getOWLClass(OWLRDFVocabulary.OWL_THING.getIRI());
/*
* Only print satisfiable classes -- otherwise we end up with bottom
* everywhere
*/
if (reasoner.isSatisfiable(clazz)) {
for (int i = 0; i < level * INDENT; i++) {
out.print(" ");
}
out.println(labelFor( clazz ));
System.out.println("I am here");
list1 = reasoner.getSubClasses(clazz, true).getFlattened();
System.out.println(list1.size());
/* Find the children and recurse */
while(flag==1)
{
// System.out.println(list1.size());
for (OWLClass child : list1) {
if (!child.isBottomEntity()) {
System.out.println(labelFor( child ));
list2.addAll(reasoner.getSubClasses(child, true).getFlattened());
distance.put(child, level);
}
else
{
distance.put(child, level);
}
}
list1.clear();
if(list2.size()!=0)
{
list1.addAll(list2);
list2.clear();
level +=1;
flag=1;
}
else
flag=0;
}
System.out.println(distance.size());
}
}
public static void main(String[] args) {
try {
/* Handle command line arguments */
LongOpt[] longopts = new LongOpt[11];
longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, '?');
longopts[1] = new LongOpt("reasoner", LongOpt.REQUIRED_ARGUMENT,
null, 'r');
longopts[2] = new LongOpt("class", LongOpt.REQUIRED_ARGUMENT,
null, 'c');
Getopt g = new Getopt("", args, "?:r:c", longopts);
int c;
// String arg;
IRI classIRI = null;
String reasonerFactoryClassName = null;
while ((c = g.getopt()) != -1) {
switch (c) {
case '?':
System.out.println("command --reasonerFactoryClassName [--class=URL] URL");
System.exit(0);
case 'r':
/* Use a reasoner */
reasonerFactoryClassName = g.getOptarg();
break;
case 'c':
/* Class to start from */
classIRI = IRI.create(g.getOptarg());
break;
}
}
int i = g.getOptind();
// We first need to obtain a copy of an
// OWLOntologyManager, which, as the name
// suggests, manages a set of ontologies.
manager = OWLManager.createOWLOntologyManager();
// We load an ontology from the URI specified
// on the command line
if (args.length <= i) {
System.out.println("No URI specified!");
//System.exit(0);
}
// System.out.println(args[i]);
File file = new File("C:/Users/Murali/Desktop/RA/ontologies/ext.owl");
//IRI documentIRI = IRI.create(args[i]);
// Now load the ontology.
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
// Report information about the ontology
System.out.println("Ontology Loaded...");
System.out.println("File: " + file);
System.out.println("Ontology : " + ontology.getOntologyID());
System.out.println("Format : "
+ manager.getOntologyFormat(ontology));
// / Create a new SimpleHierarchy object with the given reasoner.
OWLReasonerFactory reasonerFactoryin = new Reasoner.ReasonerFactory();
/*SimpleHierarchyExample simpleHierarchy = new SimpleHierarchyExample(
manager, (OWLReasonerFactory) Class.forName(reasonerFactoryClassName).newInstance());
*/
SimpleHierarchyExample simpleHierarchy = new SimpleHierarchyExample(
manager, reasonerFactoryin);
// Get Thing
if (classIRI==null) {
classIRI = OWLRDFVocabulary.OWL_THING.getIRI();
//classIRI.create("http://purl.obolibrary.org/obo/BSPO_0000085");
}
OWLClass clazz = manager.getOWLDataFactory().getOWLClass(classIRI);
// d1.node = clazz;
System.out.println("Class : " + classIRI);
// Print the hierarchy below thing
simpleHierarchy.printHierarchy(ontology, clazz );
} catch (Exception e) {
e.printStackTrace();
}
}
}