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.IOException;
21  import java.util.Set;
22  
23  import org.deri.wsmo4j.io.parser.rdf.RDFExprParser;
24  import org.omwg.logicalexpression.AttributeValueMolecule;
25  import org.omwg.logicalexpression.Implication;
26  import org.omwg.logicalexpression.LogicalExpression;
27  import org.omwg.ontology.Axiom;
28  import org.omwg.ontology.SimpleDataValue;
29  import org.openrdf.model.URI;
30  import org.openrdf.vocabulary.RDFS;
31  import org.wsmo.common.IRI;
32  import org.wsmo.factory.Factory;
33  
34  /**
35   * Helper type to serialize/deserialize xml elements with logical expressions
36   * 
37   * @author not attributable
38   */
39  class NodeLogExp {
40      
41      static void serialize(Axiom axiom, WsmlRdfSerializer serializer) {
42      	if (!axiom.listNFPValues().isEmpty()) {
43              NodeNFP.serialize(serializer.getFactory().createURI(
44              		axiom.getIdentifier().toString()), axiom, serializer);
45  		}
46      	if (serializer.axiomAsTriple) {
47      		URI axiomID = serializer.getFactory().createURI(axiom.getIdentifier().toString()); 
48      		// translating the non-functional property dc#relation from the original 
49      		// WSML file to an <rdfs:isDefinedBy> tag construct
50      		Set<Object> relationNFPs = axiom.listNFPValues(Factory.createWsmoFactory(null).
51      				createIRI(WsmlRdfSerializer.DC_RELATION));
52      		if (!relationNFPs.isEmpty()) {
53      			for (Object nfpValue : relationNFPs) {
54  					try {
55  						if (nfpValue instanceof SimpleDataValue) {
56  							String value = ((SimpleDataValue) nfpValue).toString();
57  							serializer.getWriter().writeStatement(
58  									axiomID,
59  							        serializer.getFactory().createURI(RDFS.ISDEFINEDBY),
60  							        serializer.getFactory().createLiteral(value));
61  						}
62  						else if (nfpValue instanceof IRI) {       	
63  							String value = ((IRI) nfpValue).toString();
64  							serializer.getWriter().writeStatement(
65  									axiomID,
66  									serializer.getFactory().createURI(RDFS.ISDEFINEDBY),
67  									serializer.getFactory().createURI(value));
68  						}
69  					} catch (IOException e) {
70  						// TODO Auto-generated catch block
71  						e.printStackTrace();
72  					}
73      			}
74      		}
75      		// translating the logExpr to Strings inside of a <wsml:axiom> tag construct
76      		Object[] exprs = axiom.listDefinitions().toArray();
77              for (int i = 0; i < exprs.length; i++) {
78                  LogicalExpression expr = (LogicalExpression)exprs[i];
79                  try {
80  					serializer.getWriter().writeStatement(
81  							axiomID,
82  					        serializer.getFactory().createURI(RDFExprParser.WSML_AXIOM),
83  					        serializer.getFactory().createLiteral(expr.toString(axiom.getOntology())));
84  				} catch (IOException e) {
85  					throw new RuntimeException(e);
86  				}
87              }
88      	}
89      	else {
90      		Object[] exprs = axiom.listDefinitions().toArray();
91      		for (int i = 0; i < exprs.length; i++) {
92      			LogicalExpression expr = (LogicalExpression)exprs[i];
93      			if(expr instanceof Implication) {
94      				Implication impl = (Implication)expr;
95      				if(impl.getLeftOperand() instanceof AttributeValueMolecule &&
96      						impl.getRightOperand() instanceof AttributeValueMolecule) {
97                      
98      					try {
99      						AttributeValueMolecule left = (AttributeValueMolecule)impl.getLeftOperand();
100     						AttributeValueMolecule right = (AttributeValueMolecule)impl.getRightOperand();
101                         
102     						serializer.getWriter().writeStatement(
103     								serializer.getFactory().createURI(left.getAttribute().toString()),
104     								serializer.getFactory().createURI(RDFS.SUBPROPERTYOF),
105     								serializer.getFactory().createURI(right.getAttribute().toString()));
106     					} catch(IOException e) {
107     						throw new RuntimeException(e);
108     					}
109     				} else {
110     					return;
111     				}
112     			} else {
113     				return;
114     			}
115     		}
116     	}
117     }
118 }
119 
120 /*
121  *
122 */