diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java index a015497422f36dde93e4ca88584a7b073cb0bc71..10ab169527d68b31a2251a9e9a863a5c96fefb85 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java @@ -46,6 +46,8 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor Cell cell = null; Hashtable<String, String> nslist = null; + private String namedGraph = null; + boolean embedded = false; boolean oneway = false; @@ -59,6 +61,11 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor super(writer); } + public SPARQLConstructRendererVisitor(PrintWriter writer, String namedGraph) { + this(writer); + this.namedGraph = namedGraph; + } + /** * Initialises the parameters of the renderer */ @@ -146,13 +153,15 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor // default behaviour // rel.write( writer ); } + public void visit(final Linkkey linkkey) throws AlignmentException { throw new AlignmentException("NOT IMPLEMENTED !"); } - + public void visit(final LinkkeyEquals linkkeyEquals) throws AlignmentException { throw new AlignmentException("NOT IMPLEMENTED !"); } + public void visit(final LinkkeyIntersects linkkeyIntersects) throws AlignmentException { throw new AlignmentException("NOT IMPLEMENTED !"); } @@ -195,12 +204,15 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor } protected String createConstruct(String GP1, String GP2) { - return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + NL + GP2 + "}" + NL; + if (namedGraph == null) { + return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + NL + GP2 + "}" + NL; + } else { + return createPrefixList() + "CONSTRUCT {" + NL + GP1 + "}" + NL + "WHERE {" + NL + "GRAPH <" + namedGraph + "> {"+ NL + GP2 + "}" + NL + "}" + NL; + } } protected String createCoreseQuery(String query) { return "<rule>" + NL + "<body>" + NL + "<![CDATA[" + NL + query + "]]>" + NL + "</body>" + NL + "</rule>" + NL + NL; } - - + } diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLSelectRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLSelectRendererVisitor.java index 26531faebf75df1aec1ea750642bb71044008481..cdbd3ce144d12d496531d0a77e205cb74cec14e6 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLSelectRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLSelectRendererVisitor.java @@ -53,6 +53,7 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp boolean oneway = false; boolean split = false; String splitdir = ""; + private String onto1NamedGraph, onto2NamedGraph = null; boolean edoal = false; @@ -62,6 +63,12 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp super(writer); } + public SPARQLSelectRendererVisitor(PrintWriter writer, String onto1NamedGraph, String onto2NamedGraph) { + super(writer); + this.onto1NamedGraph = onto1NamedGraph; + this.onto2NamedGraph = onto2NamedGraph; + } + public void init(Properties p) { if (p.getProperty("embedded") != null && !p.getProperty("embedded").equals("")) { embedded = true; @@ -138,21 +145,21 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp } protected void resetS1(String obj) { - if(fromOnto1ToOnto2){ - resetVariables("?s1", obj); - }else{ + if (fromOnto1ToOnto2) { + resetVariables("?s1", obj); + } else { resetVariables("?s2", obj); } } protected void resetS2(String obj) { - if(fromOnto1ToOnto2){ - resetVariables("?s2", obj); - }else{ + if (fromOnto1ToOnto2) { + resetVariables("?s2", obj); + } else { resetVariables("?s1", obj); } } - + /** * Where each element must be equal * @@ -163,34 +170,50 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp //Main part for selection resetS1("?o1"); Expression expr1 = linkkeyEquals.getExpression1(); + beginNamedGraph(onto1NamedGraph); expr1.accept(this); + endNamedGraph(onto1NamedGraph); resetS2("?o1"); Expression expr2 = linkkeyEquals.getExpression2(); + beginNamedGraph(onto2NamedGraph); expr2.accept(this); + endNamedGraph(onto2NamedGraph); //First part addToGP("MINUS { " + NL); resetS1("?o1"); + beginNamedGraph(onto1NamedGraph); expr1.accept(this); resetS1("?o2"); expr1.accept(this); + endNamedGraph(onto1NamedGraph); resetS2("?o1"); + beginNamedGraph(onto2NamedGraph); expr2.accept(this); + endNamedGraph(onto2NamedGraph); addToGP("FILTER(?s1 != ?s2 && ?o2 != ?o1 && NOT EXISTS {" + NL); resetS2("?o2"); + beginNamedGraph(onto2NamedGraph); expr2.accept(this); + endNamedGraph(onto2NamedGraph); addToGP("}) " + NL); addToGP("} " + NL); //Second part addToGP("MINUS {" + NL); resetS1("?o1"); + beginNamedGraph(onto1NamedGraph); expr1.accept(this); + endNamedGraph(onto1NamedGraph); resetS2("?o1"); + beginNamedGraph(onto2NamedGraph); expr2.accept(this); resetS2("?o2"); expr2.accept(this); + endNamedGraph(onto2NamedGraph); addToGP("FILTER(?s1 != ?s2 && ?o1 != ?o2 && NOT EXISTS {" + NL); resetS1("?o2"); + beginNamedGraph(onto1NamedGraph); expr1.accept(this); + endNamedGraph(onto1NamedGraph); addToGP("}) " + NL); addToGP("} " + NL); } @@ -204,10 +227,14 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp public void visit(final LinkkeyIntersects linkkeyIntersects) throws AlignmentException { resetS1("?o1"); Expression expr1 = linkkeyIntersects.getExpression1(); + beginNamedGraph(onto1NamedGraph); expr1.accept(this); + endNamedGraph(onto1NamedGraph); resetS2("?o1"); Expression expr2 = linkkeyIntersects.getExpression2(); + beginNamedGraph(onto2NamedGraph); expr2.accept(this); + endNamedGraph(onto2NamedGraph); } protected void generateSelect(Cell cell, Expression expr1, Expression expr2, boolean from1To2) throws AlignmentException { @@ -221,14 +248,18 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp } else { resetVariables(expr1, "s2", "o"); } + beginNamedGraph(onto1NamedGraph); expr1.accept(this); + endNamedGraph(onto1NamedGraph); listGP.add(getGP()); if (from1To2) { resetVariables(expr2, "s2", "o"); } else { resetVariables(expr2, "s1", "o"); } + beginNamedGraph(onto2NamedGraph); expr2.accept(this); + endNamedGraph(onto2NamedGraph); listGP.add(getGP()); initStructure(); String filter = "FILTER(?s1 != ?s2)"; @@ -238,7 +269,7 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp linkkey.accept(this); } listGP.add(getGP()); - } + } String query = createSelect(listGP, filter); if (corese) { throw new AlignmentException("corese case NOT IMPLEMENTED for SPARQLSelectRendererVisitor !!"); @@ -249,6 +280,18 @@ public class SPARQLSelectRendererVisitor extends GraphPatternRendererVisitor imp saveQuery(cell, query); } + protected void beginNamedGraph(String namedGraph) { + if (namedGraph != null) { + addToGP("GRAPH <" + namedGraph + "> {" + NL); + } + } + + protected void endNamedGraph(String namedGraph) { + if (namedGraph != null) { + addToGP("}" + NL); + } + } + protected String createSelect(List<String> listGP, String filter) { StringBuilder mainGPBuilder = new StringBuilder(); for (String GP : listGP) { diff --git a/test/src/SPARQLConstructVisitorTest.java b/test/src/SPARQLConstructVisitorTest.java index 3231a355013855a4557c4e57974abe9e2004e72a..4c8b9c023870a3eec8f6b27ded6ec5d927c5519c 100644 --- a/test/src/SPARQLConstructVisitorTest.java +++ b/test/src/SPARQLConstructVisitorTest.java @@ -95,6 +95,41 @@ public class SPARQLConstructVisitorTest { assertEquals(renderer.getQueryFromOnto1ToOnto2(opusCell), expectedQuery1); assertEquals(renderer.getQueryFromOnto2ToOnto1(opusCell), expectedQuery2); + + //With Named Graph + String namedGraph = "http://exmo.inrialpes.fr/connectors/one-graph"; + renderer = new SPARQLConstructRendererVisitor(writer, namedGraph); + properties = new Properties(); + renderer.init(properties); + 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://purl.org/ontology/mo/>\n" + + "CONSTRUCT {\n" + + "?s ns1:opus ?o .\n" + + "}\n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/one-graph> {\n" + + "?s ns0:opus ?o .\n" + + "}\n" + + "}\n"; + + expectedQuery2 = "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" + + "GRAPH <http://exmo.inrialpes.fr/connectors/one-graph> {\n" + + "?s ns1:opus ?o .\n" + + "}\n" + + "}\n"; + + assertEquals(renderer.getQueryFromOnto1ToOnto2(opusCell), expectedQuery1); + assertEquals(renderer.getQueryFromOnto2ToOnto1(opusCell), expectedQuery2); + //For remote sparql endpoint : // String remoteServiceURIName = "http://example.org/remoteSparql"; diff --git a/test/src/SPARQLSelectRendererVisitorTest.java b/test/src/SPARQLSelectRendererVisitorTest.java index 959ce607c8e724608999c1035a6c482b084f9fc3..987cebd36c0ec704bfec8e0a9b4b31504b723ca6 100644 --- a/test/src/SPARQLSelectRendererVisitorTest.java +++ b/test/src/SPARQLSelectRendererVisitorTest.java @@ -17,6 +17,9 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +import com.hp.hpl.jena.query.Dataset; +import com.hp.hpl.jena.query.DatasetFactory; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; @@ -34,7 +37,6 @@ import java.util.Properties; import org.semanticweb.owl.align.Cell; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; import org.testng.annotations.Test; /** @@ -166,7 +168,6 @@ public class SPARQLSelectRendererVisitorTest { // + "}\n"; // assertEquals(renderer.getQueryFromOnto2ToOnto1(cell2), expectedQuery); // } - @Test(groups = {"full", "impl", "raw"}, dependsOnMethods = {"QueryWithoutLinkkey"}) public void QueryFromSimpleLinkkeyAndIntersects() throws Exception { String alignmentFileName = "people_intersects_alignment.rdf"; @@ -239,6 +240,7 @@ public class SPARQLSelectRendererVisitorTest { } } + @Test(groups = {"full", "impl", "raw"}, dependsOnMethods = {"QueryWithoutLinkkey", "QueryFromSimpleLinkkeyAndIntersects"}) public void QueryFromSimpleLinkkeyAndEquals() throws Exception { String alignmentFileName = "people_equals_alignment.rdf"; @@ -398,6 +400,293 @@ public class SPARQLSelectRendererVisitorTest { assertTrue(resultValues.contains(expected)); } } + + @Test(groups = {"full", "impl", "raw"}, dependsOnMethods = {"QueryFromRelationLinkkeyAndIntersects"}) + public void QueryWithNamedGraphAndIntersectsLinkkey() throws Exception { + String alignmentFileName = "people_intersects_alignment.rdf"; + EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + String onto1NamedGraph = "http://exmo.inrialpes.fr/connectors/onto1-graph"; + String onto2NamedGraph = "http://exmo.inrialpes.fr/connectors/onto2-graph"; + + //With named Graph on onto1 and onto2 + SPARQLSelectRendererVisitor renderer = new SPARQLSelectRendererVisitor(writer, onto1NamedGraph, onto2NamedGraph); + Properties properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + Enumeration<Cell> cells = alignment.getElements(); + Cell cell = cells.nextElement(); + + 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 rdf:type ns0:Personne .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 rdf:type ns1:Person .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 ns0:nom ?o1 .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 ns1:givenName ?o1 .\n" + + "}\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + assertEquals(renderer.getQueryFromOnto1ToOnto2(cell), 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s2 rdf:type ns0:Personne .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s1 rdf:type ns1:Person .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s2 ns0:nom ?o1 .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s1 ns1:givenName ?o1 .\n" + + "}\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + assertEquals(renderer.getQueryFromOnto2ToOnto1(cell), expectedQuery); + + //With named Graph only on onto1 + renderer = new SPARQLSelectRendererVisitor(writer, onto1NamedGraph, null); + properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + cells = alignment.getElements(); + cell = cells.nextElement(); + + 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 rdf:type ns0:Personne .\n" + + "}\n" + + "?s2 rdf:type ns1:Person .\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 ns0:nom ?o1 .\n" + + "}\n" + + "?s2 ns1:givenName ?o1 .\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + assertEquals(renderer.getQueryFromOnto1ToOnto2(cell), 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s2 rdf:type ns0:Personne .\n" + + "}\n" + + "?s1 rdf:type ns1:Person .\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s2 ns0:nom ?o1 .\n" + + "}\n" + + "?s1 ns1:givenName ?o1 .\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + assertEquals(renderer.getQueryFromOnto2ToOnto1(cell), expectedQuery); + + //With named Graph only on onto2 + renderer = new SPARQLSelectRendererVisitor(writer, null, onto2NamedGraph); + properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + cells = alignment.getElements(); + cell = cells.nextElement(); + + 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "?s1 rdf:type ns0:Personne .\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 rdf:type ns1:Person .\n" + + "}\n" + + "?s1 ns0:nom ?o1 .\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 ns1:givenName ?o1 .\n" + + "}\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + + assertEquals(renderer.getQueryFromOnto1ToOnto2(cell), 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "?s2 rdf:type ns0:Personne .\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s1 rdf:type ns1:Person .\n" + + "}\n" + + "?s2 ns0:nom ?o1 .\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s1 ns1:givenName ?o1 .\n" + + "}\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + assertEquals(renderer.getQueryFromOnto2ToOnto1(cell), expectedQuery); + + //With Relation + alignmentFileName = "people_relation_intersects_alignment.rdf"; + alignment = Utils.loadAlignement(alignmentFileName); + stringWriter = new StringWriter(); + writer = new PrintWriter(stringWriter); + renderer = new SPARQLSelectRendererVisitor(writer, onto1NamedGraph, onto2NamedGraph); + properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + + assertEquals(alignment.nbCells(), 1); + cells = alignment.getElements(); + cell = cells.nextElement(); + + String query = renderer.getQueryFromOnto1ToOnto2(cell); + 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 rdf:type ns0:Personne .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 rdf:type ns1:Person .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 ns0:connait ?o1 .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?o2 rdf:type ns1:Person .\n" + + "?s2 ns1:knows ?o2 .\n" + + "?o2 ns1:givenName ?o1 .\n" + + "}\n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + assertEquals(expectedQuery, query); + + } + + + @Test(groups = {"full", "impl", "raw"}, dependsOnMethods = {"QueryFromRelationLinkkeyAndIntersects"}) + public void QueryWithNamedGraphAndEqualsLinkkey() throws Exception { + String alignmentFileName = "people_equals_alignment.rdf"; + EDOALAlignment alignment = Utils.loadAlignement(alignmentFileName); + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + String onto1NamedGraph = "http://exmo.inrialpes.fr/connectors/onto1-graph"; + String onto2NamedGraph = "http://exmo.inrialpes.fr/connectors/onto2-graph"; + + //With named Graph on onto1 and onto2 + SPARQLSelectRendererVisitor renderer = new SPARQLSelectRendererVisitor(writer, onto1NamedGraph, onto2NamedGraph); + Properties properties = new Properties(); + renderer.init(properties); + alignment.render(renderer); + assertEquals(alignment.nbCells(), 1); + Enumeration<Cell> cells = alignment.getElements(); + Cell cell = cells.nextElement(); + + 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://xmlns.com/foaf/0.1/>\n" + + "SELECT DISTINCT ?s1 ?s2 \n" + + "WHERE {\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 rdf:type ns0:Personne .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 rdf:type ns1:Person .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 ns0:nom ?o1 .\n" + + "}\n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto2-graph> {\n" + + "?s2 ns1:givenName ?o1 .\n" + + "}\n" + + "MINUS { \n" + + "GRAPH <http://exmo.inrialpes.fr/connectors/onto1-graph> {\n" + + "?s1 ns0:nom ?o1 .\n" + + "?s1 ns0:nom ?o2 .\n" + + "}\n" + + "?s2 ns1:givenName ?o1 .\n" + + "FILTER(?s1 != ?s2 && ?o2 != ?o1 && NOT EXISTS {\n" + + "?s2 ns1:givenName ?o2 .\n" + + "}) \n" + + "} \n" + + "MINUS {\n" + + "?s1 ns0:nom ?o1 .\n" + + "?s2 ns1:givenName ?o1 .\n" + + "?s2 ns1:givenName ?o2 .\n" + + "FILTER(?s1 != ?s2 && ?o1 != ?o2 && NOT EXISTS {\n" + + "?s1 ns0:nom ?o2 .\n" + + "}) \n" + + "} \n" + + "FILTER(?s1 != ?s2)\n" + + "}\n"; + + String query = renderer.getQueryFromOnto1ToOnto2(cell); + Dataset dataset = DatasetFactory.createMem(); + dataset.addNamedModel(onto1NamedGraph, Utils.loadValues(new String[]{"equals_people_1.rdf"})); + dataset.addNamedModel(onto2NamedGraph, Utils.loadValues(new String[]{"equals_people_2.rdf"})); + Query selectQuery = QueryFactory.create(query); + String[] expectedS1 = { + "http://exmo.inrialpes.fr/connectors-data/people#alice_c1_1"}; + String[] expectedS2 = { + "http://exmo.inrialpes.fr/connectors-data/people#alice_c1_2",}; + HashMap<String, Collection<String>> allResultValues = Utils.getResultValues(QueryExecutionFactory.create(selectQuery, dataset).execSelect()); + Collection<String> resultValues = allResultValues.get("s1"); + assertEquals(resultValues.size(), expectedS1.length); + for (String expected : expectedS1) { + assertTrue(resultValues.contains(expected), "For expected : " + expected); + } + + resultValues = allResultValues.get("s2"); + assertEquals(resultValues.size(), expectedS2.length); + for (String expected : expectedS2) { + assertTrue(resultValues.contains(expected), "For expected : " + expected); + } + + //With from onto2ToOnto1 + query = renderer.getQueryFromOnto2ToOnto1(cell);//Where ?p1 is in onto2 + dataset = DatasetFactory.createMem(); + dataset.addNamedModel(onto1NamedGraph, Utils.loadValues(new String[]{"equals_people_1.rdf"})); + dataset.addNamedModel(onto2NamedGraph, Utils.loadValues(new String[]{"equals_people_2.rdf"})); + selectQuery = QueryFactory.create(query); + allResultValues = Utils.getResultValues(QueryExecutionFactory.create(selectQuery, dataset).execSelect()); + + resultValues = allResultValues.get("s1"); + assertEquals(resultValues.size(), expectedS1.length); + for (String expected : expectedS2) {//Change here + assertTrue(resultValues.contains(expected), "For expected : " + expected); + } + + resultValues = allResultValues.get("s2"); + assertEquals(resultValues.size(), expectedS2.length); + for (String expected : expectedS1) {//Change here + assertTrue(resultValues.contains(expected), "For expected : " + expected); + } + } // // @Test(groups = {"full", "impl", "raw"}, dependsOnMethods = {"QueryFromSimpleLinkkeyAndEquals"}) // public void QueryFromRelationLinkkeyAndEquals() throws Exception {