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  import org.wsmo.factory.Factory;
30  
31  /**
32   * Helper type to serialize/deserialize xml element concept
33   * 
34   * @author not attributable
35   */
36  class NodeConcept {
37  
38      static void serialize(Concept concept, WsmlRdfSerializer serializer) {
39          if(concept.getIdentifier().toString().equals(RDFS.CLASS)) {
40              return;
41          } else {
42              try {
43                  Resource name = serializer.getFactory().createURI(concept.getIdentifier().toString());
44                  
45                  serializer.getWriter().writeStatement(
46                          name,
47                          serializer.getFactory().createURI(RDF.TYPE),
48                          serializer.getFactory().createURI(RDFS.CLASS));
49                  
50                  if (!concept.listSuperConcepts().isEmpty()) {
51                      Object[] entities = concept.listSuperConcepts().toArray();
52                      for (int i = 0; i < entities.length; i++) {
53                          serializer.getWriter().writeStatement(
54                                  name,
55                                  serializer.getFactory().createURI(RDFS.SUBCLASSOF),
56                                  serializer.getFactory().createURI(
57                                          ((Entity)entities[i]).getIdentifier().toString()));
58                      }
59                  }
60                  if (!concept.listNFPValues().isEmpty()) {
61                      NodeNFP.serialize(name, concept, serializer);
62                      Set<Object> relationNFPs = concept.listNFPValues(Factory.createWsmoFactory(null).
63              				createIRI(WsmlRdfSerializer.DC_RELATION));
64              		if (!relationNFPs.isEmpty()) {
65              			for (Object nfpValue : relationNFPs) {
66              				if (nfpValue instanceof SimpleDataValue) {
67              					String value = ((SimpleDataValue) nfpValue).toString();
68              					serializer.getWriter().writeStatement(
69              							name,
70              					        serializer.getFactory().createURI(RDFS.ISDEFINEDBY),
71              					        serializer.getFactory().createLiteral(value));
72              				}
73              				else if (nfpValue instanceof IRI) {       	
74              					String value = ((IRI) nfpValue).toString();
75              					serializer.getWriter().writeStatement(
76              							name,
77              							serializer.getFactory().createURI(RDFS.ISDEFINEDBY),
78              							serializer.getFactory().createURI(value));
79              				}
80              			}
81              		}
82                  }
83                  if (!concept.listAttributes().isEmpty()) {
84                      Object[] attrList = concept.listAttributes().toArray();
85                      for (int i = 0; i < attrList.length; i++) {
86                          Attribute attribute = (Attribute) attrList[i];
87                          // keep in mind whether it is a constraining attribute or not (for better roundtripping) 
88                          if (attribute.isConstraining()) {
89                          	serializer.getWriter().writeStatement(
90                                      serializer.getFactory().createURI(attribute.getIdentifier().toString()),
91                                      serializer.getFactory().createURI(RDFS.COMMENT),
92                                      serializer.getFactory().createLiteral(RDFExprParser.CONSTRAINING_ATTR));
93                          }
94                          // keep in mind whether it is a transitive attribute or not (for better roundtripping)
95                          if (attribute.isTransitive()) {
96                          	serializer.getWriter().writeStatement(
97                                      serializer.getFactory().createURI(attribute.getIdentifier().toString()),
98                                      serializer.getFactory().createURI(RDFS.COMMENT),
99                                      serializer.getFactory().createLiteral(RDFExprParser.TRANSITIVE_ATTR));
100                         }
101                         // keep in mind whether it is a reflexive attribute or not (for better roundtripping)
102                         if (attribute.isTransitive()) {
103                         	serializer.getWriter().writeStatement(
104                                     serializer.getFactory().createURI(attribute.getIdentifier().toString()),
105                                     serializer.getFactory().createURI(RDFS.COMMENT),
106                                     serializer.getFactory().createLiteral(RDFExprParser.REFLEXIVE_ATTR));
107                         }
108                         // keep in mind whether it is a symmetric attribute or not (for better roundtripping)
109                         if (attribute.isTransitive()) {
110                         	serializer.getWriter().writeStatement(
111                                     serializer.getFactory().createURI(attribute.getIdentifier().toString()),
112                                     serializer.getFactory().createURI(RDFS.COMMENT),
113                                     serializer.getFactory().createLiteral(RDFExprParser.SYMMETRIC_ATTR));
114                         }
115                         // keep in mind whether it is an inverse attribute or not (for better roundtripping)
116                         if (attribute.getInverseOf() != null) {
117                         	serializer.getWriter().writeStatement(
118                                     serializer.getFactory().createURI(attribute.getIdentifier().toString()),
119                                     serializer.getFactory().createURI(RDFExprParser.WSML_INVERSE_ATTR),
120                                     serializer.getFactory().createURI(attribute.getIdentifier().toString()));
121                         }
122                         serializer.getWriter().writeStatement(
123                                 serializer.getFactory().createURI(attribute.getIdentifier().toString()),
124                                 serializer.getFactory().createURI(RDF.TYPE),
125                                 serializer.getFactory().createURI(RDF.PROPERTY));
126                         serializer.getWriter().writeStatement(
127                                 serializer.getFactory().createURI(attribute.getIdentifier().toString()),
128                                 serializer.getFactory().createURI(RDFS.DOMAIN),
129                                 name);
130                         Resource attr = serializer.getFactory().createURI(attribute.getIdentifier().toString());                
131                         if (!attribute.listTypes().isEmpty()) {
132                             Value value = null;
133                             for (Iterator j = attribute.listTypes().iterator(); j.hasNext();) {
134                                 Object o  = j.next();
135                                 if (o instanceof Concept)
136                                     value = serializer.getFactory().createURI(
137                                             ((Concept)o).getIdentifier().toString());                        
138                                 else if (o instanceof WsmlDataType) {
139                                     value = serializer.getFactory().createURI(
140                                             XmlSchema.NAMESPACE + 
141                                             ((WsmlDataType) o).getIRI().getLocalName().toString());                         
142                                 }
143                                 else {
144                                     throw new RuntimeException("Unknown range for attribute!");
145                                 }
146                                 serializer.getWriter().writeStatement(
147                                         attr,
148                                         serializer.getFactory().createURI(RDFS.RANGE),
149                                         value);
150                             }
151                         }
152                         if (!attribute.listNFPValues().isEmpty()) {
153                             NodeNFP.serialize(attr,attribute, serializer);
154                         }
155                     }
156                 }
157             } catch(IOException e) {
158                 throw new RuntimeException(e);
159             }
160         }
161     }
162 }
163 
164 /*
165  * $Log$
166  * Revision 1.3  2006/11/16 14:35:09  ohamano
167  * *** empty log message ***
168  *
169  * Revision 1.2  2006/11/10 15:38:49  ohamano
170  * *** empty log message ***
171  *
172  * Revision 1.1  2006/11/10 15:02:59  ohamano
173  * *** empty log message ***
174  *
175  * Revision 1.1  2006/11/10 10:08:42  ohamano
176  * *** empty log message ***
177  *
178  *
179 */