Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 67184adf authored by Jérôme Euzenat's avatar Jérôme Euzenat
Browse files

- reengineered Sparql construct generation

parent f7b5d136
No related branches found
No related tags found
No related merge requests found
/* /*
* $Id$ * $Id$
* *
* Copyright (C) INRIA, 2012-2013 * Copyright (C) INRIA, 2012-2014
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
...@@ -42,24 +42,26 @@ import java.util.List; ...@@ -42,24 +42,26 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor implements AlignmentVisitor{ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor implements AlignmentVisitor {
Alignment alignment = null; Alignment alignment = null;
Cell cell = null; Cell cell = null;
Hashtable<String,String> nslist = null; Hashtable<String,String> nslist = null;
boolean embedded = false; boolean embedded = false;
boolean split = false;
String splitdir = ""; boolean split = false; // split each query in a file
private String content_Corese = ""; String splitdir = ""; // directory where to put query files
private String GP1;
private String GP2; private String content_Corese = ""; // resulting string for Corese
private List<String> listGP1 = new ArrayList<String>();
private List<String> listGP2 = new ArrayList<String>();
public SPARQLConstructRendererVisitor(PrintWriter writer) { public SPARQLConstructRendererVisitor(PrintWriter writer) {
super(writer); super(writer);
} }
/**
* Initialises the parameters of the renderer
*/
public void init(Properties p) { public void init(Properties p) {
if ( p.getProperty( "embedded" ) != null if ( p.getProperty( "embedded" ) != null
&& !p.getProperty( "embedded" ).equals("") ) embedded = true; && !p.getProperty( "embedded" ).equals("") ) embedded = true;
...@@ -74,10 +76,8 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor ...@@ -74,10 +76,8 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor
split = ( p.getProperty( "split" ) != null && !p.getProperty( "split" ).equals("") ); split = ( p.getProperty( "split" ) != null && !p.getProperty( "split" ).equals("") );
if ( p.getProperty( "dir" ) != null && !p.getProperty( "dir" ).equals("") ) if ( p.getProperty( "dir" ) != null && !p.getProperty( "dir" ).equals("") )
splitdir = p.getProperty( "dir" )+"/"; splitdir = p.getProperty( "dir" )+"/";
if ( p.getProperty( "indent" ) != null ) if ( p.getProperty( "indent" ) != null ) INDENT = p.getProperty( "indent" );
INDENT = p.getProperty( "indent" ); if ( p.getProperty( "newline" ) != null ) NL = p.getProperty( "newline" );
if ( p.getProperty( "newline" ) != null )
NL = p.getProperty( "newline" );
} }
public void visit( Alignment align ) throws AlignmentException { public void visit( Alignment align ) throws AlignmentException {
...@@ -91,8 +91,7 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor ...@@ -91,8 +91,7 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor
throw new AlignmentException("SPARQLSELECTRenderer: cannot render simple alignment. Need an EDOALAlignment", alex ); throw new AlignmentException("SPARQLSELECTRenderer: cannot render simple alignment. Need an EDOALAlignment", alex );
} }
} }
content_Corese = ""; content_Corese = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + NL;
content_Corese += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + NL;
content_Corese += "<!DOCTYPE rdf:RDF [" + NL; content_Corese += "<!DOCTYPE rdf:RDF [" + NL;
content_Corese += "<!ENTITY rdf \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" + NL; content_Corese += "<!ENTITY rdf \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" + NL;
content_Corese += "<!ENTITY rdfs \"http://www.w3.org/2000/01/rdf-schema#\">" + NL; content_Corese += "<!ENTITY rdfs \"http://www.w3.org/2000/01/rdf-schema#\">" + NL;
...@@ -113,159 +112,24 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor ...@@ -113,159 +112,24 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor
public void visit( Cell cell ) throws AlignmentException { public void visit( Cell cell ) throws AlignmentException {
if ( subsumedInvocableMethod( this, cell, Cell.class ) ) return; if ( subsumedInvocableMethod( this, cell, Cell.class ) ) return;
// default behaviour // default behaviour
this.cell = cell; this.cell = cell;
String query=""; URI u1 = cell.getObject1AsURI( alignment );
String query_IgnoreErrors=""; URI u2 = cell.getObject2AsURI( alignment );
String query_weaken="";
String tmp="";
URI u1 = cell.getObject1AsURI(alignment);
URI u2 = cell.getObject2AsURI(alignment);
if ( ( u1 != null && u2 != null) if ( ( u1 != null && u2 != null)
|| alignment.getLevel().startsWith("2EDOAL") ){ //expensive test || alignment.getLevel().startsWith("2EDOAL") ){ //expensive test
// Here the generation is dependent on global variables
resetVariables("s", "o"); resetVariables("s", "o");
((Expression)(cell.getObject1())).accept( this ); ((Expression)(cell.getObject1())).accept( this );
GP1 = getGP(); String GP1 = getGP();
listGP1 = new ArrayList<String>(getBGP()); List<String> listGP1 = new ArrayList<String>(getBGP());
resetVariables("s", "o"); resetVariables("s", "o");
((Expression)(cell.getObject2())).accept( this ); ((Expression)(cell.getObject2())).accept( this );
GP2 = getGP(); String GP2 = getGP();
listGP2 = new ArrayList<String>(getBGP()); List<String> listGP2 = new ArrayList<String>(getBGP());
// End of global variables
if( !GP1.contains("UNION") && !GP1.contains("FILTER") ){
for ( Enumeration<String> e = prefixList.keys() ; e.hasMoreElements(); ) {
String k = e.nextElement();
query += "PREFIX "+prefixList.get(k)+":<"+k+">"+NL;
}
query += "CONSTRUCT {"+NL;
query += GP1;
query += "}"+NL;
query += "WHERE {"+NL;
query += GP2;
query += "}"+NL;
query_weaken = query;
content_Corese += "<rule>" + NL;
content_Corese += "<body>" + NL;
content_Corese += "<![CDATA[" + NL;
content_Corese += query;
content_Corese += "]]>" + NL;
content_Corese += "</body>" + NL;
content_Corese += "</rule>" + NL + NL;
} else {
Iterator<String> list = listGP1.iterator();
while ( list.hasNext() ) {
String str = list.next();
if ( !str.contains("UNION") && !str.contains("FILTER") ) {
tmp += str;
}
}
if ( !tmp.equals("") ) {
for ( Enumeration<String> e = prefixList.keys() ; e.hasMoreElements(); ) {
String k = e.nextElement();
query_weaken += "PREFIX "+prefixList.get(k)+":<"+k+">"+NL;
}
query_weaken += "CONSTRUCT {"+NL;
query_weaken += tmp;
query_weaken += "}"+NL;
query_weaken += "WHERE {"+NL;
query_weaken += GP2;
query_weaken += "}"+NL;
}
}
for ( Enumeration<String> e = prefixList.keys() ; e.hasMoreElements(); ) {
String k = e.nextElement();
query_IgnoreErrors += "PREFIX "+prefixList.get(k)+":<"+k+">"+NL;
}
query_IgnoreErrors += "CONSTRUCT {"+NL;
query_IgnoreErrors += GP1;
query_IgnoreErrors += "}"+NL;
query_IgnoreErrors += "WHERE {"+NL;
query_IgnoreErrors += GP2;
query_IgnoreErrors += "}"+NL;
if ( corese ) return;
if( split ) {
if ( ignoreerrors ) {
createQueryFile( splitdir, query_IgnoreErrors );
} else if ( weakens ) {
createQueryFile( splitdir, query_weaken );
} else {
createQueryFile( splitdir, query );
}
} else if ( ignoreerrors ) {
writer.println( query_IgnoreErrors );
} else if ( weakens ) {
writer.println( query_weaken );
} else {
writer.println( query );
}
query="";
query_weaken = "";
query_IgnoreErrors = "";
tmp = "";
if( !GP2.contains("UNION") && !GP2.contains("FILTER") ){ generateConstruct( GP1, listGP1, GP2, listGP2 );
for ( Enumeration<String> e = prefixList.keys() ; e.hasMoreElements(); ) { generateConstruct( GP2, listGP2, GP1, listGP1 );
String k = e.nextElement();
query += "PREFIX "+prefixList.get(k)+":<"+k+">"+NL;
}
query += "CONSTRUCT {"+NL;
query += GP2;
query += "}"+NL;
query += "WHERE {"+NL;
query += GP1;
query += "}"+NL;
query_weaken = query;
content_Corese += "<rule>" + NL;
content_Corese += "<body>" + NL;
content_Corese += "<![CDATA[" + NL;
content_Corese += query;
content_Corese += "]]>" + NL;
content_Corese += "</body>" + NL;
content_Corese += "</rule>" + NL + NL;
} else {
Iterator<String> list = listGP2.iterator();
while ( list.hasNext() ) {
String str = list.next();
if ( !str.contains("UNION") && !str.contains("FILTER") ) {
tmp += str;
}
}
if ( !tmp.equals("") ) {
for ( Enumeration<String> e = prefixList.keys() ; e.hasMoreElements(); ) {
String k = e.nextElement();
query_weaken += "PREFIX "+prefixList.get(k)+":<"+k+">"+NL;
}
query_weaken += "CONSTRUCT {"+NL;
query_weaken += tmp;
query_weaken += "}"+NL;
query_weaken += "WHERE {"+NL;
query_weaken += GP1;
query_weaken += "}"+NL;
}
}
for ( Enumeration<String> e = prefixList.keys() ; e.hasMoreElements(); ) {
String k = e.nextElement();
query_IgnoreErrors += "PREFIX "+prefixList.get(k)+":<"+k+">"+NL;
}
query_IgnoreErrors += "CONSTRUCT {"+NL;
query_IgnoreErrors += GP2;
query_IgnoreErrors += "}"+NL;
query_IgnoreErrors += "WHERE {"+NL;
query_IgnoreErrors += GP1;
query_IgnoreErrors += "}"+NL;
if ( corese ) return;
if( split ) {
if ( ignoreerrors ) {
createQueryFile( splitdir, query_IgnoreErrors );
} else if ( weakens ) {
createQueryFile( splitdir, query_weaken );
} else {
createQueryFile( splitdir, query );
}
} else if ( ignoreerrors ) {
writer.println( query_IgnoreErrors );
} else if ( weakens ) {
writer.println( query_weaken );
} else { writer.println( query ); }
} }
} }
...@@ -274,4 +138,43 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor ...@@ -274,4 +138,43 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor
// default behaviour // default behaviour
// rel.write( writer ); // rel.write( writer );
} }
protected void generateConstruct( String GP1, List<String> listGP1, String GP2, List<String> listGP2 ) {
String query = "";
if ( !GP1.contains("UNION") && !GP1.contains("FILTER") ) {
query = createConstruct( GP1, GP2 );
if ( corese ) content_Corese += createCoreseQuery( query );
} else if ( weakens ) {
String tmp = "";
for ( String str : listGP1 ) {
if ( !str.contains("UNION") && !str.contains("FILTER") ) tmp += str;
}
if ( !tmp.equals("") ) query = createConstruct( tmp, GP2 );
} else if ( ignoreerrors ) {
query = createConstruct( GP1, GP2 );
}
if ( corese ) return;
if ( split ) {
createQueryFile( splitdir, query );
} else {
writer.println( query );
}
}
protected String createConstruct( String GP1, String GP2 ) {
return createPrefix( prefixList )+"CONSTRUCT {"+NL+GP1+"}"+NL+"WHERE {"+NL+GP2+"}"+NL;
}
protected String createCoreseQuery( String query ) {
return "<rule>"+NL+"<body>"+NL+"<![CDATA["+NL+query+"]]>"+NL+"</body>"+NL+"</rule>"+NL+NL;
}
protected String createPrefix( Hashtable<String,String> preflist ) {
String result = "";
for ( String k : preflist.keySet() ) {
result += "PREFIX "+preflist.get(k)+":<"+k+">"+NL;
}
return result;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment