0
|
1 package de.mpiwg.dwinter.duomo.lexdump;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.net.URI;
|
|
5 import java.net.URISyntaxException;
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.Collection;
|
|
8 import java.util.HashMap;
|
|
9 import java.util.Iterator;
|
|
10 import java.util.List;
|
|
11
|
|
12 import org.apache.log4j.Logger;
|
|
13
|
|
14 import edu.stanford.smi.protege.exception.OntologyLoadException;
|
|
15 import edu.stanford.smi.protegex.owl.ProtegeOWL;
|
|
16 import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
|
|
17 import edu.stanford.smi.protegex.owl.model.OWLClass;
|
|
18 import edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty;
|
|
19 import edu.stanford.smi.protegex.owl.model.OWLIndividual;
|
|
20 import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
|
|
21 import edu.stanford.smi.protegex.owl.model.RDFProperty;
|
|
22 import edu.stanford.smi.protegex.owl.model.RDFSDatatype;
|
|
23 import edu.stanford.smi.protegex.owl.model.RDFSLiteral;
|
|
24 import edu.stanford.smi.protegex.owl.model.impl.DefaultRDFProperty;
|
|
25 import edu.stanford.smi.protegex.owl.model.impl.DefaultRDFSLiteral;
|
|
26 import edu.stanford.smi.protegex.owl.repository.RepositoryManager;
|
|
27 import edu.stanford.smi.protegex.owl.repository.impl.LocalFolderRepository;
|
|
28
|
|
29 public class OWLImporter {
|
|
30
|
|
31 JenaOWLModel owlModel; // contains the model
|
|
32 Logger logger = Logger.getRootLogger();
|
|
33 private HashMap<String, String> typeId2className=null;
|
|
34 private HashMap<String, OWLNamedClass> typeId2class=null;
|
4
|
35 private String ontFolder;
|
|
36 private URI uri;
|
0
|
37
|
|
38 public OWLImporter(String folder, URI uri) throws OntologyLoadException {
|
|
39 // owlModel = ProtegeOWL.createJenaOWLModelFromURI(uri);
|
|
40
|
4
|
41 this.ontFolder=folder;
|
|
42 this.uri=uri;
|
|
43
|
|
44 loadOWL(folder, uri);
|
|
45 }
|
|
46
|
|
47 public void reloadOWL() throws OntologyLoadException{
|
|
48 loadOWL(ontFolder, uri);
|
|
49 }
|
|
50
|
|
51 public void loadOWL(String folder, URI uri) throws OntologyLoadException {
|
0
|
52 owlModel = ProtegeOWL.createJenaOWLModel();
|
|
53 // Load repository
|
|
54 RepositoryManager rman = owlModel.getRepositoryManager();
|
|
55
|
|
56 // Add working directory to repository manager.
|
|
57 File workingDirectory = new File(folder);
|
|
58 rman.addGlobalRepository(new LocalFolderRepository(workingDirectory));
|
|
59 // Load ontology
|
|
60
|
|
61 ((JenaOWLModel) owlModel).load(uri, "OWL-DL");
|
|
62 }
|
|
63
|
|
64 /**
|
|
65 * Drucke das Modell nach stdout
|
|
66 */
|
|
67 public void printModel() {
|
|
68 Collection<?> classes = owlModel.getUserDefinedOWLNamedClasses();
|
|
69 for (Iterator<?> it = classes.iterator(); it.hasNext();) {
|
|
70 OWLNamedClass cls = (OWLNamedClass) it.next();
|
|
71 Collection<?> instances = cls.getInstances(false);
|
|
72 System.out.println("Class " + cls.getBrowserText() + " ("
|
|
73 + instances.size() + ")");
|
|
74 for (Iterator<?> jt = instances.iterator(); jt.hasNext();) {
|
|
75 OWLIndividual individual = (OWLIndividual) jt.next();
|
|
76 System.out.println(" - " + individual.getBrowserText());
|
|
77 }
|
|
78 }
|
|
79
|
|
80 }
|
|
81
|
|
82 public static void main(String args[]) throws URISyntaxException {
|
|
83 String base = "/Users/dwinter/Documents/Projekte/Diss - data-mining/eclipseWorkspace/de.mpiwg.dwinter.duomo/owlInput";
|
|
84 URI ontologieUri = new URI(
|
|
85 "file:///Users/dwinter/Documents/Projekte/Diss%20-%20data-mining/eclipseWorkspace/de.mpiwg.dwinter.duomo/owlInput/duomoAnalysis.owl");
|
|
86 try {
|
|
87
|
|
88 OWLImporter im = new OWLImporter(base, ontologieUri);
|
|
89 im.printModel();
|
|
90 } catch (OntologyLoadException e) {
|
|
91 // TODO Auto-generated catch block
|
|
92 e.printStackTrace();
|
|
93 }
|
|
94 }
|
|
95
|
|
96 public OWLIndividual createInstance(String string) {
|
|
97 OWLNamedClass owlclass = (OWLNamedClass) owlModel
|
|
98 .getOWLNamedClass(string);
|
|
99
|
|
100 if (owlclass == null) {
|
|
101 logger.debug("Cannot find OWLClass:" + string);
|
|
102 return null;
|
|
103 }
|
|
104 //logger.debug("Create new individual of type:"+string);
|
|
105 return owlclass.createOWLIndividual(null);
|
|
106
|
|
107 }
|
|
108
|
|
109 public void setProperty(OWLIndividual identifier, String propertyName,
|
|
110 Object value) {
|
|
111 RDFProperty prop = owlModel.getRDFProperty(propertyName);
|
|
112 identifier.setPropertyValue(prop, value);
|
|
113
|
|
114 }
|
|
115
|
|
116 public void setDataTypePropery(OWLIndividual eventInstance,
|
|
117 String propertyName, String value, String lang) {
|
|
118 RDFProperty prop = owlModel.getRDFProperty(propertyName);
|
|
119 // if(OWLDatatypeProperty.class.isInstance(prop)){
|
|
120 // OWLDatatypeProperty dp = (OWLDatatypeProperty)prop;
|
|
121 // prop.set
|
|
122 //
|
|
123 // } else {
|
|
124 // logger.error("Is not a datatypeprop:"+propertyName);
|
|
125 // }
|
|
126
|
|
127
|
|
128 RDFSLiteral langLiteral = owlModel.createRDFSLiteral(value, lang);
|
|
129 eventInstance.setPropertyValue(prop, langLiteral);
|
|
130
|
|
131
|
|
132 }
|
|
133
|
|
134 public Object getRelatedIndividual(OWLIndividual cardInd,
|
|
135 String propertyName) {
|
|
136 RDFProperty prop = owlModel.getRDFProperty(propertyName);
|
|
137 if (prop == null) {
|
|
138 logger.debug("Property does not exist:" + propertyName);
|
|
139 }
|
|
140
|
|
141 Object value = cardInd.getPropertyValue(prop);
|
|
142 return value;
|
|
143 }
|
|
144
|
|
145 public OWLIndividual getIndividual(String classNameFrom,
|
|
146 String propertyNameToClass, String classNameTo,
|
|
147 String propertyName2, Object value) {
|
|
148
|
|
149 RDFProperty prop = owlModel.getRDFProperty(propertyName2);
|
|
150
|
|
151 OWLClass classTo = owlModel.getOWLNamedClass(classNameTo);
|
|
152 Collection<?> instances = classTo.getInstances(true);
|
|
153 OWLIndividual toIv = null;
|
|
154 // Such individiual mit propertyName2 und Wert value
|
|
155 for (Iterator<?> jt = instances.iterator(); jt.hasNext();) {
|
|
156 OWLIndividual iv = (OWLIndividual) jt.next();
|
|
157 Object propVal = iv.getPropertyValue(prop);
|
|
158
|
|
159 if (propVal!=null && iv.getPropertyValue(prop).equals(value)) {
|
|
160 toIv = iv;
|
|
161 break;
|
|
162 }
|
|
163 }
|
|
164
|
|
165 // kein treffer
|
|
166 if (toIv == null)
|
|
167 return null;
|
|
168
|
|
169 // jetzt suche das Invidual, dass mit dieser Instance verbunden ist
|
|
170 RDFProperty prop2 = owlModel.getRDFProperty(propertyNameToClass);
|
|
171
|
|
172 OWLClass classFrom = owlModel.getOWLNamedClass(classNameFrom);
|
|
173 Collection<?> instancesFrom = classFrom.getInstances(true);
|
|
174 OWLIndividual fromIv = null;
|
|
175 // Such individiual mit propertyName2 und Wert value
|
|
176 for (Iterator<?> jt = instancesFrom.iterator(); jt.hasNext();) {
|
|
177 OWLIndividual iv = (OWLIndividual) jt.next();
|
|
178 Object propValue = iv.getPropertyValue(prop2);
|
|
179 if (propValue!=null && iv.getPropertyValue(prop2).equals(toIv)) {
|
|
180 fromIv = iv;
|
|
181 break;
|
|
182 }
|
|
183 }
|
|
184
|
|
185 return fromIv;
|
|
186 }
|
|
187
|
|
188 public List<OWLIndividual> getIndividuals(String classNameFrom,
|
|
189 String propertyNameToClass, String classNameTo,
|
|
190 String propertyName2, Object value) {
|
|
191
|
|
192 List<OWLIndividual> returnList= new ArrayList<OWLIndividual>();
|
|
193
|
|
194 RDFProperty prop = owlModel.getRDFProperty(propertyName2);
|
|
195
|
|
196 OWLClass classTo = owlModel.getOWLNamedClass(classNameTo);
|
|
197 Collection<?> instances = classTo.getInstances(true);
|
|
198 OWLIndividual toIv = null;
|
|
199 // Such individiual mit propertyName2 und Wert value
|
|
200 for (Iterator<?> jt = instances.iterator(); jt.hasNext();) {
|
|
201 OWLIndividual iv = (OWLIndividual) jt.next();
|
|
202 if (iv.getPropertyValue(prop).equals(value)) {
|
|
203 toIv = iv;
|
|
204 break;
|
|
205 }
|
|
206 }
|
|
207
|
|
208 // kein treffer
|
|
209 if (toIv == null)
|
|
210 return null;
|
|
211
|
|
212 // jetzt suche das Invidual, dass mit dieser Instance verbunden ist
|
|
213 RDFProperty prop2 = owlModel.getRDFProperty(propertyNameToClass);
|
|
214
|
|
215 OWLClass classFrom = owlModel.getOWLNamedClass(classNameFrom);
|
|
216 Collection<?> instancesFrom = classFrom.getInstances(true);
|
|
217 //OWLIndividual fromIv = null;
|
|
218 // Such individiual mit propertyName2 und Wert value
|
|
219 for (Iterator<?> jt = instancesFrom.iterator(); jt.hasNext();) {
|
|
220 OWLIndividual iv = (OWLIndividual) jt.next();
|
|
221 if (iv.getPropertyValue(prop2).equals(toIv)) {
|
|
222 returnList.add(iv);
|
|
223 break;
|
|
224 }
|
|
225 }
|
|
226
|
|
227 return returnList;
|
|
228 }
|
|
229
|
|
230 public List<OWLIndividual> getIndividuals(String className, String propertyName,
|
|
231 OWLIndividual targetInd) {
|
|
232 List<OWLIndividual> returnList= new ArrayList<OWLIndividual>();
|
|
233
|
|
234 RDFProperty prop = owlModel.getRDFProperty(propertyName);
|
|
235
|
|
236 OWLClass cls = owlModel.getOWLNamedClass(className);
|
|
237
|
|
238 Collection<?> instances = cls.getInstances(true);
|
|
239 // Such individiual mit propertyName2 und Wert value
|
|
240 for (Iterator<?> jt = instances.iterator(); jt.hasNext();) {
|
|
241 OWLIndividual iv = (OWLIndividual) jt.next();
|
|
242 if (iv.getPropertyValue(prop).equals(targetInd)) {
|
|
243 returnList.add(iv);
|
|
244
|
|
245 }
|
|
246 }
|
|
247
|
|
248 return returnList;
|
|
249 }
|
|
250
|
|
251 public OWLIndividual createTimeSpan(String dateDcStart, String dateDcEnd) {
|
|
252 OWLIndividual timeSpan = createInstance("DatesDocument");
|
|
253
|
|
254
|
|
255
|
|
256 setProperty(timeSpan, "has_readable_date", dateDcStart);
|
|
257
|
|
258 if(!(dateDcEnd==null || dateDcEnd.equals(""))){
|
|
259 setProperty(timeSpan, "has_readable_to_date", dateDcEnd);
|
|
260 } else {
|
|
261 dateDcEnd=dateDcStart;
|
|
262 }
|
|
263
|
|
264 OWLIndividual timeSpanIdentifier = createInstance("Identifier");
|
|
265 setProperty(timeSpanIdentifier,"has_readable_id",dateDcStart+"-"+dateDcEnd);
|
|
266
|
|
267 setProperty(timeSpan,"crm:P48_has_preferred_identifier",timeSpanIdentifier);
|
|
268
|
|
269 return timeSpan;
|
|
270 }
|
|
271
|
|
272 public OWLIndividual getIndividualByReadableId(String className,String identifier){
|
|
273 return getIndividual(className, "crm:P48_has_preferred_identifier", "Identifier", "has_readable_id", identifier);
|
|
274 }
|
|
275
|
|
276 public String getClassNameFromTypeId(String typeId) {
|
|
277 if (typeId2className==null){ // hash nicht angelegt
|
|
278 createTypeId2classHashes();
|
|
279 }
|
|
280
|
|
281 return typeId2className.get(typeId);
|
|
282
|
|
283 }
|
|
284
|
|
285 private void createTypeId2classHashes() {
|
|
286 typeId2className= new HashMap<String,String>();
|
|
287 typeId2class= new HashMap<String,OWLNamedClass>();
|
|
288
|
|
289 Collection<?> classes = owlModel.getUserDefinedOWLNamedClasses();
|
|
290
|
|
291 RDFProperty property = owlModel.getRDFProperty("duomoTypeID");
|
|
292 for (Iterator<?> it = classes.iterator(); it.hasNext();) {
|
|
293 OWLNamedClass cls = (OWLNamedClass) it.next();
|
|
294 String tid = (String) cls.getPropertyValue(property);
|
|
295 if(tid!=null){
|
|
296 typeId2class.put(tid, cls);
|
|
297 typeId2className.put(tid, cls.getName());
|
|
298 logger.debug("Adding to typId2className:"+tid+"---"+cls.getName());
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302
|
|
303 public OWLNamedClass getClassFromTypeId(String typeId) {
|
|
304 if (typeId2className==null){ // hash nicht angelegt
|
|
305 createTypeId2classHashes();
|
|
306 }
|
|
307
|
|
308 return typeId2class.get(typeId);
|
|
309 }
|
|
310
|
|
311 public void save(String string) throws URISyntaxException, Exception {
|
|
312 owlModel.save(new URI(string));
|
|
313
|
|
314 }
|
|
315
|
|
316 public OWLIndividual createOrGetInstanceWithIdentifier(String classNameInstance,
|
|
317 String classNameIdentifier, String identifier) {
|
|
318 OWLIndividual ind = getIndividualByReadableId(classNameInstance, identifier);
|
|
319 if(ind==null){
|
|
320 ind = createInstance(classNameInstance);
|
|
321 OWLIndividual identifierInd = createInstance(classNameIdentifier);
|
|
322 setProperty(identifierInd, "has_readable_id", identifier);
|
|
323 setProperty(ind, "crm:P48_has_preferred_identifier",
|
3
|
324 identifierInd);
|
0
|
325 }
|
2
|
326 return ind;
|
0
|
327 }
|
|
328 }
|