1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ontotext.wsmo4j.examples;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.HashMap;
24
25 import org.omwg.ontology.Attribute;
26 import org.omwg.ontology.Concept;
27 import org.omwg.ontology.Instance;
28 import org.omwg.ontology.Ontology;
29 import org.omwg.ontology.Parameter;
30 import org.omwg.ontology.Relation;
31 import org.omwg.ontology.RelationInstance;
32 import org.omwg.ontology.WsmlDataType;
33 import org.wsmo.common.Namespace;
34 import org.wsmo.common.TopEntity;
35 import org.wsmo.common.exception.InvalidModelException;
36 import org.wsmo.factory.DataFactory;
37 import org.wsmo.factory.Factory;
38 import org.wsmo.factory.WsmoFactory;
39 import org.wsmo.wsml.Serializer;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class CreateOntologyExample {
60
61 private WsmoFactory factory;
62
63 private DataFactory dataFactory;
64
65 public static Ontology showExample() throws InvalidModelException {
66 CreateOntologyExample example = new CreateOntologyExample();
67 example.createFactory();
68 return example.createSimpleOntology();
69 }
70
71 private void createFactory() {
72
73 factory = Factory.createWsmoFactory(null);
74 dataFactory = Factory.createDataFactory(null);
75 }
76
77 private Ontology createSimpleOntology() throws InvalidModelException {
78
79 Ontology ontology = factory.createOntology(factory.createIRI("http://www.example.org/ontologies/example"));
80 Namespace nsDefault = factory.createNamespace("default", factory
81 .createIRI("http://www.example.org/ontologies/example"));
82 Namespace nsDC = factory.createNamespace("dc", factory.createIRI("http://purl.org/dc/elements/1.1"));
83
84
85 ontology.addNFPValue(factory.createIRI(nsDefault, "title"), dataFactory.createWsmlString("WSML example ontology"));
86 ontology.addNFPValue(factory.createIRI(nsDefault, "date"), dataFactory.createWsmlDate(2005, 9, 15, 12, 30));
87 ontology.addNFPValue(factory.createIRI(nsDC, "rights"), factory.createIRI("http://www.deri.org/privacy.html"));
88
89
90 Concept human = factory.createConcept(factory.createIRI(nsDefault, "Human"));
91
92
93 Attribute attributeName = human.createAttribute(factory.createIRI(nsDefault, "hasName"));
94 attributeName.addType(dataFactory.createWsmlDataType(WsmlDataType.WSML_STRING));
95
96
97 Attribute attributeAge = human.createAttribute(factory.createIRI(nsDefault, "hasAge"));
98 attributeAge.addType(dataFactory.createWsmlDataType(WsmlDataType.WSML_INTEGER));
99
100
101 Attribute attributeChild = human.createAttribute(factory.createIRI(nsDefault, "hasChild"));
102 attributeChild.addType(human);
103 attributeChild.setInverseOf(factory.createIRI(nsDefault, "hasParent"));
104
105
106 Attribute attributeParent = human.createAttribute(factory.createIRI(nsDefault, "hasParent"));
107 attributeParent.addType(human);
108 attributeParent.setInverseOf(factory.createIRI(nsDefault, "hasChild"));
109
110
111 Attribute attributeMarriedTo = human.createAttribute(factory.createIRI(nsDefault, "isMarriedTo"));
112 attributeMarriedTo.addType(human);
113 attributeMarriedTo.setSymmetric(true);
114 attributeMarriedTo.setMinCardinality(0);
115 attributeMarriedTo.setMaxCardinality(1);
116
117
118 Concept man = factory.createConcept(factory.createIRI(nsDefault, "Man"));
119 man.addSuperConcept(human);
120 Concept woman = factory.createConcept(factory.createIRI(nsDefault, "Woman"));
121 woman.addSuperConcept(human);
122
123
124 Concept country = factory.createConcept(factory.createIRI(nsDefault, "Country"));
125 Attribute attributeCountrName = country.createAttribute(factory.createIRI(nsDefault, "hasCountryName"));
126 attributeCountrName.addType(dataFactory.createWsmlDataType(WsmlDataType.WSML_STRING));
127
128
129 Relation citizenOf = factory.createRelation(factory.createIRI(nsDefault, "CitizenOf"));
130 Parameter parameterHuman = citizenOf.createParameter((byte) 0);
131 parameterHuman.addType(human);
132 Parameter parameterCountry = citizenOf.createParameter((byte) 1);
133 parameterCountry.addType(country);
134
135
136 Instance joe = factory.createInstance(factory.createIRI(nsDefault, "Joe"), man);
137 joe.addAttributeValue(attributeName.getIdentifier(), dataFactory.createWsmlString("Joe"));
138 joe.addAttributeValue(attributeAge.getIdentifier(), dataFactory.createWsmlInteger("28"));
139 joe.addAttributeValue(attributeChild.getIdentifier(), factory.getInstance(factory.createIRI(nsDefault, "Paul")));
140 joe.addAttributeValue(attributeMarriedTo.getIdentifier(), factory.getInstance(factory.createIRI(nsDefault, "Mary")));
141
142
143 Instance mary = factory.createInstance(factory.createIRI(nsDefault, "Mary"), woman);
144 mary.addAttributeValue(attributeName.getIdentifier(), dataFactory.createWsmlString("Mary"));
145 mary.addAttributeValue(attributeAge.getIdentifier(), dataFactory.createWsmlInteger("27"));
146 mary.addAttributeValue(attributeChild.getIdentifier(), factory.getInstance(factory.createIRI(nsDefault, "Paul")));
147 mary.addAttributeValue(attributeMarriedTo.getIdentifier(), factory.getInstance(factory.createIRI(nsDefault, "Joe")));
148
149
150 Instance paul = factory.createInstance(factory.createIRI(nsDefault, "Paul"), man);
151 paul.addAttributeValue(attributeName.getIdentifier(), dataFactory.createWsmlString("John"));
152 paul.addAttributeValue(attributeAge.getIdentifier(), dataFactory.createWsmlInteger("4"));
153 paul.addAttributeValue(attributeParent.getIdentifier(), factory.getInstance(factory.createIRI(nsDefault, "Joe")));
154 paul.addAttributeValue(attributeParent.getIdentifier(), factory.getInstance(factory.createIRI(nsDefault, "Mary")));
155
156
157 Instance uk = factory.createInstance(factory.createIRI(nsDefault, "UK"), country);
158 uk.addAttributeValue(attributeCountrName.getIdentifier(), dataFactory.createWsmlString("UK"));
159 Instance france = factory.createInstance(factory.createIRI(nsDefault, "France"), country);
160 france.addAttributeValue(attributeCountrName.getIdentifier(), dataFactory.createWsmlString("France"));
161
162
163 RelationInstance relInstanceJoe = factory.createRelationInstance(citizenOf);
164 relInstanceJoe.setParameterValue((byte) 0, paul);
165 relInstanceJoe.setParameterValue((byte) 1, uk);
166
167
168 RelationInstance relInstanceMary = factory.createRelationInstance(citizenOf);
169 relInstanceMary.setParameterValue((byte) 0, mary);
170 relInstanceMary.setParameterValue((byte) 1, france);
171
172
173 RelationInstance relInstancePaul = factory.createRelationInstance(citizenOf);
174 relInstancePaul.setParameterValue((byte) 0, joe);
175 relInstancePaul.setParameterValue((byte) 1, uk);
176
177
178 ontology.addConcept(human);
179 ontology.addConcept(man);
180 ontology.addConcept(woman);
181 ontology.addConcept(country);
182 ontology.addRelation(citizenOf);
183 ontology.addInstance(joe);
184 ontology.addInstance(mary);
185 ontology.addInstance(paul);
186 ontology.addInstance(uk);
187 ontology.addInstance(france);
188 ontology.addRelationInstance(relInstanceJoe);
189 ontology.addRelationInstance(relInstanceMary);
190 ontology.addRelationInstance(relInstancePaul);
191
192 return ontology;
193 }
194
195 public static void main(String[] args) throws InvalidModelException, IOException {
196 Ontology ontology = CreateOntologyExample.showExample();
197
198 Serializer serializer = Factory.createSerializer(new HashMap<String, Object>(0));
199 serializer.serialize(new TopEntity[] {ontology}, new PrintWriter(System.out));
200 }
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222