View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2006, University of Innsbruck, Austria
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 org.deri.wsmo4j.common;
19  
20  import java.util.*;
21  
22  import org.omwg.logicalexpression.*;
23  import org.omwg.ontology.*;
24  import org.wsmo.common.*;
25  import org.wsmo.common.exception.*;
26  import org.wsmo.mediator.*;
27  import org.wsmo.service.*;
28  
29  /**
30   * This utility class cleans a given TopEntity from all its
31   * previous definitions (if any).
32   *
33   * <pre>
34   * Created on 10.04.2006
35   * Committed by $Author$
36   * $Source$,
37   * </pre>
38   *
39   * @author Holger Lausen (holger.lausen@deri.org)
40   *
41   * @version $Revision$ $Date$
42   */
43  public class ClearTopEntity {
44      
45      
46      public static void clearTopEntity(Mediator m) throws SynchronisationException, InvalidModelException{
47          if (m == null){
48              return;
49          }
50          clearCommonElements(m);
51          if (m.listSources()!=null){
52              for (Iterator <IRI> mediatorI = new ArrayList <IRI>(m.listSources()).iterator(); mediatorI.hasNext();){
53                  m.removeSource(mediatorI.next());
54              }
55          }
56          m.setTarget(null);
57          m.setMediationService(null);
58      }
59      
60      public static void clearTopEntity(ServiceDescription s) throws SynchronisationException, InvalidModelException{
61          if (s == null){
62              return;
63          }
64          clearCommonElements(s);
65          if (s.listInterfaces() != null){
66              for (Iterator <Interface> interfaceI = new ArrayList <Interface> (s.listInterfaces()).iterator(); interfaceI.hasNext();){
67                  Interface i = interfaceI.next();
68                  clearNfp(i.getChoreography());
69                  clearNfp(i.getOrchestration());
70                  i.setChoreography(null);
71                  i.setOrchestration(null);
72                  
73                  s.removeInterface(i);
74              }
75          }
76          Capability cap = s.getCapability();
77          if (cap==null){
78              return;
79          }
80          clearCommonElements(cap);
81          //shared Variables
82          if (cap.listSharedVariables()!=null){
83              for (Iterator <Variable> varI = new ArrayList <Variable>(cap.listSharedVariables()).iterator();varI.hasNext();){
84                  cap.removeSharedVariable(varI.next());
85              }
86          }
87          //assumption
88          if (cap.listAssumptions()!=null){
89              for (Iterator <Axiom> i = new ArrayList <Axiom>(cap.listAssumptions()).iterator();i.hasNext();){
90                  Axiom a = i.next();
91                  clearNfp(a);
92                  cap.removeAssumption(a);
93              }
94          }
95          if (cap.listEffects()!=null){
96              for (Iterator <Axiom> i = new ArrayList <Axiom>(cap.listEffects()).iterator();i.hasNext();){
97                  Axiom a = i.next();
98                  clearNfp(a);
99                  cap.removeEffect(a);
100             }
101         }
102         if (cap.listPostConditions()!=null){
103             for (Iterator <Axiom> i = new ArrayList <Axiom>(cap.listPostConditions()).iterator();i.hasNext();){
104                 Axiom a = i.next();
105                 clearNfp(a);
106                 cap.removePostCondition(a);
107             }
108         }
109         if (cap.listPreConditions()!=null){
110             for (Iterator <Axiom> i = new ArrayList <Axiom>(cap.listPreConditions()).iterator();i.hasNext();){
111                 Axiom a = i.next();
112                 clearNfp(a);
113                 cap.removePreCondition(a);
114             }
115         }
116         s.setCapability(null);
117     }
118     
119     public static void clearTopEntity(Ontology ont) throws SynchronisationException, InvalidModelException{
120         if (ont == null){
121             return;
122         }
123         //clear concepts
124         if (ont.listConcepts()!=null){
125             for (Iterator <Concept> conceptI = new ArrayList <Concept>(ont.listConcepts()).iterator(); conceptI.hasNext(); ){
126                 Concept concept = conceptI.next();
127                 if (concept.listSuperConcepts()!=null){
128                     for (Iterator <Concept> superI = new ArrayList <Concept>(concept.listSuperConcepts()).iterator(); superI.hasNext(); ){
129                         concept.removeSuperConcept(superI.next());
130                     }
131                 }
132                 if (concept.listAttributes()!=null){
133                     for (Iterator <Attribute> attrI = new ArrayList <Attribute>(concept.listAttributes()).iterator(); attrI.hasNext(); ){
134                         Attribute attr = attrI.next();
135                         attr.setConstraining(false);
136                         attr.setInverseOf(null);
137                         attr.setReflexive(false);
138                         attr.setSymmetric(false);
139                         attr.setReflexive(false);
140                         attr.setTransitive(false);
141                         attr.setMaxCardinality(Integer.MAX_VALUE);
142                         attr.setMinCardinality(0);
143                         for (Iterator <Type> typesI = new ArrayList <Type>(attr.listTypes()).iterator(); typesI.hasNext();){
144                            attr.removeType(typesI.next());
145                         }
146                         concept.removeAttribute(attr);
147                         clearNfp(attr);
148                     }
149                 }
150                 clearNfp(concept);
151                 concept.setOntology(null);
152             }
153         }
154         //clear instances
155         if (ont.listInstances()!=null){
156             for (Iterator <Instance> instanceI = new ArrayList <Instance>(ont.listInstances()).iterator(); instanceI.hasNext();){
157                 Instance instance = instanceI.next();
158                 if (instance.listConcepts()!=null){
159                     for (Iterator <Concept> memberOfI = new ArrayList <Concept>(instance.listConcepts()).iterator(); memberOfI.hasNext();){
160                         instance.removeConcept(memberOfI.next());
161                     }
162                 }
163                 if (instance.listAttributeValues()!=null){
164                     for (Iterator <Identifier> attributeI = new ArrayList <Identifier> (instance.listAttributeValues().keySet()).iterator(); attributeI.hasNext();){
165                         instance.removeAttributeValues(attributeI.next());
166                     }
167                 }
168                 clearNfp(instance);
169                 instance.setOntology(null);
170             }
171         }
172         
173         //clear RelationInstances
174         if (ont.listRelationInstances()!=null){
175             for (Iterator <RelationInstance> instanceI = new ArrayList <RelationInstance> (ont.listRelationInstances()).iterator();instanceI.hasNext();){
176                 RelationInstance instance = instanceI.next();
177                 if (instance.listParameterValues()!=null){
178                     for (byte i = (byte)(instance.listParameterValues().size()-1); i>=0; i--){
179                         instance.setParameterValue(i,null);
180                     }
181                 }
182                 clearNfp(instance);
183                 instance.setOntology(null);
184             }
185         }
186 
187         //clear Relations
188         if (ont.listRelations()!=null){
189             for (Iterator <Relation> relI = new ArrayList <Relation> (ont.listRelations()).iterator();relI.hasNext();){
190                 Relation rel = relI.next();
191                 if (rel.listParameters()!=null){
192                     for (byte i = (byte)(rel.listParameters().size()-1); i>=0; i--){
193                         rel.removeParameter(i);
194                     }
195                 }
196                 if (rel.listSuperRelations()!=null){
197                     for (Iterator <Relation> superI = new ArrayList <Relation> (rel.listSuperRelations()).iterator();superI.hasNext();){
198                         rel.removeSuperRelation(superI.next());
199                     }
200                 }
201                 if (rel.listRelationInstances()!= null) {
202                 	for (Iterator <RelationInstance> relInstI = new ArrayList <RelationInstance> (rel.listRelationInstances()).iterator();relInstI.hasNext();) {
203                 		rel.removeRelationInstance(relInstI.next());
204                 	}
205                 }
206                 clearNfp(rel);
207                 rel.setOntology(null);
208             }
209         }
210         
211         //clearAxioms
212         if(ont.listAxioms()!=null){
213             for (Iterator <Axiom>  axiomI = new ArrayList <Axiom> (ont.listAxioms()).iterator();axiomI.hasNext();){
214                 Axiom a = axiomI.next();
215                 for (Iterator <LogicalExpression> defI = new ArrayList <LogicalExpression> (a.listDefinitions()).iterator(); defI.hasNext();){
216                     a.removeDefinition(defI.next());
217                 }
218                 clearNfp(a);
219                 a.setOntology(null);
220             }
221         }
222 
223         
224         clearCommonElements(ont);
225     }
226 
227     private static void clearNfp(Entity e) throws SynchronisationException, InvalidModelException{
228         if (e!=null && e.listNFPValues()!=null){
229             for (Iterator <IRI> nfpI = new ArrayList <IRI> (e.listNFPValues().keySet()).iterator(); nfpI.hasNext(); ){
230                 e.removeNFP(nfpI.next());
231             }
232         }
233             
234     }
235     
236     private static void clearCommonElements(TopEntity te) throws SynchronisationException, InvalidModelException{
237         //clear importsMediator
238         if (te==null){
239             return;
240         }
241         if (te.listMediators()!=null){
242             for (Iterator <IRI>  medI = new ArrayList <IRI> (te.listMediators()).iterator(); medI.hasNext();){
243                 te.removeMediator(medI.next());
244             }
245         }
246         //clear namespace
247         if (te.listNamespaces()!=null){
248             for (Iterator <Namespace> nsI = new ArrayList <Namespace>(te.listNamespaces()).iterator(); nsI.hasNext();){
249                 te.removeNamespace(nsI.next());
250             }
251         }
252         //clear importsOntology
253         if(te.listOntologies()!=null){
254             for (Iterator <Ontology> ontI = new ArrayList <Ontology> (te.listOntologies()).iterator(); ontI.hasNext();){
255                 te.removeOntology(ontI.next());
256             }
257         }
258         te.setWsmlVariant(null);
259         te.setDefaultNamespace((IRI)null);
260         clearNfp(te);
261     }
262 
263 
264 }
265 
266 
267 /*
268  *$Log$
269  *Revision 1.7  2007/04/02 12:13:28  morcen
270  *Generics support added to wsmo-api, wsmo4j and wsmo-test
271  *
272  *Revision 1.6  2007/02/13 13:01:56  alex_simov
273  *bugfix: cleaning webservices/goals did not remove the interfaces
274  *
275  *Revision 1.5  2006/12/04 09:01:59  alex_simov
276  *bugfix: ConcurrentModificationException was caused in several FOR loops
277  * using iterators
278  *
279  *Revision 1.4  2006/08/21 14:46:22  nathaliest
280  *fixed invalid model exception
281  *
282  *Revision 1.3  2006/08/10 07:36:51  nathaliest
283  *completed clearing of ontology objects
284  *
285  *Revision 1.2  2006/04/27 10:34:10  holgerlausen
286  *fixed potential null pointer exceptions
287  *
288  *Revision 1.1  2006/04/11 16:06:59  holgerlausen
289  *addressed RFE 1468651 ( http://sourceforge.net/tracker/index.php?func=detail&aid=1468651&group_id=113501&atid=665349)
290  *currently the default behaviour of the parser is still as before
291  *
292  */