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.parser.wsml;
19  
20  import java.io.*;
21  import java.math.*;
22  import java.util.*;
23  
24  import junit.framework.*;
25  
26  import org.omwg.logicalexpression.*;
27  import org.omwg.ontology.*;
28  import org.wsmo.common.*;
29  import org.wsmo.factory.*;
30  import org.wsmo.mediator.*;
31  import org.wsmo.service.*;
32  import org.wsmo.wsml.*;
33  import static test.wsmo4j.Utils.*;
34  
35  /**
36   * Interface or class description
37   *
38   * <pre>
39   * Created on Sep 8, 2005
40   * Committed by $Author$
41   * $Source$,
42   * </pre>
43   *
44   * @author Holger Lausen (holger.lausen@deri.org)
45   *
46   * @version $Revision$ $Date$
47   */
48  public class WSMLParseTest extends TestCase {
49  
50      private Parser parser = Factory.createParser(null);
51  
52      private Serializer serializer = Factory.createSerializer(null);
53  
54      private WsmoFactory factory = Factory.createWsmoFactory(null);
55  
56      private static final String DEFAULT_NS = "http://ex.org#";
57  
58      private String getOntologyHeader() {
59          return "namespace {_\"" + DEFAULT_NS + "\", \n"
60                  + "dc _\"http://purl.org/dc/elements/1.1#\", \n"
61                  + "foreign _\"http://somewhere.com/\"} \n" + "ontology myOnt12"
62                  + System.currentTimeMillis() + " \n\n";
63      }
64      
65      public void testFunctionSymbols()  throws Exception {
66          Ontology o = factory.createOntology(factory.createIRI("urn:foo"));
67          o.setDefaultNamespace(factory.createIRI("u:"));
68          
69          String test="a(b,c(f,g,h(e1))). ";
70          
71          LogicalExpressionFactory leFactory = Factory.createLogicalExpressionFactory(null);
72          LogicalExpression le = leFactory.createLogicalExpression(test,o);
73          System.out.println("BEFORE: "+test);
74          System.out.println("AFTER : "+le.toString(o));
75          String leString = le.toString();
76          LogicalExpression le2 = leFactory.createLogicalExpression(le.toString());
77          assertEquals(le, le2);
78          assertEquals(le.toString(o), test);
79      }
80      
81      public void testEmptySqName() throws Exception {
82          String wsml = "namespace {_\"http://example.com/Workplace#\"} " +
83                  "ontology _\"http://example.com/Workplace#\"";
84          TopEntity[] te = parser.parse(new StringReader(wsml));
85          StringBuffer bu = new StringBuffer();
86          serializer.serialize(te, bu);
87          
88          System.out.println(bu.toString());
89      }
90      
91      public void testMediatorAnonymousSource() throws Exception {
92          String wsml = "namespace {_\"" + DEFAULT_NS + "\"} \n"
93                  + "ooMediator testMediatorAnonymousSource \n"
94                  + "source { _\"http://www.daml.org/2001/09/countries/iso\", _# } \n";
95          
96          try {
97              parser.parse(new StringBuffer(wsml));
98              fail("Anonymous source not allowed!");
99          }
100         catch (ParserException p) {
101         }
102     }
103     
104     public void testMediatorUsage() throws Exception {
105         String wsml = "namespace {_\"" + DEFAULT_NS + "\"} \n"
106                 + "wwMediator MediatorUsage1 \n";
107         TopEntity[] topEntity = parser.parse(new StringBuffer(wsml));
108         wsml = "namespace {_\"" + DEFAULT_NS + "\"} \n"
109                 + "ontology MediatorUsage2 \n"
110                 + "usesMediator _\"" + DEFAULT_NS + "MediatorUsage1\" \n";
111         TopEntity[] topEntity2 = parser.parse(new StringBuffer(wsml));
112     }
113     
114     public void testWsmlDataValues() throws Exception {
115         String wsml = "namespace {_\"" + DEFAULT_NS + "\", \n"
116                 + "dc _\"http://purl.org/dc/elements/1.1#\", \n"
117                 + "foreign _\"http://somewhere.com/\"} \n" 
118                 + "ontology testWsmlDataValues \n"
119                 + "nfp \n"
120                 + "attr1 hasValue \"WSML example ontology\" \n"
121                 + "attr2 hasValue 5 \n"
122                 + "attr3 hasValue _date(2004,11,22) \n"
123                 + "attr4 hasValue _iri(_\"http://a#test\") \n"
124                 + "attr5 hasValue Paul \n"
125                 + "attr6 hasValue _sqname(\"dc\", \"title\") \n"
126                 + "attr7 hasValue _boolean(\"false\") \n"
127                 + "endnfp \n";
128         
129         Ontology ontology = (Ontology) parser.parse(new StringBuffer(wsml))[0];
130         for (int i = 1; i < 8; i++) {
131             Object o = ontology.listNFPValues(factory.createIRI(DEFAULT_NS + "attr" + i)).iterator().next();
132             switch (i) {
133                 case 1: assertTrue(((SimpleDataValue) o).getType().getIRI().toString()
134                         .equals(WsmlDataType.WSML_STRING)); break;
135                 case 2: assertTrue(((SimpleDataValue) o).getType().getIRI().toString()
136                         .equals(WsmlDataType.WSML_INTEGER)); break;
137                 case 3: assertTrue(((ComplexDataValue) o).getType().getIRI().toString()
138                         .equals(WsmlDataType.WSML_DATE)); break;
139                 case 4: assertTrue(((IRI) o).toString()
140                         .equals("http://a#test")); break;
141                 case 5: assertTrue(((IRI) o).toString()
142                         .equals(DEFAULT_NS + "Paul")); break;
143                 case 6: assertTrue(((IRI) o).toString()
144                         .equals("http://purl.org/dc/elements/1.1#title")); break;
145                 case 7: assertEquals(WsmlDataType.WSML_BOOLEAN,
146                         ((ComplexDataValue) o).getType().getIRI().toString()); break;
147             }
148         }
149     }
150     
151     public void testAnonymousTopEntity() throws Exception {
152         String ontString = "wsmlVariant _\"http://www.wsmo.org/2004/wsml/wsml-flight\" \n"
153             + "namespace {_\"http://ontotext#\", \n"
154             + "dc _\"http://purl.org/dc/elements/1.1#\"} \n"
155             + "ontology _# \n";
156         
157         try {
158             parser.parse(new StringBuffer(ontString));
159             fail();
160         }
161         catch (ParserException e) {
162         }
163     }
164 
165     public void testEscapeSQNameChars() throws Exception {
166         String ontString = "wsmlVariant _\"http://www.wsmo.org/2004/wsml/wsml-flight\" \n"
167                 + "namespace {_\"http://ontotext#\", \n"
168                 + "dc _\"http://purl.org/dc/elements/1.1#\"} \n"
169                 + "ontology _\"http://ontotext#myTest.ontology-test\" \n"
170                 + "ontology _\"http://ontotext#ontology\" \n";
171         TopEntity[] entities = parser.parse(new StringBuffer(ontString));
172 
173         StringBuffer output = new StringBuffer();
174         serializer.serialize(entities, output);
175 
176         entities = parser.parse(output);
177     }
178     
179     public void testInstanceAttributeValues() throws Exception {
180         String wsml = getOntologyHeader() +
181                 "concept myConcept \n" +
182                 "   myAttribute impliesType anotherConcept \n" +
183                 "instance myInstance memberOf myConcept \n" +
184                 "   myAttribute hasValue {anotherInstance1, anotherInstance2 }\n";
185         Ontology ont = (Ontology) parser.parse(new StringBuffer(wsml))[0];
186         Instance i = factory.getInstance(factory.createIRI(DEFAULT_NS + "myInstance"));
187         
188         assertEquals(i.listAttributeValues().size(), 1);
189         assertEquals(i.listAttributeValues(factory.createIRI(DEFAULT_NS + "myAttribute")).size(), 2);
190         i.addAttributeValue(factory.createIRI(DEFAULT_NS + "myAttribute"), 
191                 factory.createInstance(factory.createIRI(DEFAULT_NS + "anotherInstance3")));
192         assertEquals(i.listAttributeValues(factory.createIRI(DEFAULT_NS + "myAttribute")).size(), 3);
193         
194         StringBuffer buffer = new StringBuffer();
195         serializer.serialize(new TopEntity[] {ont}, buffer);
196         parser.parse(buffer);
197     }
198 
199     public void testValueSerialization() throws Exception {
200         String ontString = "namespace {_\"urn:example#\"} \n" + "ontology _\"urn:fzi:anonids\" \n"
201                 + "nfp \n" + "a1 hasValue \"\\\"a1\" \n" + "a2 hasValue 0 \n"
202                 + "a3 hasValue 9.0 \n" + "a4 hasValue _date(2004,12,12) \n" + "endnfp ";
203         System.out.println(ontString);
204 
205         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
206 
207         System.out.println(ont.listNFPValues(factory.createIRI("urn:example#a1")));
208         StringBuffer str = new StringBuffer();
209         serializer.serialize(new TopEntity[] { ont }, str);
210         System.out.println(str);
211         assertNotNull(ont);
212     }
213 
214     public void testBugGabor() throws Exception {
215         String ontString = "namespace {_\"urn:fzi:lordoftherings#\"} \n"
216                 + "ontology _\"urn:fzi:anonids\" \n" + "concept Y \n"
217                 + "concept X subConceptOf _# \n" + "  a impliesType Y \n"
218                 + "instance x memberOf X \n" + "a hasValue _# \n " + "axiom "
219                 + " nfp test hasValue 0.0 endnfp" + " definedBy \n "
220                 + "   ?x memberOf X :- ?x memberOf X. ";
221         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
222         assertNotNull(ont);
223     }
224 
225     public void testBug1280781() throws Exception {
226         String ontString = "wsmlVariant _\"http://www.wsmo.org/2004/wsml/wsml-flight\" \n"
227                 + "namespace {_\"urn:fzi:lordoftherings#\", \n"
228                 + "dc _\"http://purl.org/dc/elements/1.1#\"} \n"
229                 + "ontology _\"urn:fzi:lordoftherings\" \n"
230                 + "concept Living subConceptOf Being \n" + "livesAt ofType Location \n"
231                 + "instance Rivendale memberOf Location \n";
232         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
233         assertNotNull(ont);
234     }
235 
236     public void testBugGaborRelation() throws Exception {
237         String ontString = "namespace {_\"urn:fzi:lordoftherings#\"} \n"
238                 + "ontology _\"urn:fzi:locationbug\" \n" + "instance Arathorn  \n"
239                 + "  hasName hasValue \"Arathorn\"";
240         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
241         assertNotNull(ont);
242     }
243 
244     public void testBug1252521() throws Exception {
245         String ontString = "wsmlVariant _\"http://www.wsmo.org/2004/wsml/wsml-core\" \n"
246                 + " namespace { _\"http://ex.org/\" } \n" + " ontology test \n " + " concept c \n "
247                 + " a impliesType _string \n";
248         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
249         StringBuffer str = new StringBuffer();
250         serializer.serialize(new TopEntity[] { ont }, str);
251         //forget the singeltons
252         ont = null;
253         System.gc();
254         ont = (Ontology) parser.parse(str)[0];
255         assertEquals(ont.getWsmlVariant(), "http://www.wsmo.org/2004/wsml/wsml-core");
256     }
257 
258     public void testSerializeKeyWord() throws Exception {
259         String ontString = " namespace { _\"http://ex.org/\" } \n" + " ontology example \n"
260                 + " concept _\"http://ex.org/concept\" \n"
261                 + " axiom a definedBy _\"http://ex.org/concept\" .  ";
262         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
263         StringBuffer str = new StringBuffer();
264         serializer.serialize(new TopEntity[] { ont }, str);
265         System.out.println(str);
266         //try to reparse
267         ont = (Ontology) parser.parse(str)[0];
268 
269     }
270 
271     public void testBugAnonIDSerialize() throws Exception {
272         String ontString = " namespace { _\"http://x.org/\" } \n" + " ontology example \n"
273                 + " concept Dwarf " + " instance Gimli memberOf Dwarf \n "
274                 + "    hasParent hasValue Gloin \n" + "    hasParent hasValue _#";
275         Ontology ont = (Ontology) parser.parse(new StringBuffer(ontString))[0];
276         StringBuffer str = new StringBuffer();
277         serializer.serialize(new TopEntity[] { ont }, str);
278         //System.out.println(str);
279         //forget the singeltons
280         ont = null;
281         System.gc();
282         ont = (Ontology) parser.parse(str)[0];
283         Instance gimli = ont.findInstance(factory.createIRI("http://x.org/Gimli"));
284         Map attval = gimli.listAttributeValues();
285         Identifier hasParent = (Identifier) attval.keySet().iterator().next();
286         assertEquals(2, gimli.listAttributeValues(hasParent).size());
287     }
288 
289     public void testBug1249600() throws Exception {
290         String ontString = "namespace { _\"urn:test:attributes#\"} \n" + " ontology example12 \n"
291                 + " concept Human \n " + " hasParent inverseOf(hasChild) impliesType Human \n "
292                 + " hasChild impliesType Human";
293 
294         Ontology ontology = (Ontology) parser.parse(new StringBuffer(ontString))[0];
295 
296         Concept human = ontology.findConcept(factory.createIRI("urn:test:attributes#Human"));
297         Attribute hasParent = human.findAttributes(factory.createIRI("urn:test:attributes#hasParent")).iterator().next();
298         assertNotNull(hasParent);
299         Identifier hasChild = hasParent.getInverseOf();
300         assertNotNull(hasChild);
301         assertEquals(hasChild.toString(), "urn:test:attributes#hasChild");
302     }
303 
304     public void testBug1238534() throws Exception {
305         String ontString = "namespace { _\"urn:test:attributes#\"} \n" + " ontology example12 \n"
306                 + "nfp something hasValue \"with\\\"quotes\" endnfp ";
307 
308         Ontology ontology = (Ontology) parser.parse(new StringBuffer(ontString))[0];
309         StringBuffer str = new StringBuffer();
310         serializer.serialize(new TopEntity[] { ontology }, str);
311 
312     }
313 
314     public void testMetaModelling() throws Exception {
315         String test = getOntologyHeader() + "instance boing747 memberOf aircraft \n"
316                 + "instance airforceone memberOf boing747 \n" + "concept boing747 \n" + "";
317 
318         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
319         assertEquals(2, o.listInstances().size());
320 
321         Concept boing747 = o.findConcept(factory.createIRI(DEFAULT_NS + "boing747"));
322         assertEquals(1, boing747.listInstances().size());
323     }
324 
325     /**
326      * checking the cardinality parsing
327      */
328     public void testBug1293837NumericNFP() throws Exception {
329         String test = getOntologyHeader() + "nfp \n" + "  dc#a2 hasValue 10 \n" + "endnfp \n";
330         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
331 
332     }
333 
334     /**
335      * checking the cardinality parsing
336      */
337     public void testAttributeCard() throws Exception {
338         String test = getOntologyHeader() + "concept c1 attr1 ofType (1) c \n"
339                 + "concept c2 attr2 ofType (1 *) c \n" + "concept c3 attr3 ofType c \n"
340                 + "concept c4 attr4 impliesType (1 20) c \n";
341         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
342 
343         Concept c1 = o.findConcept(factory.createIRI(DEFAULT_NS + "c1"));
344         Attribute attr1 = c1.findAttributes(factory.createIRI(DEFAULT_NS + "attr1")).iterator().next();
345         assertEquals("minCard c1,attr1=1", 1, attr1.getMinCardinality());
346         // only min card is shortcut for (1 1)
347         assertEquals("maxCard c1,attr1=1", 1, attr1.getMaxCardinality());
348 
349         Concept c2 = o.findConcept(factory.createIRI(DEFAULT_NS + "c2"));
350         Attribute attr2 = c2.listAttributes().iterator().next();
351         assertEquals("minCard c2,attr2=1", 1, attr2.getMinCardinality());
352         // TODO:should be documented in API that it is * = MAX_VALUE 
353         //      (and max(XSD#INTEGER) != Integer.MAX_VALUE)
354         assertEquals("maxCard c2,attr2=*", Integer.MAX_VALUE, attr2.getMaxCardinality());
355 
356         Concept c3 = o.findConcept(factory.createIRI(DEFAULT_NS + "c3"));
357         Attribute attr3 = c3.listAttributes().iterator().next();
358         assertEquals("minCard c3,attr3=0", 0, attr3.getMinCardinality());
359         assertEquals("maxCard c3,attr3=*", Integer.MAX_VALUE, attr3.getMaxCardinality());
360 
361         Concept c4 = o.findConcept(factory.createIRI(DEFAULT_NS + "c4"));
362         Attribute attr4 = c4.listAttributes().iterator().next();
363         assertEquals("minCard c4,attr4=1", 1, attr4.getMinCardinality());
364         assertEquals("maxCard c4,attr4=20", 20, attr4.getMaxCardinality());
365     }
366 
367     public void testAxiomDefinition() throws Exception {
368         String test = getOntologyHeader() + "axiom a1223 \n" + "  definedBy a1223 impliedBy b455. ";
369         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
370         Axiom a = o.findAxiom(factory.createIRI(DEFAULT_NS + "a1223"));
371         assertEquals(a.listDefinitions().size(), 1);
372     }
373 
374     /**
375      * check attribute Features (transitive, reflexive, symmetric, inverseOf)
376      */
377     public void testAttributeFeatures() throws Exception {
378 
379         String test = getOntologyHeader() + "concept c attr symmetric inverseOf(attr1) ofType c \n"
380                 + "concept c2 attr2 transitive ofType c \n"
381                 + "concept c3 attr3 reflexive impliesType c \n";
382 
383         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
384 
385         Concept c = o.findConcept(factory.createIRI(DEFAULT_NS + "c"));
386         Attribute attr = c.listAttributes().iterator().next();
387         assertTrue("attr is constraining", attr.isConstraining());
388         assertTrue("attr is symetric", attr.isSymmetric());
389 
390         Concept c2 = o.findConcept(factory.createIRI(DEFAULT_NS + "c2"));
391         Attribute attr2 = c2.listAttributes().iterator().next();
392         assertEquals(attr2.isTransitive(), true);
393         assertEquals(attr2.isReflexive(), false);
394 
395         Concept c3 = o.findConcept(factory.createIRI(DEFAULT_NS + "c3"));
396         Attribute attr3 = c3.listAttributes().iterator().next();
397         assertEquals(attr3.isReflexive(), true);
398         assertEquals(attr3.isTransitive(), false);
399     }
400 
401     /**
402      */
403     public void testDataValueandType() throws Exception {
404         String test = getOntologyHeader() + "concept c123 \n" + "  dbl ofType _double \n"
405                 + "  int ofType _integer \n" + "  str ofType _string \n" + "  dte ofType _date \n"
406                 + "  dbl ofType _double \n" + "instance i //memberOf c\n" + "  dbl hasValue 0.0 \n"
407                 + "  int hasValue 1 \n" + "  str hasValue \"a string\" \n "
408                 + "  dte hasValue _date(2005,2,2) \n";
409 
410         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
411 
412         Concept c = o.findConcept(factory.createIRI(DEFAULT_NS + "c123"));
413         Attribute dbl = c.findAttributes(factory.createIRI(DEFAULT_NS + "dbl")).iterator().next();
414         Attribute integer = c.findAttributes(factory.createIRI(DEFAULT_NS + "int")).iterator().next();
415         Attribute str = c.findAttributes(factory.createIRI(DEFAULT_NS + "str")).iterator().next();
416         Attribute date = c.findAttributes(factory.createIRI(DEFAULT_NS + "dte")).iterator().next();
417 
418         assertEquals(WsmlDataType.WSML_DOUBLE, ((WsmlDataType) dbl.listTypes().iterator().next())
419                 .getIRI().toString());
420         assertEquals(WsmlDataType.WSML_INTEGER, ((WsmlDataType) integer.listTypes().iterator()
421                 .next()).getIRI().toString());
422         assertEquals(WsmlDataType.WSML_STRING, ((WsmlDataType) str.listTypes().iterator().next())
423                 .getIRI().toString());
424         assertEquals(WsmlDataType.WSML_DATE, ((WsmlDataType) date.listTypes().iterator().next())
425                 .getIRI().toString());
426 
427         //Instance i = o.findInstance(factory.createIRI(DEFAULT_NS +"i"));
428         //Map attrMap = i.listAttributeValues();
429         //not sure about this, best would to have getJavaValue for checking easily values
430         //assertEquals(((DataValue)attrMap.get(dbl)).toString(),"0.0");
431         //assertEquals(((DataValue)attrMap.get(integer)).toString(),"0.0");
432         //assertEquals(((DataValue)attrMap.get(str)).toString(),"0.0");
433         //assertEquals(((DataValue)attrMap.get(date)).toString(),"0.0");
434     }
435 
436     /**
437      * test for checking if an empty stream does parse and return
438      * empty array.
439      */
440     public void testEmptyStream() throws Exception {
441         String test = "";
442         TopEntity[] topEntity = parser.parse(new StringBuffer(test));
443         assertEquals("Empty Stream must empty Array", 0, topEntity.length);
444     }
445 
446     /*
447      * some checks on location ontology.
448      */
449     public void testLocationsOnt() throws Exception {
450         InputStream resStream = getStream("test/locations.wsml");
451         assertNotNull("access test WSML file", resStream);
452         String ns = "http://www.example.org/example/";
453 
454         Reader reader1 = new InputStreamReader(resStream);
455         TopEntity topEntity = parser.parse(reader1)[0];
456 
457         assertTrue("Ontology Instance", topEntity instanceof Ontology);
458 
459         Ontology onto = (Ontology) topEntity;
460 
461         IRI countryRef = factory.createIRI(ns + "country");
462         Concept country = onto.findConcept(countryRef);
463 
464         assertNotNull("contry not found", country);
465 
466         IRI countryInstRef = factory.createIRI(ns + "austria");
467         Instance austria = onto.findInstance(countryInstRef);
468 
469         assertNotNull("'austria' not found", austria);
470         Set attrSet = country.listAttributes();
471 
472         assertTrue("exactly two attributes", attrSet.size() == 2);
473 
474         assertEquals("Country should ahve 1 nfp", country.listNFPValues().size(), 1);
475 
476         Attribute fipsCode = country.findAttributes(factory.createIRI(ns + "fipsCode")).iterator().next();
477         assertEquals("exaclty on nfp on attribute", fipsCode.listNFPValues().size(), 1);
478     }
479 
480     /**
481      * checks the parsing and handling of relation 
482      * TODO clarify why it throw SynchronizationException
483      *      respectively why proxy objects only allow getIdentifier
484      */
485     public void testParserTypedRelation() throws Exception {
486         String test = getOntologyHeader() + "concept location \n"
487                 + "relation distance(ofType location, ofType location, ofType _decimal)";
488         Ontology ont = (Ontology) parser.parse(new StringBuffer(test))[0];
489 
490         Identifier distId = factory.createIRI(DEFAULT_NS + "distance");
491         Relation r = ont.findRelation(distId);
492         assertNotNull("relation distance not found", r);
493         assertEquals("wrong id", r.getIdentifier(), distId);
494         assertEquals("wrong arity of relation", r.listParameters().size(), 3);
495 
496         assertEquals("first types location should be equal", r.getParameter((byte) 1)
497                 .listTypes().iterator().next(), r.getParameter((byte) 0).listTypes()
498                 .iterator().next());
499 
500         assertEquals("first types location should be equal", WsmlDataType.WSML_DECIMAL,
501                 ((WsmlDataType) r.getParameter((byte) 2).listTypes().iterator().next()).getIRI()
502                         .toString());
503     }
504 
505     public void testParserUnTypedRelation() throws Exception {
506         String test = getOntologyHeader() + "relation someRelation/3 ";
507         Ontology ont = (Ontology) parser.parse(new StringBuffer(test))[0];
508 
509         Identifier relId = factory.createIRI(DEFAULT_NS + "someRelation");
510         Relation r = ont.findRelation(relId);
511         assertNotNull("relation distance not found", r);
512         assertEquals("wrong id", r.getIdentifier(), relId);
513         assertEquals("wrong arity of relation", r.listParameters().size(), 3);
514 
515         StringBuffer str = new StringBuffer();
516         serializer.serialize(new TopEntity[] { ont }, str);
517         //System.out.print(">>>>>"+str+"<<<<<");
518 
519         ont = null;
520         System.gc();
521 
522         ont = (Ontology) parser.parse(str)[0];
523         assertNotNull("relation distance not found", r);
524         assertEquals("wrong id", r.getIdentifier(), relId);
525         assertEquals("wrong arity of relation", r.listParameters().size(), 3);
526     }
527 
528     /**
529      * test for checking if nfps are parsed and serialized correctly. 
530      */
531     public void testSaveDcTitle() throws Exception {
532         System.gc();
533         String test = getOntologyHeader() + "  concept b12 "
534                 + "    nfp dc#title hasValue \"xyz\" endnfp \n" + "    age12 ofType _decimal \n"
535                 + " instance i12 memberOf b12 \n" + "    age12 hasValue 0.0 ";
536 
537         TopEntity[] topEntity = parser.parse(new StringBuffer(test));
538         StringBuffer str = new StringBuffer();
539         serializer.serialize(topEntity, str);
540         topEntity = null;
541         System.gc();
542 
543         Ontology ont = (Ontology) parser.parse(str)[0];
544         Concept b12 = ont.findConcept(factory.createIRI(DEFAULT_NS + "b12"));
545         assertNotNull(b12);
546         Set values = b12.listNFPValues(factory.createIRI(NFP.DC_TITLE));
547         Value v1 = (Value) values.iterator().next();
548         assertEquals("nfp value must be xyz", v1.toString(), "xyz");
549         Instance i12 = ont.findInstance(factory.createIRI(DEFAULT_NS + "i12"));
550         Attribute age12 = b12.findAttributes(factory.createIRI(DEFAULT_NS + "age12")).iterator().next();
551         DataValue v2 = (DataValue) i12.listAttributeValues(age12.getIdentifier()).iterator().next();
552         assertEquals(new BigDecimal("0.0"), v2.getValue());
553 
554         Type t = age12.listTypes().iterator().next();
555         assertTrue(WsmlDataType.class.isInstance(t));
556     }
557 
558     /**
559      * checking if attribute restrictions are local.
560      */
561     public void testAttributeLocality() throws Exception {
562         String test = getOntologyHeader() + "concept c1 a ofType c1 \n"
563                 + "concept c2 a ofType c2 \n";
564 
565         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
566 
567         Concept c1 = o.findConcept(factory.createIRI(DEFAULT_NS + "c1"));
568         Attribute a1 = c1.findAttributes(factory.createIRI(DEFAULT_NS + "a")).iterator().next();
569 
570         Concept c2 = o.findConcept(factory.createIRI(DEFAULT_NS + "c2"));
571         Attribute a2 = c2.findAttributes(factory.createIRI(DEFAULT_NS + "a")).iterator().next();
572 
573         //something wrong, see serialization:
574         //System.out.println(serialize(o));
575 
576         assertEquals(1, a1.listTypes().size());
577         assertEquals(DEFAULT_NS + "c1", ((Concept) a1.listTypes().iterator().next())
578                 .getIdentifier().toString());
579 
580         assertEquals(1, a2.listTypes().size());
581         assertEquals(DEFAULT_NS + "c2", ((Concept) a2.listTypes().iterator().next())
582                 .getIdentifier().toString());
583 
584     }
585 
586     /**
587      * checking if ParseException returns correct line numbers
588      */
589     public void testParserException() throws Exception {
590         String test = getOntologyHeader() + "ontology a concept a subc b ";
591         try {
592             parser.parse(new StringBuffer(test));
593         }
594         catch (ParserException e) {
595             assertEquals(e.getErrorLine(), 6);
596             assertEquals(e.getErrorPos(), 27);
597         }
598 
599         test = "namespace {_\"http://ex.org\"} " + "ontology nfp test hasValue 0\n "
600                 + "endNonFunctionalPropertis \n\n" + "axiom";
601         try {
602             parser.parse(new StringBuffer(test));
603         }
604         catch (ParserException e) {
605             assertEquals(4, e.getErrorLine());
606             assertEquals(1, e.getErrorPos());
607             assertEquals("'hasValue'", e.getExpectedToken());
608             assertEquals("axiom", e.getFoundToken());
609         }
610 
611         test = "ontology _\"h:dd\" axiom _\"h:jj\" definedBy\n someConcept. ";
612         try {
613             parser.parse(new StringBuffer(test));
614         }
615         catch (ParserException e) {
616             //System.out.println(e);
617             assertEquals(2, e.getErrorLine());
618             assertEquals(2, e.getErrorPos());
619             assertEquals("someConcept", e.getFoundToken());
620         }
621 
622         test = "ontology _\"h:dd\" axiom _\"h:jj\" \n" + "definedBy\n" + "a#concept. ";
623         try {
624             parser.parse(new StringBuffer(test));
625         }
626         catch (ParserException e) {
627             //System.out.println(e);
628             assertEquals(3, e.getErrorLine());
629             assertEquals(1, e.getErrorPos());
630             assertEquals("a#concept", e.getFoundToken());
631         }
632 
633         test = "namespace {_\"http://ex.org\"}\n" + "ontology ontWithoutDefaultNamespace ";
634         try {
635             Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
636             //System.out.println(o.getIdentifier());
637         }
638         catch (ParserException e) {
639             //System.out.println(e);
640         }
641 
642         test = "ontology ontWithoutDefaultNamespace ";
643         try {
644             parser.parse(new StringBuffer(test));
645         }
646         catch (ParserException e) {
647             //System.out.println(e);
648             assertEquals(1, e.getErrorLine());
649             assertEquals(10, e.getErrorPos());
650             assertEquals("ontWithoutDefaultNamespace", e.getFoundToken());
651         }
652 
653     }
654 
655     /*
656      * Check if a InputStream can be read with example file provided in spec.
657      */
658     public void testD16() throws Exception {
659         String file = "test/d16-example.wsml";
660         InputStream resStream = getClass().getClassLoader().getResourceAsStream(file);
661         assertNotNull("can't load" + file + "from ClassPath", resStream);
662 
663         Reader reader1 = new InputStreamReader(resStream);
664         TopEntity topEntity = parser.parse(reader1)[0];
665 
666         Ontology ex = (Ontology) topEntity;
667         assertTrue("7 concepts in ontology ", ex.listConcepts().size() == 7);
668 
669         StringBuffer str = new StringBuffer();
670         serializer.serialize(new TopEntity[] { ex }, str);
671         //System.out.print(str);
672     }
673 
674     public void testBug1315669() throws Exception {
675         String serviceWSML = "webService _\"" + DEFAULT_NS + "serviceIRI\" \n" + "interface _\""
676                 + DEFAULT_NS + "intIRI\" \n" + "orchestration _\"" + DEFAULT_NS
677                 + "orchestration\" \n";
678 
679         TopEntity[] entityList = parser.parse(new StringReader(serviceWSML));
680         assertTrue(entityList.length == 2);
681         assertTrue(entityList[0] instanceof WebService);
682         assertTrue(entityList[0].getIdentifier().toString().equals(DEFAULT_NS + "serviceIRI"));
683 
684         WebService service = (WebService) entityList[0];
685         assertTrue(service.listInterfaces().size() == 1);
686 
687         Interface interfc = service.listInterfaces().iterator().next();
688         assertTrue(interfc.getIdentifier().toString().equals(DEFAULT_NS + "intIRI"));
689 
690         /* Choreography tests moved to choreography module ext
691          /assertTrue(interfc.getChoreography().getIdentifier().toString().equals(DEFAULT_NS+"choreography"));
692          assertTrue(interfc.getOrchestration().getIdentifier().toString().equals(DEFAULT_NS+"orchestration"));*/
693     }
694 
695     /**
696      * checks the parsing of relation 
697      */
698     public void testParseUnTypedRelation() throws Exception {
699         String test = getOntologyHeader() + "relation distance/3 \n";
700         TopEntity topEntity = parser.parse(new StringBuffer(test))[0];
701         assertNotNull(topEntity);
702     }
703 
704     public void testReparseAnonID() throws Exception {
705         String test = getOntologyHeader() + "  concept b12 " + " instance _# memberOf b12 ";
706 
707         TopEntity[] topEntity = parser.parse(new StringBuffer(test));
708         StringBuffer str = new StringBuffer();
709         serializer.serialize(topEntity, str);
710         topEntity = null;
711         System.gc();
712 
713         Ontology ont = (Ontology) parser.parse(str)[0];
714         Concept b12 = ont.findConcept(factory.createIRI(DEFAULT_NS + "b12"));
715         assertNotNull(b12);
716         assertEquals(1, ont.listInstances().size());
717     }
718 
719     /**
720      * check if relation instance without relation can be parsed.
721      * @throws Exception
722      */
723     public void testRelationInstance() throws Exception {
724         String test = getOntologyHeader() + "relationInstance dist(ham,muc,300) \n"
725                 + "relationInstance distHamburg2Frankfurt dist(ham,fra,100) \n"
726                 + "relation validLocation(ofType _double) \n";
727         Ontology o = (Ontology) parser.parse(new StringBuffer(test))[0];
728     }
729 
730     /**
731      * check if relation instance without relation can be parsed.
732      * @throws Exception
733      */
734     public void testIRIException() throws Exception {
735         String test = getOntologyHeader() + "concept _\"hhh\" \n";
736         try {
737             parser.parse(new StringBuffer(test));
738             fail("should fail");
739         }
740         catch (ParserException p) {
741             assertEquals(6, p.getErrorLine());
742         }
743     }
744 
745     public void testWWMedator() throws Exception {
746         String test = "namespace {_\"http://test.com#\"} \n" + "wwMediator iri \n"
747                 + "source mySource \n" + "target myTarget ";
748         WWMediator ww1 = (WWMediator) parser.parse(new StringBuffer(test))[0];
749         WWMediator ww2 = (WWMediator) parser.parse(new StringBuffer(test))[0];
750     }
751 
752     public void testOOMedator() throws Exception {
753         String test = "namespace {_\"http://test.com#\"} \n" + "ooMediator mediator1 \n"
754                 + "source mySource1 \n" + "target myTarget1 \n"
755                 + "usesService _\"http://test/usedService\" \n" + "ooMediator mediator2 \n"
756                 + "source { mySource2, mySource3} \n" + "target myTarget2 \n"
757                 + "usesService mySQName \n";
758 
759         TopEntity[] mediators = parser.parse(new StringBuffer(test));
760         assertEquals(mediators.length, 2);
761         assertEquals(((OOMediator) mediators[0]).listSources().size(), 1);
762         assertNotNull(((OOMediator) mediators[0]).getMediationService());
763         assertEquals(((OOMediator) mediators[1]).listSources().size(), 2);
764 
765         assertEquals(((OOMediator) mediators[0]).getMediationService().toString(),
766                 "http://test/usedService");
767         assertEquals(((OOMediator) mediators[1]).getMediationService().toString(),
768                 "http://test.com#mySQName");
769     }
770 
771     public void testWebServiceCapability() throws Exception {
772         String test = "namespace {_\"http://test.com#\"} \n"
773                 + "webService _\"http://example.org/Germany/BirthRegistration\" \n"
774                 + "   capability _\"http://example.org/Germany/BirthRegistration#cap1\" \n"
775                 + "       sharedVariables ?child";
776         WebService webservice = (WebService) parser.parse(new StringBuffer(test))[0];
777         assertNotNull(webservice.getCapability());
778     }
779     
780 
781 
782     
783     public void testClearEntities() throws Exception{        
784         InputStream resStream = getStream("test/d16-example.wsml");
785         assertNotNull("access test WSML file", resStream);
786         Reader reader1 = new InputStreamReader(resStream);
787         
788         Map <String, Object> map = new HashMap <String, Object> ();
789         map.put(Parser.CLEAR_MODEL,"true");
790         Parser parser = Factory.createParser(map);
791         
792         TopEntity[] te = parser.parse(reader1);
793         
794         //Ontology should not be empty!
795         Ontology ont = (Ontology)te[0];
796         assertEquals(7,ont.listConcepts().size());
797         assertEquals(4,ont.listInstances().size());
798         assertEquals(1,ont.listRelationInstances().size());
799         assertEquals(1,ont.listRelations().size());
800         assertEquals(7,ont.listAxioms().size());
801         assertEquals(5,ont.listNamespaces().size());
802         assertEquals(1,ont.listMediators().size());
803         
804         //WebService should not be empty
805         WebService ws = (WebService)te[1];
806         assertEquals(3,ws.listNFPValues().keySet().size());
807         assertEquals(1,ws.listInterfaces().size());
808         Capability cap = ws.getCapability();
809         assertEquals(1,cap.listAssumptions().size());
810         assertEquals(1,cap.listPreConditions().size());
811         assertEquals(1,cap.listEffects().size());
812 
813         //WebService should not be empty
814         Mediator m = (Mediator)te[te.length-1];
815         assertEquals(1,m.listNFPValues().keySet().size());
816         assertEquals(1,m.listSources().size());
817         assertNotNull(m.getTarget());
818 
819         String test = "namespace {_\"http://www.example.org/ontologies/example#\"} \n"
820             + "ontology _\"http://www.example.org/ontologies/example\" \n" +
821               "webService bankTransaction  \n" +
822               "wgMediator _\"http://example.org/wgMediator\"";
823         TopEntity[] te1 = parser.parse(new StringBuffer(test));
824         
825         //Ontology should be empty!
826         Ontology ont1 = (Ontology)te1[0];
827         assertEquals(0,ont1.listConcepts().size());
828         assertEquals(0,ont1.listInstances().size());
829         assertEquals(0,ont1.listRelationInstances().size());
830         assertEquals(0,ont1.listRelations().size());
831         assertEquals(0,ont1.listAxioms().size());
832         assertEquals(0,ont1.listNamespaces().size());
833         assertEquals(0,ont1.listMediators().size());
834         
835         //WebService should be empty
836         WebService ws1 = (WebService)te1[1];
837         assertEquals(0,ws1.listNFPValues().keySet().size());
838         assertEquals(0,ws1.listInterfaces().size());
839         assertEquals(null,ws1.getCapability());
840 
841         //Mediator should now be empty
842         Mediator m1 = (Mediator)te1[te1.length-1];
843         assertEquals(0,m1.listNFPValues().keySet().size());
844         assertEquals(0,m1.listSources().size());
845         assertNull(m1.getTarget());
846 
847         
848     }
849 }