diff --git a/test/src/EDOALExportTest.java b/test/src/EDOALExportTest.java new file mode 100644 index 0000000000000000000000000000000000000000..472749ee8912988ccecd3ba5afae36e51d08557c --- /dev/null +++ b/test/src/EDOALExportTest.java @@ -0,0 +1,633 @@ +/* + * $Id$ + * + * Copyright (C) 2006 Digital Enterprise Research Insitute (DERI) Innsbruck + * Sourceforge version 1.3 -- 2007 + * Copyright (C) INRIA, 2009-2010 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Configuration; +import org.testng.annotations.Test; +//import org.testng.annotations.*; + +import org.semanticweb.owl.align.AlignmentVisitor; +import org.semanticweb.owl.align.AlignmentException; +import org.semanticweb.owl.align.Alignment; +import org.semanticweb.owl.align.Visitable; + +import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; +import fr.inrialpes.exmo.align.impl.Annotations; +import fr.inrialpes.exmo.align.impl.Namespace; +import fr.inrialpes.exmo.align.impl.edoal.EDOALAlignment; +import fr.inrialpes.exmo.ontowrap.Ontology; +import fr.inrialpes.exmo.ontowrap.BasicOntology; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.FileOutputStream; +import java.io.OutputStream; + +// JE: the old imports + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import java.net.URI; +import java.net.URISyntaxException; + +//import org.omwg.mediation.language.export.omwg.OmwgSyntaxFormat; +import fr.inrialpes.exmo.align.parser.SyntaxElement; +import fr.inrialpes.exmo.align.parser.SyntaxElement.Constructor; + +import fr.inrialpes.exmo.align.impl.edoal.Id; +import fr.inrialpes.exmo.align.impl.edoal.PathExpression; +import fr.inrialpes.exmo.align.impl.edoal.Expression; +import fr.inrialpes.exmo.align.impl.edoal.ClassExpression; +import fr.inrialpes.exmo.align.impl.edoal.ClassId; +import fr.inrialpes.exmo.align.impl.edoal.ClassConstruction; +import fr.inrialpes.exmo.align.impl.edoal.ClassRestriction; +import fr.inrialpes.exmo.align.impl.edoal.ClassTypeRestriction; +import fr.inrialpes.exmo.align.impl.edoal.ClassValueRestriction; +import fr.inrialpes.exmo.align.impl.edoal.ClassOccurenceRestriction; +import fr.inrialpes.exmo.align.impl.edoal.PropertyExpression; +import fr.inrialpes.exmo.align.impl.edoal.PropertyId; +import fr.inrialpes.exmo.align.impl.edoal.PropertyConstruction; +import fr.inrialpes.exmo.align.impl.edoal.PropertyRestriction; +import fr.inrialpes.exmo.align.impl.edoal.PropertyDomainRestriction; +import fr.inrialpes.exmo.align.impl.edoal.PropertyTypeRestriction; +import fr.inrialpes.exmo.align.impl.edoal.PropertyValueRestriction; +import fr.inrialpes.exmo.align.impl.edoal.RelationExpression; +import fr.inrialpes.exmo.align.impl.edoal.RelationId; +import fr.inrialpes.exmo.align.impl.edoal.RelationConstruction; +import fr.inrialpes.exmo.align.impl.edoal.RelationRestriction; +import fr.inrialpes.exmo.align.impl.edoal.RelationDomainRestriction; +import fr.inrialpes.exmo.align.impl.edoal.RelationCoDomainRestriction; +import fr.inrialpes.exmo.align.impl.edoal.InstanceExpression; +import fr.inrialpes.exmo.align.impl.edoal.InstanceId; + +import fr.inrialpes.exmo.align.impl.edoal.TransfService; +import fr.inrialpes.exmo.align.impl.edoal.Value; +import fr.inrialpes.exmo.align.impl.edoal.Datatype; +import fr.inrialpes.exmo.align.impl.edoal.Comparator; + + +/** + * These tests corresponds to the tests presented in the examples/omwg directory + */ + +public class EDOALExportTest { + + // private OmwgSyntaxFormat sf; + private RDFRendererVisitor renderer; + private PrintWriter writer; + private ByteArrayOutputStream stream; + + @Test(groups = { "full", "omwg", "raw" }) + //@BeforeClass(groups = { "full", "omwg", "raw" }) + public void setUp() throws Exception { + // JE2009: Suppress this once all tests pass with render instead of export + // sf = new OmwgSyntaxFormat(); + } + + private String render( Visitable v ) throws Exception { + // JE2009: This can only be improved if we can change the stream + stream = new ByteArrayOutputStream(); + writer = new PrintWriter ( new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + renderer = new RDFRendererVisitor( writer ); + renderer.setIndentString(""); // Indent should be empty + renderer.setNewLineString(""); + v.accept( renderer ); + writer.flush(); + writer.close(); + stream.close(); + return stream.toString(); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportPath() throws Exception { + /* + assertEquals( render( new Path(new PropertyId(new URI("http://my.beauty.url")))), + "<edoal:Property rdf:about=\"http://my.beauty.url\"/>"); + assertEquals( render( Path.EMPTY ),"<edoal:Path rdf:resource=\"http://ns.inria.fr/edoal#emptyPath\"/>"); + */ + final LinkedHashSet<PathExpression> expressions = new LinkedHashSet<PathExpression>(3); + expressions.add( new RelationId(new URI("http://my.beauty.url")) ); + expressions.add( new RelationId(new URI("http://my.nasty.url")) ); + expressions.add( new RelationId(new URI("http://my.richi.url")) ); + assertEquals( render( new RelationConstruction( Constructor.COMP, expressions ) ), + "<edoal:Relation><edoal:compose rdf:parseType=\"Collection\">" + + "<edoal:Relation rdf:about=\"http://my.beauty.url\"/>" + + "<edoal:Relation rdf:about=\"http://my.nasty.url\"/>" + + "<edoal:Relation rdf:about=\"http://my.richi.url\"/>" + + "</edoal:compose></edoal:Relation>" ); + + expressions.add( new PropertyId(new URI("http://my.final.url")) ); + assertEquals( render( new PropertyConstruction( Constructor.COMP, expressions ) ), + "<edoal:Property><edoal:compose rdf:parseType=\"Collection\">" + + "<edoal:Relation rdf:about=\"http://my.beauty.url\"/>" + + "<edoal:Relation rdf:about=\"http://my.nasty.url\"/>" + + "<edoal:Relation rdf:about=\"http://my.richi.url\"/>" + + "<edoal:Property rdf:about=\"http://my.final.url\"/>" + + "</edoal:compose></edoal:Property>" ); + } + + /* +This is a test for ontowrap... not EDOAL. +Does bot work anymore because not visitable + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportOntology() throws Exception { //URISyntax + Ontology onto = new BasicOntology(); + onto.setURI( new URI("http://path.to.the/source") ); + onto.setFormalism( "wsml" ); + onto.setFormURI( new URI("http://path.to.the/spec") ); + assertEquals( render( onto ), "<Ontology rdf:about=\"http://path.to.the/source\"><location>http://path.to.the/source</location><formalism><Formalism align:name=\"wsml\" align:uri=\"http://path.to.the/spec\"/></formalism></Ontology>"); + + onto.setURI( new URI("http://path.to.the/target") ); + onto.setFormalism( "owl" ); + onto.setFormURI( new URI("http://path.to.the/owl") ); + assertEquals( render( onto ), "<Ontology rdf:about=\"http://path.to.the/target\"><location>http://path.to.the/target</location><formalism><Formalism align:name=\"owl\" align:uri=\"http://path.to.the/owl\"/></formalism></Ontology>"); + } + */ + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportInstanceExpression() throws Exception { + final InstanceExpression toExport = new InstanceId(new URI("http://meine.tolle/instance#blah")); + assertEquals( render( toExport ), + "<edoal:Instance rdf:about=\"http://meine.tolle/instance#blah\"/>" ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprSimple() throws Exception { + final ClassExpression ce = new ClassId("Amertume"); + final String ref = "<edoal:Class rdf:about=\"Amertume\"/>"; + assertEquals( render( ce ), ref ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprSimpleError() throws Exception { + // Should raise an error + final ClassExpression ce = new ClassId("Amertume"); + final String ref = "<edoal:Class rdf:about=\"Amertume\"/>"; + assertEquals( render( ce ), ref ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassCond() throws Exception { + ClassRestriction toExport = null; + toExport = new ClassValueRestriction(new PropertyId(new URI("http://my.sister#age")),Comparator.GREATER,new Value("18")); + assertEquals( render( toExport ), "<edoal:PropertyValueRestriction>" + + "<edoal:onProperty><edoal:Property rdf:about=\"http://my.sister#age\"/></edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#greater-than\"/>" + + "<edoal:value>18</edoal:value>" + + "</edoal:PropertyValueRestriction>" ); + toExport = new ClassTypeRestriction( new PropertyId(new URI("http://my.sister#age")), new Datatype("18")); + assertEquals( render( toExport ), "<edoal:PropertyTypeRestriction>" + + "<edoal:onProperty><edoal:Property rdf:about=\"http://my.sister#age\"/></edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + + "<edoal:datatype>18</edoal:datatype>" + + "</edoal:PropertyTypeRestriction>" ); + toExport = new ClassOccurenceRestriction( new PropertyId(new URI("http://my.sister#age")), Comparator.GREATER, 18); + assertEquals( render( toExport ), "<edoal:PropertyOccurenceRestriction>" + + "<edoal:onProperty><edoal:Property rdf:about=\"http://my.sister#age\"/></edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#greater-than\"/>" + + "<edoal:value>18</edoal:value>" + + "</edoal:PropertyOccurenceRestriction>" ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprOr() throws Exception { + final Set<ClassExpression> expressions = new LinkedHashSet<ClassExpression>(3); + expressions.add( new ClassId("Acidite") ); + expressions.add( new ClassId("Amertume") ); + expressions.add( new ClassId("Astreinngence") ); + final ClassExpression ce = new ClassConstruction( Constructor.OR, expressions ); + final String ref = "<edoal:Class>" + "<edoal:or rdf:parseType=\"Collection\">" + + "<edoal:Class rdf:about=\"Acidite\"/>" + + "<edoal:Class rdf:about=\"Amertume\"/>" + + "<edoal:Class rdf:about=\"Astreinngence\"/>" + + "</edoal:or>"+"</edoal:Class>"; + assertEquals( render( ce ), ref ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprAnd() throws Exception { + final Set<ClassExpression> expressions = new LinkedHashSet<ClassExpression>(3); + expressions.add(new ClassId("Acidite")); + expressions.add(new ClassId("Amertume")); + expressions.add(new ClassId("Astreinngence")); + final ClassExpression ce = new ClassConstruction( Constructor.AND, expressions ); + final String ref = "<edoal:Class>" + "<edoal:and rdf:parseType=\"Collection\">" + + "<edoal:Class rdf:about=\"Acidite\"/>" + + "<edoal:Class rdf:about=\"Amertume\"/>" + + "<edoal:Class rdf:about=\"Astreinngence\"/>" + + "</edoal:and>"+"</edoal:Class>"; + assertEquals( render( ce ), ref ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprNot() throws Exception { + final ClassExpression ce = new ClassConstruction(Constructor.NOT, + Collections.singleton((ClassExpression)new ClassId("Acidite"))); + final String ref = "<edoal:Class>" + "<edoal:not>" + + "<edoal:Class rdf:about=\"Acidite\"/>" + "</edoal:not>" + + "</edoal:Class>"; + assertEquals( render( ce ), ref ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprOrCond() throws Exception { + final Set<ClassExpression> expressions = new LinkedHashSet<ClassExpression>(3); + expressions.add(new ClassId("Acidite")); + expressions.add(new ClassId("Amertume")); + expressions.add(new ClassId("Astreinngence")); + expressions.add(new ClassValueRestriction( new PropertyId(new URI("http://vinum#age")), Comparator.GREATER, new Value("20"))); + final ClassExpression ce = new ClassConstruction( Constructor.OR, expressions ); + assertEquals( render( ce ), "<edoal:Class>" + "<edoal:or rdf:parseType=\"Collection\">" + + "<edoal:Class rdf:about=\"Acidite\"/>" + + "<edoal:Class rdf:about=\"Amertume\"/>" + + "<edoal:Class rdf:about=\"Astreinngence\"/>" + + "<edoal:PropertyValueRestriction>" + + "<edoal:onProperty>" + + "<edoal:Property rdf:about=\"http://vinum#age\"/>" + + "</edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#greater-than\"/>" + + "<edoal:value>20</edoal:value>" + + "</edoal:PropertyValueRestriction>" + + "</edoal:or>"+ "</edoal:Class>" ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportPropertyCond() throws Exception { + assertEquals( render( new PropertyDomainRestriction(new ClassId("http://meine/tolle/restriction")) ), + "<edoal:DomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"http://meine/tolle/restriction\"/>" + + "</edoal:toClass></edoal:DomainRestriction>" ); + assertEquals( render( new PropertyValueRestriction( Comparator.EQUAL, new Value("18"))), + "<edoal:ValueRestriction>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + + "<edoal:value>18</edoal:value>" + + "</edoal:ValueRestriction>" ); + assertEquals( render( new PropertyTypeRestriction(new Datatype("int"))), + "<edoal:TypeRestriction><edoal:datatype>int</edoal:datatype></edoal:TypeRestriction>" ); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportPropertyExpr() throws Exception { + final Set<PathExpression> expressions = new LinkedHashSet<PathExpression>(2); + expressions.add(new PropertyId(new URI("http://mein/super/property0"))); + expressions.add(new PropertyId(new URI("http://mein/super/property1"))); + final PropertyId single = new PropertyId(new URI("http://mein/super/property")); + + PropertyExpression toExport = single; + assertEquals( render( toExport), "<edoal:Property rdf:about=\"http://mein/super/property\"/>"); + toExport = new PropertyConstruction( Constructor.AND, expressions ); + assertEquals( render( toExport), "<edoal:Property><edoal:and rdf:parseType=\"Collection\">" + + "<edoal:Property rdf:about=\"http://mein/super/property0\"/>" + + "<edoal:Property rdf:about=\"http://mein/super/property1\"/>" + + "</edoal:and></edoal:Property>"); + // JE2009: Illegal + /* + toExport = new PropertyExpression( + single, + Collections + .singleton((PropertyExpression) new PropertyDomainRestriction( + new ClassId("http://my/super/class"), + Comparator.EQUAL))); + assertEquals( render( toExport), "<edoal:Property rdf:about=\"http://mein/super/property\">" + + "<edoal:domainRestriction>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:domainRestriction>" + "</edoal:Property>"); + */ + + final Set<PathExpression> expressions2 = new LinkedHashSet<PathExpression>(2); + expressions2.add( new PropertyConstruction( Constructor.OR, expressions )); + expressions2.add( new PropertyValueRestriction(Comparator.EQUAL,new Value("5"))); + toExport = new PropertyConstruction( Constructor.AND, expressions2 ); + assertEquals( render( toExport), "<edoal:Property><edoal:and rdf:parseType=\"Collection\"><edoal:Property><edoal:or rdf:parseType=\"Collection\">" + + "<edoal:Property rdf:about=\"http://mein/super/property0\"/>" + + "<edoal:Property rdf:about=\"http://mein/super/property1\"/>" + + "</edoal:or></edoal:Property>" + + "<edoal:ValueRestriction>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + + "<edoal:value>5</edoal:value></edoal:ValueRestriction>" + + "</edoal:and></edoal:Property>"); + toExport = new PropertyConstruction( Constructor.NOT, Collections.singleton((PathExpression)new PropertyId(new URI("http://mein/super/property")))); + // JE2009-ERROR-HERE + /* + assertEquals( sf.export( toExport), "<edoal:Property><edoal:not>" + + "<edoal:Property rdf:about=\"http://mein/super/property\"/>" + + "</edoal:not><edoal:transf rdf:resource=\"http://mein/transformator\"></edoal:transf></edoal:Property>" ); + */ + // TODO: do the transf!!! + // JE2010: do not know what to do + //, new TransfService(new URI( "http://mein/transformator"), null)); + } + + // ------ + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportRelationCondCond() throws Exception { + RelationRestriction toExport = new RelationDomainRestriction(new ClassId("http://my/super/class")); + assertEquals( render( toExport), "<edoal:DomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:toClass></edoal:DomainRestriction>"); + toExport = new RelationCoDomainRestriction(new ClassId("http://my/super/class")); + assertEquals( render( toExport), "<edoal:CodomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:toClass></edoal:CodomainRestriction>"); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testParseRelationExpr() throws Exception { + + RelationExpression toExport = new RelationId("http://my/super/relation"); + assertEquals( render(toExport), + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>"); + // JE2009: Illegal + /* + toExport = new RelationExpression( + new RelationId("http://my/super/relation"), + Collections + .singleton((RelationExpression) new RelationDomainRestriction(new ClassId("http://my/super/class")))); + assertEquals( sf.export(toExport), + "<edoal:Relation rdf:about=\"http://my/super/relation\">" + + "<edoal:domainRestriction>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:domainRestriction>" + "</edoal:Relation>"); + */ + + + // JE 2010: I could export it as well + RelationExpression relexp = new RelationDomainRestriction( new ClassId("http://my/super/class")); + + final Set<PathExpression> expressions = new LinkedHashSet<PathExpression>(2); + expressions.add(new RelationId("http://my/super/relation0")); + expressions.add(new RelationId("http://my/super/relation1")); + expressions.add( relexp ); + + toExport = new RelationConstruction( Constructor.AND, expressions ); + assertEquals( render( toExport ), + "<edoal:Relation>" + + "<edoal:and rdf:parseType=\"Collection\">" + + "<edoal:Relation rdf:about=\"http://my/super/relation0\"/>" + + "<edoal:Relation rdf:about=\"http://my/super/relation1\"/>" + + "<edoal:DomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:toClass></edoal:DomainRestriction>" + + "</edoal:and>" + "</edoal:Relation>"); + toExport = new RelationConstruction( Constructor.OR, expressions ); + assertEquals( render( toExport ), + "<edoal:Relation>" + + "<edoal:or rdf:parseType=\"Collection\">" + + "<edoal:Relation rdf:about=\"http://my/super/relation0\"/>" + + "<edoal:Relation rdf:about=\"http://my/super/relation1\"/>" + + "<edoal:DomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:toClass></edoal:DomainRestriction>" + + "</edoal:or>" + "</edoal:Relation>"); + + final Set<PathExpression> expressions2 = new LinkedHashSet<PathExpression>(); + expressions2.add(new RelationConstruction(Constructor.NOT,Collections.singleton((PathExpression)new RelationId("http://my/super/relation")))); + expressions2.add(new RelationCoDomainRestriction(new ClassId("http://my/super/class"))); + + toExport = new RelationConstruction( Constructor.AND, expressions2 ); + assertEquals( render( toExport ), + "<edoal:Relation>" + + "<edoal:and rdf:parseType=\"Collection\">" + + "<edoal:Relation><edoal:not>" + + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>" + + "</edoal:not></edoal:Relation>" + + "<edoal:CodomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"http://my/super/class\"/>" + + "</edoal:toClass></edoal:CodomainRestriction>" + + "</edoal:and>" + "</edoal:Relation>"); + toExport = new RelationConstruction( Constructor.INVERSE, Collections.singleton((PathExpression)new RelationId("http://my/super/relation"))); + assertEquals( render( toExport ), + "<edoal:Relation>" + + "<edoal:inverse>" + + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>" + + "</edoal:inverse>" + "</edoal:Relation>"); + toExport = new RelationConstruction(Constructor.SYMMETRIC, Collections.singleton((PathExpression)new RelationId("http://my/super/relation"))); + assertEquals( render( toExport ), + "<edoal:Relation>" + + "<edoal:symmetric>" + + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>" + + "</edoal:symmetric>" + "</edoal:Relation>"); + toExport = new RelationConstruction(Constructor.TRANSITIVE, Collections.singleton((PathExpression)new RelationId("http://my/super/relation"))); + assertEquals( render( toExport ), + "<edoal:Relation>" + + "<edoal:transitive>" + + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>" + + "</edoal:transitive>" + "</edoal:Relation>"); + toExport = new RelationConstruction( Constructor.REFLEXIVE, Collections.singleton((PathExpression)new RelationId("http://my/super/relation"))); + assertEquals( render(toExport), + "<edoal:Relation>" + + "<edoal:reflexive>" + + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>" + + "</edoal:reflexive>" + "</edoal:Relation>" ); + + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testExportCell() throws Exception { + // JE2009: these types of MappingRule do not exist anymore: find the new way to do it. + /* + final MappingRule[] toExport = new MappingRule[] { + new Class2Class(null, Direction.EQUIVALENCE, new ClassId("http://my/super/class0"), + new ClassId("http://my/super/class1"), .5f), + new Relation2Class(null, Direction.MAPPING, new RelationId("http://my/super/attribute"), + new ClassId("http://my/super/instance"), .3f) }; + + final String[] outcome = new String[] { + "<Cell>" + + "<entity1><Class rdf:about=\"http://my/super/class0\"></Class></entity1>" + + "<entity2><Class rdf:about=\"http://my/super/class1\"></Class></entity2>" + + "<measure>0.5</measure>" + + "<relation>ClassEquivalence</relation>" + "</Cell>", + "<Cell>" + + "<entity1><Relation rdf:about=\"http://my/super/attribute\"></Relation></entity1>" + + "<entity2><Class rdf:about=\"http://my/super/instance\"></Class></entity2>" + + "<measure>0.3</measure>" + + "<relation>RelationClassMapping</relation>" + + "</Cell>" }; + + for (int i = 0; i < toExport.length; i++) { + assertEquals(outcome[i], sf.export(toExport[i])); + } + */ + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportAlignment() throws Exception { + + Ontology o1 = new BasicOntology(); + o1.setURI( new URI("http://source") ); + o1.setFormalism( "wsml" ); + o1.setFormURI( new URI("http://wsml") ); + Ontology o2 = new BasicOntology(); + o2.setURI( new URI("http://target") ); + o2.setFormalism( "wsml" ); + o2.setFormURI( new URI("http://wsml") ); + final EDOALAlignment doc = new EDOALAlignment(); + doc.setExtension( Namespace.ALIGNMENT.uri, Annotations.ID, "http://asdf" ); + doc.init( o1, o2 ); + + assertEquals( render( doc ), +"<?xml version='1.0' encoding='utf-8' standalone='no'?><rdf:RDF xmlns='http://knowledgeweb.semanticweb.org/heterogeneity/alignment#'"+ + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"+ + " xmlns:xsd='http://www.w3.org/2001/XMLSchema#'"+ + " xmlns:align='http://knowledgeweb.semanticweb.org/heterogeneity/alignment#'"+ + " xmlns:edoal='http://ns.inria.org/edoal/1.0/#'>"+ + "<Alignment rdf:about=\"http://asdf\"><xml>yes</xml><level>2EDOAL</level><type>**</type><id>http://asdf</id>" + + "<onto1>" + + "<Ontology rdf:about=\"http://source\"><location>http://source</location>" + + "<formalism><Formalism align:name=\"wsml\" align:uri=\"http://wsml\"/></formalism>" + + "</Ontology>" + "</onto1>" + "<onto2>" + + "<Ontology rdf:about=\"http://target\"><location>http://target</location>" + + "<formalism><Formalism align:name=\"wsml\" align:uri=\"http://wsml\"/></formalism>" + + "</Ontology>" + "</onto2>" + + "</Alignment>" +"</rdf:RDF>" ); + doc.setType( "1*" ); + assertEquals( render( doc ), +"<?xml version='1.0' encoding='utf-8' standalone='no'?><rdf:RDF xmlns='http://knowledgeweb.semanticweb.org/heterogeneity/alignment#'"+ + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"+ + " xmlns:xsd='http://www.w3.org/2001/XMLSchema#'"+ + " xmlns:align='http://knowledgeweb.semanticweb.org/heterogeneity/alignment#'"+ + " xmlns:edoal='http://ns.inria.org/edoal/1.0/#'>"+ + "<Alignment rdf:about=\"http://asdf\"><xml>yes</xml><level>2EDOAL</level><type>1*</type><id>http://asdf</id>" + + "<onto1>" + + "<Ontology rdf:about=\"http://source\"><location>http://source</location>" + + "<formalism><Formalism align:name=\"wsml\" align:uri=\"http://wsml\"/></formalism>" + + "</Ontology>" + "</onto1>" + "<onto2>" + + "<Ontology rdf:about=\"http://target\"><location>http://target</location>" + + "<formalism><Formalism align:name=\"wsml\" align:uri=\"http://wsml\"/></formalism>" + + "</Ontology>" + "</onto2>" + + "</Alignment>" +"</rdf:RDF>" ); + } + + // JE2009: Incorrect one + /* + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportRelationExprExotic() throws Exception { + final String reference = "<edoal:Relation>" + + "<edoal:and rdf:parseType=\"Collection\">" + + "<edoal:DomainRestriction><edoal:toClass>" + + "<edoal:Class rdf:about=\"r2\"/>" + "</edoal:toClass></edoal:DomainRestriction>" + + "<edoal:Relation rdf:about=\"c1\"/>" + + "<edoal:DomainRestriction><edoal:toClass>" + "<edoal:Class rdf:about=\"r1\"/>" + + "</edoal:toClass></edoal:DomainRestriction>" + + "<edoal:DomainRestriction><edoal:toClass>" + "<edoal:Class rdf:about=\"r0\"/>" + + "</edoal:toClass></edoal:DomainRestriction>" + "</edoal:and>" + "</edoal:Relation>"; + + RelationId c1 = new RelationId("c1"); + RelationId c2 = new RelationId("c2"); + ComplexExpression combined = new ComplexExpression(Arrays + .asList(new RelationId[] { c1, c2 }), Constructor.AND); + RelationRestriction con0 = new RelationDomainRestriction(new ClassId("r0")), combined); + RelationRestriction con1 = new RelationDomainRestriction(ClassId("r1")), c1); + RelationRestriction con2 = new RelationDomainRestriction(ClassId("r2")), c2); + RelationExpression ce = new RelationConstruction(combined, Arrays + .asList(new RelationRestriction[] { con0, con1, con2 })); + + assertEquals( sf.export(ce), reference ); + } + */ + + // JE2009: Incorrect one + /* + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testExportPropertyExprExotic() throws Exception { + final String reference = "<edoal:Property>" + + "<edoal:and rdf:parseType=\"Collection\">" + + "<edoal:Property rdf:about=\"c2\">" + "<edoal:valueRestriction>" + + "<edoal:value>val2</edoal:value>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + "</edoal:valueRestriction>" + + "</edoal:Property>" + "<edoal:Property rdf:about=\"c1\">" + + "<edoal:valueRestriction>" + "<edoal:value>val1</edoal:value>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + "</edoal:valueRestriction>" + + "</edoal:Property>" + "</edoal:and>" + "<edoal:valueRestriction>" + + "<edoal:value>val0</edoal:value>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + "</edoal:valueRestriction>" + + "</edoal:Property>"; + + PropertyId c1 = new PropertyId("c1"); + PropertyId c2 = new PropertyId("c2"); + ComplexExpression combined = new ComplexExpression(Arrays + .asList(new PropertyId[] { c1, c2 }), Constructor.AND); + PropertyRestriction con0 = new PropertyValueRestriction(new Restriction( + new Value("val0"), Comparator.EQUAL), combined); + PropertyRestriction con1 = new PropertyValueRestriction(new Restriction( + new Value("val1"), Comparator.EQUAL), c1); + PropertyRestriction con2 = new PropertyValueRestriction(new Restriction( + new Value("val2"), Comparator.EQUAL), c2); + PropertyExpression ce = new PropertyConstruction(combined, Arrays + .asList(new PropertyRestriction[] { con0, con1, con2 })); + + assertEquals( sf.export(ce), reference ); + } + */ + + // JE2009: Incorrect one + /* + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testExportClassExprExotic() throws Exception { + final String reference = "<edoal:Class>" + + "<edoal:and rdf:parseType=\"Collection\">" + + "<edoal:Class rdf:about=\"c2\">" + "<edoal:attributeValueCondition>" + + "<edoal:Restriction>" + + "<edoal:onProperty><edoal:Property rdf:about=\"a2\"/></edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + + "<edoal:value>val2</edoal:value>" + "</edoal:Restriction>" + + "</edoal:attributeValueCondition>" + "</edoal:Class>" + + "<edoal:Class rdf:about=\"c1\">" + "<edoal:attributeValueCondition>" + + "<edoal:Restriction>" + + "<edoal:onProperty><edoal:Property rdf:about=\"a1\"/></edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + + "<edoal:value>val1</edoal:value>" + "</edoal:Restriction>" + + "</edoal:attributeValueCondition>" + "</edoal:Class>" + "</edoal:and>" + + "<edoal:attributeValueCondition>" + "<edoal:Restriction>" + + "<edoal:onProperty><edoal:Property rdf:about=\"a0\"/></edoal:onProperty>" + + "<edoal:comparator rdf:resource=\"http://www.w3.org/2001/XMLSchema#equals\"/>" + + "<edoal:value>val0</edoal:value>" + "</edoal:Restriction>" + + "</edoal:attributeValueCondition>" + "</edoal:Class>"; + + ClassId c1 = new ClassId("c1"); + ClassId c2 = new ClassId("c2"); + ComplexExpression combined = new ComplexExpression(Arrays + .asList(new ClassId[] { c1, c2 }), Constructor.AND); + ClassRestriction con0 = new ClassValueRestriction(new PropertyId("a0"), Comparator.EQUAL,new Value("val0")), + combined); + ClassRestriction con1 = new ClassValueRestriction(new PropertyId("a1"), Comparator.EQUAL, new Value("val1")), c1); + ClassRestriction con2 = new ClassValueRestriction(new PropertyId("a2"), Comparator.EQUAL,new Value("val2")), c2); + ClassExpression ce = new ClassExpression(combined, Arrays + .asList(new ClassRestriction[] { con0, con1, con2 })); + + assertEquals(reference, sf.export(ce)); + } + */ + +} diff --git a/test/src/EDOALParserTest.java b/test/src/EDOALParserTest.java new file mode 100644 index 0000000000000000000000000000000000000000..eed65360bb13d5aa71f2aff22ba03cea6b0d6a5f --- /dev/null +++ b/test/src/EDOALParserTest.java @@ -0,0 +1,768 @@ +/* + * $Id$ + * + * Copyright (C) 2006 Digital Enterprise Research Insitute (DERI) Innsbruck + * Sourceforge version 1.2 -- 2006 + * Copyright (C) INRIA, 2009-2010 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Configuration; +import org.testng.annotations.Test; +//import org.testng.annotations.*; + +import org.semanticweb.owl.align.AlignmentVisitor; +import org.semanticweb.owl.align.AlignmentException; +import org.semanticweb.owl.align.Alignment; + +import fr.inrialpes.exmo.align.parser.AlignmentParser; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.FileOutputStream; + +// JE: the old imports + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.net.URI; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import fr.inrialpes.exmo.align.impl.edoal.Path; +import fr.inrialpes.exmo.align.impl.edoal.Value; +import fr.inrialpes.exmo.align.impl.edoal.PropertyId; +import fr.inrialpes.exmo.align.impl.edoal.ClassId; +import fr.inrialpes.exmo.align.impl.edoal.InstanceId; +import fr.inrialpes.exmo.align.impl.edoal.RelationId; + +// JE2009: These tests have been made for the old XPathParser +// which does not exist anymore... hence nothing can work +// Even the document builder is useless + +/** + * These tests corresponds to the tests presented in the examples/omwg directory + */ + +public class EDOALParserTest { + + private static DocumentBuilder BUILDER; + + @Test(groups = { "full", "omwg", "raw" }) + public void setUp() throws Exception { + final DocumentBuilderFactory FAC = DocumentBuilderFactory.newInstance(); + FAC.setValidating(false); + FAC.setNamespaceAware(false); + DocumentBuilder doc = null; + try { + doc = FAC.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + BUILDER = doc; + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) + public void testParsePath() { + // Does not work becaue it is supposed to work with RDF models... + /* + Utility uparser = new Utility(); + assertEquals( uparser.parsePath( "<Property rdf:about='http://my.beauty.url'/>" ), + + final String[] toParse = new String[] { + "<Property rdf:about='http://my.beauty.url'/>", + "<Path rdf:resource='http://URI#empty'/>", + "<Path><first><Property rdf:about='http://my.beauty.url'/></first>" + + "<next><Path><first><Property rdf:about='http://my.nasty.url'/></first>" + + "<next><Path><first><Property rdf:about='http://my.richi.url'/></first>" + + "<next><Property rdf:about='http://my.final.url'/></next>" + + "</Path></next></Path></next></Path>" }; + final Path[] outcome = new Path[] { + new Path( new PropertyId( "http://my.beauty.url" ) ), + Path.EMPTY, + new Path(new PropertyId( "http://my.beauty.url"), "http://my.nasty.url", + "http://my.richi.url", "http://my.final.url") }; + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parsePath", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + */ + } + + /* + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseOntology() { + final String[] toParse = new String[] { + "<onto1><formalism name='wsml' uri='http://path.to.the/spec'/>" + + "<uri>http://path.to.the/source</uri></onto1>", + "<onto2><formalism name='owl' uri='http://path.to.the/owl'/>" + + "<uri>http://path.to.the/target</uri></onto2>" }; + final OntologyId[] outcome = new OntologyId[] { + new OntologyId(new URI("http://path.to.the/source"), "wsml", + new URI("http://path.to.the/spec")), + new OntologyId(new URI("http://path.to.the/target"), "owl", + new URI("http://path.to.the/owl")) }; + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseOntology", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseInstanceExpression() { + final String[] toParse = new String[] { "<Instance rdf:about='http://meine.tolle/instance#blah'/>" }; + final InstanceExpr[] outcome = new InstanceExpr[] { new InstanceExpr( + new InstanceId("http://meine.tolle/instance#blah")) }; + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseInstanceExpr", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseRestriction() { + final String[] toParse = new String[] { + "<Restriction><onProperty>" + + "<Property rdf:about='http://my.sister#age'/></onProperty>" + + "<comparator rdf:resource='greater-than'/>" + + "<value>18</value></Restriction>", + "<Restriction><onProperty>\n\t<Path>" + + "\n\t<first><Property rdf:about='hasTerroir'/></first>" + + "<next><Property rdf:about='LocatedIn'/></next>" + + "</Path>\n\t</onProperty>" + + "<comparator rdf:resource='equal'/>" + + "<value>Europe</value></Restriction>" }; + final RestrictionConst[] outcome = new RestrictionConst[] { + new RestrictionConst(new Path("http://my.sister#age"), + new Restriction(new SimpleValue("18"), + Comparator.GREATER)), + new RestrictionConst(new Path("hasTerroir", "LocatedIn"), + new Restriction(new SimpleValue("Europe"), + Comparator.EQUAL)) }; + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseRestriction", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseClassCond() { + final String[] toParse = new String[] { + "<attributeValueCondition>" + + "<Restriction>" + + "<onProperty><Property rdf:about='http://my.sister#age'/></onProperty>" + + "<comparator ref:resource='greater-than'/>" + + "<value>18</value>" + "</Restriction>" + + "</attributeValueCondition>", + "<attributeTypeCondition>" + + "<Restriction>" + + "<onProperty><Property rdf:about='http://my.sister#age'/></onProperty>" + + "<comparator ref:resource='equal'/>" + + "<value>18</value>" + "</Restriction>" + + "</attributeTypeCondition>", + "<attributeOccurenceCondition>" + + "<Restriction>" + + "<onProperty><Property rdf:about='http://my.sister#age'/></onProperty>" + + "<comparator ref:resource='greater-than'/>" + + "<value>18</value>" + "</Restriction>" + + "</attributeOccurenceCondition>" }; + final Set[] outcome = new Set[] { + Collections.singleton(new PropertyValueCondition(new Path( + "http://my.sister#age"), new Restriction( + new SimpleValue("18"), Comparator.GREATER))), + Collections.singleton(new PropertyTypeCondition(new Path( + "http://my.sister#age"), new Restriction( + new SimpleValue("18"), Comparator.EQUAL))), + Collections.singleton(new PropertyOccurenceCondition(new Path( + "http://my.sister#age"), new Restriction( + new SimpleValue("18"), Comparator.GREATER))) }; + + try { + runParseWithNodelist(toParse, outcome, XpathParser.class + .getDeclaredMethod("parseClassCond", NodeList.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseClassExpr() { + final String[] toParse = new String[] { + "<Class>" + "<or parse:Type='Collection'>" + + "<Class rdf:about='Acidite'/>" + + "<Class rdf:about='Amertume'/>" + + "<Class rdf:about='Astreinngence'/>" + "</or>" + + "</Class>", + "<Class>" + "<and parse:Type='Collection'>" + + "<Class rdf:about='Acidite'/>" + + "<Class rdf:about='Amertume'/>" + + "<Class rdf:about='Astreinngence'/>" + "</and>" + + "</Class>", + "<Class>" + "<not parse:Type='Collection'>" + + "<Class rdf:about='Acidite'/>" + "</not>" + + "</Class>", + "<Class>" + "<or parse:Type='Collection'>" + + "<Class rdf:about='Acidite'/>" + + "<Class rdf:about='Amertume'/>" + + "<Class rdf:about='Astreinngence'/>" + "</or>" + + "<attributeValueCondition>" + "<Restriction>" + + "<onProperty>" + + "<Property rdf:about='http://vinum#age'/>" + + "</onProperty>" + + "<comparator ref:resource='greater-than'/>" + + "<value>20</value>" + "</Restriction>" + + "</attributeValueCondition>" + "</Class>", + "<Class rdf:about='Amertume'>" + "<attributeValueCondition>" + + "<Restriction>" + "<onProperty>" + + "<Property rdf:about='http://vinum#age'/>" + + "</onProperty>" + + "<comparator ref:resource='greater-than'/>" + + "<value>20</value>" + "</Restriction>" + + "</attributeValueCondition>" + "</Class>", + "<Class rdf:about='Amertume'></Class>" }; + final Set<ExpressionDefinition> expressions = new HashSet<ExpressionDefinition>( + 3); + expressions.add(new ClassId("Acidite")); + expressions.add(new ClassId("Amertume")); + expressions.add(new ClassId("Astreinngence")); + + final ExpressionDefinition ref = new ComplexExpression(expressions, + Operator.OR); + + final Set<ClassCondition> conditions0 = Collections + .singleton((ClassCondition) new PropertyValueCondition( + new Path("http://vinum#age"), new Restriction( + new SimpleValue("20"), Comparator.GREATER), ref)); + final Set<ClassCondition> conditions1 = Collections + .singleton((ClassCondition) new PropertyValueCondition( + new Path("http://vinum#age"), new Restriction( + new SimpleValue("20"), Comparator.GREATER), + new ClassId("Amertume"))); + + final ClassExpr[] outcome = new ClassExpr[] { + new ClassExpr(expressions, Operator.OR, Collections.EMPTY_SET), + new ClassExpr(expressions, Operator.AND, Collections.EMPTY_SET), + new ClassExpr( + Collections + .singleton((ExpressionDefinition) new ClassId( + "Acidite")), Operator.NOT, + Collections.EMPTY_SET), + new ClassExpr(expressions, Operator.OR, conditions0), + new ClassExpr(new ClassId("Amertume"), conditions1), + new ClassExpr(new ClassId("Amertume"), null) }; + + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseClassExpr", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParsePropertyCond() { + final String[] toParse = new String[] { + "<valueCondition>" + "<value>18</value>" + + "<comparator rdf:resource='equal'/>" + + "</valueCondition>", + "<domainRestriction>" + + "<Class rdf:about='http://meine/tolle/restriction'></Class>" + + "</domainRestriction>", + "<typeCondition>int</typeCondition>" }; + final Object[] outcome = new Object[] { + Collections.singleton(new ValueCondition(new Restriction( + new SimpleValue("18"), Comparator.EQUAL))), + Collections.singleton(new DomainPropertyCondition( + new Restriction(new ClassId( + "http://meine/tolle/restriction"), + Comparator.EQUAL))), + Collections.singleton(new TypeCondition(new Restriction( + new SimpleValue("int"), Comparator.EQUAL))) }; + try { + runParseWithNodelist(toParse, outcome, XpathParser.class + .getDeclaredMethod("parsePropertyCond", NodeList.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParsePropertyExpr() { + final String[] toParse = new String[] { + "<Property rdf:about='http://mein/super/property'></Property>", + "<Property><and rdf:parseType='Collection'>" + + "<Property rdf:about='http://mein/super/property0'></Property>" + + "<Property rdf:about='http://mein/super/property1'></Property>" + + "</and></Property>", + "<Property rdf:about='http://mein/super/property'>" + + "<domainRestriction>" + + "<Class rdf:about='http://my/super/class'></Class>" + + "</domainRestriction>" + "</Property>", + "<Property><or rdf:parseType='Collection'>" + + "<Property rdf:about='http://mein/super/property0'></Property>" + + "<Property rdf:about='http://mein/super/property1'></Property>" + + "</or>" + + "<valueCondition><value>5</value>" + + "<comparator rdf:resource='equals'/></valueCondition>" + + "</Property>", + "<Property><not>" + + "<Property rdf:about='http://mein/super/property'></Property>" + + "</not><transf rdf:resource='http://mein/transformator'></transf></Property>" }; + + final Set<ExpressionDefinition> expressions = new HashSet<ExpressionDefinition>( + 2); + expressions.add(new PropertyId("http://mein/super/property0")); + expressions.add(new PropertyId("http://mein/super/property1")); + final PropertyId single = new PropertyId("http://mein/super/property"); + + // TODO: do the transf!!! + final Object[] outcome = new Object[] { + new PropertyExpr(single, null), + new PropertyExpr(expressions, Operator.AND, null), + new PropertyExpr( + single, + Collections + .singleton((PropertyCondition) new DomainPropertyCondition( + new Restriction(new ClassId( + "http://my/super/class"), + Comparator.EQUAL), single))), + new PropertyExpr( + expressions, + Operator.OR, + Collections + .singleton((PropertyCondition) new ValueCondition( + new Restriction(new SimpleValue("5"), + Comparator.EQUAL), + new ComplexExpression(expressions, + Operator.OR)))), + new PropertyExpr(Collections + .singleton((ExpressionDefinition) new PropertyId( + "http://mein/super/property")), Operator.NOT, + null) }; + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parsePropertyExpr", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseRelationCond() { + final String[] toParse = new String[] { + "<domainRestriction>" + + "<Class rdf:about='http://my/super/class'></Class>" + + "</domainRestriction>", + "<codomainRestriction>" + + "<Class rdf:about='http://my/super/class'></Class>" + + "</codomainRestriction>" }; + final Set[] outcome = new Set[] { + Collections.singleton(new DomainRelationCondition( + new Restriction(new ClassId("http://my/super/class"), + Comparator.EQUAL))), + Collections.singleton(new CoDomainRelationCondition( + new Restriction(new ClassId("http://my/super/class"), + Comparator.EQUAL))) }; + + try { + runParseWithNodelist(toParse, outcome, XpathParser.class + .getDeclaredMethod("parseRelationCond", NodeList.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseRelationExpr() { + final String[] toParse = new String[] { + "<Relation rdf:about='http://my/super/relation'></Relation>", + "<Relation rdf:about='http://my/super/relation'>" + + "<domainRestriction>" + + "<Class rdf:about='http://my/super/class'></Class>" + + "</domainRestriction>" + "</Relation>", + "<Relation>" + + "<and rdf:parseType='Collection'>" + + "<Relation rdf:about='http://my/super/relation0'></Relation>" + + "<Relation rdf:about='http://my/super/relation1'></Relation>" + + "</and>" + "<domainRestriction>" + + "<Class rdf:about='http://my/super/class'></Class>" + + "</domainRestriction>" + "</Relation>", + "<Relation>" + + "<or rdf:parseType='Collection'>" + + "<Relation rdf:about='http://my/super/relation0'></Relation>" + + "<Relation rdf:about='http://my/super/relation1'></Relation>" + + "</or>" + "</Relation>", + "<Relation>" + + "<not>" + + "<Relation rdf:about='http://my/super/relation'></Relation>" + + "</not>" + "<codomainRestriction>" + + "<Class rdf:about='http://my/super/class'></Class>" + + "</codomainRestriction>" + "</Relation>", + "<Relation>" + + "<inverse>" + + "<Relation rdf:about='http://my/super/relation'></Relation>" + + "</inverse>" + "</Relation>", + "<Relation>" + + "<symmetric>" + + "<Relation rdf:about='http://my/super/relation'></Relation>" + + "</symmetric>" + "</Relation>", + "<Relation>" + + "<transitive>" + + "<Relation rdf:about='http://my/super/relation'></Relation>" + + "</transitive>" + "</Relation>", + "<Relation>" + + "<reflexive>" + + "<Relation rdf:about='http://my/super/relation'></Relation>" + + "</reflexive>" + "</Relation>" }; + + final Set<RelationId> expressions = new HashSet<RelationId>(2); + expressions.add(new RelationId("http://my/super/relation0")); + expressions.add(new RelationId("http://my/super/relation1")); + + final RelationExpr[] outcome = new RelationExpr[] { + new RelationExpr(new RelationId("http://my/super/relation"), + null), + new RelationExpr( + new RelationId("http://my/super/relation"), + Collections + .singleton((RelationCondition) new DomainRelationCondition( + new Restriction(new ClassId( + "http://my/super/class"), + Comparator.EQUAL), + new RelationId( + "http://my/super/relation")))), + new RelationExpr( + expressions, + Operator.AND, + Collections + .singleton((RelationCondition) new DomainRelationCondition( + new Restriction(new ClassId( + "http://my/super/class"), + Comparator.EQUAL), + new ComplexExpression(expressions, + Operator.AND)))), + new RelationExpr(expressions, Operator.OR, null), + new RelationExpr( + Collections.singleton(new RelationId( + "http://my/super/relation")), + Operator.NOT, + Collections + .singleton((RelationCondition) new CoDomainRelationCondition( + new Restriction(new ClassId( + "http://my/super/class"), + Comparator.EQUAL), + new ComplexExpression( + Collections + .singleton(new RelationId( + "http://my/super/relation")), + Operator.NOT)))), + new RelationExpr(Collections.singleton(new RelationId( + "http://my/super/relation")), Operator.INVERSE, null), + new RelationExpr(Collections.singleton(new RelationId( + "http://my/super/relation")), Operator.SYMMETRIC, null), + new RelationExpr(Collections.singleton(new RelationId( + "http://my/super/relation")), Operator.TRANSITIVE, null), + new RelationExpr(Collections.singleton(new RelationId( + "http://my/super/relation")), Operator.REFLEXIVE, null) }; + + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseRelationExpr", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseCell() { + final String[] toParse = new String[] { + + "<Cell>" + + "<entity1><Class rdf:about='http://my/super/class0'></Class></entity1>" + + "<entity2><Class rdf:about='http://my/super/class1'></Class></entity2>" + + "<measure>0.5</measure>" + + "<relation>ClassEquivalence</relation>" + "</Cell>", + "<Cell>" + + "<entity1><Relation rdf:about='http://my/super/attribute'></Relation></entity1>" + + "<entity2><Class rdf:about='http://my/super/instance'></Class></entity2>" + + "<measure>0.3</measure>" + + "<relation>RelationClassMapping</relation>" + + "</Cell>" }; + final Object[] outcome = new Object[] { + new Class2Class(null, Direction.EQUIVALENCE, new ClassExpr( + new ClassId("http://my/super/class0"), null), + new ClassExpr(new ClassId("http://my/super/class1"), + null), .5f), + new Relation2Class(null, Direction.MAPPING, new RelationExpr( + new RelationId("http://my/super/attribute"), null), + new ClassExpr(new ClassId("http://my/super/instance"), + null), .3f) }; + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseCell", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParseAlignment() { + final String[] toParse = new String[] { + "<Alignment>" + "<dc:identifier rdf:resource='http://asdf'/>" + + "<onto1>" + + "<formalism name='wsml' uri='http://wsml'/>" + + "<uri>http://source</uri>" + "</onto1>" + "<onto2>" + + "<formalism name='wsml' uri='http://wsml'/>" + + "<uri>http://target</uri>" + "</onto2>" + + "</Alignment>", + "<Alignment>" + "<dc:identifier rdf:resource='http://asdf'/>" + + "<onto1>" + + "<formalism name='wsml' uri='http://wsml'/>" + + "<uri>http://source</uri>" + "</onto1>" + "<onto2>" + + "<formalism name='wsml' uri='http://wsml'/>" + + "<uri>http://target</uri>" + "</onto2>" + + "<level>2oml</level>" + "<type>**</type>" + + "</Alignment>" }; + + final MappingDocument doc0 = new MappingDocument( + new URI("http://asdf"), new OntologyId( + new URI("http://source"), "wsml", + new URI("http://wsml")), new OntologyId(new URI( + "http://target"), "wsml", new URI("http://wsml"))); + final MappingDocument doc1 = new MappingDocument( + new URI("http://asdf"), new OntologyId( + new URI("http://source"), "wsml", + new URI("http://wsml")), new OntologyId(new URI( + "http://target"), "wsml", new URI("http://wsml"))); + doc1.setLevel(Level.LEVEL2OML); + doc1.setArity(Arity.NONE_NONE); + + final Object[] outcome = new Object[] { doc0, doc1 }; + + try { + runParse(toParse, outcome, XpathParser.class.getDeclaredMethod( + "parseAlignment", Node.class)); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testParsePov() { + // parse path + try { + String toParse = "<parent><Property rdf:about='http://my/super/uri'/></parent>"; + Document doc = BUILDER.parse(new ByteArrayInputStream(toParse + .getBytes())); + assertEquals("Couldn't parse a path pov", new Path( + "http://my/super/uri"), XpathParser.parsePov(doc + .getFirstChild())); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + // parse uri + try { + String toParse = "<parent>http://my/super/pov</parent>"; + Document doc = BUILDER.parse(new ByteArrayInputStream(toParse + .getBytes())); + assertEquals("Couldn't parse a path pov", new URI( + "http://my/super/pov"), XpathParser.parsePov(doc + .getFirstChild())); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + // parse value + try { + String toParse = "<parent>5</parent>"; + Document doc = BUILDER.parse(new ByteArrayInputStream(toParse + .getBytes())); + assertEquals("Couldn't parse a path pov", new SimpleValue("5"), + XpathParser.parsePov(doc.getFirstChild())); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public void testExotic() { + final String toParse = "<Class>" + "<and rdf:parseType='Collection'>" + + "<Class rdf:about='c1'>" + "<attributeValueCondition>" + + "<Restriction>" + "<onAttribute>" + + "<Attribute rdf:about='a1'/>" + "</onAttribute>" + + "<comparator rdf:resource='equal'/>" + "<value>val1</value>" + + "</Restriction>" + "</attributeValueCondition>" + "</Class>" + + "<Class rdf:about='c2'>" + "<attributeValueCondition>" + + "<Restriction><onAttribute>" + "<Attribute rdf:about='a2'/>" + + "</onAttribute>" + "<comparator rdf:resource='equal'/>" + + "<value>val2</value>" + "</Restriction>" + + "</attributeValueCondition>" + "</Class>" + "</and>" + + "<attributeValueCondition>" + "<Restriction><onAttribute>" + + "<Attribute rdf:about='a0'/>" + "</onAttribute>" + + "<comparator rdf:resource='equal'/>" + "<value>val0</value>" + + "</Restriction>" + "</attributeValueCondition>" + "</Class>"; + + ClassId c1 = new ClassId("c1"); + ClassId c2 = new ClassId("c2"); + ComplexExpression combined = new ComplexExpression(Arrays + .asList(new ClassId[] { c1, c2 }), Operator.AND); + ClassCondition con0 = new AttributeValueCondition(new Path("a0"), + new Restriction(new SimpleValue("val0"), Comparator.EQUAL), + combined); + ClassCondition con1 = new AttributeValueCondition(new Path("a1"), + new Restriction(new SimpleValue("val1"), Comparator.EQUAL), c1); + ClassCondition con2 = new AttributeValueCondition(new Path("a2"), + new Restriction(new SimpleValue("val2"), Comparator.EQUAL), c2); + ClassExpr ce = new ClassExpr(combined, Arrays + .asList(new ClassCondition[] { con0, con1, con2 })); + try { + ByteArrayInputStream is = new ByteArrayInputStream(toParse + .getBytes()); + Document d = BUILDER.parse(is); + assertEquals(ce, XpathParser.parseClassExpr(d.getFirstChild())); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public static void runParse(final String[] toParse, final Object[] outcome, + final Method method) { + int counter = 0; + for (final String xml : toParse) { + final ByteArrayInputStream is = new ByteArrayInputStream(xml + .getBytes()); + try { + final Document doc = BUILDER.parse(is); + final Object o = method.invoke(null, doc.getFirstChild()); + assertEquals("Couldn't parse: " + xml, outcome[counter++], o); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + @Test(groups = { "full", "omwg" }, dependsOnMethods = {"setUp"}) + public static void runParseWithNodelist(final String[] toParse, + final Object[] outcome, final Method method) { + int counter = 0; + for (final String xml : toParse) { + final ByteArrayInputStream is = new ByteArrayInputStream(xml + .getBytes()); + try { + final Document doc = BUILDER.parse(is); + final Object o = method.invoke(null, doc.getChildNodes()); + assertEquals("Couldn't parse: " + xml, outcome[counter++], o); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +*/ + +} diff --git a/test/src/EDOALTest.java b/test/src/EDOALTest.java new file mode 100644 index 0000000000000000000000000000000000000000..edbb9595f3ff8e9fd3a31a1a2923fe89d058e727 --- /dev/null +++ b/test/src/EDOALTest.java @@ -0,0 +1,136 @@ +/* + * $Id$ + * + * Copyright (C) INRIA, 2008-2010 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Configuration; +import org.testng.annotations.Test; +//import org.testng.annotations.*; + +import org.semanticweb.owl.align.AlignmentVisitor; +import org.semanticweb.owl.align.AlignmentException; +import org.semanticweb.owl.align.Alignment; + +import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; +import fr.inrialpes.exmo.align.parser.AlignmentParser; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.FileOutputStream; + + +/* + * + * JE: 2010 + * + * THIS SHOULD TEST ALL ERRORS RAISED BY CONSTRUCTORS + * + * + * + * + */ + +/** + * These tests corresponds to the tests presented in the examples/omwg directory + */ + +public class EDOALTest { + + private Alignment alignment = null; + private AlignmentParser aparser1 = null; + + @Test(groups = { "full", "omwg", "raw" }) + public void loadPrintTest() throws Exception { + /* +java -cp ../../lib/procalign.jar fr.inrialpes.exmo.align.util.ParserPrinter wine.xml > wine2.xml + */ + aparser1 = new AlignmentParser( 0 ); + assertNotNull( aparser1 ); + alignment = aparser1.parse( "examples/omwg/wine.xml" ); + assertNotNull( alignment ); + //assertTrue( alignment instanceof EDOALAlignment ); + FileOutputStream stream = new FileOutputStream("test/output/wine2.xml"); + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new RDFRendererVisitor( writer ); + alignment.render( renderer ); + writer.flush(); + writer.close(); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"loadPrintTest"}) + public void roundTripTest() throws Exception { + /* +java -cp ../../lib/procalign.jar fr.inrialpes.exmo.align.util.ParserPrinter wine2.xml > wine3.xml + */ + aparser1.initAlignment( null ); + alignment = aparser1.parse( "test/output/wine2.xml" ); + assertNotNull( alignment ); + FileOutputStream stream = new FileOutputStream("test/output/wine3.xml"); + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new RDFRendererVisitor( writer ); + alignment.render( renderer ); + writer.flush(); + writer.close(); + } + + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"roundTripTest"}) + public void diffTest() throws Exception { + /* +diff wine2.xml wine3.xml + */ + aparser1.initAlignment( null ); + Alignment oldal = aparser1.parse( "test/output/wine2.xml" ); + aparser1.initAlignment( null ); + alignment = aparser1.parse( "test/output/wine3.xml" ); + assertNotNull( alignment ); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new RDFRendererVisitor( writer ); + oldal.render( renderer ); + writer.flush(); + writer.close(); + String wine2 = stream.toString(); + stream = new ByteArrayOutputStream(); + writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + renderer = new RDFRendererVisitor( writer ); + alignment.render( renderer ); + writer.flush(); + writer.close(); + assertEquals( "".equals( wine2 ), false ); + // Provisory results, EDOAL is not well pretty printed + // AND THE RESULT GIVES DIFFERENT VALUES !!! + assertEquals( wine2.length(), 4343 ); + // This does not work because (at least) the order of correspondences is never the same... + //assertEquals( wine2, stream.toString() ); + } +}