diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java index 3b199b985bb089fd6b037869d29d2fc790ee184d..dfdb068074ee42d03aa81d7d7ce2f3bc1a6ae7fa 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java @@ -77,6 +77,8 @@ import java.util.LinkedList; public abstract class GraphPatternRendererVisitor extends IndentedRendererVisitor implements EDOALVisitor { final static Logger logger = LoggerFactory.getLogger(GraphPatternRendererVisitor.class); + protected static String FEDERATED_SERVICE_INTRODUCTION = "FEDERATED_SERVICE_BEGIN"; + protected static String FEDERATED_SERVICE_FINALIZATION = "FEDERATED_SERVICE_END"; Alignment alignment = null; Cell cell = null; @@ -237,13 +239,21 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito } + private static String cleanQuery(String query, String serviceName){ + if(serviceName == null || serviceName == "" ){ + return query.replaceAll(FEDERATED_SERVICE_INTRODUCTION, "").replaceAll(FEDERATED_SERVICE_FINALIZATION, ""); + }else{ + return query.replaceAll(FEDERATED_SERVICE_INTRODUCTION, String.format("\nSERVICE <%s> {", serviceName)).replaceAll(FEDERATED_SERVICE_FINALIZATION, "}\n"); + } + } + public void saveQuery(Object referer, String query) { //Query is stored in memory if(!queries.containsKey(referer)){ queries.put(referer, new LinkedList()); } queries.get(referer).add(query); - + query = cleanQuery(query, null); if (split) { BufferedWriter out = null; try { @@ -263,8 +273,18 @@ public abstract class GraphPatternRendererVisitor extends IndentedRendererVisito } } + /** + * Produce Query only for local call. + * @param referer + * @param index + * @return + */ public String getQuery(Object referer, int index){ - return queries.get(referer).get(index); + return getQuery(referer, index, null); + } + + public String getQuery(Object referer, int index, URI remoteURI){ + return cleanQuery(queries.get(referer).get(index), (remoteURI == null ? null : remoteURI.toString())); } protected String createPrefixList() { diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java index c78c648f9bce31d33def755304cceb68cb6f334d..29b724a7c882029c0f591b3d13fed5a9496a9741 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java @@ -181,7 +181,7 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor } protected String createConstruct(String GP1, String GP2) { - return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + NL + GP2 + "}" + NL; + return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + FEDERATED_SERVICE_INTRODUCTION + NL + GP2 + FEDERATED_SERVICE_FINALIZATION + "}" + NL; } protected String createCoreseQuery(String query) { diff --git a/test/input/alignment1.rdf b/test/input/alignment1.rdf index 26e80142e963767f152ab5fad1d7d5e8fce8f745..4acb24d069bd65dfb894b375d0096a612f902c00 100644 --- a/test/input/alignment1.rdf +++ b/test/input/alignment1.rdf @@ -15,11 +15,11 @@ <time>34</time> <ns0:ouri2>http://purl.org/ontology/mo/</ns0:ouri2> <id>http://localhost:8089/alid/1394718240757/976</id> - <ns0:ouri1>http://exmo.inria.fr/connectors/example_musical_work_desc/</ns0:ouri1> + <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.inria.fr/connectors/example_musical_work_desc/"> - <location>file:///home/nicolas/Bureau/Discotheka/Inria/Mission/tests/musical_work_source_desc.owl</location> + <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> @@ -27,16 +27,16 @@ </onto1> <onto2> <Ontology rdf:about="http://purl.org/ontology/mo/"> - <location>file:///home/nicolas/Bureau/Discotheka/Inria/Mission/tests/musicontology1.2.rdf</location> + <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.inria.fr/connectors#cell_key"> + <Cell rdf:about="http://exmo.inrialpes.fr/connectors#cell_key"> <entity1> - <edoal:Property rdf:about="http://exmo.inria.fr/connectors#key" /> + <edoal:Property rdf:about="http://exmo.inrialpes.fr/connectors#key" /> </entity1> <entity2> <edoal:Property> diff --git a/test/input/alignment2.rdf b/test/input/alignment2.rdf new file mode 100644 index 0000000000000000000000000000000000000000..e06864835efe3c277734a0c0d7f7d537fc1ecd86 --- /dev/null +++ b/test/input/alignment2.rdf @@ -0,0 +1,63 @@ +<?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>/home/tests/example_musical_work_desc.owl</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>http://purl.org/ontology/mo/</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#cell1"> + <entity1> + <edoal:Class rdf:about="http://exmo.inrialpes.fr/connectors#RootElement"/> + </entity1> + <entity2> + <edoal:Class rdf:about="http://purl.org/ontology/mo/MusicalWork"/> + </entity2> + <relation>=</relation> + <measure rdf:datatype='http://www.w3.org/2001/XMLSchema#float'>1.0</measure> + <linkkey> + <Linkkey type="rule"> + <bindings> + <Corresp type="eq"> + <property1> + <edoal:Property rdf:about="http://purl.org/ontology/mo/opus" /> + </property1> + <property2> + <edoal:Property rdf:about="http://exmo.inrialpes.fr/connectors#number" /> + </property2> + </Corresp> + </bindings> + </Linkkey> + </linkkey> + </Cell> + </map> + </Alignment> +</rdf:RDF> diff --git a/test/src/SPARQLConstructVisitorTest.java b/test/src/SPARQLConstructVisitorTest.java index b3bf986265482b7b6b27fffbe6b61797b6f13f6b..c68bb1ac1c0a6ff9b976c17b15e865b37e254db1 100644 --- a/test/src/SPARQLConstructVisitorTest.java +++ b/test/src/SPARQLConstructVisitorTest.java @@ -1,5 +1,4 @@ -import fr.inrialpes.exmo.align.impl.ObjectAlignment; import fr.inrialpes.exmo.align.impl.edoal.ClassId; import fr.inrialpes.exmo.align.impl.edoal.EDOALAlignment; import fr.inrialpes.exmo.align.impl.edoal.EDOALCell; @@ -7,20 +6,15 @@ import fr.inrialpes.exmo.align.impl.edoal.Expression; import fr.inrialpes.exmo.align.impl.edoal.PropertyId; import fr.inrialpes.exmo.align.impl.rel.EquivRelation; import fr.inrialpes.exmo.align.impl.renderer.SPARQLConstructRendererVisitor; -import fr.inrialpes.exmo.align.parser.AlignmentParser; -import fr.inrialpes.exmo.align.parser.RDFParser; -import java.io.FileInputStream; -import java.io.InputStream; +import fr.inrialpes.exmo.align.test.Utils; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URI; import java.util.Enumeration; import java.util.Properties; -import org.semanticweb.owl.align.Alignment; import org.semanticweb.owl.align.Cell; import org.semanticweb.owl.align.Relation; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -35,19 +29,6 @@ import org.testng.annotations.Test; */ public class SPARQLConstructVisitorTest { - private Alignment alignment = null; - private ObjectAlignment oalignment = null; - - private static EDOALAlignment loadAlignement(String fileName) throws Exception { - AlignmentParser aparser = new AlignmentParser(0); - assertNotNull(aparser); - aparser.initAlignment(null); - RDFParser alignmentParser = new RDFParser(); - InputStream alignIn = new FileInputStream("test/input/" + fileName); - EDOALAlignment loadedAlignment = alignmentParser.parse(alignIn); - return loadedAlignment; - } - // Read the alignement that will be rendered by everyone @BeforeClass(groups = {"full", "impl", "raw"}) private void init() throws Exception { @@ -63,7 +44,7 @@ public class SPARQLConstructVisitorTest { public void ConstructSimplePropertiesRelation() throws Exception { EDOALAlignment alignment = new EDOALAlignment(); Relation opusRelation = new EquivRelation(); - Expression opusExpression1 = new PropertyId(new URI("http://exmo.inria.fr/connectors#opus")); + Expression opusExpression1 = new PropertyId(new URI("http://exmo.inrialpes.fr/connectors#opus")); Expression opusExpression2 = new PropertyId(new URI("http://purl.org/ontology/mo/opus")); EDOALCell opusCell = new EDOALCell("1", opusExpression1, opusExpression2, opusRelation, 1.0); alignment.addAlignCell(opusCell); @@ -76,7 +57,7 @@ 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.inria.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s ns0:opus ?o .\n" @@ -86,7 +67,7 @@ public class SPARQLConstructVisitorTest { + "}\n"; String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns0:<http://exmo.inria.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s ns1:opus ?o .\n" @@ -96,6 +77,37 @@ public class SPARQLConstructVisitorTest { + "}\n"; assertEquals(renderer.getQuery(opusCell, 0), expectedQuery1); assertEquals(renderer.getQuery(opusCell, 1), expectedQuery2); + + //For remote sparql endpoint : + + + String remoteServiceURIName = "http://example.org/remoteSparql"; + URI remoteServiceURI = new URI(remoteServiceURIName); + expectedQuery1 = String.format("PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s ns0:opus ?o .\n" + + "}\n" + + "WHERE {\n" + + "SERVICE <%s> {\n" + + "?s ns1:opus ?o .\n" + + "}\n" + + "}\n", remoteServiceURIName); + + expectedQuery2 = String.format("PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s ns1:opus ?o .\n" + + "}\n" + + "WHERE {\n" + + "SERVICE <%s> {\n" + + "?s ns0:opus ?o .\n" + + "}\n" + + "}\n", remoteServiceURIName); + assertEquals(renderer.getQuery(opusCell, 0, remoteServiceURI), expectedQuery1); + assertEquals(renderer.getQuery(opusCell, 1, remoteServiceURI), expectedQuery2); } /** @@ -107,7 +119,7 @@ public class SPARQLConstructVisitorTest { public void ConstructSimpleClassesRelation() throws Exception { EDOALAlignment alignment = new EDOALAlignment(); Relation classesRelation = new EquivRelation(); - Expression rootElementExpression = new ClassId(new URI("http://exmo.inria.fr/connectors#RootElement")); + Expression rootElementExpression = new ClassId(new URI("http://exmo.inrialpes.fr/connectors#RootElement")); Expression musicalWorkExpression = new ClassId(new URI("http://purl.org/ontology/mo/MusicalWork")); EDOALCell classCell = new EDOALCell("1", rootElementExpression, musicalWorkExpression, classesRelation, 1.0); alignment.addAlignCell(classCell); @@ -120,7 +132,7 @@ 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.inria.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s rdf:type ns0:RootElement .\n" @@ -130,7 +142,7 @@ public class SPARQLConstructVisitorTest { + "}\n"; String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" - + "PREFIX ns0:<http://exmo.inria.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" + "?s rdf:type ns1:MusicalWork .\n" @@ -146,7 +158,7 @@ public class SPARQLConstructVisitorTest { public void ConstructComposePropertyRelation() throws Exception { String[] alignmentFilesNames = {"alignment1.rdf"}; for (String alignmentFileName : alignmentFilesNames) { - EDOALAlignment alignment = loadAlignement(alignmentFileName); + EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); SPARQLConstructRendererVisitor renderer = new SPARQLConstructRendererVisitor(writer); @@ -159,7 +171,7 @@ public class SPARQLConstructVisitorTest { String expectedQuery1 = "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.inria.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX ns3:<http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX ns2:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" @@ -173,7 +185,7 @@ public class SPARQLConstructVisitorTest { 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.inria.fr/connectors#>\n" + + "PREFIX ns0:<http://exmo.inrialpes.fr/connectors#>\n" + "PREFIX ns3:<http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX ns2:<http://purl.org/ontology/mo/>\n" + "CONSTRUCT {\n" diff --git a/test/src/SPARQLSelectRendererVisitorTest.java b/test/src/SPARQLSelectRendererVisitorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..afee166b3f06b7ed9a5eb655a661483fc63a32f0 --- /dev/null +++ b/test/src/SPARQLSelectRendererVisitorTest.java @@ -0,0 +1,70 @@ + +import fr.inrialpes.exmo.align.impl.edoal.EDOALAlignment; +import fr.inrialpes.exmo.align.impl.renderer.SPARQLSelectRendererVisitor; +import fr.inrialpes.exmo.align.test.Utils; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URI; +import java.util.Enumeration; +import java.util.Properties; +import org.semanticweb.owl.align.Cell; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; +import org.testng.annotations.Test; + +/** + * + * @author Nicolas Guillouet <nicolas.guillouet@inria.fr> + */ +public class SPARQLSelectRendererVisitorTest { + + @Test(groups = {"full", "impl", "raw"}) + public void QueryFromSimpleLinkkey() throws Exception { + String alignmentFileName = "alignment2.rdf"; + EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + SPARQLSelectRendererVisitor renderer = new SPARQLSelectRendererVisitor(writer); + Properties properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + Enumeration<Cell> cells = alignment.getElements(); + Cell cell = cells.nextElement(); + URI remoteURI = new URI("http://example.org/data.rdf"); + String expectedQuery1 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns0:<http://exmo.inria.fr/connectors#>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT ?s1 ?s2" + + "WHERE {\n" + + "?s1 rdf:type ns0:RootElement .\n" + + "?s1 ns0:number ?o1 ." + + "SERVICE <http://example.org/data.rdf> {" + + "?s2 rdf:type ns1:MusicalWork . \n" + + "?s2 ns1:opus ?o1" + + "}\n" + + "}\n"; + + String expectedQuery2 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + + "PREFIX ns0:<http://exmo.inria.fr/connectors#>\n" + + "PREFIX ns1:<http://purl.org/ontology/mo/>\n" + + "CONSTRUCT ?s1 ?s2" + + "WHERE {\n" + + "?s2 rdf:type ns1:MusicalWork . \n" + + "?s2 ns1:opus ?o1" + + "SERVICE <http://example.org/data.rdf> {" + + "?s1 rdf:type ns0:RootElement .\n" + + "?s1 ns0:number ?o1 ." + + "}\n" + + "}\n"; + assertEquals(renderer.getQuery(cell, 0, remoteURI), expectedQuery1, "FOR alignment file " + alignmentFileName); + assertEquals(renderer.getQuery(cell, 1, remoteURI), expectedQuery2, "FOR alignment file " + alignmentFileName); + fail("HAVE TODO"); + } + + @Test(groups = {"full", "impl", "raw"}) + public void HaveTodo() throws Exception { + fail("HAVE TODO : with (in + eq) => OPTIONAL (with SERVICE call on remote) / many correspondances / With transformations / On local (FROM ...) or remote sparql endpoint (SERVICE) "); + } + +}