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.*;
22 import java.util.*;
23
24 import org.omwg.ontology.*;
25 import org.wsmo.common.*;
26 import org.wsmo.common.exception.*;
27 import org.wsmo.factory.*;
28 import org.wsmo.wsml.*;
29
30
31
32
33
34
35
36
37
38
39 public class LoadOntologyExample {
40
41 private WsmoFactory factory;
42
43 private LogicalExpressionFactory leFactory;
44
45 private DataFactory dataFactory;
46
47 private Parser parser;
48
49 private Serializer serializer;
50
51 public static void showExample() throws InvalidModelException {
52 LoadOntologyExample example = new LoadOntologyExample();
53 example.createFactoryParserSerializer();
54 example.serializeParseOntology();
55 }
56
57 private void createFactoryParserSerializer() {
58 HashMap <String, Object> props = new HashMap <String, Object>();
59
60
61 factory = Factory.createWsmoFactory(null);
62 leFactory = Factory.createLogicalExpressionFactory(null);
63 dataFactory = Factory.createDataFactory(null);
64
65 props.put(Factory.WSMO_FACTORY, factory);
66 props.put(Factory.LE_FACTORY, leFactory);
67 props.put(Factory.DATA_FACTORY, dataFactory);
68
69 parser = Factory.createParser(props);
70 serializer = Factory.createSerializer(null);
71 }
72
73 private void serializeParseOntology() throws InvalidModelException {
74
75
76 Ontology ontology = CreateOntologyExample.showExample();
77
78 try {
79
80 StringWriter writer = new StringWriter();
81 serializer.serialize(new TopEntity[] { ontology }, writer);
82 System.out.println(writer.getBuffer().toString());
83
84
85 TopEntity[] entities = parser.parse(new StringReader(writer.getBuffer().toString()));
86 if (entities.length != 1) {
87 throw new RuntimeException("The number of parsed entities is not 1!");
88 }
89 }
90 catch (IOException ex) {
91
92 }
93 catch (ParserException ex) {
94 System.out.println("Invalid WSML token encountered at line " + ex.getErrorLine()
95 + " position " + ex.getErrorPos());
96 }
97 }
98
99 public static void main(String[] args) throws InvalidModelException {
100 showExample();
101 }
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118