From 6f18de507098dae61b28f13e79b40746ed0b0dd6 Mon Sep 17 00:00:00 2001
From: Nicolas Guillouet <nicolas@meaningengines.com>
Date: Tue, 8 Jul 2014 15:42:25 +0000
Subject: [PATCH] Modification of queries generation (on
 fr.inrialpes.exmo.align.impl.renderer.GraphPatternRendererVisitor) for using
 federated queries + first test for SPARQLSelectRendererVisitor

---
 .../renderer/GraphPatternRendererVisitor.java | 24 ++++++-
 .../SPARQLConstructRendererVisitor.java       |  2 +-
 test/input/alignment1.rdf                     | 12 ++--
 test/input/alignment2.rdf                     | 63 +++++++++++++++++
 test/src/SPARQLConstructVisitorTest.java      | 70 +++++++++++--------
 test/src/SPARQLSelectRendererVisitorTest.java | 70 +++++++++++++++++++
 6 files changed, 203 insertions(+), 38 deletions(-)
 create mode 100644 test/input/alignment2.rdf
 create mode 100644 test/src/SPARQLSelectRendererVisitorTest.java

diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/GraphPatternRendererVisitor.java
index 3b199b98..dfdb0680 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 c78c648f..29b724a7 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 26e80142..4acb24d0 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 00000000..e0686483
--- /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 b3bf9862..c68bb1ac 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 00000000..afee166b
--- /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) ");
+    }
+
+}
-- 
GitLab