Skip to content
Snippets Groups Projects
Commit adfb95c6 authored by rachid.yahiaoui's avatar rachid.yahiaoui
Browse files

AutoScan Mode [ Experimental Stage ]

parent aaadfbd8
No related branches found
Tags coby_1.8_08_04_2021
No related merge requests found
......@@ -9,6 +9,7 @@ import java.util.regex.Pattern ;
import org.apache.logging.log4j.Logger ;
import org.apache.logging.log4j.LogManager ;
import it.unibz.inf.ontop.model.term.Variable ;
import static java.util.stream.Collectors.toSet;
/**
*
......@@ -63,6 +64,13 @@ public class Mapping {
unmodifiableSet (
variablesMapping ) ;
}
public Set<String> getVariablesMappingAsString() {
return variablesMapping.stream()
.map( variable -> variable.getName() )
.collect( toSet()) ;
}
public String getNodeId() {
return nodeId ;
......
package dataRiv.core.scanner;
/**
*
* @author ryahiaoui
*/
import dataRiv.core.Mapping;
import dataRiv.core.ObdaManager;
import dataRiv.core.scanner.SqlAnalyzer.DB_KIND;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import java.util.Properties;
/**
*
* @author ryahiaoui
*/
public class Main {
public static void main( String[] args ) throws Exception {
String obdaPath = "/home/ryahiaoui/Téléchargements/coby/COBY_1.8_01_04_2021/05_04_21/coby/coby_bin/coby_bin-01/pipeline/SI/FORET/output/01_mappings/0_mapping_CSV_CarbonDioxide.obda" ;
DriverManager.setLoginTimeout( 10 ) ;
Properties connecProperties = new Properties() ;
connecProperties.setProperty("user" , "ryahiaoui" ) ;
connecProperties.setProperty("password", "yahiaoui" ) ;
connecProperties.setProperty("connectTimeout", "10" ) ; // in seconds
connecProperties.setProperty("socketTimeout" , "0" ) ; // in seconds // 0 Means Disable
connecProperties.setProperty("ReadOnly" , "true" ) ;
Connection connection = DriverManager.getConnection ( "jdbc:postgresql://127.0.0.1/foret?sendBufferSize=5000" ,
connecProperties ) ;
List<Mapping> obdaMappings = ObdaManager.loadOBDA( obdaPath, null ) ;
/** Use AutoScan Mode to propagate the slq filters through the graph. **/
obdaMappings = ObdaManagerAutoScan.scanMappings( obdaMappings, connection , DB_KIND.POSTGRES ) ;
obdaMappings.forEach( mapping -> System.out.println( mapping.getId() + " : " + mapping.getQuery() + "\n") ) ;
// System.out.println( "obdaMappings = " + obdaMappings );
// ObdaManagerAutoScan.termIri.forEach( (k,v) -> {
//
// System.out.println( k + " : " + v );
// });
}
}
......@@ -22,32 +22,37 @@ public class ObdaManagerAutoScan {
public static List<Mapping> scanMappings( List<Mapping> mappings , Connection cnn , DB_KIND dbKind ) throws Exception {
Mapping root = locateRootMapping( mappings ) ;
Mapping root = locateRootMapping( mappings ) ;
QueryFilter queryFilter = new QueryFilter() ;
QueryFilter queryFilter = new QueryFilter() ;
/** Extract Filters from the ROOT MAPPING . **/
SqlAnalyzer.buildSqlQuery( cnn, root.getQuery(), queryFilter, dbKind ) ;
/** Extract Filters from the ROOT MAPPING . **/
SqlAnalyzer.buildSqlQuery( cnn ,
root.getQuery() ,
queryFilter ,
dbKind ,
root.getVariablesMappingAsString()) ;
Stack<Mapping> mappingStack = new Stack<>() ;
Stack<Mapping> mappingStack = new Stack<>() ;
if ( root.getVariablesMapping() == null ) return null ;
root.getChildsUris().forEach( childUri -> {
Mapping map = findMappingByUri( mappings , childUri ) ;
if ( map != null ) mappingStack.push( map ) ;
if ( root.getVariablesMapping() == null ) return null ;
root.getChildsUris().forEach( childUri -> {
Mapping map = findMappingByUri( mappings , childUri ) ;
if ( map != null ) mappingStack.push( map ) ;
} ) ;
while ( ! mappingStack.empty() ) {
while ( ! mappingStack.empty() ) {
Mapping current = mappingStack.pop() ;
Mapping current = mappingStack.pop() ;
if( current != null ) {
String scannedSqlQuery = SqlAnalyzer.buildSqlQuery( cnn ,
current.getQuery() ,
queryFilter ,
dbKind ) ;
dbKind ,
current.getVariablesMappingAsString() ) ;
current.setSqlQuery( scannedSqlQuery ) ;
if( current.getChildsUris() != null &&
......@@ -78,10 +83,10 @@ public class ObdaManagerAutoScan {
.flatMap(Set::stream)
.collect(toSet() ) ;
rootUris.addAll( childsUris ) ;
mappings.forEach( (Mapping mapping ) -> {
rootUris.removeAll( mapping.getChildsUris() ) ;
rootUris.addAll( childsUris ) ;
mappings.forEach( (Mapping mapping ) -> {
rootUris.removeAll( mapping.getChildsUris() ) ;
} ) ;
if( rootUris.isEmpty() ) {
......
......@@ -6,7 +6,7 @@ package dataRiv.core.scanner ;
* @author ryahiaoui
*/
import java.util.Set ;
import java.sql.Connection ;
import java.io.StringReader ;
import java.sql.SQLException ;
......@@ -37,7 +37,11 @@ public class SqlAnalyzer {
private static final Logger LOGGER = Logger.getLogger( SqlAnalyzer.class.getName() ) ;
public static String buildSqlQuery( Connection cnn , String sqlQuery , QueryFilter queryFilter , DB_KIND dbKind ) throws Exception {
public static String buildSqlQuery( Connection cnn ,
String sqlQuery ,
QueryFilter queryFilter ,
DB_KIND dbKind ,
Set<String> variableMappings ) throws Exception {
try ( PreparedStatement ps = cnn.prepareStatement(sqlQuery) ) {
......@@ -51,7 +55,10 @@ public class SqlAnalyzer {
String labelColumn = metaData.getColumnLabel(i) ;
String fullColumnName = getFullNameSqlParamAt ( sqlQuery, i -1 ) ;
if(fullColumnName == null ) fullColumnName = labelColumn ;
if( fullColumnName == null ) fullColumnName = labelColumn ;
if( ! variableMappings.contains( labelColumn )) continue ;
if ( queryFilter.containsKey( "{" + labelColumn + "}" ) ) {
String filter = queryFilter.getFilterByKey( "{" + labelColumn + "}" ) ;
......@@ -117,7 +124,7 @@ public class SqlAnalyzer {
String query ,
DB_KIND dbKind ) {
if ( null != dbKind ) {
if ( null != dbKind ) {
switch ( dbKind ) {
......@@ -163,7 +170,7 @@ public class SqlAnalyzer {
} catch ( JSQLParserException ex ) {
throw new RuntimeException( ex + "\n" + " Query : " + sqlQuery +
"\n" + "Clause : " + clause ) ;
"\n" + "Clause : " + clause ) ;
}
}
}
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