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.deri.wsmo4j.logicalexpression.*;
33  import org.omwg.ontology.*;
34  import org.w3c.dom.*;
35  import org.wsmo.common.*;
36  import org.wsmo.common.Entity;
37  import org.wsmo.common.exception.*;
38  
39  import com.ontotext.wsmo4j.serializer.xml.*;
40  
41  /**
42   * Helper type to serialize/deserialize xml element relation
43   * 
44   * @author not attributable
45   */
46  class NodeRelation {
47      static Relation deserialize(Node xmlNode, WsmlXmlParser parser) throws InvalidModelException {
48          if (xmlNode == null || parser == null || xmlNode.getNodeName() != "relation") {
49              throw new IllegalArgumentException();
50          }
51  
52          IRI relationIri = parser.getFactory()
53                  .createIRI(WsmlXmlHelper.getAttrValue(xmlNode, "name"));
54          Relation relation = parser.getFactory().createRelation(relationIri);
55  
56          byte arity = Byte.parseByte(WsmlXmlHelper.getAttrValue(xmlNode, "arity"));
57          byte currentArity = 0;
58  
59          NodeList nodes = xmlNode.getChildNodes();
60          for (int i = 0; i < nodes.getLength(); i++) {
61              Node node = nodes.item(i);
62              if (node.getNodeName() == "superRelation" && node.getNodeType() == Node.ELEMENT_NODE) {
63                  IRI superRelIri = parser.getFactory().createIRI(WsmlXmlHelper.getElementText(node));
64                  Relation superRel = parser.getFactory().getRelation(superRelIri);
65                  relation.addSuperRelation(superRel);
66                  superRel.addSubRelation(relation);
67  
68              }
69              else if (node.getNodeName() == "parameters" && node.getNodeType() == Node.ELEMENT_NODE) {
70                  NodeList parametersNodes = node.getChildNodes();
71                  for (int j = 0; j < parametersNodes.getLength(); j++) {
72                      Node paramNode = parametersNodes.item(j);
73                      if (paramNode.getNodeType() == Node.ELEMENT_NODE
74                              && paramNode.getNodeName() == "parameter") {
75                          String paramType = WsmlXmlHelper.getAttrValue(paramNode, "type");
76                          Parameter param = relation.createParameter(currentArity++);
77                          if (paramType.equals("inferring")) {
78                              param.setConstraining(false);
79                          }
80                          else {
81                              param.setConstraining(true);
82                          }
83                          NodeList rangeNodes = paramNode.getChildNodes();
84                          for (int k = 0; k < rangeNodes.getLength(); k++) {
85                              Node childNode = rangeNodes.item(k);
86                              if (childNode.getNodeName() == "range"
87                                      && childNode.getNodeType() == Node.ELEMENT_NODE) {
88                                  ConstantTransformer cf = ConstantTransformer.getInstance();
89                                  String type = WsmlXmlHelper.getElementText(childNode);
90                                  if (cf.isDataType(type)) {
91                                      param.addType(parser.getDataFactory().createWsmlDataType(type));
92                                  }
93                                  else {
94                                      param.addType(parser.getFactory().createConcept(
95                                              parser.getFactory().createIRI(type)));
96                                  }
97                              }
98                          }
99                      }
100                 }
101                 if (currentArity != arity) {
102                     throw new IllegalArgumentException("Relation exepected "
103                             + String.valueOf(arity) + "but " + String.valueOf(currentArity)
104                             + " parameters were found!");
105                 }
106             }
107             else if (node.getNodeName() == "nonFunctionalProperties"
108                     && node.getNodeType() == Node.ELEMENT_NODE) {
109                 NodeNFP.deserialize(relation, node, parser);
110             }
111         }
112 
113         return relation;
114     }
115 
116     static Element serialize(Relation relation, WsmlXmlSerializer serializer) {
117         Element relationElement = serializer.createElement("relation");
118         relationElement.setAttribute("name", relation.getIdentifier().toString());
119         int arity = 0;
120 
121         if (!relation.listSuperRelations().isEmpty()) {
122             Object[] entities = relation.listSuperRelations().toArray();
123             for (int i = 0; i < entities.length; i++) {
124                 Element superRel = serializer.createElement("superRelation");
125                 relationElement.appendChild(superRel);
126                 superRel.appendChild(serializer.createTextNode(((Entity) entities[i])
127                         .getIdentifier().toString()));
128             }
129         }
130 
131         if (!relation.listNFPValues().isEmpty()) {
132             NodeNFP.serialize(relationElement, relation, serializer);
133         }
134 
135         if (!relation.listParameters().isEmpty()) {
136             Element parametersElement = serializer.createElement("parameters");
137             relationElement.appendChild(parametersElement);
138 
139             for (Iterator i = relation.listParameters().iterator(); i.hasNext();) {
140                 arity++;
141                 Parameter param = (Parameter) i.next();
142                 Element paramElement = serializer.createElement("parameter");
143                 paramElement.setAttribute("type", param.isConstraining() == true ? "constraining"
144                         : "inferring");
145                 parametersElement.appendChild(paramElement);
146 
147                 for (Iterator j = param.listTypes().iterator(); j.hasNext();) {
148                     Type type = (Type) j.next();
149                     Element rangeElement = serializer.createElement("range");
150                     paramElement.appendChild(rangeElement);
151                     if (type instanceof Concept) {
152                         rangeElement.appendChild(serializer.createTextNode(
153                                 ((Concept) type).getIdentifier().toString()));
154                     }
155                     else {
156                         rangeElement.appendChild(serializer.createTextNode(
157                                 ((WsmlDataType) type).getIRI().toString()));
158                     }
159                 }
160             }
161         }
162 
163         relationElement.setAttribute("arity", String.valueOf(arity));
164         return relationElement;
165     }
166 }
167 
168 /*
169  * $Log$
170  * Revision 1.3  2006/07/04 14:30:30  vassil_momtchev
171  * datatype parsing problem fixed
172  *
173  * Revision 1.2  2006/03/29 11:20:51  vassil_momtchev
174  * mediator support added; some code refactored; minor bugs fixed
175  *
176  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
177  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
178  *
179  * Revision 1.4  2005/09/16 14:02:45  alex_simov
180  * Identifier.asString() removed, use Object.toString() instead
181  * (Implementations MUST override toString())
182  *
183  * Revision 1.3  2005/08/31 09:19:14  vassil_momtchev
184  * use Type and Value instead of Object where appropriate bug SF 1276677
185  *
186  * Revision 1.2  2005/08/08 08:24:40  vassil_momtchev
187  * javadoc added, bugfixes
188  *
189 */