diff --git a/README.TXT b/README.TXT index 90deaa814471e970a7674031d4ca112cac7a5e36..64ec18cb558832f14f31dab1273051ba26e63d53 100644 --- a/README.TXT +++ b/README.TXT @@ -1,10 +1,10 @@ ####################################################################### # Ontology alignment API and implementation # -# 06/12/2014, version 4.7 # +# 06/12/2015, version 4.8 # # http://alignapi.gforge.inria.fr # ####################################################################### -Copyright (C) 2003-2014 INRIA. +Copyright (C) 2003-2015 INRIA. Copyright (C) 2004-2005 Universit� de Montr�al. Copyright (C) 2005 CNR Pisa. Copyright (C) 2005 Konstantinos A. Nedas. diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java index 37928015875a36a71a02f8334ef0d8083d780bde..ad2bcf230aa8946c6542779d118148a5b130c26c 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java @@ -102,6 +102,7 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito private int numberNs = 0; private int fileIndex = 1; private String sub = ""; + private boolean rel = false; protected String obj = ""; private String strBGP = ""; protected List<String> listBGP = null; @@ -141,33 +142,19 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito } protected void resetVariables(Expression expr, String s, String o) throws AlignmentException { - if (expr instanceof ClassExpression) { - resetVariables((ClassExpression) expr, s, o); - } else if (expr instanceof PathExpression) { - resetVariables((PathExpression) expr, s, o); - } else if (expr instanceof InstanceExpression) { - resetVariables((InstanceExpression) expr, s, o); + initStructure(); + if ( expr instanceof ClassExpression ) { + resetVariables("?" + s, createVarName()); + } else if ( expr instanceof PathExpression ) { + resetVariables("?" + s, "?" + o); + } else if ( expr instanceof InstanceExpression ) { + resetVariables("?" + s, createVarName()); } else { throw new AlignmentException("Cannot render as query : " + expr); } } - protected void resetVariables(ClassExpression expr, String s, String o) { - initStructure(); - resetVariables("?" + s, createVarName()); - } - - protected void resetVariables(PathExpression expr, String s, String o) { - initStructure(); - resetVariables("?" + s, "?" + o); - } - - protected void resetVariables(InstanceExpression expr, String s, String o) { - initStructure(); - resetVariables("?" + s, createVarName()); - } - - protected void resetVariables(String s, String o) { + protected void resetVariables( String s, String o ) { sub = s; obj = o; } @@ -178,7 +165,7 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito * It may be necessary to generate blanks. */ protected String createVarName() { - if (blanks) { + if ( blanks ) { obj = "_:o" + ++varsIndexcount; } else { obj = "?o" + ++varsIndexcount; @@ -186,6 +173,14 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito return obj; } + protected String createPropertyVarName() { + if ( blanks ) { + return "_:o" + ++varsIndexcount; + } else { + return "?o" + ++varsIndexcount; + } + } + protected void addToGP(String str) { strBGP += str; } @@ -476,14 +471,14 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito inClassRestriction = false; } - public void visit(final PropertyId e) throws AlignmentException { - if (e.getURI() != null) { - String id = registerPrefix(e.getURI()); + public void visit( final PropertyId e ) throws AlignmentException { + if ( e.getURI() != null ) { + String id = registerPrefix( e.getURI() ); String temp = obj; - if (valueRestriction != null && !inClassRestriction && op != Constructor.COMP && flagRestriction) { + if ( valueRestriction != null && !inClassRestriction && op != Constructor.COMP && flagRestriction ) { obj = "\"" + valueRestriction.toString() + "\""; } - if (flagRestriction && inClassRestriction) { + if ( flagRestriction && inClassRestriction ) { objectsRestriction.add(obj); } // createVarName(); //JE2014! @@ -492,14 +487,15 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito } } - public void visit(final PropertyConstruction e) throws AlignmentException { + public void visit( final PropertyConstruction e ) throws AlignmentException { op = e.getOperator(); + //System.err.println( "P: < "+sub+", "+op+", "+obj+" >" ); if (op == Constructor.OR) { - if (valueRestriction != null && !inClassRestriction) { + if ( valueRestriction != null && !inClassRestriction ) { obj = "\"" + valueRestriction.toString() + "\""; } int size = e.getComponents().size(); - for (final PathExpression re : e.getComponents()) { + for ( final PathExpression re : e.getComponents() ) { strBGP += "{" + NL; re.accept(this); size--; @@ -509,19 +505,37 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito strBGP += "}" + NL; } } - objectsRestriction.add(obj); + objectsRestriction.add( obj ); + } else if (op == Constructor.AND) { + if ( valueRestriction != null && !inClassRestriction ) { + obj = "\"" + valueRestriction.toString() + "\""; + } + String temp = obj; + rel = false; + List<PathExpression> codomaintotreat = new ArrayList<PathExpression>(); + for ( final PathExpression re : e.getComponents() ) { + if ( re instanceof PropertyTypeRestriction || re instanceof PropertyValueRestriction ) { + codomaintotreat.add( re ); + } else { + re.accept(this); + obj = temp; + rel = true; + } + } + for ( final PathExpression re : codomaintotreat ) { + re.accept(this); + obj = temp; + } + objectsRestriction.add( obj ); } else if (op == Constructor.NOT) { strBGP += "FILTER (NOT EXISTS {" + NL; for (final PathExpression re : e.getComponents()) { re.accept(this); } strBGP += "})" + NL; - } else if (op == Constructor.COMP) { + } else if ( op == Constructor.COMP ) { int size = e.getComponents().size(); String tempSub = sub; - //if ( blanks && ( this.getClass() == SPARQLConstructRendererVisitor.class || this.getClass() == SPARQLLinkkerRendererVisitor.class ) ) { - // obj = "_:o" + ++count; - //} String tempObj = obj; for (final PathExpression re : e.getComponents()) { size--; @@ -545,6 +559,7 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito obj = tempObj; sub = tempSub; } else { + // JE2015: Unclear here! if (valueRestriction != null && !inClassRestriction) { obj = "\"" + valueRestriction.toString() + "\""; } @@ -568,6 +583,10 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito public void visit( final PropertyValueRestriction c ) throws AlignmentException { String str = ""; + if ( !rel ) { + strBGP += sub+" "+createPropertyVarName()+" "+obj+" ." + NL; + rel = true; // only one is necessary + } String val = valueToCompare( c.getValue(), obj ); /* if (c.getComparator().getURI().equals(Comparator.EQUAL.getURI())) { @@ -594,17 +613,21 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito public void visit(final PropertyTypeRestriction c) throws AlignmentException { String str = ""; - if (!objectsRestriction.isEmpty()) { + if ( !rel ) { + strBGP += sub+" "+createPropertyVarName()+" "+obj+" ." + NL; + rel = true; // only one is necessary + } + if ( !objectsRestriction.isEmpty() ) { Iterator<String> listObj = objectsRestriction.iterator(); int size = objectsRestriction.size(); if (size > 0) { str = "FILTER (datatype(" + listObj.next() + ") = "; - visit(c.getType()); + visit( c.getType() ); str += "xsd:" + datatype; } - while (listObj.hasNext()) { + while ( listObj.hasNext() ) { str += " && datatype(" + listObj.next() + ") = "; - visit(c.getType()); + visit( c.getType() ); str += "xsd:" + datatype; } str += ")" + NL; @@ -630,8 +653,9 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito } } - public void visit(final RelationConstruction e) throws AlignmentException { + public void visit( final RelationConstruction e ) throws AlignmentException { op = e.getOperator(); + //System.err.println( "R: < "+sub+", "+op+", "+obj+" >" ); if (op == Constructor.OR) { int size = e.getComponents().size(); if (valueRestriction != null && !inClassRestriction) { @@ -650,29 +674,38 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito } } objectsRestriction.add(obj); - } else if (op == Constructor.AND) { + } else if ( op == Constructor.AND ) { if (valueRestriction != null && !inClassRestriction) { obj = valueRestriction.toString(); } String temp = obj; - for (final PathExpression re : e.getComponents()) { - re.accept(this); - obj = temp; + rel = false; + List<PathExpression> codomaintotreat = new ArrayList<PathExpression>(); + for ( final PathExpression re : e.getComponents() ) { + if ( re instanceof RelationCoDomainRestriction ) { + codomaintotreat.add( re ); + } else { + // This should be ordered... + re.accept(this); + obj = temp; + rel = true; + } } - objectsRestriction.add(obj); + for ( final PathExpression re : codomaintotreat ) { + re.accept(this); + obj = temp; + } + objectsRestriction.add( obj ); } else if (op == Constructor.NOT) { strBGP += "FILTER (NOT EXISTS {" + NL; for (final PathExpression re : e.getComponents()) { re.accept(this); } strBGP += "})" + NL; - } else if (op == Constructor.COMP) { + } else if ( op == Constructor.COMP ) { int size = e.getComponents().size(); String temp = sub; - //if ( blanks && (this.getClass() == SPARQLConstructRendererVisitor.class || this.getClass() == SPARQLLinkkerRendererVisitor.class ) ) { - //obj = "_:o" + ++count; createVarName(); - //} for (final PathExpression re : e.getComponents()) { re.accept(this); size--; @@ -729,7 +762,7 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito if (valueRestriction != null && !inClassRestriction) { obj = valueRestriction.toString(); } - for (final PathExpression re : e.getComponents()) { + for ( final PathExpression re : e.getComponents() ) { re.accept(this); objectsRestriction.add(obj); if (weakens && !strBGP.equals("") && !inClassRestriction) { @@ -741,8 +774,12 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito //obj = "?o" + ++count; } - public void visit(final RelationCoDomainRestriction c) throws AlignmentException { + public void visit( final RelationCoDomainRestriction c ) throws AlignmentException { String stemp = sub; + if ( !rel ) { + strBGP += sub+" "+createPropertyVarName()+" "+obj+" ." + NL; + rel = true; + } sub = obj; flagRestriction = true; c.getCoDomain().accept(this); diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java index b635629879e35a8bd6505596f5df336d3707a935..91d859b9c3b20434cf13cb7d22c7741b1e28bef2 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java @@ -171,48 +171,46 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor throw new AlignmentException("NOT IMPLEMENTED !"); } - protected void generateConstruct(Cell cell, Expression expr1, Expression expr2) throws AlignmentException { + protected void generateConstruct( Cell cell, Expression expr1, Expression expr2 ) throws AlignmentException { // Here the generation is dependent on global variables blanks = true; - resetVariables(expr1, "s", "o"); - expr1.accept(this); - String GP1 = getGP(); - List<String> listGP1 = new ArrayList<String>(getBGP()); - blanks = requestedblanks; - resetVariables(expr2, "s", "o"); - expr2.accept(this); + resetVariables( expr2, "s", "o" ); + expr2.accept( this ); String GP2 = getGP(); + List<String> listGP2 = new ArrayList<String>( getBGP() ); + blanks = requestedblanks; // ?? + resetVariables( expr1, "s", "o" ); + expr1.accept( this ); + String GP1 = getGP(); // End of global variables String query = ""; - if (!GP1.contains("UNION") && !GP1.contains("FILTER")) { - query = createConstruct(GP1, GP2); - if (corese) { - content_Corese += createCoreseQuery(query); + if ( !GP2.contains("UNION") && !GP2.contains("FILTER") ) { + query = createConstruct( GP1, GP2 ); + if ( corese ) { + content_Corese += createCoreseQuery( query ); } - } else if (weakens) { + } else if ( weakens ) { String tmp = ""; - for (String str : listGP1) { - if (!str.contains("UNION") && !str.contains("FILTER")) { + for( String str : listGP2 ) { + if ( !str.contains("UNION") && !str.contains("FILTER") ) { tmp += str; } } - if (!tmp.equals("")) { - query = createConstruct(tmp, GP2); + if ( !tmp.equals("") ) { + query = createConstruct( tmp, GP1 ); } - } else if (ignoreerrors) { - query = createConstruct(GP1, GP2); - } - if (corese) { - return; + } else if ( ignoreerrors ) { + query = createConstruct( GP1, GP2 ); } - saveQuery(cell, query); + if ( corese ) return; + saveQuery( cell, query ); } - protected String createConstruct(String GP1, String GP2) { + protected String createConstruct( String GP1, String GP2 ) { if (namedGraph == null) { - return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + NL + GP2 + "}" + NL; + return createPrefixList() + "CONSTRUCT {" + NL + GP2 + "}" + NL + "WHERE {" + NL + GP1 + "}" + NL; } else { - return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + NL + "GRAPH <" + namedGraph + "> {"+ NL + GP2 + "}" + NL + "}" + NL; + return createPrefixList() + "CONSTRUCT {" + NL + GP2 + "}" + NL + "WHERE {" + NL + "GRAPH <" + namedGraph + "> {"+ NL + GP1 + "}" + NL + "}" + NL; } } diff --git a/test/input/alignment1.rdf b/test/input/alignment1.rdf index 4acb24d069bd65dfb894b375d0096a612f902c00..e9474d0da12e24eb852609fc1a6da16d438bd11e 100644 --- a/test/input/alignment1.rdf +++ b/test/input/alignment1.rdf @@ -42,14 +42,19 @@ <edoal:Property> <edoal:compose rdf:parseType="Collection"> <edoal:Relation> - <edoal:compose rdf:parseType="Collection"> + <edoal:and rdf:parseType="Collection"> <edoal:RelationCoDomainRestriction> <edoal:class> <edoal:Class rdf:about="http://purl.org/NET/c4dm/keys.owl#Key"/> </edoal:class> </edoal:RelationCoDomainRestriction> + <edoal:RelationDomainRestriction> + <edoal:class> + <edoal:Class rdf:about="http://purl.org/NET/c4dm/keys.owl#Work"/> + </edoal:class> + </edoal:RelationDomainRestriction> <edoal:Relation rdf:about="http://purl.org/ontology/mo/key" /> - </edoal:compose> + </edoal:and> </edoal:Relation> <edoal:Property rdf:about="http://www.w3.org/2000/01/rdf-schema#label"/> </edoal:compose> diff --git a/test/input/alignment1b.rdf b/test/input/alignment1b.rdf new file mode 100644 index 0000000000000000000000000000000000000000..aa02ab18ddedd944c1aca42b722d7efaad5ca067 --- /dev/null +++ b/test/input/alignment1b.rdf @@ -0,0 +1,54 @@ +<?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:ns0='http://exmo.inrialpes.fr/align/service#' + 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://localhost:8089/alid/1394718240757/976"> + <xml>yes</xml> + <level>2EDOAL</level> + <type>?*</type> + <pretty>test1</pretty> + <ns0:cached>Thu Mar 13 14:44:05 CET 2014</ns0:cached> + <ns0:stored>null</ns0:stored> + <time>34</time> + <ns0:ouri2>http://purl.org/ontology/mo/</ns0:ouri2> + <id>http://localhost:8089/alid/1394718240757/976</id> + <ns0:ouri1>http://exmo.inrialpes.fr/connectors/example_musical_work_desc/</ns0:ouri1> + <method>fr.inrialpes.exmo.align.impl.method.SubsDistNameAlignment</method> + <onto1> + <Ontology rdf:about="http://exmo.inrialpes.fr/connectors/example_musical_work_desc/"> + <location></location> + <formalism> + <Formalism align:name="OWL2.0" align:uri="http://www.w3.org/2002/07/owl#"/> + </formalism> + </Ontology> + </onto1> + <onto2> + <Ontology rdf:about="http://purl.org/ontology/mo/"> + <location></location> + <formalism> + <Formalism align:name="OWL2.0" align:uri="http://www.w3.org/2002/07/owl#"/> + </formalism> + </Ontology> + </onto2> + <map> + <Cell rdf:about="http://exmo.inrialpes.fr/connectors#cell_key"> + <entity1> + <edoal:Property rdf:about="http://exmo.inrialpes.fr/connectors#key" /> + </entity1> + <entity2> + <edoal:Property> + <edoal:compose rdf:parseType="Collection"> + <edoal:Relation rdf:about="http://purl.org/ontology/mo/key" /> + <edoal:Property rdf:about="http://www.w3.org/2000/01/rdf-schema#label"/> + </edoal:compose> + </edoal:Property> + </entity2> + <relation>=</relation> + <measure rdf:datatype='http://www.w3.org/2001/XMLSchema#float'>1.0</measure> + </Cell> + </map> +</Alignment> +</rdf:RDF> diff --git a/test/input/alignment1c.rdf b/test/input/alignment1c.rdf new file mode 100644 index 0000000000000000000000000000000000000000..4853947c64086e24da9bfb5e3df2e1b72ea15801 --- /dev/null +++ b/test/input/alignment1c.rdf @@ -0,0 +1,90 @@ +<?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:ns0='http://exmo.inrialpes.fr/align/service#' + 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://localhost:8089/alid/1394718240757/976"> + <xml>yes</xml> + <level>2EDOAL</level> + <type>?*</type> + <pretty>test1</pretty> + <ns0:cached>Thu Mar 13 14:44:05 CET 2014</ns0:cached> + <ns0:stored>null</ns0:stored> + <time>34</time> + <ns0:ouri2>http://purl.org/ontology/mo/</ns0:ouri2> + <id>http://localhost:8089/alid/1394718240757/976</id> + <ns0:ouri1>http://exmo.inrialpes.fr/connectors/example_musical_work_desc/</ns0:ouri1> + <method>fr.inrialpes.exmo.align.impl.method.SubsDistNameAlignment</method> + <onto1> + <Ontology rdf:about="http://exmo.inrialpes.fr/connectors/example_musical_work_desc/"> + <location></location> + <formalism> + <Formalism align:name="OWL2.0" align:uri="http://www.w3.org/2002/07/owl#"/> + </formalism> + </Ontology> + </onto1> + <onto2> + <Ontology rdf:about="http://purl.org/ontology/mo/"> + <location></location> + <formalism> + <Formalism align:name="OWL2.0" align:uri="http://www.w3.org/2002/07/owl#"/> + </formalism> + </Ontology> + </onto2> + <map> + <Cell rdf:about="http://exmo.inrialpes.fr/connectors#cell_key"> + <entity1> + <edoal:Property rdf:about="http://exmo.inrialpes.fr/connectors#key" /> + </entity1> + <entity2> + <edoal:Property> + <edoal:compose rdf:parseType="Collection"> + <edoal:RelationCoDomainRestriction> + <edoal:class> + <edoal:Class rdf:about="http://purl.org/NET/c4dm/keys.owl#Key"/> + </edoal:class> + </edoal:RelationCoDomainRestriction> + <edoal:Property> + <edoal:compose rdf:parseType="Collection"> + <edoal:Relation rdf:about="http://purl.org/ontology/mo/key" /> + <edoal:Property rdf:about="http://www.w3.org/2000/01/rdf-schema#label"/> + </edoal:compose> + </edoal:Property> + </edoal:compose> + </edoal:Property> + </entity2> + <relation>=</relation> + <measure rdf:datatype='http://www.w3.org/2001/XMLSchema#float'>1.0</measure> + </Cell> + </map> + <!-- Alternative formulation --> + <!--map> + <Cell rdf:about="http://exmo.inrialpes.fr/connectors#cell_key"> + <entity1> + <edoal:Property rdf:about="http://exmo.inrialpes.fr/connectors#key" /> + </entity1> + <entity2> + <edoal:Property> + <edoal:compose rdf:parseType="Collection"> + <edoal:Relation> + <edoal:compose rdf:parseType="Collection"> + <edoal:RelationCoDomainRestriction> + <edoal:class> + <edoal:Class rdf:about="http://purl.org/NET/c4dm/keys.owl#Key"/> + </edoal:class> + </edoal:RelationCoDomainRestriction> + <edoal:Relation rdf:about="http://purl.org/ontology/mo/key" /> + </edoal:compose> + </edoal:Relation> + <edoal:Property rdf:about="http://www.w3.org/2000/01/rdf-schema#label"/> + </edoal:compose> + </edoal:Property> + </entity2> + <relation>=</relation> + <measure rdf:datatype='http://www.w3.org/2001/XMLSchema#float'>1.0</measure> + </Cell> + </map--> +</Alignment> +</rdf:RDF> diff --git a/test/src/SPARQLConstructVisitorTest.java b/test/src/SPARQLConstructVisitorTest.java index 8666b6c234a9ecbdec5950f56b8c2ce916d3b52c..dd1b4bbf8b3c98f0b072a6dd5ee483fa5aa4aa3a 100644 --- a/test/src/SPARQLConstructVisitorTest.java +++ b/test/src/SPARQLConstructVisitorTest.java @@ -80,9 +80,9 @@ public class SPARQLConstructVisitorTest { alignment.render(renderer); String expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns1:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" - + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s ns0:opus ?o .\n" + "}\n" @@ -93,9 +93,9 @@ public class SPARQLConstructVisitorTest { // Reverse direction String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns1:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" - + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s ns0:opus ?o .\n" + "}\n" @@ -118,9 +118,9 @@ public class SPARQLConstructVisitorTest { alignment.render(renderer); expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns1:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" - + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s ns0:opus ?o .\n" + "}\n" @@ -133,9 +133,9 @@ public class SPARQLConstructVisitorTest { // Reverse direction expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns1:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" - + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s ns0:opus ?o .\n" + "}\n" @@ -206,27 +206,27 @@ public class SPARQLConstructVisitorTest { alignment.render(renderer); String expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns1:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" - + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" - + "?s rdf:type ns0:RootElement .\n" + + "?s rdf:type ns0:MusicalWork .\n" + "}\n" + "WHERE {\n" - + "?s rdf:type ns1:MusicalWork .\n" + + "?s rdf:type ns1:RootElement .\n" + "}\n"; assertEquals( renderer.getQuery( classCell ), expectedQuery1 ); // Reverse direction String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns1:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" - + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" - + "?s rdf:type ns0:MusicalWork .\n" + + "?s rdf:type ns0:RootElement .\n" + "}\n" + "WHERE {\n" - + "?s rdf:type ns1:RootElement .\n" + + "?s rdf:type ns1:MusicalWork .\n" + "}\n"; EDOALAlignment inval = alignment.inverse(); EDOALCell classCell2 = (EDOALCell)inval.getElements().nextElement(); @@ -237,7 +237,54 @@ public class SPARQLConstructVisitorTest { } @Test(groups = {"full", "impl", "raw"}) - public void ConstructComposePropertyRelation() throws Exception { + public void ConstructSimpleComposePropertyRelation() throws Exception { + String alignmentFileName = "alignment1b.rdf"; + EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + SPARQLConstructRendererVisitor renderer = new SPARQLConstructRendererVisitor(writer); + Properties properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + Cell cell = alignment.getElements().nextElement(); + + String expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns2:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" + + "PREFIX ns1:<http://www.w3.org/2000/01/rdf-schema#>\n" + + "PREFIX ns0:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s ns0:key _:o2 .\n" + + "_:o2 ns1:label ?o .\n" + + "}\n" + + "WHERE {\n" + + "?s ns2:key ?o .\n" + + "}\n"; + assertEquals( renderer.getQuery( cell ), expectedQuery1 ); + + // Reverse direction + String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" + + "PREFIX ns2:<http://www.w3.org/2000/01/rdf-schema#>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s ns0:key ?o .\n" + + "}\n" + + "WHERE {\n" + + "?s ns1:key ?o2 .\n" + + "?o2 ns2:label ?o .\n" + + "}\n"; + EDOALAlignment inval = alignment.inverse(); + renderer = new SPARQLConstructRendererVisitor(writer); + renderer.init(properties); + inval.render(renderer); + assertEquals( renderer.getQuery( inval.getElements().nextElement() ), expectedQuery2 ); + } + + @Test(groups = {"full", "impl", "raw"}) + public void ConstructComplexComposePropertyRelation() throws Exception { String alignmentFileName = "alignment1.rdf"; EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); StringWriter stringWriter = new StringWriter(); @@ -250,6 +297,24 @@ public class SPARQLConstructVisitorTest { Cell cell = alignment.getElements().nextElement(); String expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns0:<http://purl.org/NET/c4dm/keys.owl#>\n" + + "PREFIX ns3:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" + + "PREFIX ns2:<http://www.w3.org/2000/01/rdf-schema#>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s rdf:type ns0:Work .\n" + + "?s ns1:key _:o2 .\n" + + "_:o2 rdf:type ns0:Key .\n" + + "_:o2 ns2:label ?o .\n" + + "}\n" + + "WHERE {\n" + + "?s ns3:key ?o .\n" + + "}\n"; + assertEquals( renderer.getQuery( cell ), expectedQuery1 ); + + // Reverse direction + String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX ns1:<http://purl.org/NET/c4dm/keys.owl#>\n" + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" @@ -259,110 +324,68 @@ public class SPARQLConstructVisitorTest { + "?s ns0:key ?o .\n" + "}\n" + "WHERE {\n" - + "?o3 rdf:type ns1:Key .\n" - + "?o3 ns2:key ?o4 .\n" - + "?o4 ns3:label ?o .\n" + + "?s rdf:type ns1:Work .\n" + + "?s ns2:key ?o2 .\n" + + "?o2 rdf:type ns1:Key .\n" + + "?o2 ns3:label ?o .\n" + "}\n"; - assertEquals( renderer.getQuery( cell ), expectedQuery1 ); + EDOALAlignment inval = alignment.inverse(); + renderer = new SPARQLConstructRendererVisitor(writer); + renderer.init(properties); + inval.render(renderer); + assertEquals( renderer.getQuery( inval.getElements().nextElement() ), expectedQuery2 ); + } - // Reverse direction - String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + @Test(groups = {"full", "impl", "raw"}) + public void ConstructWeirdComposePropertyRelation() throws Exception { + String alignmentFileName = "alignment1c.rdf"; + EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + SPARQLConstructRendererVisitor renderer = new SPARQLConstructRendererVisitor(writer); + Properties properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + Cell cell = alignment.getElements().nextElement(); + + String expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX ns0:<http://purl.org/NET/c4dm/keys.owl#>\n" + "PREFIX ns3:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" + "PREFIX ns2:<http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" - + "_:o3 rdf:type ns0:Key .\n" - + "_:o3 ns1:key _:o4 .\n" + + "?s _:o3 _:o2 .\n" + + "_:o2 rdf:type ns0:Key .\n" + + "_:o2 ns1:key _:o4 .\n" + "_:o4 ns2:label ?o .\n" + "}\n" + "WHERE {\n" + "?s ns3:key ?o .\n" + "}\n"; + assertEquals( renderer.getQuery( cell ), expectedQuery1 ); + + // Reverse direction + String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns1:<http://purl.org/NET/c4dm/keys.owl#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>\n" + + "PREFIX ns3:<http://www.w3.org/2000/01/rdf-schema#>\n" + + "PREFIX ns2:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s ns0:key ?o .\n" + + "}\n" + + "WHERE {\n" + + "?s ?o3 ?o2 .\n" + + "?o2 rdf:type ns1:Key .\n" + + "?o2 ns2:key ?o4 .\n" + + "?o4 ns3:label ?o .\n" + + "}\n"; EDOALAlignment inval = alignment.inverse(); renderer = new SPARQLConstructRendererVisitor(writer); renderer.init(properties); inval.render(renderer); assertEquals( renderer.getQuery( inval.getElements().nextElement() ), expectedQuery2 ); } - -// @Test(groups = {"full", "impl", "raw"}) -// public void ConstructFromAndProperty() throws Exception { -// String alignmentFileName = "alignment_with_and_property.rdf"; -// EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); -// StringWriter stringWriter = new StringWriter(); -// PrintWriter writer = new PrintWriter(stringWriter); -// SPARQLConstructRendererVisitor renderer = new SPARQLConstructRendererVisitor(writer); -// Properties properties = new Properties(); -// renderer.init(properties); -// alignment.render(renderer); -// assertEquals(alignment.nbCells(), 2); -// Enumeration<Cell> cells = alignment.getElements(); -// -// Cell cell1 = null; -// Cell cell2 = null; -// while (cells.hasMoreElements()) { -// Cell cell = cells.nextElement(); -// if (cell.getId().equals("http://exmo.inrialpes.fr/connectors-core#cell1")) { -// cell1 = cell; -// } else { -// cell2 = cell; -// } -// } -// //First test : with "and" property on linkkey -// String expectedQuery = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" -// + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors-core#>\n" -// + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" -// + "CONSTRUCT {\n" -// + "?s rdf:type ns1:MusicalWork .\n" -// + "}\n" -// + "WHERE {\n" -// + "?s rdf:type ns0:RootElement .\n" -// + "}\n"; -// String realQuery = renderer.getQueryFromOnto1ToOnto2(cell1); -// assertEquals(realQuery, expectedQuery); -// expectedQuery = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" -// + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors-core#>\n" -// + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" -// + "CONSTRUCT {\n" -// + "?s rdf:type ns0:RootElement .\n" -// + "}\n" -// + "WHERE {\n" -// + "?s rdf:type ns1:MusicalWork .\n" -// + "}\n"; -// realQuery = renderer.getQueryFromOnto2ToOnto1(cell1); -// assertEquals(realQuery, expectedQuery); -// //Second test : with "and" property on entity -// expectedQuery = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" -// + "PREFIX ns2:<http://exmo.inrialpes.fr/connectors-data#>\n" -// + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors-core#>\n" -// + "PREFIX ns3:<http://purl.org/NET/c4dm/keys.owl#>\n" -// + "PREFIX ns4:<http://www.w3.org/2000/01/rdf-schema#>\n" -// + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" -// + "CONSTRUCT {\n" -// + "?s rdf:type ns3:Key .\n" -// + "?s ns4:label ?o .\n"//@ J�r�me : we have ?o2 instead of ?o -// + "}\n" -// + "WHERE {\n" -// + "?s ns2:tonalite ?o .\n" -// + "}\n"; -// realQuery = renderer.getQueryFromOnto1ToOnto2(cell2); -// assertEquals(realQuery, expectedQuery); -// expectedQuery = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" -// + "PREFIX ns2:<http://exmo.inrialpes.fr/connectors-data#>\n" -// + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors-core#>\n" -// + "PREFIX ns3:<http://purl.org/NET/c4dm/keys.owl#>\n" -// + "PREFIX ns4:<http://www.w3.org/2000/01/rdf-schema#>\n" -// + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" -// + "CONSTRUCT {\n" -// + "?s ns2:tonalite ?o .\n" -// + "}\n" -// + "WHERE {\n" -// + "?s rdf:type ns3:Key .\n" -// + "?s ns4:label ?o .\n"//@ J�r�me : we have ?o2 instead of ?o -// + "}\n"; -// realQuery = renderer.getQueryFromOnto2ToOnto1(cell2); -// assertEquals(realQuery, expectedQuery); -// } } diff --git a/test/src/SPARQLLinkkerRendererVisitorTest.java b/test/src/SPARQLLinkkerRendererVisitorTest.java index 3f64ca6e39aee74b90ce3198264705cea663ce11..b9574d7de5ab89e5e49f18d4dee178a561641afd 100644 --- a/test/src/SPARQLLinkkerRendererVisitorTest.java +++ b/test/src/SPARQLLinkkerRendererVisitorTest.java @@ -420,8 +420,8 @@ System.err.println( renderer.getQuery( cell ) ); + "?s1 rdf:type ns0:Personne .\n" + "?s2 rdf:type ns1:Person .\n" + "?s1 ns0:connait ?o3 .\n" - + "?o5 rdf:type ns1:Person .\n" + "?s2 ns1:knows ?o5 .\n" + + "?o5 rdf:type ns1:Person .\n" + "?o5 ns1:givenName ?o4 .\n" // JE2015: Here we have a URI (o3) compared to a string (o4) + "FILTER( lcase(str(?o3)) = lcase(str(?o4)) )\n" @@ -688,8 +688,8 @@ System.err.println( renderer.getQuery( cell ) ); + "?s1 ns0:connait ?o3 .\n" + "}\n" + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" - + "?o5 rdf:type ns1:Person .\n" + "?s2 ns1:knows ?o5 .\n" + + "?o5 rdf:type ns1:Person .\n" + "?o5 ns1:givenName ?o4 .\n" + "}\n" // JE2015: Here we have a URI (o3) compared to a string (o4)