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.io.serializer.rdf;
19  
20  import java.io.*;
21  import java.util.*;
22  
23  import org.deri.wsmo4j.io.parser.rdf.RDFExprParser;
24  import org.omwg.ontology.*;
25  import org.openrdf.model.*;
26  import org.openrdf.model.Value;
27  import org.openrdf.vocabulary.*;
28  import org.wsmo.common.*;
29  
30  /**
31   * Helper type to serialize/deserialize xml element relation
32   * 
33   * @author not attributable
34   */
35  class NodeRelation {
36  
37      static void serialize(Relation relation, WsmlRdfSerializer serializer) {
38          if (!relation.listParameters().isEmpty() && relation.listParameters().size()==2) {
39              try {
40                  URI rel = serializer.getFactory().createURI(relation.getIdentifier().toString());
41                  
42                  serializer.getWriter().writeStatement(
43                          rel,
44                          serializer.getFactory().createURI(RDF.TYPE),
45                          serializer.getFactory().createURI(RDF.PROPERTY));
46                  
47                  Set domain = relation.listParameters().get(0).listTypes();
48                  Set range = relation.listParameters().get(1).listTypes();
49                  // keep in mind whether it is a constraining relation parameter or not (for better roundtripping) 
50                  if (relation.listParameters().get(0).isConstraining()) {
51                  	serializer.getWriter().writeStatement(
52                              serializer.getFactory().createURI(relation.getIdentifier().toString()),
53                              serializer.getFactory().createURI(RDFS.COMMENT),
54                              serializer.getFactory().createLiteral(RDFExprParser.CONSTRAINING_REL_1));
55                  }
56                  if (relation.listParameters().get(1).isConstraining()) {
57                  	serializer.getWriter().writeStatement(
58                              serializer.getFactory().createURI(relation.getIdentifier().toString()),
59                              serializer.getFactory().createURI(RDFS.COMMENT),
60                              serializer.getFactory().createLiteral(RDFExprParser.CONSTRAINING_REL_2));
61                  }
62                  
63                  for (Iterator j = domain.iterator(); j.hasNext();) {
64                      Type type = (Type)j.next();
65                      Value val = null;
66                      if (type instanceof Concept) {
67                          val = serializer.getFactory().createURI(
68                                  ((Concept)type).getIdentifier().toString());
69                      }
70                      else {
71                          val = serializer.getFactory().createURI(
72                                  XmlSchema.NAMESPACE + 
73                                  ((WsmlDataType)type).getIRI().getLocalName().toString());
74                      }
75                      serializer.getWriter().writeStatement(
76                              rel,
77                              serializer.getFactory().createURI(RDFS.DOMAIN),
78                              val);
79                  }
80                  for(Iterator i = range.iterator(); i.hasNext();) {
81                      Type type = (Type)i.next();
82                      Value val = null;
83                      if (type instanceof Concept) {
84                          val = serializer.getFactory().createURI(
85                                  ((Concept)type).getIdentifier().toString());
86                      }
87                      else {
88                          val = serializer.getFactory().createURI(
89                                  XmlSchema.NAMESPACE + 
90                                  ((WsmlDataType)type).getIRI().getLocalName().toString());
91                      }
92                      serializer.getWriter().writeStatement(
93                              rel,
94                              serializer.getFactory().createURI(RDFS.RANGE),
95                              val);
96                  }
97                  if (!relation.listSuperRelations().isEmpty()) {
98                      Object[] entities = relation.listSuperRelations().toArray();
99                      for (int i = 0; i < entities.length; i++) {
100                         serializer.getWriter().writeStatement(
101                                 rel,
102                                 serializer.getFactory().createURI(RDFS.SUBPROPERTYOF),
103                                 serializer.getFactory().createURI(
104                                         ((Entity) entities[i]).getIdentifier().toString()));
105                     }
106                 }
107                 if (!relation.listNFPValues().isEmpty()) {
108                     NodeNFP.serialize(rel, relation, serializer);
109                 }
110             } catch(IOException e) {
111                 throw new RuntimeException(e);
112             }
113         } else {
114             return;
115         }
116     }
117 }
118 
119 /*
120  * $Log$
121  * Revision 1.3  2007/04/02 12:13:23  morcen
122  * Generics support added to wsmo-api, wsmo4j and wsmo-test
123  *
124  * Revision 1.2  2006/11/10 15:38:49  ohamano
125  * *** empty log message ***
126  *
127  * Revision 1.1  2006/11/10 15:02:59  ohamano
128  * *** empty log message ***
129  *
130  * Revision 1.1  2006/11/10 10:08:42  ohamano
131  * *** empty log message ***
132  *
133  *
134 */