1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, University if 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 test.wsmo4j.serializer.wsml;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import junit.framework.*;
24  
25  import org.deri.wsmo4j.common.ClearTopEntity;
26  import org.omwg.logicalexpression.*;
27  import org.omwg.ontology.*;
28  import org.wsmo.common.*;
29  import org.wsmo.factory.*;
30  import org.wsmo.wsml.*;
31  
32  /**
33   * Interface or class description
34   *
35   * <pre>
36   * Created on Sep 8, 2005
37   * Committed by $Author$
38   * $Source$,
39   * </pre>
40   *
41   * @author Holger Lausen (holger.lausen@deri.org)
42   *
43   * @version $Revision$ $Date$
44   */
45  public class WSMLSerializeTest extends TestCase {
46  
47      private Parser parser = Factory.createParser(null);
48  
49      private Serializer serializer = Factory.createSerializer(null);;
50  
51      private WsmoFactory factory = Factory.createWsmoFactory(null);
52      private LogicalExpressionFactory leFactory = Factory.createLogicalExpressionFactory(null);
53  
54      private static final String DEFAULT_NS = "http://ex.org#";
55  
56      private String getOntologyHeader() {
57          return "namespace {_\"" + DEFAULT_NS + "\", \n"
58                  + "ex _\"http://somewhere.com/\"} \n" + "ontology myOnt12"
59                  + System.currentTimeMillis() + " \n\n";
60      }
61      
62      public void testSameLESerialization() throws Exception{
63          Map <String, Object> m = new HashMap <String, Object> ();
64          m.put(Parser.CACHE_LOGICALEXPRESSION_STRING, "true");
65          parser = Factory.createParser(m);
66          
67          String exp = "namespace {a _\"foo:bar#\" , _\"foo:bar#\"} \n" +
68                  " ontology a \n" +
69                  " //definedBy \n" +
70                  " /* \\*/" +
71                  "  " +
72                  " axiom a definedBy \n" +
73                  "   a#a implies b and c. c :- d.\n" +
74                  " axiom e definedBy a. \n" +
75                  " webService w\n" +
76                  " capability c postcondition f definedBy \n" +
77                  " a:-c.\n" +
78                  " ";
79          String test = getOntologyHeader() + "axiom a definedBy " + exp;
80                  
81          TopEntity[] te = parser.parse(new StringBuffer(exp));
82          StringBuffer out = new StringBuffer();
83          Ontology o = (Ontology)te[0];
84          Axiom a = o.listAxioms().iterator().next();
85          LogicalExpression le = a.listDefinitions().iterator().next();
86          assertEquals("a#a implies b and c.", le.toString(o));
87          o.removeNamespace("a");
88  //        System.out.println(le.toString(o));
89          assertFalse("a#a implies b and c.".equals(le.toString(o)));
90      }
91      
92      /**
93       * checks that parsed IRI is equal also namespaces do change
94       * @throws Exception
95       */
96      public void testStableIRI() throws Exception{
97          String test = "namespace {_\"urn:foo\"} " +
98                  " ontology a concept bar ";
99                  
100         Ontology ont = (Ontology)parser.parse(new StringBuffer(test))[0];
101         StringBuffer out = new StringBuffer();
102         serializer.serialize(new TopEntity[]{ont}, out);
103         ClearTopEntity.clearTopEntity(ont);
104         ont = (Ontology)parser.parse(out)[0];
105         assertEquals(factory.createIRI("urn:foobar"), 
106                 ont.listConcepts().iterator().next().getIdentifier());
107     }    
108     
109     public void testInvalidSQNameChars() throws Exception {
110     	String test = "namespace {_\"" + DEFAULT_NS + "\"} " +
111         " ontology example concept conA concept _\"http://ex.org#con%20B\"";
112     	
113     	Ontology ont = (Ontology)parser.parse(new StringBuffer(test))[0];
114     	ont.addConcept(factory.createConcept(factory.createIRI(DEFAULT_NS + "concept%20C")));
115         StringBuffer out = new StringBuffer();
116         serializer.serialize(new TopEntity[]{ont}, out);
117 //        System.out.println(out);
118     }
119     
120 }