View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, 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  package com.ontotext.ordi.wsmo4rdf.impl;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.omwg.logicalexpression.LogicalExpression;
28  import org.omwg.ontology.Attribute;
29  import org.omwg.ontology.Axiom;
30  import org.omwg.ontology.Concept;
31  import org.omwg.ontology.DataValue;
32  import org.omwg.ontology.Instance;
33  import org.omwg.ontology.Ontology;
34  import org.omwg.ontology.Parameter;
35  import org.omwg.ontology.Relation;
36  import org.omwg.ontology.RelationInstance;
37  import org.omwg.ontology.Value;
38  import org.omwg.ontology.Variable;
39  import org.wsmo.common.Entity;
40  import org.wsmo.common.IRI;
41  import org.wsmo.common.Identifier;
42  import org.wsmo.common.TopEntity;
43  import org.wsmo.common.WSML;
44  import org.wsmo.common.exception.InvalidModelException;
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.Mediator;
50  import org.wsmo.mediator.OOMediator;
51  import org.wsmo.mediator.WGMediator;
52  import org.wsmo.mediator.WWMediator;
53  import org.wsmo.service.Capability;
54  import org.wsmo.service.Choreography;
55  import org.wsmo.service.Goal;
56  import org.wsmo.service.Interface;
57  import org.wsmo.service.Orchestration;
58  import org.wsmo.service.WebService;
59  import org.wsmo.wsml.ParserException;
60  
61  import com.ontotext.ordi.tripleset.TConnection;
62  import com.ontotext.ordi.wsmo4rdf.Constants;
63  import com.ontotext.ordi.wsmo4rdf.WSMLfromTriples;
64  
65  public class WSMLfromTriplesImpl implements WSMLfromTriples {
66  
67      private WsmoFactory wsmoFactory;
68      private LogicalExpressionFactory leFactory;
69      private WSMLConnectionAdapterImpl adapter;
70  
71      public WSMLfromTriplesImpl(WsmoFactory wsmoFactory,
72              LogicalExpressionFactory leFactory, TConnection storeConnection) {
73          if (wsmoFactory == null || leFactory == null || storeConnection == null) {
74              throw new IllegalArgumentException(
75                      "Null is not allowed for this constructor!");
76          }
77          if (storeConnection.isOpen() == false) {
78              throw new IllegalArgumentException(
79                      "The provided connection is not opened!");
80          }
81  
82          this.wsmoFactory = wsmoFactory;
83          this.leFactory = leFactory;
84          this.adapter = new WSMLConnectionAdapterImpl(storeConnection);
85      }
86  
87      public Set<Entity> construct(Identifier id) {
88          Set<Entity> entities = new HashSet<Entity>();
89          WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
90                  wsmoFactory.createIRI(Constants.RDF_type), (Identifier) null);
91          if (iter.hasNext()) {
92              WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
93              if (t.getObject() instanceof IRI) {
94                  Class<? extends Entity> cls = Util.identifierToClass((IRI) t
95                          .getObject());
96                  if (cls != null)
97                      entities.add(construct(id, cls));
98              }
99          }
100         iter.close();
101         return entities;
102     }
103 
104     public Entity construct(Identifier id, Class<? extends Entity> cls) {
105         if (cls.isAssignableFrom(Ontology.class)) {
106             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
107                     wsmoFactory.createIRI(Constants.WSML_ontology)).hasNext() == false)
108                 return null;
109             return _constructOntology(id);
110         } else if (cls.isAssignableFrom(Axiom.class)) {
111             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
112                     wsmoFactory.createIRI(Constants.WSML_axiom)).hasNext() == false)
113                 return null;
114             return _constructAxiom(id);
115         } else if (cls.isAssignableFrom(Concept.class)) {
116             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
117                     wsmoFactory.createIRI(Constants.WSML_concept)).hasNext() == false)
118                 return null;
119             return _constructConcept(id);
120         } else if (cls.isAssignableFrom(Relation.class)) {
121             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
122                     wsmoFactory.createIRI(Constants.WSML_Relation)).hasNext() == false)
123                 return null;
124             return _constructRelation(id);
125         } else if (cls.isAssignableFrom(Instance.class)) {
126             if (adapter.search(id, null, null).hasNext() == false)
127                 return null;
128             return _constructInstance(id);
129         } else if (cls.isAssignableFrom(RelationInstance.class)) {
130             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
131                     wsmoFactory.createIRI(Constants.WSML_RelationInstance))
132                     .hasNext() == false)
133                 return null;
134             return _constructRelationInstance(id);
135         } else if (cls.isAssignableFrom(Attribute.class)) {
136             if (adapter.search(id,
137                     wsmoFactory.createIRI(Constants.WSML_forAttribute),
138                     wsmoFactory.createIRI(Constants.WSML_Attribute)).hasNext() == false)
139                 return null;
140             return _constructUnknownAttribute(id);
141         } else if (cls.isAssignableFrom(OOMediator.class)) {
142             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
143                     wsmoFactory.createIRI(Constants.WSML_ooMediator)).hasNext() == false)
144                 return null;
145             return _constructOOMediator(id);
146         } else if (cls.isAssignableFrom(GGMediator.class)) {
147             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
148                     wsmoFactory.createIRI(Constants.WSML_ggMediator)).hasNext() == false)
149                 return null;
150             return _constructGGMediator(id);
151         } else if (cls.isAssignableFrom(WWMediator.class)) {
152             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
153                     wsmoFactory.createIRI(Constants.WSML_wwMediator)).hasNext() == false)
154                 return null;
155             return _constructWWMediator(id);
156         } else if (cls.isAssignableFrom(WGMediator.class)) {
157             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
158                     wsmoFactory.createIRI(Constants.WSML_wgMediator)).hasNext() == false)
159                 return null;
160             return _constructWGMediator(id);
161         } else if (cls.isAssignableFrom(Capability.class)) {
162             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
163                     wsmoFactory.createIRI(Constants.WSML_Capability)).hasNext() == false)
164                 return null;
165             return _constructCapability(id);
166         } else if (cls.isAssignableFrom(Interface.class)) {
167             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
168                     wsmoFactory.createIRI(Constants.WSML_Interface)).hasNext() == false)
169                 return null;
170             return _constructInterface(id);
171         } else if (cls.isAssignableFrom(Goal.class)) {
172             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
173                     wsmoFactory.createIRI(Constants.WSML_goal)).hasNext() == false)
174                 return null;
175             return _constructGoal(id);
176         } else if (cls.isAssignableFrom(WebService.class)) {
177             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
178                     wsmoFactory.createIRI(Constants.WSML_webService)).hasNext() == false)
179                 return null;
180             return _constructWebService(id);
181         } else if (cls.isAssignableFrom(Choreography.class)) {
182             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
183                     wsmoFactory.createIRI(Constants.WSML_Choreography))
184                     .hasNext() == false)
185                 return null;
186             return _constructChoreography(id);
187         } else if (cls.isAssignableFrom(Orchestration.class)) {
188             if (adapter.search(id, wsmoFactory.createIRI(Constants.RDF_type),
189                     wsmoFactory.createIRI(Constants.WSML_Orchestration))
190                     .hasNext() == false)
191                 return null;
192             return _constructOrchestration(id);
193         }
194         throw new IllegalArgumentException("Unknown class IRI: "
195                 + cls.getName());
196     }
197 
198     protected void _processNFPs(Identifier id, Entity item) {
199         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
200                 null, (Identifier) null);
201         try {
202             while (iter.hasNext()) {
203                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
204                 IRI pred = t.getPredicate();
205                 if (adapter.hasStatement(pred, wsmoFactory
206                         .createIRI(Constants.RDF_type), wsmoFactory
207                         .createIRI(Constants.OWL_AnnotationProperty))) {
208                     Object v = t.getObject();
209                     if (v instanceof Value)
210                         item.addNFPValue(pred, (Value) v);
211                     else
212                         item.addNFPValue(t.getPredicate(), (Identifier) v);
213                 }
214             }
215         } catch (Exception e) {
216             e.printStackTrace();
217         } finally {
218             iter.close();
219         }
220     }
221 
222     private Entity _constructAxiom(Identifier id) {
223         Axiom item = wsmoFactory.createAxiom(id);
224         _processNFPs(id, item);
225         // owner
226         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
227                 wsmoFactory.createIRI(Constants.PART_hasPart_directly), id);
228         try {
229             while (iter.hasNext()) {
230                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
231                 // sanity check
232                 if (adapter.hasStatement(t.getSubject(), wsmoFactory
233                         .createIRI(Constants.RDF_type), wsmoFactory
234                         .createIRI(Constants.WSML_ontology)))
235                     item.setOntology(wsmoFactory.getOntology((IRI) t
236                             .getSubject()));
237             }
238         } catch (Exception ex) {
239             ex.printStackTrace();
240         } finally {
241             iter.close();
242         }
243         iter = adapter.search(id, wsmoFactory
244                 .createIRI(Constants.RDFS_isDefinedBy), (Identifier) null);
245         try {
246             while (iter.hasNext()) {
247                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
248                 LogicalExpression le = leFactory.createLogicalExpression(t
249                         .getObject().toString());
250                 item.addDefinition(le);
251             }
252         } catch (Exception ex) {
253             ex.printStackTrace();
254         } finally {
255             iter.close();
256         }
257         return item;
258     }
259 
260     private Entity _constructConcept(Identifier id) {
261         Concept item = wsmoFactory.createConcept(id);
262         _processNFPs(id, item);
263         // owner
264         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
265                 wsmoFactory.createIRI(Constants.PART_hasPart_directly), id);
266         try {
267             while (iter.hasNext()) {
268                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
269                 if (adapter.hasStatement(t.getSubject(), wsmoFactory
270                         .createIRI(Constants.RDF_type), wsmoFactory
271                         .createIRI(Constants.WSML_ontology)))
272                     item.setOntology(wsmoFactory.getOntology((IRI) t
273                             .getSubject()));
274             }
275         } catch (Exception ex) {
276             ex.printStackTrace();
277         } finally {
278             iter.close();
279         }
280         // superconcepts
281         iter = adapter.search(id, wsmoFactory
282                 .createIRI(Constants.RDFS_subClassOf), (Identifier) null);
283         try {
284             while (iter.hasNext()) {
285                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
286                 Concept superC = wsmoFactory.getConcept((Identifier) t
287                         .getObject());
288                 item.addSuperConcept(superC);
289             }
290         } catch (Exception ex) {
291             ex.printStackTrace();
292         } finally {
293             iter.close();
294         }
295 
296         // attributes
297         iter = adapter.search(id, wsmoFactory
298                 .createIRI(Constants.PART_hasPart_directly), (Identifier) null);
299         try {
300             while (iter.hasNext()) {
301                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
302                 Identifier node = (Identifier) t.getObject();
303                 Identifier attrId = adapter.searchForObject(node, wsmoFactory
304                         .createIRI(Constants.WSML_forAttribute),
305                         Identifier.class);
306                 _constructAttribute(item, attrId);
307             }
308         } catch (Exception ex) {
309             ex.printStackTrace();
310         } finally {
311             iter.close();
312         }
313         return item;
314     }
315 
316     private Entity _constructAttribute(Concept parent, Identifier id) {
317         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
318                 wsmoFactory.createIRI(Constants.WSML_forAttribute), id);
319         Identifier node = null;
320         try {
321             while (iter.hasNext()) {
322                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
323                 Identifier guessNode = t.getSubject();
324                 if (adapter.hasStatement(parent.getIdentifier(), wsmoFactory
325                         .createIRI(Constants.PART_hasPart_directly), guessNode)) {
326                     node = guessNode;
327                     break;
328                 }
329             }
330         } catch (Exception ex) {
331             ex.printStackTrace();
332         } finally {
333             iter.close();
334         }
335         if (node == null)
336             throw new RuntimeException("no statements define attribute :"
337                     + id.toString() + " for concept "
338                     + parent.getIdentifier().toString());
339 
340         Attribute item = null;
341         try {
342             item = parent.createAttribute((IRI) id);
343         } catch (InvalidModelException ime) {
344             ime.printStackTrace();
345             return null;
346         }
347 
348         _processNFPs(node, item);
349 
350         iter = adapter.search(node, null, (Identifier) null);
351         try {
352             boolean isConstraining = false;
353             while (iter.hasNext()) {
354                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
355                 IRI predicate = t.getPredicate();
356                 if (0 == predicate.toString().compareTo(
357                         Constants.WSML_forAttribute.toString()))
358                     continue;
359                 // @todo: later, whether it was an attribute
360                 // if (0 ==
361                 // predicate.toString().compareTo(Constants.RDF_type.toString()))
362                 // continue;
363                 if (0 == predicate.toString().compareTo(
364                         Constants.RDF_type.toString())) {
365                     if (0 == t.getObject().toString().compareTo(
366                             Constants.WSML_transitiveAttributeDefinition
367                                     .toString()))
368                         item.setTransitive(true);
369                     if (0 == t.getObject().toString().compareTo(
370                             Constants.WSML_reflexiveAttributeDefinition
371                                     .toString()))
372                         item.setReflexive(true);
373                     if (0 == t.getObject().toString().compareTo(
374                             Constants.WSML_symmetricAttributeDefinition
375                                     .toString()))
376                         item.setSymmetric(true);
377                 }
378                 if (0 == predicate.toString().compareTo(
379                         Constants.WSML_inverseOf.toString())) {
380                     item.setInverseOf((IRI) t.getObject());
381                 }
382 
383                 if (0 == predicate.toString().compareTo(
384                         Constants.WSML_ofType.toString())) {
385                     isConstraining = true;
386                     item.addType(wsmoFactory.getConcept((IRI) t.getObject()));
387                 }
388                 if (0 == predicate.toString().compareTo(
389                         Constants.WSML_impliesType.toString())) {
390                     item.addType(wsmoFactory.getConcept((IRI) t.getObject()));
391                 }
392                 // cardinality handling
393                 if (0 == predicate.toString().compareTo(
394                         Constants.WSML_minCardinality.toString())) {
395                     int card = Integer.parseInt(t.getObject().toString());
396                     item.setMinCardinality(card);
397                 }
398                 if (0 == predicate.toString().compareTo(
399                         Constants.WSML_maxCardinality.toString())) {
400                     int card = Integer.parseInt(t.getObject().toString());
401                     item.setMaxCardinality(card);
402                 }
403             } // iter
404             item.setConstraining(isConstraining);
405         } catch (Exception ex) {
406             ex.printStackTrace();
407         } finally {
408             iter.close();
409         }
410         return item;
411     }
412 
413     private Entity _constructRelation(Identifier id) {
414         Relation item = wsmoFactory.createRelation(id);
415         _processNFPs(id, item);
416 
417         // owner
418         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
419                 wsmoFactory.createIRI(Constants.PART_hasPart_directly), id);
420         try {
421             while (iter.hasNext()) {
422                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
423                 if (adapter.hasStatement(t.getSubject(), wsmoFactory
424                         .createIRI(Constants.RDF_type), wsmoFactory
425                         .createIRI(Constants.WSML_ontology)))
426                     item.setOntology(wsmoFactory.getOntology((IRI) t
427                             .getSubject()));
428             }
429         } catch (Exception ex) {
430             ex.printStackTrace();
431         } finally {
432             iter.close();
433         }
434 
435         // cupreconcepts
436         iter = adapter.search(id, wsmoFactory
437                 .createIRI(Constants.WSML_subRelationOf), (Identifier) null);
438         try {
439             while (iter.hasNext()) {
440                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
441                 Relation superC = wsmoFactory.getRelation((Identifier) t
442                         .getObject());
443                 item.addSuperRelation(superC);
444             }
445         } catch (Exception ex) {
446             ex.printStackTrace();
447         } finally {
448             iter.close();
449         }
450 
451         // Parameters
452         Identifier node = adapter.searchForObject(id, wsmoFactory
453                 .createIRI(Constants.PART_hasPart_directly), Identifier.class);
454         if (node == null) {
455             // case when relation is super-type for another relation and there
456             // is no ontology information
457             return item;
458         }
459         Identifier last = null;
460         byte count = 0;
461         do {
462             if (last != null) {
463                 node = adapter.searchForObject(node, wsmoFactory
464                         .createIRI(Constants.RDF_rest), Identifier.class);
465             }
466             if (node.toString().equals(Constants.RDF_nil))
467                 break;
468             Identifier local = adapter.searchForObject(node, wsmoFactory
469                     .createIRI(Constants.RDF_first), Identifier.class);
470 
471             iter = adapter.search(local, null, (Identifier) null);
472             try {
473                 Parameter p = item.createParameter(count++);
474                 while (iter.hasNext()) {
475                     WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
476                     IRI pred = (IRI) t.getPredicate();
477                     if (0 == pred.toString().compareTo(
478                             Constants.WSML_ofType.toString())) {
479                         p.addType(wsmoFactory.getConcept((Identifier) t
480                                 .getObject()));
481                     }
482                     if (0 == pred.toString().compareTo(
483                             Constants.WSML_impliesType.toString())) {
484                         p.addType(wsmoFactory.getConcept((Identifier) t
485                                 .getObject()));
486                     }
487                 }
488             } catch (Exception ex2) {
489 
490             } finally {
491                 iter.close();
492             }
493             last = node;
494         } while (adapter.hasStatement(node, wsmoFactory
495                 .createIRI(Constants.RDF_rest), (Identifier) null));
496         return item;
497     } // relation
498 
499     private Entity _constructInstance(Identifier id) {
500         Instance item = wsmoFactory.createInstance(id);
501         _processNFPs(id, item);
502 
503         // owner
504         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
505                 wsmoFactory.createIRI(Constants.PART_hasPart_directly), id);
506         try {
507             while (iter.hasNext()) {
508                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
509                 if (adapter.hasStatement(t.getSubject(), wsmoFactory
510                         .createIRI(Constants.RDF_type), wsmoFactory
511                         .createIRI(Constants.WSML_ontology)))
512                     item.setOntology(wsmoFactory.getOntology((IRI) t
513                             .getSubject()));
514             }
515         } catch (Exception ex) {
516             ex.printStackTrace();
517         } finally {
518             iter.close();
519         }
520 
521         // supreconcepts
522         iter = adapter.search(id, null, (Identifier) null);
523         try {
524             while (iter.hasNext()) {
525                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
526                 IRI pred = (IRI) t.getPredicate();
527                 if (0 == pred.toString().compareTo(
528                         Constants.RDF_type.toString())) {
529                     Concept superC = wsmoFactory.getConcept((Identifier) t
530                             .getObject());
531                     item.addConcept(superC);
532                 } else if (adapter.hasStatement(pred, wsmoFactory
533                         .createIRI(Constants.RDF_type), wsmoFactory
534                         .createIRI(Constants.WSML_Attribute))) {
535                     // @todo:check if it is an attribute
536                     // Attribute attr = wsmoFactory.getAttribute(pred);
537                     Object ob = t.getObject();
538                     if (ob instanceof Identifier)
539                         item.addAttributeValue(pred, wsmoFactory
540                                 .getInstance((Identifier) ob));
541                     else
542                         item.addAttributeValue(pred, (Value) ob);
543                 }
544             }
545         } catch (Exception ex) {
546             ex.printStackTrace();
547         } finally {
548             iter.close();
549         }
550         return item;
551     }
552 
553     // @todo: check for (<last> rest rdf:nil) statement
554     private Entity _constructRelationInstance(Identifier id) {
555         ArrayList<Ontology> owners = new ArrayList<Ontology>();
556         // owner
557         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
558                 wsmoFactory.createIRI(Constants.PART_hasPart_directly), id);
559         try {
560             while (iter.hasNext()) {
561                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
562                 if (adapter.hasStatement(t.getSubject(), wsmoFactory
563                         .createIRI(Constants.RDF_type), wsmoFactory
564                         .createIRI(Constants.WSML_ontology)))
565                     owners.add(wsmoFactory.getOntology((IRI) t.getSubject()));
566             }
567         } catch (Exception ex) {
568             ex.printStackTrace();
569         } finally {
570             iter.close();
571         }
572 
573         IRI pred = wsmoFactory.createIRI(Constants.RDF_type);
574         Identifier relID = adapter.searchForObjectByJoin(id, pred, pred,
575                 wsmoFactory.createIRI(Constants.WSML_Relation));
576         if (relID == null)
577             return null;
578         Relation memberOf = (Relation) _constructRelation(relID);
579         RelationInstance item = null;
580         try {
581             item = wsmoFactory.createRelationInstance(id, memberOf);
582             _processNFPs(id, item);
583             Iterator<Ontology> iterOwners = owners.iterator();
584             while (iterOwners.hasNext()) {
585                 item.setOntology((Ontology) iterOwners.next());
586             }
587             item.setRelation(memberOf);
588         } catch (InvalidModelException ex) {
589             ex.printStackTrace();
590             return null;
591         }
592         // process paramvalues
593         Identifier node = adapter.searchForObject(id, wsmoFactory
594                 .createIRI(Constants.PART_hasPart_directly), Identifier.class);
595         byte count = 0;
596         try {
597             do {
598                 Object o = adapter.searchForObject(node, wsmoFactory
599                         .createIRI(Constants.RDF_first), Object.class);
600                 if (o instanceof Identifier)
601                     item.setParameterValue(count++, wsmoFactory
602                             .getInstance((Identifier) o));
603                 else
604                     item.setParameterValue(count++, (Value) o);
605                 node = adapter.searchForObject(node, wsmoFactory
606                         .createIRI(Constants.RDF_rest), Identifier.class);
607             } while (node != null && !node.toString().equals(Constants.RDF_nil));
608         } catch (Exception ex) {
609             ex.printStackTrace();
610         } finally {
611             // empty
612         }
613         return item;
614     }
615 
616     // //// mediators
617     protected void _processTopEntity(TopEntity item) {
618         Identifier id = item.getIdentifier();
619         _processNFPs(id, item);
620 
621         // set wsml variant
622         Object wsmlVariant = adapter.searchForObject(id, wsmoFactory
623                 .createIRI(Constants.WSML_variant), null);
624         if (wsmlVariant == null) {
625             item.setWsmlVariant(WSML.WSML_CORE);
626         }
627         item.setWsmlVariant(wsmlVariant.toString());
628 
629         // import ontologies
630         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
631                 wsmoFactory.createIRI(Constants.WSML_importsOntology),
632                 (Identifier) null);
633         try {
634             while (iter.hasNext()) {
635                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
636                 item.addOntology(wsmoFactory.getOntology((IRI) t.getObject()));
637             }
638         } catch (Exception ex) {
639             ex.printStackTrace();
640         } finally {
641             iter.close();
642         }
643         // uses mediator
644         iter = adapter.search(id, wsmoFactory
645                 .createIRI(Constants.WSML_usesMediator), (Identifier) null);
646         try {
647             while (iter.hasNext()) {
648                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
649                 item.addMediator((IRI) t.getObject());
650             }
651         } catch (Exception ex) {
652             ex.printStackTrace();
653         } finally {
654             iter.close();
655         }
656     }
657 
658     private Mediator _constructMediator(Mediator item) {
659         Identifier id = item.getIdentifier();
660         _processTopEntity(item);
661         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter
662                 .search(id, wsmoFactory.createIRI(Constants.WSML_source),
663                         (Identifier) null);
664         try {
665             while (iter.hasNext()) {
666                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
667                 item.addSource((IRI) t.getObject());
668             }
669         } catch (Exception ex) {
670             ex.printStackTrace();
671         } finally {
672             iter.close();
673         }
674         Identifier target = adapter.searchForObject(id, wsmoFactory
675                 .createIRI(Constants.WSML_target), Identifier.class);
676         try {
677             if (target != null)
678                 item.setTarget((IRI) target);
679         } catch (InvalidModelException ime) {
680             ime.printStackTrace();
681         }
682         Identifier usesService = adapter.searchForObject(id, wsmoFactory
683                 .createIRI(Constants.WSML_usesService), Identifier.class);
684         try {
685             if (target != null)
686                 item.setMediationService((IRI) usesService);
687         } catch (InvalidModelException ime) {
688             ime.printStackTrace();
689         }
690         return item;
691     }
692 
693     private Entity _constructOOMediator(Identifier id) {
694         OOMediator item = wsmoFactory.createOOMediator((IRI) id);
695         return _constructMediator(item);
696     }
697 
698     private Entity _constructWWMediator(Identifier id) {
699         WWMediator item = wsmoFactory.createWWMediator((IRI) id);
700         return _constructMediator(item);
701     }
702 
703     private Entity _constructWGMediator(Identifier id) {
704         WGMediator item = wsmoFactory.createWGMediator((IRI) id);
705         return _constructMediator(item);
706     }
707 
708     private Entity _constructGGMediator(Identifier id) {
709         GGMediator item = wsmoFactory.createGGMediator((IRI) id);
710         return _constructMediator(item);
711     }
712 
713     private Entity _constructOntology(Identifier id) {
714         Ontology item = wsmoFactory.createOntology((IRI) id);
715         _processTopEntity(item);
716         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
717                 wsmoFactory.createIRI(Constants.PART_hasPart_directly),
718                 (Identifier) null);
719         try {
720             while (iter.hasNext()) {
721                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
722                 Identifier ontologyItem = (Identifier) t.getObject();
723                 if (adapter.hasStatement(ontologyItem, wsmoFactory
724                         .createIRI(Constants.RDF_type), wsmoFactory
725                         .createIRI(Constants.WSML_axiom))) {
726                     item.addAxiom((Axiom) _constructAxiom(ontologyItem));
727                 } else if (adapter.hasStatement(ontologyItem, wsmoFactory
728                         .createIRI(Constants.RDF_type), wsmoFactory
729                         .createIRI(Constants.WSML_concept))) {
730                     item.addConcept((Concept) _constructConcept(ontologyItem));
731                 } else if (adapter.hasStatement(ontologyItem, wsmoFactory
732                         .createIRI(Constants.RDF_type), wsmoFactory
733                         .createIRI(Constants.WSML_Relation))) {
734                     item
735                             .addRelation((Relation) _constructRelation(ontologyItem));
736                 } else if (adapter.hasStatement(ontologyItem, wsmoFactory
737                         .createIRI(Constants.RDF_type), wsmoFactory
738                         .createIRI(Constants.WSML_RelationInstance))) {
739                     item
740                             .addRelationInstance((RelationInstance) _constructRelationInstance(ontologyItem));
741                 } else {
742                     // / add it as instance
743                     item
744                             .addInstance((Instance) _constructInstance(ontologyItem));
745                 }
746             }
747         } catch (Exception ex) {
748             ex.printStackTrace();
749         } finally {
750             iter.close();
751         }
752 
753         return item;
754     }
755 
756     private Entity _constructGoal(Identifier id) {
757         Goal item = wsmoFactory.createGoal((IRI) id);
758         _processTopEntity(item);
759 
760         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
761                 wsmoFactory.createIRI(Constants.PART_hasPart_directly),
762                 (Identifier) null);
763         try {
764             while (iter.hasNext()) {
765                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
766                 IRI goalPart = (IRI) t.getObject();
767                 if (adapter.hasStatement(goalPart, wsmoFactory
768                         .createIRI(Constants.RDF_type), wsmoFactory
769                         .createIRI(Constants.WSML_Capability))) {
770                     item
771                             .setCapability((Capability) _constructCapability(goalPart));
772                 } else if (adapter.hasStatement(goalPart, wsmoFactory
773                         .createIRI(Constants.RDF_type), wsmoFactory
774                         .createIRI(Constants.WSML_Interface))) {
775                     item
776                             .addInterface((Interface) _constructInterface(goalPart));
777                 }
778             }
779         } catch (Exception ex) {
780             ex.printStackTrace();
781         } finally {
782             iter.close();
783         }
784         return item;
785     }
786 
787     private Entity _constructWebService(Identifier id) {
788         WebService item = wsmoFactory.createWebService((IRI) id);
789         _processTopEntity(item);
790         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
791                 wsmoFactory.createIRI(Constants.PART_hasPart_directly),
792                 (Identifier) null);
793         try {
794             while (iter.hasNext()) {
795                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
796                 IRI servicePart = (IRI) t.getObject();
797                 if (adapter.hasStatement(servicePart, wsmoFactory
798                         .createIRI(Constants.RDF_type), wsmoFactory
799                         .createIRI(Constants.WSML_Capability))) {
800                     item
801                             .setCapability((Capability) _constructCapability(servicePart));
802                     _constructCapability(servicePart);
803                 } else if (adapter.hasStatement(servicePart, wsmoFactory
804                         .createIRI(Constants.RDF_type), wsmoFactory
805                         .createIRI(Constants.WSML_Interface))) {
806                     item
807                             .addInterface((Interface) _constructInterface(servicePart));
808                     _constructInterface(servicePart);
809                 }
810             }
811         } catch (Exception ex) {
812             ex.printStackTrace();
813         } finally {
814             iter.close();
815         }
816         return item;
817     }
818 
819     private Entity _constructCapability(Identifier id) {
820         Capability item = wsmoFactory.createCapability((IRI) id);
821         _processTopEntity(item);
822 
823         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(id,
824                 wsmoFactory.createIRI(Constants.WSML_sharedVariable),
825                 (Identifier) null);
826         try {
827             while (iter.hasNext()) {
828                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
829                 item.addSharedVariable((Variable) t.getObject());
830             }
831         } catch (Exception ex) {
832             ex.printStackTrace();
833         } finally {
834             iter.close();
835         }
836 
837         iter = adapter.search(id, wsmoFactory
838                 .createIRI(Constants.WSML_hasAssumption), (Identifier) null);
839         try {
840             while (iter.hasNext()) {
841                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
842                 item.addAssumption((Axiom) _constructAxiom((Identifier) t
843                         .getObject()));
844             }
845         } catch (Exception ex) {
846             ex.printStackTrace();
847         } finally {
848             iter.close();
849         }
850 
851         iter = adapter.search(id, wsmoFactory
852                 .createIRI(Constants.WSML_hasPrecondition), (Identifier) null);
853         try {
854             while (iter.hasNext()) {
855                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
856                 item.addPreCondition((Axiom) _constructAxiom((Identifier) t
857                         .getObject()));
858             }
859         } catch (Exception ex) {
860             ex.printStackTrace();
861         } finally {
862             iter.close();
863         }
864 
865         iter = adapter.search(id, wsmoFactory
866                 .createIRI(Constants.WSML_hasPostcondition), (Identifier) null);
867         try {
868             while (iter.hasNext()) {
869                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
870                 item.addPostCondition((Axiom) _constructAxiom((Identifier) t
871                         .getObject()));
872             }
873         } catch (Exception ex) {
874             ex.printStackTrace();
875         } finally {
876             iter.close();
877         }
878 
879         iter = adapter.search(id, wsmoFactory
880                 .createIRI(Constants.WSML_hasEffect), (Identifier) null);
881         try {
882             while (iter.hasNext()) {
883                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
884                 item.addEffect((Axiom) _constructAxiom((Identifier) t
885                         .getObject()));
886             }
887         } catch (Exception ex) {
888             ex.printStackTrace();
889         } finally {
890             iter.close();
891         }
892 
893         return item;
894     }
895 
896     private Entity _constructInterface(Identifier id) {
897         Interface item = wsmoFactory.createInterface((IRI) id);
898         _processTopEntity(item);
899         Identifier orch = adapter.searchForObject(id, wsmoFactory
900                 .createIRI(Constants.WSML_Orchestration), Identifier.class);
901         if (orch != null)
902             item.setOrchestration(item.createOrchestration(orch));
903         Identifier chor = adapter.searchForObject(id, wsmoFactory
904                 .createIRI(Constants.WSML_Choreography), Identifier.class);
905         if (adapter.hasStatement(chor, wsmoFactory.createIRI("urn:"
906                 + org.wsmo.service.choreography.Choreography.class.getName()),
907                 (Identifier) null)) {
908             DataValue value = adapter.searchForObject(chor, wsmoFactory.createIRI("urn:"
909                             + org.wsmo.service.choreography.Choreography.class
910                                     .getName()), DataValue.class);
911             item.setChoreography(_createChoreographyFromString(value.getValue().toString()));
912         } else {
913             if (chor != null)
914                 item.setChoreography(item.createChoreography(chor));
915         }
916         return item;
917     }
918 
919     private Entity _constructOrchestration(Identifier id) {
920         return null;
921     }
922 
923     private Entity _constructChoreography(Identifier id) {
924         return null;
925     }
926 
927     private Entity _constructUnknownAttribute(Identifier id) {
928         WSMLConnectionAdapterImpl.IteratorAdapter iter = adapter.search(null,
929                 wsmoFactory.createIRI(Constants.WSML_forAttribute), id);
930         Identifier node = null;
931         try {
932             while (iter.hasNext()) {
933                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
934                 node = t.getSubject();
935             }
936         } catch (Exception ex) {
937             ex.printStackTrace();
938         } finally {
939             iter.close();
940         }
941         if (node == null)
942             return null;
943         Concept theHolder = null;
944         iter = adapter.search(null, wsmoFactory
945                 .createIRI(Constants.WSML_attribute_definition), node);
946         try {
947             while (iter.hasNext()) {
948                 theHolder = wsmoFactory.getConcept(iter.next().getSubject());
949                 break;
950             }
951         } catch (Exception ex) {
952             ex.printStackTrace();
953         } finally {
954             iter.close();
955         }
956         if (theHolder != null)
957             return _constructAttribute(theHolder, id);
958         return null;
959     }
960 
961     private org.wsmo.service.Choreography _createChoreographyFromString(
962             String wsml) {
963         StringBuffer buffer = new StringBuffer();
964         // write the rule information + the CompoundFact text
965         buffer
966                 .append("webService _\"http://not_existing9876543210987654321#w321\"\r\n");
967         buffer
968                 .append("interface _\"http://not_existing9876543210987654321#i321\"\r\n");
969         buffer.append(wsml);
970         // parse the input
971         try {
972             Map<String, Object> params = new HashMap<String, Object>();
973             params.put(Factory.PROVIDER_CLASS, 
974                     "org.deri.wsmo4j.io.parser.wsml.ParserImpl");
975             org.wsmo.wsml.Parser parser = Factory.createParser(params);
976             parser.parse(buffer);
977             Interface iface = wsmoFactory.getInterface(wsmoFactory
978                     .createIRI("http://not_existing9876543210987654321#i321"));
979             return iface.getChoreography();
980         } catch (RuntimeException ex) {
981             throw new RuntimeException(
982                     "Implementation of choreography could not be found!", ex);
983         } catch (ParserException ex) {
984             throw new RuntimeException(
985                     "Internal error in the deserialization of String:\n"
986                             + buffer.toString() + "!", ex);
987         } catch (InvalidModelException ex) {
988             throw new RuntimeException(
989                     "Internal error in the deserialization!", ex);
990         }
991     }
992 }