View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5                             University of Innsbruck, Austria
6   
7    This library is free software; you can redistribute it and/or modify it under
8    the terms of the GNU Lesser General Public License as published by the Free
9    Software Foundation; either version 2.1 of the License, or (at your option)
10   any later version.
11   This library is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14   details.
15   You should have received a copy of the GNU Lesser General Public License along
16   with this library; if not, write to the Free Software Foundation, Inc.,
17   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  
20  /**
21   * <p>Title: WSMO4J</p>
22   * <p>Description: WSMO API and a Reference Implementation</p>
23   * <p>Copyright:  Copyright (c) 2004-2005</p>
24   * <p>Company: OntoText Lab. / SIRMA </p>
25   */
26  
27  package com.ontotext.wsmo4j.parser.wsml;
28  
29  import java.util.*;
30  import java.util.Map.Entry;
31  
32  import org.deri.wsmo4j.common.*;
33  import org.omwg.ontology.*;
34  import org.wsmo.common.*;
35  import org.wsmo.common.exception.*;
36  import org.wsmo.factory.*;
37  import org.wsmo.service.*;
38  import org.wsmo.wsml.compiler.analysis.*;
39  import org.wsmo.wsml.compiler.node.*;
40  
41  import com.ontotext.wsmo4j.parser.*;
42  
43  public class ServiceAnalysis extends ASTAnalysis {
44  
45      private WsmoFactory factory;
46      
47      private LogicalExpressionFactory leFactory;
48      
49      private ASTAnalysisContainer container;
50      
51      private boolean cleanOnParse = false; 
52  
53      public ServiceAnalysis(ASTAnalysisContainer container, WsmoFactory factory, 
54              LogicalExpressionFactory leFactory) {
55          if (container == null || factory == null) {
56              throw new IllegalArgumentException();
57          }
58          this.factory = factory;
59          this.container = container;
60          this.leFactory = leFactory;
61  
62          // register the handled nodes
63          container.registerNodeHandler(AWebservice.class, this);
64          container.registerNodeHandler(AGoal.class, this);
65          container.registerNodeHandler(ACapability.class, this);
66          container.registerNodeHandler(APostconditionPrePostAssOrEff.class, this);
67          container.registerNodeHandler(APreconditionPrePostAssOrEff.class, this);
68          container.registerNodeHandler(AAssumptionPrePostAssOrEff.class, this);
69          container.registerNodeHandler(AEffectPrePostAssOrEff.class, this);
70          container.registerNodeHandler(ASharedvardef.class, this);
71          container.registerNodeHandler(AInterface.class, this);
72          container.registerNodeHandler(AChoreography.class, this);
73          container.registerNodeHandler(AOrchestration.class, this);
74      }
75      
76      public void setCleanOnParse(boolean cleanOnParse){
77          this.cleanOnParse=cleanOnParse;
78      }
79  
80      private void cleanServiceDescription(ServiceDescription s){
81          //clean previous contents of this mediator:
82          try {
83              ClearTopEntity.clearTopEntity(s);
84          }
85          catch (SynchronisationException e) {
86              // should never happen
87              throw new RuntimeException("Error During Cleaning TopEntity from previous defintions",e);
88          }
89          catch (InvalidModelException e) {
90              // should never happen
91              throw new RuntimeException("Error During Cleaning TopEntity from previous defintions",e);
92          }
93  
94      }
95  
96      public void inAWebservice(AWebservice node) {
97          TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTWebservice());
98          node.getId().apply(container.getNodeHandler(PId.class));
99          IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
100         WebService ws = factory.createWebService(iri);
101         if (cleanOnParse){
102             cleanServiceDescription(ws);
103         }
104         copyNSAndAddToStacks(ws);
105     }
106 
107     public void outAWebservice(AWebservice node) {
108         container.popFromStack(Entity.class, WebService.class);
109     }
110 
111     public void inAGoal(AGoal node) {
112         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTGoal());
113         node.getId().apply(container.getNodeHandler(PId.class));
114         IRI id = (IRI) container.popFromStack(Identifier.class, IRI.class);
115         Goal g = factory.createGoal(id);
116         if (cleanOnParse){
117             cleanServiceDescription(g);
118         }
119         copyNSAndAddToStacks(g);
120     }
121 
122     public void outAGoal(AGoal node) {
123         container.popFromStack(Entity.class, Goal.class);
124     }
125 
126     public void inACapability(ACapability node) {
127         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTCapability());
128         node.getId().apply(container.getNodeHandler(PId.class));
129         IRI id = (IRI) container.popFromStack(Identifier.class, IRI.class);
130         Capability capability = factory.createCapability(id);
131 
132         // find if it's in a Goal
133         Stack entities = container.getStack(Entity.class);
134         for (int i = entities.size() - 1; i >= 0; i--) {
135             Entity entity = (Entity) entities.elementAt(i);
136             if (entity instanceof ServiceDescription) {
137                 ((ServiceDescription) entity).setCapability(capability);
138                 break;
139             }
140         }
141 
142         copyNSAndAddToStacks(capability);
143     }
144 
145     public void outACapability(ACapability node) {
146         container.popFromStack(Entity.class, Capability.class);
147     }
148 
149     public void outAPostconditionPrePostAssOrEff(APostconditionPrePostAssOrEff node) {
150         Capability capability = (Capability) container.peekFromStack(TopEntity.class,
151                 Capability.class);
152         try {
153             capability.addPostCondition((Axiom) container.peekFromStack(Axiom.class, Axiom.class));
154         }
155         catch (InvalidModelException e) {
156             throw new WrappedInvalidModelException(e);
157         }
158     }
159 
160     public void outAPreconditionPrePostAssOrEff(APreconditionPrePostAssOrEff node) {
161         Capability capability = (Capability) container.peekFromStack(TopEntity.class,
162                 Capability.class);
163         try {
164             capability.addPreCondition((Axiom) container.popFromStack(Axiom.class, Axiom.class));
165         }
166         catch (InvalidModelException e) {
167             throw new WrappedInvalidModelException(e);
168         }
169     }
170 
171     public void outAAssumptionPrePostAssOrEff(AAssumptionPrePostAssOrEff node) {
172         Capability capability = (Capability) container.peekFromStack(TopEntity.class,
173                 Capability.class);
174         try {
175             capability.addAssumption((Axiom) container.popFromStack(Axiom.class, Axiom.class));
176         }
177         catch (InvalidModelException e) {
178             throw new WrappedInvalidModelException(e);
179         }
180     }
181 
182     public void outAEffectPrePostAssOrEff(AEffectPrePostAssOrEff node) {
183         Capability capability = (Capability) container.peekFromStack(TopEntity.class,
184                 Capability.class);
185         try {
186             capability.addEffect((Axiom) container.popFromStack(Axiom.class, Axiom.class));
187         }
188         catch (InvalidModelException e) {
189             throw new WrappedInvalidModelException(e);
190         }
191     }
192 
193     public void inASharedvardef(ASharedvardef node) {
194         final Capability capability = (Capability) container.peekFromStack(TopEntity.class,
195                 Capability.class);
196         if (node != null) {
197             node.apply(new DepthFirstAdapter() {
198                 public void caseTVariable(TVariable var) {
199                     String str = var.getText().trim();
200                     try {
201                         capability.addSharedVariable(leFactory.createVariable(str));
202                     }
203                     catch (InvalidModelException ime) {
204                         throw new WrappedInvalidModelException(ime);
205                     }
206                 }
207             } // close anonymous class
208                     );
209         }
210     }
211 
212     public void inAInterface(AInterface node) {
213         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTInterface());
214         node.getId().apply(container.getNodeHandler(PId.class));
215         IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
216         Interface iface = factory.createInterface(iri);
217 
218         // find if it's a child of WebService or Goal
219         Stack entities = container.getStack(Entity.class);
220         for (int i = entities.size() - 1; i >= 0; i--) {
221             Entity entity = (Entity) entities.elementAt(i);
222             if (entity instanceof WebService) {
223                 ((WebService) entity).addInterface(iface);
224                 break;
225             }
226             else if (entity instanceof Goal) {
227                 ((Goal) entity).addInterface(iface);
228                 break;
229             }
230         }
231 
232         copyNSAndAddToStacks(iface);
233     }
234 
235     public void outAInterface(AInterface node) {
236         container.popFromStack(Entity.class, Interface.class);
237     }
238     
239     public void inAOrchestration(AOrchestration node) {
240         Identifier id = null;
241         if (node.getId() != null) {
242             node.getId().apply(container.getNodeHandler(PId.class));
243             id = (Identifier) container.popFromStack(Identifier.class, Identifier.class);
244         }
245         else {
246             id = factory.createAnonymousID();
247         }
248         Interface iface = (Interface) container.peekFromStack(TopEntity.class, Interface.class);
249         Orchestration orchestration = iface.createOrchestration(id);
250         container.getStack(Orchestration.class).push(orchestration);
251         container.getStack(Entity.class).push(orchestration);
252     }
253     
254     public void outAOrchestration(AOrchestration node) {
255         container.popFromStack(Orchestration.class, Orchestration.class);
256         container.popFromStack(Entity.class, Orchestration.class);
257     }
258     
259     public void inAChoreography(AChoreography node) {        
260         Identifier id = null;
261         if (node.getId() != null) {
262             node.getId().apply(container.getNodeHandler(PId.class));
263             id = (Identifier) container.popFromStack(Identifier.class, Identifier.class);
264         }
265         else {
266             id = factory.createAnonymousID();
267         }
268         Interface iface = (Interface) container.peekFromStack(TopEntity.class, Interface.class);
269         Choreography choreography = iface.createChoreography(id);
270         container.getStack(Choreography.class).push(choreography);
271         container.getStack(Entity.class).push(choreography);
272     }
273     
274     public void outAChoreography(AChoreography node) {
275         container.popFromStack(Choreography.class, Choreography.class);
276         container.popFromStack(Entity.class, Choreography.class);
277     }
278 
279     private void copyNSAndAddToStacks(TopEntity entity) {
280         TopEntityAnalysis.addNamespaceAndVariant(entity, container.getStack(Namespace.class),
281                 container.getStack(AWsmlvariant.class));
282         container.getStack(TopEntity.class).push(entity);
283         container.getStack(Entity.class).push(entity);
284     }
285     
286     public static void copyNFP(Entity oldE, Entity newE) {
287         for (Iterator i = oldE.listNFPValues().entrySet().iterator(); i.hasNext();) {
288             Entry entry = (Entry) i.next();
289             Iterator j = ((Set) entry.getValue()).iterator();
290             try {
291                 while (j.hasNext()) {
292                     Object value = j.next();
293                     if (value instanceof Identifier) {
294                         newE.addNFPValue((IRI)entry.getKey(), (Identifier) value);
295                     }
296                     else if (value instanceof Value) {
297                         newE.addNFPValue((IRI)entry.getKey(), (Value) value);
298                     }
299                 }
300             }
301             catch (InvalidModelException ex) {
302                 throw new WrappedInvalidModelException(ex);
303             }
304         }
305     }
306 }
307 
308 /*
309  * $Log$
310  * Revision 1.11  2006/12/04 11:17:19  vassil_momtchev
311  * wsmo4j parser always processe the choreography/orchestration; the extended parser will swap the implementation with the appropriate
312  *
313  * Revision 1.10  2006/10/25 07:05:49  vassil_momtchev
314  * adapted to handle the node from the new grammar
315  *
316  * Revision 1.9  2006/06/21 07:46:29  vassil_momtchev
317  * createVariable(String) method moved from WsmoFactory to LogicalExpressionFactory interface
318  *
319  * Revision 1.8  2006/04/24 08:04:58  holgerlausen
320  * improved error handling in case of topentities without identifier
321  * moved thomas unit test to "open" package, since it does not break expected behavior, but just document some derivations from the spec
322  *
323  * Revision 1.7  2006/04/11 16:06:58  holgerlausen
324  * addressed RFE 1468651 ( http://sourceforge.net/tracker/index.php?func=detail&aid=1468651&group_id=113501&atid=665349)
325  * currently the default behaviour of the parser is still as before
326  *
327  * Revision 1.6  2006/02/13 09:48:52  vassil_momtchev
328  * the code to handle the topentities identifier validity refactored
329  *
330  * Revision 1.5  2006/02/10 14:37:25  vassil_momtchev
331  * parser addapted to the grammar changes; unused class variables removed;
332  *
333  * Revision 1.4  2005/12/21 14:40:38  vassil_momtchev
334  * choreography orchestration parsing added
335  *
336  * Revision 1.3  2005/12/07 09:12:10  vassil_momtchev
337  * refactored the ServiceDescription check in inACapability method
338  *
339  * Revision 1.2  2005/12/07 09:03:54  vassil_momtchev
340  * capabality was not assigned to the webservice
341  *
342  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
343  * AST analyses
344  *
345 */