From 67184adfc121d682d32afca105f31ac3b7aa2e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr> Date: Mon, 2 Jun 2014 09:51:19 +0000 Subject: [PATCH] - reengineered Sparql construct generation --- .../SPARQLConstructRendererVisitor.java | 229 +++++------------- 1 file changed, 66 insertions(+), 163 deletions(-) diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java index 3f810dfa..ddec0dd8 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/SPARQLConstructRendererVisitor.java @@ -1,7 +1,7 @@ /* * $Id$ * - * Copyright (C) INRIA, 2012-2013 + * Copyright (C) INRIA, 2012-2014 * * 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 @@ -42,24 +42,26 @@ import java.util.List; import java.util.Properties; import java.util.Set; -public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor implements AlignmentVisitor{ +public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor implements AlignmentVisitor { Alignment alignment = null; Cell cell = null; Hashtable<String,String> nslist = null; + boolean embedded = false; - boolean split = false; - String splitdir = ""; - private String content_Corese = ""; - private String GP1; - private String GP2; - private List<String> listGP1 = new ArrayList<String>(); - private List<String> listGP2 = new ArrayList<String>(); + + boolean split = false; // split each query in a file + String splitdir = ""; // directory where to put query files + + private String content_Corese = ""; // resulting string for Corese public SPARQLConstructRendererVisitor(PrintWriter writer) { super(writer); - } - + } + + /** + * Initialises the parameters of the renderer + */ public void init(Properties p) { if ( p.getProperty( "embedded" ) != null && !p.getProperty( "embedded" ).equals("") ) embedded = true; @@ -74,10 +76,8 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor split = ( p.getProperty( "split" ) != null && !p.getProperty( "split" ).equals("") ); if ( p.getProperty( "dir" ) != null && !p.getProperty( "dir" ).equals("") ) splitdir = p.getProperty( "dir" )+"/"; - if ( p.getProperty( "indent" ) != null ) - INDENT = p.getProperty( "indent" ); - if ( p.getProperty( "newline" ) != null ) - NL = p.getProperty( "newline" ); + if ( p.getProperty( "indent" ) != null ) INDENT = p.getProperty( "indent" ); + if ( p.getProperty( "newline" ) != null ) NL = p.getProperty( "newline" ); } public void visit( Alignment align ) throws AlignmentException { @@ -91,8 +91,7 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor 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 += "<!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; @@ -113,159 +112,24 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor public void visit( Cell cell ) throws AlignmentException { if ( subsumedInvocableMethod( this, cell, Cell.class ) ) return; // default behaviour - this.cell = cell; - String query=""; - String query_IgnoreErrors=""; - String query_weaken=""; - String tmp=""; - URI u1 = cell.getObject1AsURI(alignment); - URI u2 = cell.getObject2AsURI(alignment); + this.cell = cell; + URI u1 = cell.getObject1AsURI( alignment ); + URI u2 = cell.getObject2AsURI( alignment ); if ( ( u1 != null && u2 != null) || alignment.getLevel().startsWith("2EDOAL") ){ //expensive test + // Here the generation is dependent on global variables resetVariables("s", "o"); ((Expression)(cell.getObject1())).accept( this ); - GP1 = getGP(); - listGP1 = new ArrayList<String>(getBGP()); + String GP1 = getGP(); + List<String> listGP1 = new ArrayList<String>(getBGP()); resetVariables("s", "o"); ((Expression)(cell.getObject2())).accept( this ); - GP2 = getGP(); - listGP2 = new ArrayList<String>(getBGP()); - - 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 = ""; + String GP2 = getGP(); + List<String> listGP2 = new ArrayList<String>(getBGP()); + // End of global variables - if( !GP2.contains("UNION") && !GP2.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 += 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 ); } + generateConstruct( GP1, listGP1, GP2, listGP2 ); + generateConstruct( GP2, listGP2, GP1, listGP1 ); } } @@ -274,4 +138,43 @@ public class SPARQLConstructRendererVisitor extends GraphPatternRendererVisitor // default behaviour // 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; + } + } -- GitLab