View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5    
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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   * <p>
43   * Title:
44   * </p>
45   * <p>
46   * Description:
47   * </p>
48   * <p>
49   * Copyright: Copyright (c) 2004-2005
50   * </p>
51   * <p>
52   * Company: Ontotext Lab., Sirma AI
53   * </p>
54   * 
55   * @author not attributable
56   * @version 1.0
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      // use default implementation for factory
73      factory = Factory.createWsmoFactory(null);
74      dataFactory = Factory.createDataFactory(null);
75    }
76  
77    private Ontology createSimpleOntology() throws InvalidModelException {
78      // TODO: TO BE UPDATED!
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      // add NFP values
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      // create a concept human
90      Concept human = factory.createConcept(factory.createIRI(nsDefault, "Human"));
91  
92      // create attribute of concept hasName of type wsmlString
93      Attribute attributeName = human.createAttribute(factory.createIRI(nsDefault, "hasName"));
94      attributeName.addType(dataFactory.createWsmlDataType(WsmlDataType.WSML_STRING));
95  
96      // create attribute of concept hasAge of type wsmlInteger
97      Attribute attributeAge = human.createAttribute(factory.createIRI(nsDefault, "hasAge"));
98      attributeAge.addType(dataFactory.createWsmlDataType(WsmlDataType.WSML_INTEGER));
99  
100     // create attribute of concept hasChild of type Human inverseOf(hasParent)
101     Attribute attributeChild = human.createAttribute(factory.createIRI(nsDefault, "hasChild"));
102     attributeChild.addType(human);
103     attributeChild.setInverseOf(factory.createIRI(nsDefault, "hasParent"));
104 
105     // create attribute of concept hasParent of type Human inverseOf(hasChild)
106     Attribute attributeParent = human.createAttribute(factory.createIRI(nsDefault, "hasParent"));
107     attributeParent.addType(human);
108     attributeParent.setInverseOf(factory.createIRI(nsDefault, "hasChild"));
109 
110     // create atrribute of concept isMarriedTo of type Human symmetric
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     // create a concept man and woman
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     // create a concept Country
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     // create relation CitizenOf with parameters Human and Country
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     // create Man with name Joe age 28, wife Mary and son Paul
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     // create Woman with name Joe age 27, husband Joe and son Paul
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     // create Man with name Paul age 4 and parent Mary and Joe
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     // create countries UK, France
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     // Joe would be from UK
163     RelationInstance relInstanceJoe = factory.createRelationInstance(citizenOf);
164     relInstanceJoe.setParameterValue((byte) 0, paul);
165     relInstanceJoe.setParameterValue((byte) 1, uk);
166 
167     // Mary from France
168     RelationInstance relInstanceMary = factory.createRelationInstance(citizenOf);
169     relInstanceMary.setParameterValue((byte) 0, mary);
170     relInstanceMary.setParameterValue((byte) 1, france);
171 
172     // Paul from UK
173     RelationInstance relInstancePaul = factory.createRelationInstance(citizenOf);
174     relInstancePaul.setParameterValue((byte) 0, joe);
175     relInstancePaul.setParameterValue((byte) 1, uk);
176 
177     // add all ontology elements to the ontology
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  * $Log$
207  * Revision 1.6  2007/06/06 07:27:54  lcekov
208  * Add main method to the example
209  *
210  * Revision 1.5  2007/06/06 07:15:52  lcekov
211  * example had compilation errors
212  *
213  * Revision 1.4  2007/06/06 06:59:40  lcekov
214  * This is commit with errors in one of the examples. It should trigger automatic build of wsmo4j project
215  * Revision 1.3 2006/02/10 14:41:38
216  * vassil_momtchev examples updated
217  * 
218  * Revision 1.2 2005/09/23 12:20:42 holgerlausen *** empty log message ***
219  * 
220  * Revision 1.1 2005/09/19 10:20:22 vassil_momtchev inherite OntologyExample
221  * 
222  */