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  package com.ontotext.wsmo4j.parser.xml;
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   * @author not attributable
26   * @version 1.0
27   *
28   */
29  
30  import java.util.*;
31  
32  import org.omwg.ontology.*;
33  import org.w3c.dom.*;
34  import org.wsmo.common.*;
35  import org.wsmo.common.exception.*;
36  import org.wsmo.service.*;
37  import org.wsmo.wsml.*;
38  
39  import com.ontotext.wsmo4j.serializer.xml.*;
40  
41  /**
42   * Helper type to serialize/deserialize xml element capability
43   * 
44   * @author not attributable
45   */
46  class NodeCapability {
47      static Capability deserialize(Node xmlNode, WsmlXmlParser parser)
48              throws InvalidModelException, ParserException  {
49          if (xmlNode == null || parser == null || xmlNode.getNodeName() != "capability") {
50              throw new IllegalArgumentException();
51          }
52  
53          IRI iriCapability = parser.getFactory().createIRI(
54                  WsmlXmlHelper.getAttrValue(xmlNode, "name"));
55          Capability capability = parser.getFactory().createCapability(iriCapability);
56          NodeTopEntity.processTopEntityNode(capability, xmlNode, parser);
57  
58          NodeList nodes = xmlNode.getChildNodes();
59          for (int i = 0; i < nodes.getLength(); i++) {
60              Node node = nodes.item(i);
61  
62              if (node.getNodeName() == "nonFunctionalProperties"
63                      && node.getNodeType() == Node.ELEMENT_NODE) {
64                  NodeNFP.deserialize(capability, node, parser);
65              }
66              else if (node.getNodeName() == "sharedVariable" && node.getNodeType() == Node.ELEMENT_NODE) {
67                  NodeList childNodes = node.getChildNodes();
68                  for (int j = 0; j < childNodes.getLength(); j++) {
69                      Node childNode = childNodes.item(j);
70                      if (childNode.getNodeName() == "variable") {
71                          capability.addSharedVariable(
72                                  parser.getLEFactory().createVariable(
73                                          WsmlXmlHelper.getElementText(node)));
74                      }
75                  }
76              }
77              else if (node.getNodeName() == "precondition") {
78                  capability.addPreCondition(NodeLogExp.deserialize(node, parser));
79              }
80              else if (node.getNodeName() == "assumption") {
81                  capability.addAssumption(NodeLogExp.deserialize(node, parser));
82              }
83              else if (node.getNodeName() == "postcondition") {
84                  capability.addPostCondition(NodeLogExp.deserialize(node, parser));
85              }
86              else if (node.getNodeName() == "effect") {
87                  capability.addEffect(NodeLogExp.deserialize(node, parser));
88              }
89          }
90          
91          return capability;
92      }
93      
94      static Element serialize(Capability capability, WsmlXmlSerializer serializer) {
95          Element capabilityElement = serializer.createElement("capability");
96          NodeTopEntity.serializeTopEntity(capabilityElement, capability, serializer);
97          
98          if (!capability.listNFPValues().isEmpty()) {
99              NodeNFP.serialize(capabilityElement, capability, serializer);
100         }
101         
102         if (!capability.listSharedVariables().isEmpty()) {
103             Element sharedVarElement = serializer.createElement("sharedVariable");
104             capabilityElement.appendChild(sharedVarElement);
105             for (Iterator i = capability.listSharedVariables().iterator(); i.hasNext();) {
106                 Variable var = (Variable) i.next();
107                 Element variableElement = serializer.createElement("variable");
108                 variableElement.appendChild(serializer.createTextNode("?" + var.getName()));
109             }
110         }
111         
112         if (!capability.listPreConditions().isEmpty()) {
113             for (Iterator i = capability.listPreConditions().iterator(); i.hasNext();) {
114                 Axiom axiom = (Axiom) i.next();
115                 capabilityElement.appendChild(serializer.createElement("precondition").appendChild(
116                         NodeLogExp.serialize("precondition", axiom, serializer)));
117             }
118         }
119         
120         if (!capability.listAssumptions().isEmpty()) {
121             for (Iterator i = capability.listPreConditions().iterator(); i.hasNext();) {
122                 Axiom axiom = (Axiom) i.next();
123                 capabilityElement.appendChild(serializer.createElement("assumption").appendChild(
124                         NodeLogExp.serialize("assumption", axiom, serializer)));
125             }
126         }
127         
128         if (!capability.listPostConditions().isEmpty()) {
129             for (Iterator i = capability.listPostConditions().iterator(); i.hasNext();) {
130                 Axiom axiom = (Axiom) i.next();
131                 capabilityElement.appendChild(serializer.createElement("postcondition").appendChild(
132                         NodeLogExp.serialize("postcondition", axiom, serializer)));
133             }
134         }
135         
136         if (!capability.listEffects().isEmpty()) {
137             for (Iterator i = capability.listEffects().iterator(); i.hasNext();) {
138                 Axiom axiom = (Axiom) i.next();
139                 capabilityElement.appendChild(serializer.createElement("effect").appendChild(
140                         NodeLogExp.serialize("effect", axiom, serializer)));
141             }
142         }
143         
144         return capabilityElement;
145     }
146 }
147 
148 /*
149  * $Log$
150  * Revision 1.3  2006/06/21 07:46:29  vassil_momtchev
151  * createVariable(String) method moved from WsmoFactory to LogicalExpressionFactory interface
152  *
153  * Revision 1.2  2006/03/29 11:20:51  vassil_momtchev
154  * mediator support added; some code refactored; minor bugs fixed
155  *
156  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
157  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
158  *
159  * Revision 1.8  2005/09/16 14:02:45  alex_simov
160  * Identifier.asString() removed, use Object.toString() instead
161  * (Implementations MUST override toString())
162  *
163  * Revision 1.7  2005/08/31 09:24:22  vassil_momtchev
164  * add InvalidModelException and ParserException to LogicalExpressionFactory::createLogixExpression methods
165  *
166  * Revision 1.6  2005/08/31 09:04:42  alex_simov
167  * fix: Variable objects must be created only by the WsmoFactory
168  *
169  * Revision 1.5  2005/08/31 08:36:14  alex_simov
170  * bugfix: Variable.getName() now returns the name without leading '?'
171  *
172  * Revision 1.4  2005/08/08 08:24:40  vassil_momtchev
173  * javadoc added, bugfixes
174  *
175 */