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.factory;
20  
21  /**
22   * <p>Title: WSMO4J</p>
23   * <p>Description: WSMO API and a Reference Implementation</p>
24   * <p>Copyright:  Copyright (c) 2004-2005</p>
25   * <p>Company: OntoText Lab. / SIRMA </p>
26   * @author not attributable
27   * @version 1.0
28   *
29   */
30  
31  import java.lang.reflect.*;
32  import java.util.*;
33  
34  import org.omwg.ontology.*;
35  import org.wsmo.common.*;
36  import org.wsmo.common.exception.*;
37  import org.wsmo.factory.*;
38  import org.wsmo.mediator.*;
39  import org.wsmo.service.*;
40  
41  import com.ontotext.wsmo4j.service.*;
42  import com.ontotext.wsmo4j.ontology.*;
43  import com.ontotext.wsmo4j.common.*;
44  import com.ontotext.wsmo4j.mediator.*;
45  
46  
47  public class WsmoFactoryImpl
48      implements WsmoFactory {
49  
50      static EntityRegistry getReg() {
51          return EntityRegistry.get();
52      }
53  
54      public WsmoFactoryImpl(Map <String, Object> map) {
55          getReg();
56          Factory.getLocatorManager().addLocator(getReg());
57      }
58  
59      public Goal createGoal(IRI id) {
60          _validateIdentifier(id);
61          Goal alredyUsed = (Goal)_prologCreate(id, Goal.class);
62          if (alredyUsed != null) {
63              IDReference ref = _checkNotResolved(alredyUsed);
64              if (ref == null)
65                  return alredyUsed;
66              ref.setDelegate(new GoalImpl(id));
67              return alredyUsed;
68          }
69          alredyUsed = new GoalImpl(id);
70          getReg().registerEntity(alredyUsed);
71          return alredyUsed;
72      }
73  
74      public WebService createWebService(IRI id) {
75          _validateIdentifier(id);
76  
77          WebService alredyUsed = (WebService)_prologCreate(id, WebService.class);
78          if (alredyUsed != null) {
79              IDReference ref = _checkNotResolved(alredyUsed);
80              if (ref == null)
81                  return alredyUsed;
82              ref.setDelegate(new WebServiceImpl(id));
83              return alredyUsed;
84          }
85          alredyUsed = new WebServiceImpl(id);
86          getReg().registerEntity(alredyUsed);
87          return alredyUsed;
88      }
89  
90      public Capability createCapability(IRI id) {
91          _validateIdentifier(id);
92          Capability alredyUsed = (Capability)_prologCreate(id, Capability.class);
93          if (alredyUsed != null) {
94              IDReference ref = _checkNotResolved(alredyUsed);
95              if (ref == null)
96                  return alredyUsed;
97              ref.setDelegate(new CapabilityImpl(id));
98              return alredyUsed;
99          }
100         alredyUsed = new CapabilityImpl(id);
101         getReg().registerEntity(alredyUsed);
102         return alredyUsed;
103     }
104 
105     public Interface createInterface(IRI id) {
106         _validateIdentifier(id);
107         Interface alredyUsed = (Interface)_prologCreate(id, Interface.class);
108         if (alredyUsed != null) {
109             IDReference ref = _checkNotResolved(alredyUsed);
110             if (ref == null)
111                 return alredyUsed;
112             ref.setDelegate(new InterfaceImpl(id));
113             return alredyUsed;
114         }
115         alredyUsed = new InterfaceImpl(id);
116         getReg().registerEntity(alredyUsed);
117         return alredyUsed;
118     }
119 
120     public Ontology createOntology(IRI id) {
121         _validateIdentifier(id);
122         Ontology alredyUsed = (Ontology)_prologCreate(id, Ontology.class);
123         if (alredyUsed != null) {
124             IDReference ref = _checkNotResolved(alredyUsed);
125             if (ref == null)
126                 return alredyUsed;
127             ref.setDelegate(new OntologyImpl(id));
128             return alredyUsed;
129         }
130         alredyUsed = new OntologyImpl(id);
131         getReg().registerEntity(alredyUsed);
132         return alredyUsed;
133     }
134 
135 
136     public OOMediator createOOMediator(IRI id) {
137         OOMediator alredyUsed = (OOMediator)_prologCreate(id, OOMediator.class);
138         if (alredyUsed != null) {
139             IDReference ref = _checkNotResolved(alredyUsed);
140             if (ref == null)
141                 return alredyUsed;
142             ref.setDelegate(new OOMediatorImpl(id));
143             return alredyUsed;
144         }
145         alredyUsed = new OOMediatorImpl(id);
146         getReg().registerEntity(alredyUsed);
147         return alredyUsed;
148     }
149     public WWMediator createWWMediator(IRI id) {
150         WWMediator alredyUsed = (WWMediator)_prologCreate(id, WWMediator.class);
151         if (alredyUsed != null) {
152             IDReference ref = _checkNotResolved(alredyUsed);
153             if (ref == null)
154                 return alredyUsed;
155             ref.setDelegate(new WWMediatorImpl(id));
156             return alredyUsed;
157         }
158         alredyUsed = new WWMediatorImpl(id);
159         getReg().registerEntity(alredyUsed);
160         return alredyUsed;
161     }
162     public WGMediator createWGMediator(IRI id) {
163         WGMediator alredyUsed = (WGMediator)_prologCreate(id, WGMediator.class);
164         if (alredyUsed != null) {
165             IDReference ref = _checkNotResolved(alredyUsed);
166             if (ref == null)
167                 return alredyUsed;
168             ref.setDelegate(new WGMediatorImpl(id));
169             return alredyUsed;
170         }
171         alredyUsed = new WGMediatorImpl(id);
172         getReg().registerEntity(alredyUsed);
173         return alredyUsed;
174     }
175     public GGMediator createGGMediator(IRI id) {
176         GGMediator alredyUsed = (GGMediator)_prologCreate(id, GGMediator.class);
177         if (alredyUsed != null) {
178             IDReference ref = _checkNotResolved(alredyUsed);
179             if (ref == null)
180                 return alredyUsed;
181             ref.setDelegate(new GGMediatorImpl(id));
182             return alredyUsed;
183         }
184         alredyUsed = new GGMediatorImpl(id);
185         getReg().registerEntity(alredyUsed);
186         return alredyUsed;
187     }
188 
189     public Axiom createAxiom(Identifier id) {
190         _validateIdentifier(id);
191         Axiom alredyUsed = (Axiom)_prologCreate(id, Axiom.class);
192         if (alredyUsed != null) {
193             IDReference ref = _checkNotResolved(alredyUsed);
194             if (ref == null)
195                 return alredyUsed;
196             ref.setDelegate(new AxiomImpl(id));
197             return alredyUsed;
198         }
199         alredyUsed = new AxiomImpl(id);
200         getReg().registerEntity(alredyUsed);
201         return alredyUsed;
202     }
203 
204     public Concept createConcept(Identifier id) {
205         _validateIdentifier(id);
206         Concept alredyUsed = (Concept)_prologCreate(id, Concept.class);
207         if (alredyUsed != null) {
208             IDReference ref = _checkNotResolved(alredyUsed);
209             if (ref == null)
210                 return alredyUsed;
211             ref.setDelegate(new ConceptImpl(id, this));
212             return alredyUsed;
213         }
214         alredyUsed = new ConceptImpl(id, this);
215         getReg().registerEntity(alredyUsed);
216         return alredyUsed;
217     }
218 
219     public Relation createRelation(Identifier id) {
220         _validateIdentifier(id);
221 
222         Relation alredyUsed = (Relation)_prologCreate(id, Relation.class);
223         if (alredyUsed != null) {
224             IDReference ref = _checkNotResolved(alredyUsed);
225             if (ref == null)
226                 return alredyUsed;
227             ref.setDelegate(new RelationImpl(id));
228             return alredyUsed;
229         }
230         alredyUsed = new RelationImpl(id);
231         getReg().registerEntity(alredyUsed);
232         return alredyUsed;
233     }
234 
235     public Instance createInstance(Identifier id) {
236         _validateIdentifier(id);
237 
238         Instance alredyUsed = (Instance)_prologCreate(id, Instance.class);
239         if (alredyUsed != null) {
240             IDReference ref = _checkNotResolved(alredyUsed);
241             if (ref == null)
242                 return alredyUsed;
243             ref.setDelegate(new InstanceImpl(id));
244             return alredyUsed;
245         }
246         alredyUsed = new InstanceImpl(id);
247         getReg().registerEntity(alredyUsed);
248         return alredyUsed;
249     }
250 
251     //@todo: fix the exception that should be thrown
252     /**
253      * Method is not part of the interface. It's internally used by the ConceptImpl
254      * to register the Attribute to the EntityRegistry
255      * @param attribute to add to the list of the created objects
256      */
257     public void createAttribute(Attribute attribute) {
258         _validateIdentifier(attribute.getIdentifier());
259         Attribute alredyUsed = (Attribute)_prologCreate(attribute.getIdentifier(), Attribute.class);
260         if (alredyUsed != null) {
261             IDReference ref = _checkNotResolved(alredyUsed);
262             if (ref == null)
263                 return;
264             ref.setDelegate(attribute);
265             return;
266         }
267         alredyUsed = attribute;
268         getReg().registerEntity(alredyUsed);
269         return;
270     }
271 
272     public Instance createInstance(Identifier id, Concept concept)
273         throws SynchronisationException, InvalidModelException {
274 
275         Instance result = createInstance(id);
276         result.addConcept(concept);
277 
278         return result;
279     }
280 
281     public RelationInstance createRelationInstance(Relation rel)
282             throws SynchronisationException, InvalidModelException {
283         Identifier anonId = createAnonymousID();
284         return createRelationInstance(anonId, rel);
285     }
286 
287     public RelationInstance createRelationInstance(Identifier id, Relation rel)
288         throws SynchronisationException, InvalidModelException {
289         _validateIdentifier(id);
290         RelationInstance alredyUsed = (RelationInstance)_prologCreate(id, RelationInstance.class);
291         if (alredyUsed != null) {
292             IDReference ref = _checkNotResolved(alredyUsed);
293             if (ref == null) {
294                 // The following lines fix an inconsistency exposed after ClearTopEntity#clearTopEntity().
295                 // The problem is caused by inability to untie the connection RelationInstance -> Relation
296                 // and this disables automatic adding the reverse connection on rebuilding.
297                 if (false == rel.listRelationInstances().contains(alredyUsed)) {
298                     rel.addRelationInstance(alredyUsed);
299                 }
300                 return alredyUsed;
301             }
302             ref.setDelegate(new RelationInstanceImpl(id, rel));
303             return alredyUsed;
304         }
305         alredyUsed = new RelationInstanceImpl(id, rel);
306         getReg().registerEntity(alredyUsed);
307         return alredyUsed;       
308     }
309 
310     public Namespace createNamespace(String prefix, IRI iri) {
311         if (null == iri) {
312             throw new IllegalArgumentException("Illegal namespace URI supplied: null value ...");
313         }
314 
315         return new NamespaceImpl(prefix, iri);
316 
317     }
318 
319     public IRI createIRI(String fullIRI) {
320 
321         if (null == fullIRI || 0 == fullIRI.trim().length()) {
322             throw new IllegalArgumentException("Illegal IRI supplied: ["+ fullIRI +"] ...");
323         }
324 
325         return new IRIImpl(fullIRI);
326     }
327 
328     public IRI createIRI(Namespace ns, String localPart) {
329         
330         if (ns == null) {
331             throw new IllegalArgumentException("Namespace not supplied: it can't be null.");
332         }
333         if (null == localPart 
334                 || 0 == localPart.trim().length()) {
335             throw new IllegalArgumentException("Illegal local name supplied.");
336         }
337         StringBuffer rawIRI = new StringBuffer();
338         String nsIRI = ns.getIRI().toString();
339         rawIRI.append(nsIRI);
340         if (false == nsIRI.endsWith("#")
341                 && false == nsIRI.endsWith("/")) {
342             rawIRI.append('#');
343         }
344         rawIRI.append(localPart);
345         return new IRIImpl(rawIRI.toString());
346     }
347 
348     public UnnumberedAnonymousID createAnonymousID() {
349         return new UnnumberedAnonymousIDImpl();
350     }
351     
352     private void _validateIdentifier(Identifier id) {
353         //@todo - add more checks, e.g. whether an entity with such ID already exists?
354         if (null == id) {
355             throw new IllegalArgumentException("Invalid ID supplied: null value ...");
356         }
357     }
358 
359 
360     private Object _prologCreate(Identifier id, Class iface) {
361         Object o = null;
362         try {
363             o = getReg().isRegistered(id, iface);
364         } catch (RuntimeException rte) {
365             rte.printStackTrace();
366             throw new SynchronisationException(rte);
367         }
368         return o;
369     }
370 
371     private IDReference _checkNotResolved(Entity obj) {
372         if (! (obj instanceof Proxy) )
373             return null;
374         Object ref = Proxy.getInvocationHandler(obj);
375         if (ref instanceof IDReference) {
376             if (((IDReference)ref).isResolved())
377                 return null;
378             return (IDReference)ref;
379         }
380         throw new SynchronisationException();
381     }
382 
383     public Goal getGoal(IRI id) {
384         Entity inst = null;
385         try {
386              inst = getReg().isRegistered(id, Goal.class);
387             if (inst == null) {
388                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Goal.class});
389                 getReg().registerEntity(inst);
390             }
391         } catch (RuntimeException rte) {
392             throw new SynchronisationException(rte);
393         }
394         return (Goal)inst;
395     }
396     public WebService getWebService(IRI id) {
397         Entity inst = null;
398         try {
399              inst = getReg().isRegistered(id, WebService.class);
400             if (inst == null) {
401                 inst = (Entity)IDReference.createNewReference(id, new Class[]{WebService.class});
402                 getReg().registerEntity(inst);
403             }
404         } catch (RuntimeException rte) {
405             throw new SynchronisationException(rte);
406         }
407         return (WebService)inst;
408     }
409     public Interface getInterface(IRI id) {
410         Entity inst = null;
411         try {
412              inst = getReg().isRegistered(id, Interface.class);
413             if (inst == null) {
414                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Interface.class});
415                 getReg().registerEntity(inst);
416             }
417         } catch (RuntimeException rte) {
418             throw new SynchronisationException(rte);
419         }
420         return (Interface)inst;
421     }
422     public Capability getCapability(IRI id) {
423         Entity inst = null;
424         try {
425              inst = getReg().isRegistered(id, Capability.class);
426             if (inst == null) {
427                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Capability.class});
428                 getReg().registerEntity(inst);
429             }
430         } catch (RuntimeException rte) {
431             throw new SynchronisationException(rte);
432         }
433         return (Capability)inst;
434     }
435     public Ontology getOntology(IRI id) {
436         Entity inst = null;
437         try {
438              inst = getReg().isRegistered(id, Ontology.class);
439             if (inst == null) {
440                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Ontology.class});
441                 getReg().registerEntity(inst);
442             }
443         } catch (RuntimeException rte) {
444             throw new SynchronisationException(rte);
445         }
446         return (Ontology)inst;
447     }
448     public OOMediator getOOMediator(IRI id) {
449         Entity inst = null;
450         try {
451              inst = getReg().isRegistered(id, OOMediator.class);
452             if (inst == null) {
453                 inst = (Entity)IDReference.createNewReference(id, new Class[]{OOMediator.class});
454                 getReg().registerEntity(inst);
455             }
456         } catch (RuntimeException rte) {
457             throw new SynchronisationException(rte);
458         }
459         return (OOMediator)inst;
460     }
461     public WWMediator getWWMediator(IRI id) {
462         Entity inst = null;
463         try {
464              inst = getReg().isRegistered(id, WWMediator.class);
465             if (inst == null) {
466                 inst = (Entity)IDReference.createNewReference(id, new Class[]{WWMediator.class});
467                 getReg().registerEntity(inst);
468             }
469         } catch (RuntimeException rte) {
470             throw new SynchronisationException(rte);
471         }
472         return (WWMediator)inst;
473     }
474     public WGMediator getWGMediator(IRI id) {
475         Entity inst = null;
476         try {
477              inst = getReg().isRegistered(id, WGMediator.class);
478             if (inst == null) {
479                 inst = (Entity)IDReference.createNewReference(id, new Class[]{WGMediator.class});
480                 getReg().registerEntity(inst);
481             }
482         } catch (RuntimeException rte) {
483             throw new SynchronisationException(rte);
484         }
485         return (WGMediator)inst;
486     }
487     public GGMediator getGGMediator(IRI id) {
488         Entity inst = null;
489         try {
490              inst = getReg().isRegistered(id, GGMediator.class);
491             if (inst == null) {
492                 inst = (Entity)IDReference.createNewReference(id, new Class[]{GGMediator.class});
493                 getReg().registerEntity(inst);
494             }
495         } catch (RuntimeException rte) {
496             throw new SynchronisationException(rte);
497         }
498         return (GGMediator)inst;
499     }
500     public Axiom getAxiom(Identifier id) {
501         Entity inst = null;
502         try {
503              inst = getReg().isRegistered(id, Axiom.class);
504             if (inst == null) {
505                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Axiom.class});
506                 getReg().registerEntity(inst);
507             }
508         } catch (RuntimeException rte) {
509             throw new SynchronisationException(rte);
510         }
511         return (Axiom)inst;
512     }
513     public Concept getConcept(Identifier id) {
514         Entity inst = null;
515         try {
516              inst = getReg().isRegistered(id, Concept.class);
517             if (inst == null) {
518                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Concept.class});
519                 getReg().registerEntity(inst);
520             }
521         } catch (RuntimeException rte) {
522             throw new SynchronisationException(rte);
523         }
524         return (Concept)inst;
525     }
526     public Instance getInstance(Identifier id) {
527         Entity inst = null;
528         try {
529              inst = getReg().isRegistered(id, Instance.class);
530             if (inst == null) {
531                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Instance.class});
532                 getReg().registerEntity(inst);
533             }
534 
535         } catch (RuntimeException rte) {
536             throw new SynchronisationException(rte);
537         }
538         return (Instance)inst;
539     }
540     public Relation getRelation(Identifier id) {
541         Entity inst = null;
542         try {
543              inst = getReg().isRegistered(id, Relation.class);
544             if (inst == null) {
545                 inst = (Entity)IDReference.createNewReference(id, new Class[]{Relation.class});
546                 getReg().registerEntity(inst);
547             }
548         } catch (RuntimeException rte) {
549             throw new SynchronisationException(rte);
550         }
551         return (Relation)inst;
552     }
553     public RelationInstance getRelationInstance(Identifier id) {
554         Entity inst = null;
555         try {
556              inst = getReg().isRegistered(id, RelationInstance.class);
557             if (inst == null) {
558                 inst = (Entity)IDReference.createNewReference(id, new Class[]{RelationInstance.class});
559                 getReg().registerEntity(inst);
560             }
561         } catch (RuntimeException rte) {
562             throw new SynchronisationException(rte);
563         }
564         return (RelationInstance)inst;
565     }
566 
567     /**
568      * Return existing Varible instance
569      * @param name The name of the var
570      * @return existing var
571      */
572     public Variable getVariable(String name) {
573         throw new UnsupportedOperationException();
574     }
575 
576 }