-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCreateSKOSExample.java
More file actions
292 lines (231 loc) · 14.3 KB
/
CreateSKOSExample.java
File metadata and controls
292 lines (231 loc) · 14.3 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package example;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
/*
* 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
*/
import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntologyChangeException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom;
import org.semanticweb.owlapi.vocab.DublinCoreVocabulary;
import org.semanticweb.skos.AddAssertion;
import org.semanticweb.skos.SKOSAnnotation;
import org.semanticweb.skos.SKOSAnnotationAssertion;
import org.semanticweb.skos.SKOSAnnotationProperty;
import org.semanticweb.skos.SKOSChange;
import org.semanticweb.skos.SKOSChangeException;
import org.semanticweb.skos.SKOSConcept;
import org.semanticweb.skos.SKOSConceptScheme;
import org.semanticweb.skos.SKOSCreationException;
import org.semanticweb.skos.SKOSDataFactory;
import org.semanticweb.skos.SKOSDataRelationAssertion;
import org.semanticweb.skos.SKOSDataset;
import org.semanticweb.skos.SKOSEntity;
import org.semanticweb.skos.SKOSEntityAssertion;
import org.semanticweb.skos.SKOSLiteral;
import org.semanticweb.skos.SKOSObject;
import org.semanticweb.skos.SKOSObjectRelationAssertion;
import org.semanticweb.skos.SKOSResource;
import org.semanticweb.skos.SKOSStorageException;
import org.semanticweb.skos.SKOSTypedLiteral;
import org.semanticweb.skos.SKOSUntypedLiteral;
import org.semanticweb.skos.properties.SKOSAltLabelProperty;
import org.semanticweb.skosapibinding.SKOSFormatExt;
import org.semanticweb.skosapibinding.SKOSManager;
import org.semanticweb.skosapibinding.SKOStoOWLConverter;
/**
* Author: Simon Jupp<br>
* Date: Mar 4, 2008<br>
* The University of Manchester<br>
* Bio-Health Informatics Group<br>
*/
public class CreateSKOSExample {
public static String baseURI = "http://skosexample.com/";
public CreateSKOSExample() {
}
public static void main(String[] args) throws SKOSCreationException {
// new manager for SKOS vocabularies
SKOSManager man = new SKOSManager();
// a skos vocabulary is essentially an OWL ontology that can contain multiple concept schemes
SKOSDataset vocab = man.createSKOSDataset(URI.create(baseURI));
// a data factory is used to create types of SKOS Objects
SKOSDataFactory factory = man.getSKOSDataFactory();
SKOSConceptScheme scheme = factory.getSKOSConceptScheme(URI.create(baseURI + "conceptSchemeA"));
// make a concept scheme assertion in a particular data set
SKOSEntityAssertion schemaAss = factory.getSKOSEntityAssertion(scheme);
SKOSConcept concept1 = factory.getSKOSConcept(URI.create(baseURI + "concept1"));
SKOSConcept concept2 = factory.getSKOSConcept(URI.create(baseURI + "concept2"));
// just add it with a Concept Assertion
SKOSEntityAssertion conAss1 = factory.getSKOSEntityAssertion(concept1);
SKOSEntityAssertion conAss2 = factory.getSKOSEntityAssertion(concept2);
// make assertion on concepts in your SKOS vocabulary - you do it by making SKOS assertion objects
SKOSObjectRelationAssertion inScheme = factory.getSKOSObjectRelationAssertion(concept1, factory.getSKOSInSchemeProperty(), scheme);
SKOSObjectRelationAssertion inScheme1 = factory.getSKOSObjectRelationAssertion(concept2, factory.getSKOSInSchemeProperty(), scheme);
SKOSObjectRelationAssertion topConcept = factory.getSKOSObjectRelationAssertion(scheme, factory.getSKOSHasTopConceptProperty(), concept1);
// add object relation between individuals, this is with respect to a vocabulary
SKOSObjectRelationAssertion assertion1 = factory.getSKOSObjectRelationAssertion(concept2, factory.getSKOSBroaderProperty(), concept1);
// add data relation on individuals, this is with respect to a vocabulary
SKOSAnnotation labelAnno = factory.getSKOSAnnotation(factory.getSKOSPrefLabelProperty().getURI(), "Some Label", "en");
SKOSAnnotation altLabelAnno = factory.getSKOSAnnotation(factory.getSKOSAltLabelProperty().getURI(), "Another Label", "en");
SKOSAnnotationAssertion assertion2 = factory.getSKOSAnnotationAssertion(concept2, labelAnno);
SKOSAnnotationAssertion assertion3 = factory.getSKOSAnnotationAssertion(concept2, altLabelAnno);
/* creating a sub property of altLabel */
SKOSAnnotationProperty newAltLabel = factory.getSKOSAnnotationProperty(URI.create("http://myexample/altLable/newSubAltLabel"));
SKOSAltLabelProperty altLabel = factory.getSKOSAltLabelProperty();
SKOStoOWLConverter converter = new SKOStoOWLConverter();
OWLOntologyManager owlMan = man.getOWLManger();
OWLDataFactory owlFact = owlMan.getOWLDataFactory();
OWLSubAnnotationPropertyOfAxiom axiom = owlFact.getOWLSubAnnotationPropertyOfAxiom(converter.getAsOWLAnnotationProperty(newAltLabel), converter.getAsOWLAnnotationProperty(altLabel));
try {
owlMan.applyChange(new AddAxiom(converter.getAsOWLOntology(vocab), axiom));
} catch (OWLOntologyChangeException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
/*
* The full legal use of SKOS documantation propertiiies canno't be supported by this implmentation with the OWL API
* The documantation properties can be used as either data or object properties, depending on how they are being used
*/
// SKOS documentation properties as object properties
// we might want to create some abritary resource, e.g. a web page that is used as a scope note
SKOSResource someResource = factory.getSKOSResource(URI.create("http://www.wikipedia.org/someExample"));
SKOSObjectRelationAssertion assertion4 = factory.getSKOSObjectRelationAssertion(concept2, factory.getSKOSScopeNoteObjectProperty(), someResource);
// we might want to use the documentation properties with a filler that is a literal
SKOSDataRelationAssertion assertion5 = factory.getSKOSDataRelationAssertion(concept2, factory.getSKOSScopeNoteDataProperty(), "A scope note for the concept", "en");
/* Annotations - you can assign any kind of annotation (e.g. dublic core) to your entities
* here is an example adding a dc:creator and a rdfs:comment
*/
SKOSAnnotation anno1 = factory.getSKOSAnnotation(DublinCoreVocabulary.DATE.getIRI().toURI(), "12-07-2008");
SKOSAnnotation anno2 = factory.getSKOSAnnotation(DublinCoreVocabulary.CREATOR.getIRI().toURI(), "Simon Jupp", "en");
SKOSAnnotation anno3 = factory.getSKOSAnnotation(URI.create("http://my-custom-annotation.com/example"), someResource);
SKOSAnnotation anno4 = factory.getSKOSAnnotation(DublinCoreVocabulary.CREATOR.getIRI().toURI(), factory.getSKOSUntypedConstant("Simon Jupp", "en"));
// todo need to work on typed on objects
//factory.getSKOSAnnotationsByURI(DublinCoreVocabulary.CREATOR.getURI(), factory.getSKOSTypedConstant("String", "Simon Jupp"));
SKOSAnnotationAssertion assertion6 = factory.getSKOSAnnotationAssertion(concept2, anno1);
SKOSAnnotationAssertion assertion7 = factory.getSKOSAnnotationAssertion(concept2, anno2);
SKOSAnnotationAssertion assertion8 = factory.getSKOSAnnotationAssertion(concept2, anno3);
SKOSAnnotationAssertion assertion9 = factory.getSKOSAnnotationAssertion(concept2, anno4);
/* Add some utility stuff fomr creating single or multiple inScheme objects,
* these are afetr all just ObjectPropertyAssertions */
// or add it to concept shceme in a paryicular vocab
// SKOSInSchemeAssertion insSchemeAss = factory.getSKOSInSchemeAssertion(vocab, concept1, scheme);
// this can also takes sets
// SKOSInSchemeAssertion insSchemeAss = factory.getSKOSInsCHEMEAssertion(vocab, Set <SKOSConcept> concept1, scheme);
List<SKOSChange> addList = new ArrayList<SKOSChange>();
addList.add(new AddAssertion(vocab, schemaAss));
addList.add(new AddAssertion(vocab, inScheme));
addList.add(new AddAssertion(vocab, inScheme1));
addList.add(new AddAssertion(vocab, conAss1));
addList.add(new AddAssertion(vocab, conAss2));
addList.add(new AddAssertion(vocab, topConcept));
addList.add(new AddAssertion(vocab, assertion1));
addList.add(new AddAssertion(vocab, assertion2));
addList.add(new AddAssertion(vocab, assertion3));
addList.add(new AddAssertion(vocab, assertion4));
addList.add(new AddAssertion(vocab, assertion5));
addList.add(new AddAssertion(vocab, assertion6));
addList.add(new AddAssertion(vocab, assertion7));
addList.add(new AddAssertion(vocab, assertion8));
addList.add(new AddAssertion(vocab, assertion9));
try {
// man.applyChange(new AddAssertion(vocab, schemaAss));
// man.applyChange(new AddAssertion(vocab, inScheme));
// man.applyChange(new AddAssertion(vocab, inScheme1));
// man.applyChange(new AddAssertion(vocab, conAss1));
// man.applyChange(new AddAssertion(vocab, conAss2));
// man.applyChange(new AddAssertion(vocab, topConcept));
// man.applyChange(new AddAssertion(vocab, assertion1));
// man.applyChange(new AddAssertion(vocab, assertion2));
// man.applyChange(new AddAssertion(vocab, assertion3));
man.applyChanges(addList);
System.err.println("writing ontology");
man.save(vocab, SKOSFormatExt.RDFXML, URI.create("file:/Users/jupp/tmp/testskos/testskos.rdf"));
} catch (SKOSChangeException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SKOSStorageException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//
// // create a Label relation...
// List<OWLConstant> labels = new ArrayList<OWLConstant>();
// labels.add(factory.getOWLUntypedConstant("Some Label"));
// labels.add(factory.getOWLUntypedConstant("Another Label"));
//
// SKOSLabelRelation labRel = factory.getSKOSLabelRelation(URI.create(baseURI + "labelRel1"), labels);
// vocab.addSKOSLabelRelation(labRel);
//
// SKOSObjectRelationAssertion assertion4 = factory.getSKOSLabelRelationAssertion(concept2, labRel);
//
System.out.println("Read");
// get all the concept schemes
for (SKOSConceptScheme sch : vocab.getSKOSConceptSchemes()) {
System.err.println("Concept Scheme_______");
// get all the concepts in that scheme
for (SKOSConcept co : vocab.getConceptsInScheme(sch)) {
System.out.println("\tConcept: " + co.getURI());
// get any pref labels
for (SKOSAnnotation con : co.getSKOSAnnotationsByURI(vocab, factory.getSKOSPrefLabelProperty().getURI())) {
if (con.getAnnotationValueAsConstant() instanceof SKOSUntypedLiteral) {
SKOSUntypedLiteral unCon = con.getAnnotationValueAsConstant().getAsSKOSUntypedLiteral();
System.out.println("\t\tPrefLabel: " + unCon.getLiteral() + " lang: " + unCon.getLang());
}
else if (con.getAnnotationValueAsConstant() instanceof SKOSTypedLiteral) {
SKOSTypedLiteral unCon = con.getAnnotationValueAsConstant().getAsSKOSTypedLiteral();
System.out.println("\t\tPrefLabel: " + unCon.getLiteral());
}
}
// get definitions
for (SKOSObject object : co.getSKOSRelatedEntitiesByProperty(vocab, factory.getSKOSDefinitionObjectProperty())) {
if (object instanceof SKOSLiteral) {
}
else if (object instanceof SKOSEntity) {
}
}
// get broader concepts from all schemes
for (SKOSEntity c : co.getSKOSRelatedEntitiesByProperty(vocab, factory.getSKOSBroaderProperty())) {
System.out.println("\t\thasBroader: " + c.getURI());
}
// get label relations
// for (SKOSLabelRelation rel : co.getSKOSLabelRelations(vocab)) {
//
// Iterator it = rel.getLabels().iterator();
// while (it.hasNext()) {
// OWLConstant con = (OWLConstant) it.next();
// System.out.println("Releated Labels: " + con.getLiteral() );
// }
// }
}
}
//
// // do a simple lookup
// SKOSEntity entity = vocab.getSKOSEntity("Another Label");
// System.out.println("Entity lookup : " + entity.asSKOSConcept().getAsOWLIndividual().getURI());
//
//
//
// } catch (OWLOntologyChangeException e) {
// e.printStackTrace();
// } catch (OWLOntologyStorageException e) {
// e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
// }
}
}