1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.wsmo4rdf.impl;
19
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.util.Set;
23 import java.util.UUID;
24
25 import org.omwg.ontology.Attribute;
26 import org.omwg.ontology.Axiom;
27 import org.omwg.ontology.Concept;
28 import org.omwg.ontology.DataValue;
29 import org.omwg.ontology.Instance;
30 import org.omwg.ontology.Ontology;
31 import org.omwg.ontology.Relation;
32 import org.omwg.ontology.RelationInstance;
33 import org.omwg.ontology.WsmlDataType;
34 import org.openrdf.model.Literal;
35 import org.openrdf.model.Resource;
36 import org.openrdf.model.URI;
37 import org.openrdf.model.Value;
38 import org.openrdf.model.ValueFactory;
39 import org.openrdf.model.impl.ValueFactoryImpl;
40 import org.wsmo.common.Entity;
41 import org.wsmo.common.IRI;
42 import org.wsmo.common.Identifier;
43 import org.wsmo.common.TopEntity;
44 import org.wsmo.factory.DataFactory;
45 import org.wsmo.factory.Factory;
46 import org.wsmo.factory.LogicalExpressionFactory;
47 import org.wsmo.factory.WsmoFactory;
48 import org.wsmo.mediator.GGMediator;
49 import org.wsmo.mediator.OOMediator;
50 import org.wsmo.mediator.WGMediator;
51 import org.wsmo.mediator.WWMediator;
52 import org.wsmo.service.Capability;
53 import org.wsmo.service.Choreography;
54 import org.wsmo.service.Goal;
55 import org.wsmo.service.Interface;
56 import org.wsmo.service.Orchestration;
57 import org.wsmo.service.WebService;
58 import org.wsmo.wsml.Parser;
59
60 import com.ontotext.ordi.wsmo4rdf.Constants;
61
62 public class Util {
63
64 private static ValueFactory vFactory = new ValueFactoryImpl();
65 private static WsmoFactory wsmoFactory = Factory.createWsmoFactory(null);
66 private static LogicalExpressionFactory leFactory = Factory
67 .createLogicalExpressionFactory(null);
68 private static DataFactory dataFactory = Factory.createDataFactory(null);
69 private static Parser wsmoParser = Factory.createParser(null);
70
71 /**
72 * Replace the default DataFactory implementation.
73 *
74 * @param factory
75 * instance to be used
76 */
77 public static void setDataFactory(DataFactory factory) {
78 if (factory == null) {
79 throw new IllegalArgumentException();
80 }
81 Util.dataFactory = factory;
82 }
83
84 public static URI toURI(Object wsmlValue) {
85 if (wsmlValue == null) {
86 return null;
87 } else if (wsmlValue instanceof IRI) {
88 return vFactory.createURI(((IRI) wsmlValue).toString());
89 } else if (wsmlValue instanceof Identifier) {
90 return vFactory.createURI("urn:uuid:" + UUID.randomUUID());
91 } else if (wsmlValue instanceof Entity) {
92 return toURI(((Instance) wsmlValue).getIdentifier());
93 }
94 throw new IllegalArgumentException("Unsupported type supplied!");
95 }
96
97 public static Value toValue(Object wsmlValue) {
98 if (wsmlValue == null) {
99 return null;
100 } else if (wsmlValue instanceof IRI) {
101 return vFactory.createURI(((IRI) wsmlValue).toString());
102 } else if (wsmlValue instanceof Identifier) {
103 return vFactory.createURI("urn:uuid:" + UUID.randomUUID());
104 } else if (wsmlValue instanceof Entity) {
105 return toValue(((Instance) wsmlValue).getIdentifier());
106 } else if (wsmlValue instanceof DataValue) {
107
108 return vFactory.createLiteral(wsmlValue.toString());
109 }
110 return vFactory.createLiteral(wsmlValue.toString());
111 }
112
113 public static Value toValue(Object wsmlValue, URI type) {
114 String value = wsmlValue.toString();
115 return vFactory.createLiteral(value, type);
116 }
117
118 public static Identifier toIdentifier(Resource resource) {
119 if (resource == null) {
120 return null;
121 } else if (resource instanceof URI) {
122 return wsmoFactory.createIRI(((URI) resource).toString());
123 } else {
124 return wsmoFactory.createAnonymousID();
125 }
126 }
127
128 public static Object toValue(Value value) {
129 if (value == null) {
130 return null;
131 } else if (value instanceof Resource) {
132 return (Object) toIdentifier((Resource) value);
133 } else if (value instanceof Literal) {
134 String val = ((Literal) value).getLabel();
135 if (val.startsWith("?") && isValidVariableName(val.substring(1))) {
136 return leFactory.createVariable(val);
137 }
138 return createDataValueFromLiteral((Literal) value);
139 }
140 throw new IllegalArgumentException(
141 "Should never happens in correct RDF model!");
142 }
143
144 private static DataValue createDataValueFromLiteral(Literal literal) {
145 WsmlDataType type = null;
146 URI data = literal.getDatatype();
147 if (data != null) {
148 try {
149 type = dataFactory.createWsmlDataType(data.toString());
150 } catch (IllegalArgumentException iae) {
151 return dataFactory.createWsmlString(literal.getLabel());
152 }
153 try {
154 return dataFactory.createDataValueFromJavaObject(type, literal
155 .getLabel());
156 } catch (IllegalArgumentException iae) {
157 String lit = literal.getLabel();
158 try {
159 return dataFactory.createWsmlInteger(lit);
160 } catch (NumberFormatException nfe) {
161 try {
162 return dataFactory.createWsmlDecimal(lit);
163 } catch (NumberFormatException nfe2) {
164 return dataFactory.createWsmlString(lit);
165 }
166 }
167 }
168 } else {
169 String lit = literal.getLabel();
170 try {
171 return dataFactory.createWsmlInteger(lit);
172 } catch (NumberFormatException nfe) {
173 try {
174 return dataFactory.createWsmlDecimal(lit);
175 } catch (NumberFormatException nfe2) {
176 int pos = lit.indexOf('(');
177 if (pos > 0) {
178 try {
179 String toParse = "ontology _\"urn:foo\" nfp _\"urn:foo\" hasValue "
180 + lit + " endnfp";
181
182 Reader reader = new StringReader(toParse);
183 TopEntity[] topEntities = null;
184 try {
185 topEntities = wsmoParser.parse(reader);
186 } catch (Exception ex) {
187 }
188 Ontology onto = (Ontology) topEntities[0];
189 Set<Object> set = onto.listNFPValues(wsmoFactory
190 .createIRI("urn:foo"));
191 return (DataValue) set.iterator().next();
192 } catch (Exception ex) {
193 return dataFactory.createWsmlString(lit);
194 }
195 } else
196 return dataFactory.createWsmlString(lit);
197 }
198 }
199 }
200 }
201
202 public static Class<? extends Entity> identifierToClass(Identifier id) {
203 if (id == null) {
204 return null;
205 } else if (id.toString().equals(Constants.WSML_ontology)) {
206 return Ontology.class;
207 } else if (id.toString().equals(Constants.WSML_axiom)) {
208 return Axiom.class;
209 } else if (id.toString().equals(Constants.WSML_concept)) {
210 return Concept.class;
211 } else if (id.toString().equals(Constants.WSML_Relation)) {
212 return Relation.class;
213 } else if (id.toString().equals(Constants.WSML_RelationInstance)) {
214 return RelationInstance.class;
215 } else if (id.toString().equals(Constants.WSML_attribute_definition)) {
216 return Attribute.class;
217 } else if (id.toString().equals(Constants.WSML_ooMediator)) {
218 return OOMediator.class;
219 } else if (id.toString().equals(Constants.WSML_ggMediator)) {
220 return GGMediator.class;
221 } else if (id.toString().equals(Constants.WSML_wwMediator)) {
222 return WWMediator.class;
223 } else if (id.toString().equals(Constants.WSML_wgMediator)) {
224 return WGMediator.class;
225 } else if (id.toString().equals(Constants.WSML_Capability)) {
226 return Capability.class;
227 } else if (id.toString().equals(Constants.WSML_Interface)) {
228 return Interface.class;
229 } else if (id.toString().equals(Constants.WSML_goal)) {
230 return Goal.class;
231 } else if (id.toString().equals(Constants.WSML_webService)) {
232 return WebService.class;
233 } else if (id.toString().equals(Constants.WSML_Choreography)) {
234 return Choreography.class;
235 } else if (id.toString().equals(Constants.WSML_Orchestration)) {
236 return Orchestration.class;
237 }
238 return null;
239 }
240
241 private static boolean isValidVariableName(String name) {
242 for (int i = 0; i < name.length(); i++) {
243 if (!isValidVariableChar(name.charAt(i))) {
244 return false;
245 }
246 }
247 return true;
248 }
249
250 private static boolean isValidVariableChar(char chr) {
251 return
252 (chr >= '\u0041' && chr <= '\u005A')
253 || (chr >= '\u0061' && chr <= '\u007A')
254 ||
255
256 (chr >= '\u4E00' && chr <= '\u9FA5') || (chr == '\u3007')
257 || (chr >= '\u3021' && chr <= '\u3029') ||
258
259 (chr >= '\u0030' && chr <= '\u0039');
260 }
261 }