diff --git a/pom.xml b/pom.xml index 480f725..2236126 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ 4.0.0 - gr.ekt + gr.seab r2rml 0.3 jar @@ -9,6 +9,7 @@ true 3.2.1.RELEASE + UTF-8 @@ -26,12 +27,13 @@ org.apache.maven.plugins maven-jar-plugin + 2.4 true - gr.ekt.r2rml.beans.Main + gr.seab.r2rml.beans.Main @@ -140,8 +142,8 @@ Nikolaos Konstantinou nkons at cn.ntua.gr http://www.cn.ntua.gr/~nkons/ - National Technical University of Athens - http://www.ntua.gr + Hellenic Academic Libraries Link/National Technical University of Athens (HEAL-Link/NTUA) + http://www.seab.gr committer diff --git a/r2rml-parser.bat b/r2rml-parser.bat index 7706f7f..71573d6 100644 --- a/r2rml-parser.bat +++ b/r2rml-parser.bat @@ -1,3 +1,3 @@ @echo off echo This is R2RML Parser 0.3-alpha -java -cp "./*;./lib/*;" gr.ekt.r2rml.beans.Main %1 \ No newline at end of file +java -cp "./*;./lib/*;" gr.seab.r2rml.beans.Main %1 \ No newline at end of file diff --git a/src/main/java/gr/ekt/r2rml/beans/Database.java b/src/main/java/gr/seab/r2rml/beans/Database.java similarity index 93% rename from src/main/java/gr/ekt/r2rml/beans/Database.java rename to src/main/java/gr/seab/r2rml/beans/Database.java index 2cf7789..225be2d 100644 --- a/src/main/java/gr/ekt/r2rml/beans/Database.java +++ b/src/main/java/gr/seab/r2rml/beans/Database.java @@ -1,29 +1,29 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.beans; - -import java.sql.Connection; -import java.sql.ResultSet; - -import com.hp.hpl.jena.sdb.Store; -import com.hp.hpl.jena.sdb.sql.SDBConnection; - -public interface Database { - - public Connection openConnection(); - - public SDBConnection openJenaConnection(); - - public ResultSet query(String query); - - public Store jenaStore(); -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.beans; + +import java.sql.Connection; +import java.sql.ResultSet; + +import com.hp.hpl.jena.sdb.Store; +import com.hp.hpl.jena.sdb.sql.SDBConnection; + +public interface Database { + + public Connection openConnection(); + + public SDBConnection openJenaConnection(); + + public ResultSet query(String query); + + public Store jenaStore(); +} diff --git a/src/main/java/gr/ekt/r2rml/beans/DatabaseImpl.java b/src/main/java/gr/seab/r2rml/beans/DatabaseImpl.java similarity index 96% rename from src/main/java/gr/ekt/r2rml/beans/DatabaseImpl.java rename to src/main/java/gr/seab/r2rml/beans/DatabaseImpl.java index 10da6d0..8d70b93 100644 --- a/src/main/java/gr/ekt/r2rml/beans/DatabaseImpl.java +++ b/src/main/java/gr/seab/r2rml/beans/DatabaseImpl.java @@ -1,188 +1,188 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.beans; - - -import java.io.FileInputStream; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Properties; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.hp.hpl.jena.sdb.SDBFactory; -import com.hp.hpl.jena.sdb.Store; -import com.hp.hpl.jena.sdb.StoreDesc; -import com.hp.hpl.jena.sdb.sql.SDBConnection; -import com.hp.hpl.jena.sdb.store.DatabaseType; -import com.hp.hpl.jena.sdb.store.LayoutType; - -/** - * Database functions - * @author nkons - * - */ -public class DatabaseImpl implements Database { - - private static final Logger log = LoggerFactory.getLogger(Database.class); - - private Connection connection; - - private SDBConnection jenaConnection; - - private Store store; - - private Properties properties = new Properties(); - - private String propertiesFilename; - - private Util util; - - public DatabaseImpl() { - - } - - /** - * - */ - public DatabaseImpl(String propertiesFilename) { - try { - properties.load(new FileInputStream(propertiesFilename)); - } catch (Exception e) { - e.printStackTrace(); - System.exit(0); - } - } - - public Connection openConnection() { - log.info("Establishing source (relational) connection."); - if (connection == null) { - try { - String driver = properties.getProperty("db.driver"); - Class.forName(driver); - String databaseType = util.findDatabaseType(driver); - String dbConnectionString = "jdbc:" + databaseType + "://" + properties.getProperty("db.host") + ":" + properties.getProperty("db.port") + "/" + properties.getProperty("db.name"); - connection = DriverManager.getConnection(dbConnectionString, properties.getProperty("db.login"), properties.getProperty("db.password")); - - log.info("Established source (relational) connection."); - return connection; - } catch (Exception e) { - e.printStackTrace(); - System.exit(0); - } - } else { - return connection; - } - return null; - } - - public SDBConnection openJenaConnection() { - log.info("Establishing target (jena/triplestore) connection."); - if (jenaConnection == null) { - try { - - String jenaDriver = properties.getProperty("jena.db.driver"); - Class.forName(jenaDriver); - String jenaDatabaseType = util.findDatabaseType(jenaDriver); - String jenaConnectionString = "jdbc:" + jenaDatabaseType + "://" + properties.getProperty("jena.db.host") + ":" + properties.getProperty("jena.db.port") + "/" + properties.getProperty("jena.db.name"); - log.info("jena repository at " + jenaConnectionString); - - jenaConnection = new SDBConnection(jenaConnectionString, properties.getProperty("jena.db.login"), properties.getProperty("jena.db.password")) ; - - StoreDesc storeDesc = null; - if ("postgresql".equals(jenaDatabaseType)) { - storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.PostgreSQL); - } else if ("mysql".equals(jenaDatabaseType)) { - storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.MySQL); - } - store = SDBFactory.connectStore(jenaConnection, storeDesc); - - if ("true".equals(properties.getProperty("jena.cleanDbOnStartup"))) { - log.info("Cleaning up database"); - store.getTableFormatter().create(); - } - - log.info("Established target (jena/triplestore) connection."); - - return jenaConnection; - } catch (Exception e) { - //e.printStackTrace(); - log.error(e.getMessage()); - System.exit(0); - } - } else { - return jenaConnection; - } - return null; - } - - public Store jenaStore() { - try { - @SuppressWarnings("unused") - long storeSize = store.getSize(); - } catch (Exception e) { - log.info("Initializing Jena store."); - store.getTableFormatter().create(); - } - return store; - } - - public ResultSet query(String query) { - ResultSet result = null; - - try { - if (connection == null) openConnection(); - - if (!StringUtils.containsIgnoreCase(query, "limit") && properties.containsKey("db.limit")) { - query += " LIMIT " + properties.getProperty("db.limit"); - } - //log.info(query); - //PreparedStatement preparedStatement = connection.prepareStatement(query); - Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); - result = statement.executeQuery(query); - } catch (SQLException e) { - e.printStackTrace(); - System.exit(0); - } - return result; - } - - public String getPropertiesFilename() { - return propertiesFilename; - } - - public void setPropertiesFilename(String propertiesFilename) { - this.propertiesFilename = propertiesFilename; - } - - public Util getUtil() { - return util; - } - - public void setUtil(Util util) { - this.util = util; - } - - public Properties getProperties() { - return properties; - } - - public void setProperties(Properties properties) { - this.properties = properties; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.beans; + + +import java.io.FileInputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.hp.hpl.jena.sdb.SDBFactory; +import com.hp.hpl.jena.sdb.Store; +import com.hp.hpl.jena.sdb.StoreDesc; +import com.hp.hpl.jena.sdb.sql.SDBConnection; +import com.hp.hpl.jena.sdb.store.DatabaseType; +import com.hp.hpl.jena.sdb.store.LayoutType; + +/** + * Database functions + * @author nkons + * + */ +public class DatabaseImpl implements Database { + + private static final Logger log = LoggerFactory.getLogger(Database.class); + + private Connection connection; + + private SDBConnection jenaConnection; + + private Store store; + + private Properties properties = new Properties(); + + private String propertiesFilename; + + private Util util; + + public DatabaseImpl() { + + } + + /** + * + */ + public DatabaseImpl(String propertiesFilename) { + try { + properties.load(new FileInputStream(propertiesFilename)); + } catch (Exception e) { + e.printStackTrace(); + System.exit(0); + } + } + + public Connection openConnection() { + log.info("Establishing source (relational) connection."); + if (connection == null) { + try { + String driver = properties.getProperty("db.driver"); + Class.forName(driver); + String databaseType = util.findDatabaseType(driver); + String dbConnectionString = "jdbc:" + databaseType + "://" + properties.getProperty("db.host") + ":" + properties.getProperty("db.port") + "/" + properties.getProperty("db.name"); + connection = DriverManager.getConnection(dbConnectionString, properties.getProperty("db.login"), properties.getProperty("db.password")); + + log.info("Established source (relational) connection."); + return connection; + } catch (Exception e) { + e.printStackTrace(); + System.exit(0); + } + } else { + return connection; + } + return null; + } + + public SDBConnection openJenaConnection() { + log.info("Establishing target (jena/triplestore) connection."); + if (jenaConnection == null) { + try { + + String jenaDriver = properties.getProperty("jena.db.driver"); + Class.forName(jenaDriver); + String jenaDatabaseType = util.findDatabaseType(jenaDriver); + String jenaConnectionString = "jdbc:" + jenaDatabaseType + "://" + properties.getProperty("jena.db.host") + ":" + properties.getProperty("jena.db.port") + "/" + properties.getProperty("jena.db.name"); + log.info("jena repository at " + jenaConnectionString); + + jenaConnection = new SDBConnection(jenaConnectionString, properties.getProperty("jena.db.login"), properties.getProperty("jena.db.password")) ; + + StoreDesc storeDesc = null; + if ("postgresql".equals(jenaDatabaseType)) { + storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.PostgreSQL); + } else if ("mysql".equals(jenaDatabaseType)) { + storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.MySQL); + } + store = SDBFactory.connectStore(jenaConnection, storeDesc); + + if ("true".equals(properties.getProperty("jena.cleanDbOnStartup"))) { + log.info("Cleaning up database"); + store.getTableFormatter().create(); + } + + log.info("Established target (jena/triplestore) connection."); + + return jenaConnection; + } catch (Exception e) { + //e.printStackTrace(); + log.error(e.getMessage()); + System.exit(0); + } + } else { + return jenaConnection; + } + return null; + } + + public Store jenaStore() { + try { + @SuppressWarnings("unused") + long storeSize = store.getSize(); + } catch (Exception e) { + log.info("Initializing Jena store."); + store.getTableFormatter().create(); + } + return store; + } + + public ResultSet query(String query) { + ResultSet result = null; + + try { + if (connection == null) openConnection(); + + if (!StringUtils.containsIgnoreCase(query, "limit") && properties.containsKey("db.limit")) { + query += " LIMIT " + properties.getProperty("db.limit"); + } + //log.info(query); + //PreparedStatement preparedStatement = connection.prepareStatement(query); + Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); + result = statement.executeQuery(query); + } catch (SQLException e) { + e.printStackTrace(); + System.exit(0); + } + return result; + } + + public String getPropertiesFilename() { + return propertiesFilename; + } + + public void setPropertiesFilename(String propertiesFilename) { + this.propertiesFilename = propertiesFilename; + } + + public Util getUtil() { + return util; + } + + public void setUtil(Util util) { + this.util = util; + } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/beans/Generator.java b/src/main/java/gr/seab/r2rml/beans/Generator.java similarity index 98% rename from src/main/java/gr/ekt/r2rml/beans/Generator.java rename to src/main/java/gr/seab/r2rml/beans/Generator.java index eb09dfb..022aa49 100644 --- a/src/main/java/gr/ekt/r2rml/beans/Generator.java +++ b/src/main/java/gr/seab/r2rml/beans/Generator.java @@ -9,14 +9,14 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. */ -package gr.ekt.r2rml.beans; +package gr.seab.r2rml.beans; -import gr.ekt.r2rml.entities.LogicalTableMapping; -import gr.ekt.r2rml.entities.MappingDocument; -import gr.ekt.r2rml.entities.PredicateObjectMap; -import gr.ekt.r2rml.entities.Template; -import gr.ekt.r2rml.entities.TermType; -import gr.ekt.r2rml.entities.sql.SelectQuery; +import gr.seab.r2rml.entities.LogicalTableMapping; +import gr.seab.r2rml.entities.MappingDocument; +import gr.seab.r2rml.entities.PredicateObjectMap; +import gr.seab.r2rml.entities.Template; +import gr.seab.r2rml.entities.TermType; +import gr.seab.r2rml.entities.sql.SelectQuery; import java.io.File; import java.io.FileNotFoundException; @@ -500,7 +500,10 @@ public void createTriples(MappingDocument mappingDocument) { } } tripleCount++; - if (tripleCount % 1000 == 0) log.info("At " + tripleCount + " triples"); + if (tripleCount % 1000 == 0) { + log.info("At " + tripleCount + " triples"); + System.out.println("At " + tripleCount + " triples"); + } } rs.close(); diff --git a/src/main/java/gr/ekt/r2rml/beans/Main.java b/src/main/java/gr/seab/r2rml/beans/Main.java similarity index 90% rename from src/main/java/gr/ekt/r2rml/beans/Main.java rename to src/main/java/gr/seab/r2rml/beans/Main.java index 19623d1..40162a1 100644 --- a/src/main/java/gr/ekt/r2rml/beans/Main.java +++ b/src/main/java/gr/seab/r2rml/beans/Main.java @@ -1,70 +1,72 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.beans; - -import gr.ekt.r2rml.entities.MappingDocument; - -import java.io.File; -import java.util.Calendar; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class Main { - private static final Logger log = LoggerFactory.getLogger(Main.class); - - public static void main(String[] args) { - Calendar c0 = Calendar.getInstance(); - long t0 = c0.getTimeInMillis(); - String appContextFile = "app-context.xml"; - if (args.length > 0) { - File f = new File(args[0]); - if (f.exists()) { - log.info("Spring context descriptor set to " + args[0]); - appContextFile = args[0]; - } else { - log.info("File " + args[0] + " not in classpath, using app-context.xml instead"); - } - } else { - log.info("Spring context file not provided, using app-context.xml"); - } - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(appContextFile); - - Parser parser = (Parser) context.getBean("parser"); - MappingDocument mappingDocument = parser.parse(); - - mappingDocument.getTimestamps().add(t0); //0 Started - mappingDocument.getTimestamps().add(Calendar.getInstance().getTimeInMillis()); //1 Finished parsing. Starting generating result model. - - Generator generator = (Generator) context.getBean("generator"); - generator.setProperties(parser.getProperties()); - generator.setResultModel(parser.getResultModel()); - - //Actually do the output - generator.createTriples(mappingDocument); - - context.close(); - Calendar c1 = Calendar.getInstance(); - long t1 = c1.getTimeInMillis(); - log.info("Finished in " + (t1 - t0) + " milliseconds. Done."); - mappingDocument.getTimestamps().add(Calendar.getInstance().getTimeInMillis()); //5 - - //output the result -// for (int i = 0; i < mappingDocument.getTimestamps().size(); i++) { -// if (i > 1) { -// System.out.println((mappingDocument.getTimestamps().get(i).longValue() - mappingDocument.getTimestamps().get(i - 1).longValue())); -// } -// } -// System.out.println("Parse. Generate in memory. Dump to disk/database. Log. - Alltogether: " + String.valueOf(mappingDocument.getTimestamps().get(5).longValue() - mappingDocument.getTimestamps().get(0).longValue())); - } -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.beans; + +import gr.seab.r2rml.entities.MappingDocument; + +import java.io.File; +import java.util.Calendar; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class Main { + private static final Logger log = LoggerFactory.getLogger(Main.class); + + public static void main(String[] args) { + Calendar c0 = Calendar.getInstance(); + long t0 = c0.getTimeInMillis(); + String appContextFile = "app-context.xml"; + if (args.length > 0) { + File f = new File(args[0]); + if (f.exists()) { + log.info("Spring context descriptor set to " + args[0]); + appContextFile = args[0]; + } else { + log.info("File " + args[0] + " not in classpath, using app-context.xml instead"); + } + } else { + log.info("Spring context file not provided, using app-context.xml"); + } + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(appContextFile); + + Parser parser = (Parser) context.getBean("parser"); + MappingDocument mappingDocument = parser.parse(); + + mappingDocument.getTimestamps().add(t0); //0 Started + mappingDocument.getTimestamps().add(Calendar.getInstance().getTimeInMillis()); //1 Finished parsing. Starting generating result model. + + Generator generator = (Generator) context.getBean("generator"); + generator.setProperties(parser.getProperties()); + generator.setResultModel(parser.getResultModel()); + + //Actually do the output + generator.createTriples(mappingDocument); + + context.close(); + Calendar c1 = Calendar.getInstance(); + long t1 = c1.getTimeInMillis(); + log.info("Finished in " + (t1 - t0) + " milliseconds. Done."); + mappingDocument.getTimestamps().add(Calendar.getInstance().getTimeInMillis()); //5 Finished. + //log.info("5 Finished."); + + //output the result +// for (int i = 0; i < mappingDocument.getTimestamps().size(); i++) { +// if (i > 0) { +// System.out.println((mappingDocument.getTimestamps().get(i).longValue() - mappingDocument.getTimestamps().get(i - 1).longValue())); +// } +// } +// System.out.println("Parse. Generate in memory. Dump to disk/database. Log. - Alltogether: " + String.valueOf(mappingDocument.getTimestamps().get(5).longValue() - mappingDocument.getTimestamps().get(0).longValue())); + System.out.println("Done."); + } +} diff --git a/src/main/java/gr/ekt/r2rml/beans/Parser.java b/src/main/java/gr/seab/r2rml/beans/Parser.java similarity index 96% rename from src/main/java/gr/ekt/r2rml/beans/Parser.java rename to src/main/java/gr/seab/r2rml/beans/Parser.java index 59cd04c..b04dfb3 100644 --- a/src/main/java/gr/ekt/r2rml/beans/Parser.java +++ b/src/main/java/gr/seab/r2rml/beans/Parser.java @@ -1,818 +1,818 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.beans; - -import gr.ekt.r2rml.beans.util.LogicalTableMappingComparator; -import gr.ekt.r2rml.entities.LogicalTableMapping; -import gr.ekt.r2rml.entities.LogicalTableView; -import gr.ekt.r2rml.entities.MappingDocument; -import gr.ekt.r2rml.entities.PredicateObjectMap; -import gr.ekt.r2rml.entities.RefObjectMap; -import gr.ekt.r2rml.entities.SubjectMap; -import gr.ekt.r2rml.entities.Template; -import gr.ekt.r2rml.entities.TermType; -import gr.ekt.r2rml.entities.sparql.LocalResultSet; -import gr.ekt.r2rml.entities.sql.SelectQuery; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Map; -import java.util.Properties; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.hp.hpl.jena.datatypes.BaseDatatype; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.NodeIterator; -import com.hp.hpl.jena.rdf.model.Property; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.ResIterator; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.sdb.SDBFactory; -import com.hp.hpl.jena.util.FileManager; - -/** - * Parses a valid R2RML file and produces a mapping document - * @see MappingDocument - * @author nkons - * - */ -public class Parser { - private static final Logger log = LoggerFactory.getLogger(Parser.class); - - /** - * The mapping definitions - */ - private Model mapModel; - - /** - * The resulting model, containing the input model and all the generated triples - */ - private Model resultModel; - - /** - * The base namespace for the result model - */ - private String baseNs; - - /** - * The rr namespace. Should not be changed. - */ - private final String rrNs = "http://www.w3.org/ns/r2rml#"; - - /** - * The xsd namespace. Should not be changed. - */ - private final String xsdNs = "http://www.w3.org/2001/XMLSchema#"; - - /** - * @see MappingDocument - */ - MappingDocument mappingDocument = new MappingDocument(); - - private String propertiesFilename; - /** - * The properties, as read from the properties file. - */ - private Properties properties = new Properties(); - - private boolean verbose; - - private Database db; - - private Util util; - - public Parser() { - - } - - public Parser(String propertiesFilename) { - this.propertiesFilename = propertiesFilename; - try { - if (StringUtils.isNotEmpty(propertiesFilename)) { - properties.load(new FileInputStream(propertiesFilename)); - log.info("Loaded properties from " + propertiesFilename); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @SuppressWarnings("unchecked") - public MappingDocument parse() { - - init(); - - log.info("Initialized."); - - try { - //First find the logical table views - LinkedList logicalTableViews = findLogicalTableViews(); - mappingDocument.setLogicalTableViews(logicalTableViews); - log.info("Mapping document has " + logicalTableViews.size() + " logical table views."); - - LinkedList logicalTableMappings = findLogicalTableMappings(); - mappingDocument.setLogicalTableMappings(logicalTableMappings); - log.info("Mapping document has " + logicalTableMappings.size() + " logical table mappings."); - - for (int i = 0; i < mappingDocument.getLogicalTableMappings().size(); i++) { - LogicalTableMapping logicalTableMapping = mappingDocument.getLogicalTableMappings().get(i); - logicalTableMapping.setSubjectMap(createSubjectMapForResource(mapModel.getResource(logicalTableMapping.getUri()))); - logicalTableMapping.setPredicateObjectMaps(createPredicateObjectMapsForResource(mapModel.getResource(logicalTableMapping.getUri()))); - mappingDocument.getLogicalTableMappings().set(i, logicalTableMapping); - } - - //Sorting: evaluate first the logical table mappings without reference to a parent triples map - @SuppressWarnings("rawtypes") - Comparator c = new LogicalTableMappingComparator(); - Collections.sort(mappingDocument.getLogicalTableMappings(), c); - - if (verbose) { - log.info("Logical table mappings will be parsed in the following order:"); - for (LogicalTableMapping ltm : mappingDocument.getLogicalTableMappings()) { - log.info(" Table mapping uri: " + ltm.getUri()); - } - } - //resultModel.write(System.out, "TURTLE"); - - //sparql("SELECT ?s ?p ?o FROM <" + baseNs + "> WHERE { ?s ?p ?o }", resultModel); - - } catch (Exception e) { - e.printStackTrace(); - } - - return mappingDocument; - } - - public SubjectMap createSubjectMapForResource(Resource r) { - log.info("Processing subject map for: <" + r.getURI() + ">"); - SubjectMap subjectMap = new SubjectMap(); - - NodeIterator iter = mapModel.listObjectsOfProperty(r, mapModel.getProperty(rrNs + "subjectMap")); - while (iter.hasNext()) { //should be only 1 - RDFNode rn = iter.next(); - NodeIterator iterTemplate = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "template")); - while (iterTemplate.hasNext()) { - RDFNode rnTemplate = iterTemplate.next(); - - if (rnTemplate.isLiteral()) { - log.info("Processing literal subject template: " + rnTemplate.asLiteral().toString()); - - //Verify that it is indeed a literal and not some other type. Property rr:termType can have one of the following - //values: rr:IRI, rr:BlankNode or rr:Literal - NodeIterator iterTermType = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "termType")); - if (iterTermType.hasNext()) { - while (iterTermType.hasNext()) { - RDFNode rnTermType = iterTermType.next(); - if (rnTermType.isResource() && rnTermType.asResource().getNameSpace().equals(rrNs)) { - String termType = rnTermType.asResource().getLocalName(); - log.info("Found rr:termType " + termType); - if ("IRI".equals(termType)) { - Template template = new Template(rnTemplate.asLiteral().toString(), TermType.IRI, baseNs, resultModel); - subjectMap.setTemplate(template); - } else if ("BlankNode".equals(termType)) { - Template template = new Template(rnTemplate.asLiteral().toString(), TermType.BLANKNODE, baseNs, resultModel); - subjectMap.setTemplate(template); - } else if ("Literal".equals(termType)) { - Template template = new Template(rnTemplate.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); - subjectMap.setTemplate(template); - } else { - log.error("Unknown term type: " + termType + ". Terminating."); - System.exit(0); - } - } - } - } else { - Template template = new Template(rnTemplate.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); - subjectMap.setTemplate(template); - } - } else { - log.info("Processing node subject template: " + rnTemplate.asNode().toString()); - Template template = new Template(rnTemplate.asNode().toString(), TermType.IRI, baseNs, resultModel); - subjectMap.setTemplate(template); - } - - log.info("Logical table mapping uri is " + r.getURI()); - LogicalTableMapping ltm = mappingDocument.findLogicalTableMappingByUri(r.getURI()); - LogicalTableView ltv = ltm.getView(); - SelectQuery sq = ltv.getSelectQuery(); - subjectMap.setSelectQuery(sq); - } - - NodeIterator iterColumn = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "column")); - while (iterColumn.hasNext()) { - RDFNode rnColumn = iterColumn.next(); - String tempColumn = rnColumn.asLiteral().toString(); - String templateText = "{" + tempColumn + "}"; - - NodeIterator iterTermType = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "termType")); - if (iterTermType.hasNext()) { - while (iterTermType.hasNext()) { - RDFNode rnTermType = iterTermType.next(); - if (rnTermType.isResource() && rnTermType.asResource().getNameSpace().equals(rrNs)) { - String termType = rnTermType.asResource().getLocalName(); - if (verbose) log.info("Found rr:termType " + termType); - if ("IRI".equals(termType)) { - Template template = new Template(templateText, TermType.IRI, baseNs, resultModel); - subjectMap.setTemplate(template); - } else if ("BlankNode".equals(termType)) { - Template template = new Template(templateText, TermType.BLANKNODE, baseNs, resultModel); - subjectMap.setTemplate(template); - } else if ("Literal".equals(termType)) { - Template template = new Template(templateText, TermType.LITERAL, baseNs, resultModel); - subjectMap.setTemplate(template); - } else { - log.error("Unknown term type: " + termType + ". Terminating."); - System.exit(0); - } - } - } - } else { - Template template = new Template(templateText, TermType.LITERAL, baseNs, resultModel); - subjectMap.setTemplate(template); - } - log.info("Added subject template " + templateText + " from column " + tempColumn); - } - - NodeIterator iterClass = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "class")); - ArrayList classUris = new ArrayList(); - while (iterClass.hasNext()) { //can be more than 1 - RDFNode rnClass = iterClass.next(); - if (verbose) log.info("Subject class is: " + rnClass.asResource().getURI()); - classUris.add(rnClass.asResource().getURI()); - } - subjectMap.setClassUris(classUris); - - NodeIterator iterGraphMap = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "graphMap")); - while (iterGraphMap.hasNext()) { //should be only 1 - RDFNode rnGraphMap = iterGraphMap.next(); - log.info("triples belong to graphMap " + rnGraphMap.asResource().getURI()); - if (rnGraphMap.asResource().getURI() == null) { - log.info("graphMap is either a template or a constant"); - //TODO GraphMap should be inserted in subject - } - } - } - - NodeIterator iterSubject = mapModel.listObjectsOfProperty(r, mapModel.getProperty(rrNs + "subject")); - while (iterSubject.hasNext()) { //should be only 1 - RDFNode rnSubject = iterSubject.next(); - log.info("Found subject: " + rnSubject.toString()); - Template template = new Template(rnSubject.toString(), TermType.IRI, baseNs, resultModel); - subjectMap.setTemplate(template); - } - - return subjectMap; - } - - public ArrayList createPredicateObjectMapsForResource(Resource r) { - log.info("Processing predicate object maps for: <" + r.getURI() + ">"); - ArrayList predicateObjectMaps = new ArrayList(); - NodeIterator iterPredicateObject = mapModel.listObjectsOfProperty(r, mapModel.getProperty(rrNs + "predicateObjectMap")); - while (iterPredicateObject.hasNext()) { - RDFNode rnPredicateObject = iterPredicateObject.next(); - - PredicateObjectMap predicateObjectMap = new PredicateObjectMap(); - - NodeIterator iterPredicate = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "predicate")); - ArrayList predicates = new ArrayList(); - while (iterPredicate.hasNext()) { //can return more than 1 - RDFNode rnPredicate = iterPredicate.next(); - if (verbose) log.info("Predicate is: " + rnPredicate.asResource().getURI()); - predicates.add(rnPredicate.asResource().getURI()); - } - - NodeIterator iterPredicateMap = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "predicateMap")); - while (iterPredicateMap.hasNext()) { //can return more than 1 - RDFNode rnPredicateMap = iterPredicateMap.next(); - - NodeIterator iterConstant = mapModel.listObjectsOfProperty(rnPredicateMap.asResource(), mapModel.getProperty(rrNs + "constant")); - while (iterConstant.hasNext()) { - RDFNode rnConstant = iterConstant.next(); - - if (rnConstant.isLiteral()) { - log.info("Adding predicate map constant literal: " + rnConstant.asNode().toString() + "."); - predicates.add(rnConstant.asLiteral().toString()); - } else { - log.info("Adding predicate map constant uri: " + rnConstant.asNode().toString() + "."); - predicates.add(rnConstant.asResource().getURI()); - } - } - } - predicateObjectMap.setPredicates(predicates); - - NodeIterator iterObject1 = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "object")); - while (iterObject1.hasNext()) { - RDFNode rnObject = iterObject1.next(); - log.info("Found object: " + rnObject.toString()); - if (rnObject.isLiteral()) { - log.info("Adding object map constant: " + rnObject.asLiteral().toString() + ". Treating it as a template with no fields."); - Template template = new Template(rnObject.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); - predicateObjectMap.setObjectTemplate(template); - } else { - log.info("Adding object map constant: " + rnObject.asNode().toString() + ". Treating it as a template with no fields."); - Template template = new Template(rnObject.asNode().toString(), TermType.IRI, baseNs, resultModel); - predicateObjectMap.setObjectTemplate(template); - } - } - - NodeIterator iterObjectMap2 = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "objectMap")); - while (iterObjectMap2.hasNext()) { - RDFNode rnObjectMap = iterObjectMap2.next(); - - //Must have here an rr:column, an rr:template, an rr:constant, or an rr:parentTriplesMap - - NodeIterator iterTemplate = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "template")); - while (iterTemplate.hasNext()) { //should return only 1 - RDFNode rnTemplate = iterTemplate.next(); - - if (rnTemplate.isLiteral()) { - log.info("Processing object map template: " + rnTemplate.asLiteral().toString()); - Template template = new Template(rnTemplate.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); - predicateObjectMap.setObjectTemplate(template); - } else { - log.info("Processing object map template: " + rnTemplate.asNode().toString()); - Template template = new Template(rnTemplate.asNode().toString(), TermType.IRI, baseNs, resultModel); - predicateObjectMap.setObjectTemplate(template); - } - //predicateObjectMap.setObjectColumn(predicateObjectMap.getObjectTemplate().getFields().get(0)); - //System.out.println("added objectQuery " + "SELECT " + predicateObjectMap.getObjectTemplate().getFields().get(0) + " FROM " + mappingDocument.findLogicalTableMappingByUri(r.getURI()).getView().getQuery().getTables().get(0).getName()); - //System.out.println("added objectTemplate " + rnTemplate.asLiteral().toString()); - } - - NodeIterator iterColumn = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "column")); - while (iterColumn.hasNext()) { - RDFNode rnColumn = iterColumn.next(); - String tempField = rnColumn.asLiteral().toString(); - - //objectFields.add(tempField); - predicateObjectMap.setObjectColumn(tempField); - log.info("Added object column: " + tempField); - } - - NodeIterator iterLanguage = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "language")); - while (iterLanguage.hasNext()) { - RDFNode rnLanguage = iterLanguage.next(); - String language = rnLanguage.asLiteral().toString(); - - predicateObjectMap.setLanguage(new Template(language, TermType.LITERAL, baseNs, resultModel)); - log.info("Added language: " + language); - } - - NodeIterator iterDataType = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "datatype")); - while (iterDataType.hasNext()) { - RDFNode rnDataType = iterDataType.next(); - - if (xsdNs.equals(rnDataType.asResource().getNameSpace())) { - String dataType = rnDataType.asResource().getLocalName(); - log.info("Found datatype xsd:" + dataType); - BaseDatatype baseDataType = util.findDataType(dataType); - predicateObjectMap.setDataType(baseDataType); - } - } - - NodeIterator iterConstant = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "constant")); - while (iterConstant.hasNext()) { - RDFNode rnConstant = iterConstant.next(); - if (rnConstant.isLiteral()) { - log.info("Adding object map constant literal: " + rnConstant.asLiteral().toString()); - Template template = new Template(rnConstant.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); - predicateObjectMap.setObjectTemplate(template); - } else { - log.info("Adding object map constant iri: " + rnConstant.asNode().toString()); - Template template = new Template(rnConstant.asNode().toString(), TermType.IRI, baseNs, resultModel); - predicateObjectMap.setObjectTemplate(template); - } - } - - //If there is a type declared, add it the object template - NodeIterator iterTermType = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "termType")); - if (iterTermType.hasNext()) { - Template template = predicateObjectMap.getObjectTemplate(); - while (iterTermType.hasNext()) { //should only be 1 - RDFNode rnTermType = iterTermType.next(); - if (rnTermType.isResource() && rnTermType.asResource().getNameSpace().equals(rrNs)) { - String termType = rnTermType.asResource().getLocalName(); - log.info("Found rr:termType " + termType); - if ("IRI".equals(termType)) { - template.setTermType(TermType.IRI); - } else if ("BlankNode".equals(termType)) { - template.setTermType(TermType.BLANKNODE); - } else if ("Literal".equals(termType)) { - template.setTermType(TermType.LITERAL); - } else { - log.error("Unknown term type: " + termType + ". Terminating."); - System.exit(0); - } - } - } - predicateObjectMap.setObjectTemplate(template); - } - - NodeIterator iterParentTriplesMap = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "parentTriplesMap")); - while (iterParentTriplesMap.hasNext()) { - RDFNode rnParentTriplesMap = iterParentTriplesMap.next(); - log.info("Found rr:parentTriplesMap " + rnParentTriplesMap.asResource().getURI()); - RefObjectMap refObjectMap = new RefObjectMap(); - refObjectMap.setParentTriplesMapUri(rnParentTriplesMap.asResource().getURI()); - - NodeIterator iterJoinCondition = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "joinCondition")); - while (iterJoinCondition.hasNext()) { - RDFNode rnJoinCondition = iterJoinCondition.next(); - log.info("Found rr:joinCondition " + rnJoinCondition.asResource().getURI()); - NodeIterator iterChild = mapModel.listObjectsOfProperty(rnJoinCondition.asResource(), mapModel.getProperty(rrNs + "child")); - while (iterChild.hasNext()) { - RDFNode rnChild = iterChild.next(); - log.info("Found rr:child " + rnChild.asLiteral().toString()); - refObjectMap.setChild(rnChild.asLiteral().toString()); - } - - NodeIterator iterParent = mapModel.listObjectsOfProperty(rnJoinCondition.asResource(), mapModel.getProperty(rrNs + "parent")); - while (iterParent.hasNext()) { - RDFNode rnParent = iterParent.next(); - log.info("Found rr:parent " + rnParent.asLiteral().toString()); - refObjectMap.setParent(rnParent.asLiteral().toString()); - } - } - predicateObjectMap.setRefObjectMap(refObjectMap); - } - } - predicateObjectMaps.add(predicateObjectMap); - } - - return predicateObjectMaps; - } - - public LinkedList findLogicalTableMappings() { - LinkedList results = new LinkedList(); - Property logicalTable = mapModel.getProperty(rrNs + "logicalTable"); - ResIterator iter1 = mapModel.listSubjectsWithProperty(logicalTable); - while (iter1.hasNext()) { - Resource r = iter1.nextResource(); - log.info("Found logical table: <" + r.getURI() + ">"); - NodeIterator iter1b = mapModel.listObjectsOfProperty(r, logicalTable); - while (iter1b.hasNext()) { //should be only 1 - RDFNode rn = iter1b.next(); - if (r.getURI() != null) { - LogicalTableMapping logicalTableMapping = new LogicalTableMapping(); - logicalTableMapping.setUri(r.getURI()); - log.info("Looking up logical table view " + rn.asResource().getURI()); - if (rn.asResource().getURI() != null) { - LogicalTableView ltv = mappingDocument.findLogicalTableViewByUri(rn.asResource().getURI()); - logicalTableMapping.setView(ltv); - - if (!contains(results, logicalTableMapping.getUri())) - results.add(logicalTableMapping); - } else { - //Then we can either have a rr:sqlQuery with the query or a rr:tableName with the table name - LocalResultSet sparqlResults = util.sparql(mapModel, "SELECT ?z2 WHERE { <" + r.getURI() + "> rr:logicalTable ?z1 . ?z1 rr:sqlQuery ?z2 . } "); - if (sparqlResults.getRows().size() > 0) { - String sqlQuery = sparqlResults.getRows().get(0).getResources().get(0).getLocalName(); - if (sqlQuery != null) { - sqlQuery = sqlQuery.replaceAll("[\r\n]+", " "); - if (sqlQuery.indexOf(';') != -1) sqlQuery = sqlQuery.replace(';', ' '); - - SelectQuery test = new SelectQuery(sqlQuery, properties); - LogicalTableView logicalTableView = mappingDocument.findLogicalTableViewByQuery(test.getQuery()); - logicalTableMapping.setView(logicalTableView); - - if (!contains(results, logicalTableMapping.getUri())) - results.add(logicalTableMapping); - } else { - log.error("Could not find rr:sqlQuery."); - } - } else { - sparqlResults = util.sparql(mapModel, "SELECT ?z2 WHERE { <" + r.getURI() + "> rr:logicalTable ?z1 . ?z1 rr:tableName ?z2 . } "); - if (sparqlResults.getRows().size() > 0) { - String tableName = sparqlResults.getRows().get(0).getResources().get(0).getLocalName(); - tableName = tableName.replaceAll("\"", ""); - log.info("Found tableName " + tableName); - LogicalTableView logicalTableView = new LogicalTableView(); - SelectQuery sq = new SelectQuery(createQueryForTable(tableName), properties); - logicalTableView.setSelectQuery(sq); - logicalTableMapping.setView(logicalTableView); - - if (!contains(results, logicalTableMapping.getUri())) - results.add(logicalTableMapping); - } - } - } - - log.info("Added logical table mapping from uri <" + r.getURI() + ">"); - } else { - log.info("Did not add logical table mapping from NULL uri"); - } - } - } - - Property tableName = mapModel.getProperty(rrNs + "tableName"); - ResIterator iter2 = mapModel.listSubjectsWithProperty(tableName); - while (iter2.hasNext()) { - Resource r = iter2.nextResource(); - if (r.isLiteral()) { - log.info("Found literal with a table name: <" + r.asLiteral().toString() + ">"); - } else { - log.info("Found resource with a table name: <" + r.getURI() + ">"); - } - - NodeIterator iter2b = mapModel.listObjectsOfProperty(r, tableName); - while (iter2b.hasNext()) { //should be only 1 - RDFNode rn = iter2b.next(); - LogicalTableMapping logicalTableMapping = new LogicalTableMapping(); - - LogicalTableView logicalTableView = new LogicalTableView(); - String newTable = rn.asLiteral().toString(); - newTable = util.stripQuotes(newTable); - log.info("Found table name: " + newTable); - SelectQuery sq = new SelectQuery(createQueryForTable(newTable), properties); - log.info("Setting SQL query for table " + newTable + ": " + sq.getQuery()); - logicalTableView.setSelectQuery(sq); - if (r.getURI() == null) { - //figure out to which TriplesMap this rr:tableName belongs - log.info("Found rr:tableName without parent."); - //LocalResultSet sparqlResults = util.sparql(mapModel, "SELECT ?x WHERE { ?x rr:logicalTable ?z . ?z rr:tableName " + newTable + " . } "); - if (util.findDatabaseType(properties.getProperty("db.driver")).equals("postgresql")) { - newTable = "\"\\\"" + newTable + "\\\"\""; - } else { - newTable = "\"" + newTable + "\""; - } - LocalResultSet sparqlResults = util.sparql(mapModel, "SELECT ?x WHERE { ?x rr:logicalTable ?z . ?z rr:tableName " + newTable + " . } "); - - if (sparqlResults.getRows().size() > 0) { - String triplesMapUri = sparqlResults.getRows().get(0).getResources().get(0).getUri(); - logicalTableMapping.setUri(triplesMapUri); - } else { - log.error("Could not find triples map."); - } - } else { - logicalTableMapping.setUri(r.getURI()); - } - logicalTableMapping.setView(logicalTableView); - - if (!contains(results, logicalTableMapping.getUri())) - results.add(logicalTableMapping); - } - } - - //prevent java.util.ConcurrentModificationException - for (Iterator it = results.iterator(); it.hasNext(); ) { - LogicalTableMapping logicalTableMapping = it.next(); - if (logicalTableMapping.getView() == null ) { - it.remove(); - } - } - - return results; - } - - public LinkedList findLogicalTableViews() { - LinkedList results = new LinkedList(); - Property sqlQuery = mapModel.getProperty(rrNs + "sqlQuery"); - ResIterator iter = mapModel.listSubjectsWithProperty(sqlQuery); - while (iter.hasNext()) { - Resource r = iter.nextResource(); - NodeIterator iter2 = mapModel.listObjectsOfProperty(r, sqlQuery); - while (iter2.hasNext()) { //should only have one - RDFNode rn = iter2.next(); - LogicalTableView logicalTableView = new LogicalTableView(); - logicalTableView.setUri(r.getURI()); - String query = rn.asLiteral().toString(); - //Windows standard line separator is "\r\n"--a carriage-return followed by a linefeed. The regex below matches any number of either of the two characters - query = query.replaceAll("[\r\n]+", " "); - if (query.indexOf(';') != -1) query = query.replace(';', ' '); - - log.info("Found query: <" + r.getURI() + "> with value: " + query); - //Testing. Add a LIMIT 10 to avoid large datasets. - try { - java.sql.ResultSet rs = db.query(query + " LIMIT 10"); - rs.close(); - log.info("Query tested ok."); - } catch (SQLException e) { - e.printStackTrace(); - } - - SelectQuery selectQuery = new SelectQuery(query, properties); - - //if the dump is incremental, add an order by to ensure results are retrieved in the same order - if (properties.containsKey("jena.storeOutputModelInDatabase") - && properties.getProperty("jena.storeOutputModelInDatabase").contains("false") - && properties.containsKey("default.incremental") - && properties.getProperty("default.incremental").contains("true")) { - String q = selectQuery.getQuery(); - if (q.toLowerCase().indexOf("order by") == -1) { - String fieldName = selectQuery.getFields().get(0).getName(); - int as = fieldName.toLowerCase().indexOf(" as "); - if (as > -1) { - fieldName = fieldName.substring(0, as); - q += " order by " + fieldName; - selectQuery.setQuery(q); - log.info("Added ORDER BY to query that now is: " + q); - } - } - } - - logicalTableView.setSelectQuery(selectQuery); - results.add(logicalTableView); - } - } - return results; - } - - public String createQueryForTable(String tableName) { - //If true, we are in postgres, otherwise in mysql - boolean postgres = util.findDatabaseType(properties.getProperty("db.driver")).equals("postgresql"); - - String result = "SELECT "; - try { - ArrayList fields = new ArrayList(); - - java.sql.ResultSet rs; - if (postgres) { - rs = db.query("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tableName + "'"); - } else { - rs = db.query("DESCRIBE " + tableName); - } - - rs.beforeFirst(); - while (rs.next()) { - //mysql: fields.add(rs.getString("Field")); - if (postgres) { - fields.add("\"" + rs.getString(1) + "\""); - } else { - fields.add(rs.getString("Field")); - } - } - for (String f : fields) { - if (postgres) { - result += "\"" + tableName + "\"" + "." + f + ", "; - } else { - result += tableName + "." + f + ", "; - } - } - result = result.substring(0, result.lastIndexOf(',')); - rs.close(); - - if (postgres) { - result += " FROM " + "\"" + tableName + "\""; - } else { - result += " FROM " + tableName; - } - result += " ORDER BY " + fields.get(0); //add order to ensure same order regardless the implementation - - } catch (Exception e) { - e.printStackTrace(); - } - - log.info("Result is: " + result); - return result; - } - - public void init() { - log.info("Initializing."); - - //test source database - db.openConnection(); - - //test destination database - if (properties.containsKey("jena.storeOutputModelInDatabase") && properties.getProperty("jena.storeOutputModelInDatabase").contains("true")) { - db.openJenaConnection(); - } - - this.baseNs = properties.getProperty("default.namespace"); - String mappingFilename = properties.getProperty("mapping.file"); - - InputStream isMap = FileManager.get().open(mappingFilename); - mapModel = ModelFactory.createDefaultModel(); - try { - mapModel.read(isMap, baseNs, properties.getProperty("mapping.file.type")); - } catch (Exception e) { - log.error("Error reading input model"); - System.exit(0); - } - //mapModel.write(System.out, properties.getProperty("mapping.file.type")); - - String inputModelFileName = properties.getProperty("input.model"); - - Model resultBaseModel = ModelFactory.createDefaultModel(); - if (StringUtils.isNotBlank(inputModelFileName)) { - InputStream isRes = FileManager.get().open(inputModelFileName); - resultBaseModel.read(isRes, baseNs, properties.getProperty("input.model.type")); - } - //resultBaseModel.write(System.out, properties.getProperty("input.model.type")); - - String storeInDatabase = properties.getProperty("jena.storeOutputModelInDatabase"); - String cleanDbOnStartup = properties.getProperty("jena.cleanDbOnStartup"); - - verbose = properties.containsKey("default.verbose") && properties.getProperty("default.verbose").contains("true"); - - log.info("Initialising Parser"); - - if (Boolean.valueOf(storeInDatabase)) { - if (Boolean.valueOf(cleanDbOnStartup)) { - db.jenaStore().getTableFormatter().create(); - } - - Model resultDbModel = SDBFactory.connectDefaultModel(db.jenaStore()); - - if (StringUtils.isNotBlank(inputModelFileName)) { - InputStream isRes = FileManager.get().open(inputModelFileName); - resultDbModel.read(isRes, baseNs, properties.getProperty("input.model.type")); - } - log.info("Store size is " + db.jenaStore().getSize()); -// if (store.getSize() > 0) { -// StmtIterator sIter = resultDbModel.listStatements(); -// for ( ; sIter.hasNext() ; ) { -// Statement stmt = sIter.nextStatement() ; -// log.info("stmt " + stmt) ; -// } -// sIter.close() ; -// } - - //Model resultDbModel = ModelFactory.createModelRDBMaker(db.getJenaConnection()).createModel("fresh"); - resultDbModel.add(resultBaseModel); - - //resultModel = ModelFactory.createInfModel(ReasonerRegistry.getRDFSReasoner(), resultDbModel); - resultModel = ModelFactory.createDefaultModel(); - - Map prefixes = mapModel.getNsPrefixMap(); - log.info("Copy " + prefixes.size() + " prefixes from map model to persistent."); - for (String s : mapModel.getNsPrefixMap().keySet()) { - log.info(s + ": " + mapModel.getNsPrefixMap().get(s)); - } - resultModel.setNsPrefixes(prefixes); - mappingDocument.setPrefixes(prefixes); - - } else { - //resultModel = ModelFactory.createInfModel(ReasonerRegistry.getRDFSReasoner(), resultBaseModel); - resultModel = ModelFactory.createDefaultModel(); - resultModel.setNsPrefixes(mapModel.getNsPrefixMap()); - mappingDocument.setPrefixes(mapModel.getNsPrefixMap()); - } - } - - boolean contains(LinkedList logicalTableMappings, String uri) { - for (LogicalTableMapping logicalTableMapping : logicalTableMappings) { - if (StringUtils.equals(logicalTableMapping.getUri(), uri)) return true; - } - return false; - } - - public String getPropertiesFilename() { - return propertiesFilename; - } - - public void setPropertiesFilename(String propertiesFilename) { - this.propertiesFilename = propertiesFilename; - } - - public Database getDb() { - return db; - } - - public void setDb(Database db) { - this.db = db; - } - - public Util getUtil() { - return util; - } - - public void setUtil(Util util) { - this.util = util; - } - - public Model getResultModel() { - return resultModel; - } - - public Properties getProperties() { - return properties; - } - - public void setProperties(Properties properties) { - this.properties = properties; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.beans; + +import gr.seab.r2rml.beans.util.LogicalTableMappingComparator; +import gr.seab.r2rml.entities.LogicalTableMapping; +import gr.seab.r2rml.entities.LogicalTableView; +import gr.seab.r2rml.entities.MappingDocument; +import gr.seab.r2rml.entities.PredicateObjectMap; +import gr.seab.r2rml.entities.RefObjectMap; +import gr.seab.r2rml.entities.SubjectMap; +import gr.seab.r2rml.entities.Template; +import gr.seab.r2rml.entities.TermType; +import gr.seab.r2rml.entities.sparql.LocalResultSet; +import gr.seab.r2rml.entities.sql.SelectQuery; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.hp.hpl.jena.datatypes.BaseDatatype; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.NodeIterator; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.ResIterator; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.sdb.SDBFactory; +import com.hp.hpl.jena.util.FileManager; + +/** + * Parses a valid R2RML file and produces a mapping document + * @see MappingDocument + * @author nkons + * + */ +public class Parser { + private static final Logger log = LoggerFactory.getLogger(Parser.class); + + /** + * The mapping definitions + */ + private Model mapModel; + + /** + * The resulting model, containing the input model and all the generated triples + */ + private Model resultModel; + + /** + * The base namespace for the result model + */ + private String baseNs; + + /** + * The rr namespace. Should not be changed. + */ + private final String rrNs = "http://www.w3.org/ns/r2rml#"; + + /** + * The xsd namespace. Should not be changed. + */ + private final String xsdNs = "http://www.w3.org/2001/XMLSchema#"; + + /** + * @see MappingDocument + */ + MappingDocument mappingDocument = new MappingDocument(); + + private String propertiesFilename; + /** + * The properties, as read from the properties file. + */ + private Properties properties = new Properties(); + + private boolean verbose; + + private Database db; + + private Util util; + + public Parser() { + + } + + public Parser(String propertiesFilename) { + this.propertiesFilename = propertiesFilename; + try { + if (StringUtils.isNotEmpty(propertiesFilename)) { + properties.load(new FileInputStream(propertiesFilename)); + log.info("Loaded properties from " + propertiesFilename); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + public MappingDocument parse() { + + init(); + + log.info("Initialized."); + + try { + //First find the logical table views + LinkedList logicalTableViews = findLogicalTableViews(); + mappingDocument.setLogicalTableViews(logicalTableViews); + log.info("Mapping document has " + logicalTableViews.size() + " logical table views."); + + LinkedList logicalTableMappings = findLogicalTableMappings(); + mappingDocument.setLogicalTableMappings(logicalTableMappings); + log.info("Mapping document has " + logicalTableMappings.size() + " logical table mappings."); + + for (int i = 0; i < mappingDocument.getLogicalTableMappings().size(); i++) { + LogicalTableMapping logicalTableMapping = mappingDocument.getLogicalTableMappings().get(i); + logicalTableMapping.setSubjectMap(createSubjectMapForResource(mapModel.getResource(logicalTableMapping.getUri()))); + logicalTableMapping.setPredicateObjectMaps(createPredicateObjectMapsForResource(mapModel.getResource(logicalTableMapping.getUri()))); + mappingDocument.getLogicalTableMappings().set(i, logicalTableMapping); + } + + //Sorting: evaluate first the logical table mappings without reference to a parent triples map + @SuppressWarnings("rawtypes") + Comparator c = new LogicalTableMappingComparator(); + Collections.sort(mappingDocument.getLogicalTableMappings(), c); + + if (verbose) { + log.info("Logical table mappings will be parsed in the following order:"); + for (LogicalTableMapping ltm : mappingDocument.getLogicalTableMappings()) { + log.info(" Table mapping uri: " + ltm.getUri()); + } + } + //resultModel.write(System.out, "TURTLE"); + + //sparql("SELECT ?s ?p ?o FROM <" + baseNs + "> WHERE { ?s ?p ?o }", resultModel); + + } catch (Exception e) { + e.printStackTrace(); + } + + return mappingDocument; + } + + public SubjectMap createSubjectMapForResource(Resource r) { + log.info("Processing subject map for: <" + r.getURI() + ">"); + SubjectMap subjectMap = new SubjectMap(); + + NodeIterator iter = mapModel.listObjectsOfProperty(r, mapModel.getProperty(rrNs + "subjectMap")); + while (iter.hasNext()) { //should be only 1 + RDFNode rn = iter.next(); + NodeIterator iterTemplate = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "template")); + while (iterTemplate.hasNext()) { + RDFNode rnTemplate = iterTemplate.next(); + + if (rnTemplate.isLiteral()) { + log.info("Processing literal subject template: " + rnTemplate.asLiteral().toString()); + + //Verify that it is indeed a literal and not some other type. Property rr:termType can have one of the following + //values: rr:IRI, rr:BlankNode or rr:Literal + NodeIterator iterTermType = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "termType")); + if (iterTermType.hasNext()) { + while (iterTermType.hasNext()) { + RDFNode rnTermType = iterTermType.next(); + if (rnTermType.isResource() && rnTermType.asResource().getNameSpace().equals(rrNs)) { + String termType = rnTermType.asResource().getLocalName(); + log.info("Found rr:termType " + termType); + if ("IRI".equals(termType)) { + Template template = new Template(rnTemplate.asLiteral().toString(), TermType.IRI, baseNs, resultModel); + subjectMap.setTemplate(template); + } else if ("BlankNode".equals(termType)) { + Template template = new Template(rnTemplate.asLiteral().toString(), TermType.BLANKNODE, baseNs, resultModel); + subjectMap.setTemplate(template); + } else if ("Literal".equals(termType)) { + Template template = new Template(rnTemplate.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); + subjectMap.setTemplate(template); + } else { + log.error("Unknown term type: " + termType + ". Terminating."); + System.exit(0); + } + } + } + } else { + Template template = new Template(rnTemplate.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); + subjectMap.setTemplate(template); + } + } else { + log.info("Processing node subject template: " + rnTemplate.asNode().toString()); + Template template = new Template(rnTemplate.asNode().toString(), TermType.IRI, baseNs, resultModel); + subjectMap.setTemplate(template); + } + + log.info("Logical table mapping uri is " + r.getURI()); + LogicalTableMapping ltm = mappingDocument.findLogicalTableMappingByUri(r.getURI()); + LogicalTableView ltv = ltm.getView(); + SelectQuery sq = ltv.getSelectQuery(); + subjectMap.setSelectQuery(sq); + } + + NodeIterator iterColumn = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "column")); + while (iterColumn.hasNext()) { + RDFNode rnColumn = iterColumn.next(); + String tempColumn = rnColumn.asLiteral().toString(); + String templateText = "{" + tempColumn + "}"; + + NodeIterator iterTermType = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "termType")); + if (iterTermType.hasNext()) { + while (iterTermType.hasNext()) { + RDFNode rnTermType = iterTermType.next(); + if (rnTermType.isResource() && rnTermType.asResource().getNameSpace().equals(rrNs)) { + String termType = rnTermType.asResource().getLocalName(); + if (verbose) log.info("Found rr:termType " + termType); + if ("IRI".equals(termType)) { + Template template = new Template(templateText, TermType.IRI, baseNs, resultModel); + subjectMap.setTemplate(template); + } else if ("BlankNode".equals(termType)) { + Template template = new Template(templateText, TermType.BLANKNODE, baseNs, resultModel); + subjectMap.setTemplate(template); + } else if ("Literal".equals(termType)) { + Template template = new Template(templateText, TermType.LITERAL, baseNs, resultModel); + subjectMap.setTemplate(template); + } else { + log.error("Unknown term type: " + termType + ". Terminating."); + System.exit(0); + } + } + } + } else { + Template template = new Template(templateText, TermType.LITERAL, baseNs, resultModel); + subjectMap.setTemplate(template); + } + log.info("Added subject template " + templateText + " from column " + tempColumn); + } + + NodeIterator iterClass = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "class")); + ArrayList classUris = new ArrayList(); + while (iterClass.hasNext()) { //can be more than 1 + RDFNode rnClass = iterClass.next(); + if (verbose) log.info("Subject class is: " + rnClass.asResource().getURI()); + classUris.add(rnClass.asResource().getURI()); + } + subjectMap.setClassUris(classUris); + + NodeIterator iterGraphMap = mapModel.listObjectsOfProperty(rn.asResource(), mapModel.getProperty(rrNs + "graphMap")); + while (iterGraphMap.hasNext()) { //should be only 1 + RDFNode rnGraphMap = iterGraphMap.next(); + log.info("triples belong to graphMap " + rnGraphMap.asResource().getURI()); + if (rnGraphMap.asResource().getURI() == null) { + log.info("graphMap is either a template or a constant"); + //TODO GraphMap should be inserted in subject + } + } + } + + NodeIterator iterSubject = mapModel.listObjectsOfProperty(r, mapModel.getProperty(rrNs + "subject")); + while (iterSubject.hasNext()) { //should be only 1 + RDFNode rnSubject = iterSubject.next(); + log.info("Found subject: " + rnSubject.toString()); + Template template = new Template(rnSubject.toString(), TermType.IRI, baseNs, resultModel); + subjectMap.setTemplate(template); + } + + return subjectMap; + } + + public ArrayList createPredicateObjectMapsForResource(Resource r) { + log.info("Processing predicate object maps for: <" + r.getURI() + ">"); + ArrayList predicateObjectMaps = new ArrayList(); + NodeIterator iterPredicateObject = mapModel.listObjectsOfProperty(r, mapModel.getProperty(rrNs + "predicateObjectMap")); + while (iterPredicateObject.hasNext()) { + RDFNode rnPredicateObject = iterPredicateObject.next(); + + PredicateObjectMap predicateObjectMap = new PredicateObjectMap(); + + NodeIterator iterPredicate = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "predicate")); + ArrayList predicates = new ArrayList(); + while (iterPredicate.hasNext()) { //can return more than 1 + RDFNode rnPredicate = iterPredicate.next(); + if (verbose) log.info("Predicate is: " + rnPredicate.asResource().getURI()); + predicates.add(rnPredicate.asResource().getURI()); + } + + NodeIterator iterPredicateMap = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "predicateMap")); + while (iterPredicateMap.hasNext()) { //can return more than 1 + RDFNode rnPredicateMap = iterPredicateMap.next(); + + NodeIterator iterConstant = mapModel.listObjectsOfProperty(rnPredicateMap.asResource(), mapModel.getProperty(rrNs + "constant")); + while (iterConstant.hasNext()) { + RDFNode rnConstant = iterConstant.next(); + + if (rnConstant.isLiteral()) { + log.info("Adding predicate map constant literal: " + rnConstant.asNode().toString() + "."); + predicates.add(rnConstant.asLiteral().toString()); + } else { + log.info("Adding predicate map constant uri: " + rnConstant.asNode().toString() + "."); + predicates.add(rnConstant.asResource().getURI()); + } + } + } + predicateObjectMap.setPredicates(predicates); + + NodeIterator iterObject1 = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "object")); + while (iterObject1.hasNext()) { + RDFNode rnObject = iterObject1.next(); + log.info("Found object: " + rnObject.toString()); + if (rnObject.isLiteral()) { + log.info("Adding object map constant: " + rnObject.asLiteral().toString() + ". Treating it as a template with no fields."); + Template template = new Template(rnObject.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); + predicateObjectMap.setObjectTemplate(template); + } else { + log.info("Adding object map constant: " + rnObject.asNode().toString() + ". Treating it as a template with no fields."); + Template template = new Template(rnObject.asNode().toString(), TermType.IRI, baseNs, resultModel); + predicateObjectMap.setObjectTemplate(template); + } + } + + NodeIterator iterObjectMap2 = mapModel.listObjectsOfProperty(rnPredicateObject.asResource(), mapModel.getProperty(rrNs + "objectMap")); + while (iterObjectMap2.hasNext()) { + RDFNode rnObjectMap = iterObjectMap2.next(); + + //Must have here an rr:column, an rr:template, an rr:constant, or an rr:parentTriplesMap + + NodeIterator iterTemplate = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "template")); + while (iterTemplate.hasNext()) { //should return only 1 + RDFNode rnTemplate = iterTemplate.next(); + + if (rnTemplate.isLiteral()) { + log.info("Processing object map template: " + rnTemplate.asLiteral().toString()); + Template template = new Template(rnTemplate.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); + predicateObjectMap.setObjectTemplate(template); + } else { + log.info("Processing object map template: " + rnTemplate.asNode().toString()); + Template template = new Template(rnTemplate.asNode().toString(), TermType.IRI, baseNs, resultModel); + predicateObjectMap.setObjectTemplate(template); + } + //predicateObjectMap.setObjectColumn(predicateObjectMap.getObjectTemplate().getFields().get(0)); + //System.out.println("added objectQuery " + "SELECT " + predicateObjectMap.getObjectTemplate().getFields().get(0) + " FROM " + mappingDocument.findLogicalTableMappingByUri(r.getURI()).getView().getQuery().getTables().get(0).getName()); + //System.out.println("added objectTemplate " + rnTemplate.asLiteral().toString()); + } + + NodeIterator iterColumn = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "column")); + while (iterColumn.hasNext()) { + RDFNode rnColumn = iterColumn.next(); + String tempField = rnColumn.asLiteral().toString(); + + //objectFields.add(tempField); + predicateObjectMap.setObjectColumn(tempField); + log.info("Added object column: " + tempField); + } + + NodeIterator iterLanguage = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "language")); + while (iterLanguage.hasNext()) { + RDFNode rnLanguage = iterLanguage.next(); + String language = rnLanguage.asLiteral().toString(); + + predicateObjectMap.setLanguage(new Template(language, TermType.LITERAL, baseNs, resultModel)); + log.info("Added language: " + language); + } + + NodeIterator iterDataType = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "datatype")); + while (iterDataType.hasNext()) { + RDFNode rnDataType = iterDataType.next(); + + if (xsdNs.equals(rnDataType.asResource().getNameSpace())) { + String dataType = rnDataType.asResource().getLocalName(); + log.info("Found datatype xsd:" + dataType); + BaseDatatype baseDataType = util.findDataType(dataType); + predicateObjectMap.setDataType(baseDataType); + } + } + + NodeIterator iterConstant = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "constant")); + while (iterConstant.hasNext()) { + RDFNode rnConstant = iterConstant.next(); + if (rnConstant.isLiteral()) { + log.info("Adding object map constant literal: " + rnConstant.asLiteral().toString()); + Template template = new Template(rnConstant.asLiteral().toString(), TermType.LITERAL, baseNs, resultModel); + predicateObjectMap.setObjectTemplate(template); + } else { + log.info("Adding object map constant iri: " + rnConstant.asNode().toString()); + Template template = new Template(rnConstant.asNode().toString(), TermType.IRI, baseNs, resultModel); + predicateObjectMap.setObjectTemplate(template); + } + } + + //If there is a type declared, add it the object template + NodeIterator iterTermType = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "termType")); + if (iterTermType.hasNext()) { + Template template = predicateObjectMap.getObjectTemplate(); + while (iterTermType.hasNext()) { //should only be 1 + RDFNode rnTermType = iterTermType.next(); + if (rnTermType.isResource() && rnTermType.asResource().getNameSpace().equals(rrNs)) { + String termType = rnTermType.asResource().getLocalName(); + log.info("Found rr:termType " + termType); + if ("IRI".equals(termType)) { + template.setTermType(TermType.IRI); + } else if ("BlankNode".equals(termType)) { + template.setTermType(TermType.BLANKNODE); + } else if ("Literal".equals(termType)) { + template.setTermType(TermType.LITERAL); + } else { + log.error("Unknown term type: " + termType + ". Terminating."); + System.exit(0); + } + } + } + predicateObjectMap.setObjectTemplate(template); + } + + NodeIterator iterParentTriplesMap = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "parentTriplesMap")); + while (iterParentTriplesMap.hasNext()) { + RDFNode rnParentTriplesMap = iterParentTriplesMap.next(); + log.info("Found rr:parentTriplesMap " + rnParentTriplesMap.asResource().getURI()); + RefObjectMap refObjectMap = new RefObjectMap(); + refObjectMap.setParentTriplesMapUri(rnParentTriplesMap.asResource().getURI()); + + NodeIterator iterJoinCondition = mapModel.listObjectsOfProperty(rnObjectMap.asResource(), mapModel.getProperty(rrNs + "joinCondition")); + while (iterJoinCondition.hasNext()) { + RDFNode rnJoinCondition = iterJoinCondition.next(); + log.info("Found rr:joinCondition " + rnJoinCondition.asResource().getURI()); + NodeIterator iterChild = mapModel.listObjectsOfProperty(rnJoinCondition.asResource(), mapModel.getProperty(rrNs + "child")); + while (iterChild.hasNext()) { + RDFNode rnChild = iterChild.next(); + log.info("Found rr:child " + rnChild.asLiteral().toString()); + refObjectMap.setChild(rnChild.asLiteral().toString()); + } + + NodeIterator iterParent = mapModel.listObjectsOfProperty(rnJoinCondition.asResource(), mapModel.getProperty(rrNs + "parent")); + while (iterParent.hasNext()) { + RDFNode rnParent = iterParent.next(); + log.info("Found rr:parent " + rnParent.asLiteral().toString()); + refObjectMap.setParent(rnParent.asLiteral().toString()); + } + } + predicateObjectMap.setRefObjectMap(refObjectMap); + } + } + predicateObjectMaps.add(predicateObjectMap); + } + + return predicateObjectMaps; + } + + public LinkedList findLogicalTableMappings() { + LinkedList results = new LinkedList(); + Property logicalTable = mapModel.getProperty(rrNs + "logicalTable"); + ResIterator iter1 = mapModel.listSubjectsWithProperty(logicalTable); + while (iter1.hasNext()) { + Resource r = iter1.nextResource(); + log.info("Found logical table: <" + r.getURI() + ">"); + NodeIterator iter1b = mapModel.listObjectsOfProperty(r, logicalTable); + while (iter1b.hasNext()) { //should be only 1 + RDFNode rn = iter1b.next(); + if (r.getURI() != null) { + LogicalTableMapping logicalTableMapping = new LogicalTableMapping(); + logicalTableMapping.setUri(r.getURI()); + log.info("Looking up logical table view " + rn.asResource().getURI()); + if (rn.asResource().getURI() != null) { + LogicalTableView ltv = mappingDocument.findLogicalTableViewByUri(rn.asResource().getURI()); + logicalTableMapping.setView(ltv); + + if (!contains(results, logicalTableMapping.getUri())) + results.add(logicalTableMapping); + } else { + //Then we can either have a rr:sqlQuery with the query or a rr:tableName with the table name + LocalResultSet sparqlResults = util.sparql(mapModel, "SELECT ?z2 WHERE { <" + r.getURI() + "> rr:logicalTable ?z1 . ?z1 rr:sqlQuery ?z2 . } "); + if (sparqlResults.getRows().size() > 0) { + String sqlQuery = sparqlResults.getRows().get(0).getResources().get(0).getLocalName(); + if (sqlQuery != null) { + sqlQuery = sqlQuery.replaceAll("[\r\n]+", " "); + if (sqlQuery.indexOf(';') != -1) sqlQuery = sqlQuery.replace(';', ' '); + + SelectQuery test = new SelectQuery(sqlQuery, properties); + LogicalTableView logicalTableView = mappingDocument.findLogicalTableViewByQuery(test.getQuery()); + logicalTableMapping.setView(logicalTableView); + + if (!contains(results, logicalTableMapping.getUri())) + results.add(logicalTableMapping); + } else { + log.error("Could not find rr:sqlQuery."); + } + } else { + sparqlResults = util.sparql(mapModel, "SELECT ?z2 WHERE { <" + r.getURI() + "> rr:logicalTable ?z1 . ?z1 rr:tableName ?z2 . } "); + if (sparqlResults.getRows().size() > 0) { + String tableName = sparqlResults.getRows().get(0).getResources().get(0).getLocalName(); + tableName = tableName.replaceAll("\"", ""); + log.info("Found tableName " + tableName); + LogicalTableView logicalTableView = new LogicalTableView(); + SelectQuery sq = new SelectQuery(createQueryForTable(tableName), properties); + logicalTableView.setSelectQuery(sq); + logicalTableMapping.setView(logicalTableView); + + if (!contains(results, logicalTableMapping.getUri())) + results.add(logicalTableMapping); + } + } + } + + log.info("Added logical table mapping from uri <" + r.getURI() + ">"); + } else { + log.info("Did not add logical table mapping from NULL uri"); + } + } + } + + Property tableName = mapModel.getProperty(rrNs + "tableName"); + ResIterator iter2 = mapModel.listSubjectsWithProperty(tableName); + while (iter2.hasNext()) { + Resource r = iter2.nextResource(); + if (r.isLiteral()) { + log.info("Found literal with a table name: <" + r.asLiteral().toString() + ">"); + } else { + log.info("Found resource with a table name: <" + r.getURI() + ">"); + } + + NodeIterator iter2b = mapModel.listObjectsOfProperty(r, tableName); + while (iter2b.hasNext()) { //should be only 1 + RDFNode rn = iter2b.next(); + LogicalTableMapping logicalTableMapping = new LogicalTableMapping(); + + LogicalTableView logicalTableView = new LogicalTableView(); + String newTable = rn.asLiteral().toString(); + newTable = util.stripQuotes(newTable); + log.info("Found table name: " + newTable); + SelectQuery sq = new SelectQuery(createQueryForTable(newTable), properties); + log.info("Setting SQL query for table " + newTable + ": " + sq.getQuery()); + logicalTableView.setSelectQuery(sq); + if (r.getURI() == null) { + //figure out to which TriplesMap this rr:tableName belongs + log.info("Found rr:tableName without parent."); + //LocalResultSet sparqlResults = util.sparql(mapModel, "SELECT ?x WHERE { ?x rr:logicalTable ?z . ?z rr:tableName " + newTable + " . } "); + if (util.findDatabaseType(properties.getProperty("db.driver")).equals("postgresql")) { + newTable = "\"\\\"" + newTable + "\\\"\""; + } else { + newTable = "\"" + newTable + "\""; + } + LocalResultSet sparqlResults = util.sparql(mapModel, "SELECT ?x WHERE { ?x rr:logicalTable ?z . ?z rr:tableName " + newTable + " . } "); + + if (sparqlResults.getRows().size() > 0) { + String triplesMapUri = sparqlResults.getRows().get(0).getResources().get(0).getUri(); + logicalTableMapping.setUri(triplesMapUri); + } else { + log.error("Could not find triples map."); + } + } else { + logicalTableMapping.setUri(r.getURI()); + } + logicalTableMapping.setView(logicalTableView); + + if (!contains(results, logicalTableMapping.getUri())) + results.add(logicalTableMapping); + } + } + + //prevent java.util.ConcurrentModificationException + for (Iterator it = results.iterator(); it.hasNext(); ) { + LogicalTableMapping logicalTableMapping = it.next(); + if (logicalTableMapping.getView() == null ) { + it.remove(); + } + } + + return results; + } + + public LinkedList findLogicalTableViews() { + LinkedList results = new LinkedList(); + Property sqlQuery = mapModel.getProperty(rrNs + "sqlQuery"); + ResIterator iter = mapModel.listSubjectsWithProperty(sqlQuery); + while (iter.hasNext()) { + Resource r = iter.nextResource(); + NodeIterator iter2 = mapModel.listObjectsOfProperty(r, sqlQuery); + while (iter2.hasNext()) { //should only have one + RDFNode rn = iter2.next(); + LogicalTableView logicalTableView = new LogicalTableView(); + logicalTableView.setUri(r.getURI()); + String query = rn.asLiteral().toString(); + //Windows standard line separator is "\r\n"--a carriage-return followed by a linefeed. The regex below matches any number of either of the two characters + query = query.replaceAll("[\r\n]+", " "); + if (query.indexOf(';') != -1) query = query.replace(';', ' '); + + log.info("Found query: <" + r.getURI() + "> with value: " + query); + //Testing. Add a LIMIT 10 to avoid large datasets. + try { + java.sql.ResultSet rs = db.query(query + " LIMIT 10"); + rs.close(); + log.info("Query tested ok."); + } catch (SQLException e) { + e.printStackTrace(); + } + + SelectQuery selectQuery = new SelectQuery(query, properties); + + //if the dump is incremental, add an order by to ensure results are retrieved in the same order + if (properties.containsKey("jena.storeOutputModelInDatabase") + && properties.getProperty("jena.storeOutputModelInDatabase").contains("false") + && properties.containsKey("default.incremental") + && properties.getProperty("default.incremental").contains("true")) { + String q = selectQuery.getQuery(); + if (q.toLowerCase().indexOf("order by") == -1) { + String fieldName = selectQuery.getFields().get(0).getName(); + int as = fieldName.toLowerCase().indexOf(" as "); + if (as > -1) { + fieldName = fieldName.substring(0, as); + q += " order by " + fieldName; + selectQuery.setQuery(q); + log.info("Added ORDER BY to query that now is: " + q); + } + } + } + + logicalTableView.setSelectQuery(selectQuery); + results.add(logicalTableView); + } + } + return results; + } + + public String createQueryForTable(String tableName) { + //If true, we are in postgres, otherwise in mysql + boolean postgres = util.findDatabaseType(properties.getProperty("db.driver")).equals("postgresql"); + + String result = "SELECT "; + try { + ArrayList fields = new ArrayList(); + + java.sql.ResultSet rs; + if (postgres) { + rs = db.query("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tableName + "'"); + } else { + rs = db.query("DESCRIBE " + tableName); + } + + rs.beforeFirst(); + while (rs.next()) { + //mysql: fields.add(rs.getString("Field")); + if (postgres) { + fields.add("\"" + rs.getString(1) + "\""); + } else { + fields.add(rs.getString("Field")); + } + } + for (String f : fields) { + if (postgres) { + result += "\"" + tableName + "\"" + "." + f + ", "; + } else { + result += tableName + "." + f + ", "; + } + } + result = result.substring(0, result.lastIndexOf(',')); + rs.close(); + + if (postgres) { + result += " FROM " + "\"" + tableName + "\""; + } else { + result += " FROM " + tableName; + } + result += " ORDER BY " + fields.get(0); //add order to ensure same order regardless the implementation + + } catch (Exception e) { + e.printStackTrace(); + } + + log.info("Result is: " + result); + return result; + } + + public void init() { + log.info("Initializing."); + + //test source database + db.openConnection(); + + //test destination database + if (properties.containsKey("jena.storeOutputModelInDatabase") && properties.getProperty("jena.storeOutputModelInDatabase").contains("true")) { + db.openJenaConnection(); + } + + this.baseNs = properties.getProperty("default.namespace"); + String mappingFilename = properties.getProperty("mapping.file"); + + InputStream isMap = FileManager.get().open(mappingFilename); + mapModel = ModelFactory.createDefaultModel(); + try { + mapModel.read(isMap, baseNs, properties.getProperty("mapping.file.type")); + } catch (Exception e) { + log.error("Error reading input model"); + System.exit(0); + } + //mapModel.write(System.out, properties.getProperty("mapping.file.type")); + + String inputModelFileName = properties.getProperty("input.model"); + + Model resultBaseModel = ModelFactory.createDefaultModel(); + if (StringUtils.isNotBlank(inputModelFileName)) { + InputStream isRes = FileManager.get().open(inputModelFileName); + resultBaseModel.read(isRes, baseNs, properties.getProperty("input.model.type")); + } + //resultBaseModel.write(System.out, properties.getProperty("input.model.type")); + + String storeInDatabase = properties.getProperty("jena.storeOutputModelInDatabase"); + String cleanDbOnStartup = properties.getProperty("jena.cleanDbOnStartup"); + + verbose = properties.containsKey("default.verbose") && properties.getProperty("default.verbose").contains("true"); + + log.info("Initialising Parser"); + + if (Boolean.valueOf(storeInDatabase)) { + if (Boolean.valueOf(cleanDbOnStartup)) { + db.jenaStore().getTableFormatter().create(); + } + + Model resultDbModel = SDBFactory.connectDefaultModel(db.jenaStore()); + + if (StringUtils.isNotBlank(inputModelFileName)) { + InputStream isRes = FileManager.get().open(inputModelFileName); + resultDbModel.read(isRes, baseNs, properties.getProperty("input.model.type")); + } + log.info("Store size is " + db.jenaStore().getSize()); +// if (store.getSize() > 0) { +// StmtIterator sIter = resultDbModel.listStatements(); +// for ( ; sIter.hasNext() ; ) { +// Statement stmt = sIter.nextStatement() ; +// log.info("stmt " + stmt) ; +// } +// sIter.close() ; +// } + + //Model resultDbModel = ModelFactory.createModelRDBMaker(db.getJenaConnection()).createModel("fresh"); + resultDbModel.add(resultBaseModel); + + //resultModel = ModelFactory.createInfModel(ReasonerRegistry.getRDFSReasoner(), resultDbModel); + resultModel = ModelFactory.createDefaultModel(); + + Map prefixes = mapModel.getNsPrefixMap(); + log.info("Copy " + prefixes.size() + " prefixes from map model to persistent."); + for (String s : mapModel.getNsPrefixMap().keySet()) { + log.info(s + ": " + mapModel.getNsPrefixMap().get(s)); + } + resultModel.setNsPrefixes(prefixes); + mappingDocument.setPrefixes(prefixes); + + } else { + //resultModel = ModelFactory.createInfModel(ReasonerRegistry.getRDFSReasoner(), resultBaseModel); + resultModel = ModelFactory.createDefaultModel(); + resultModel.setNsPrefixes(mapModel.getNsPrefixMap()); + mappingDocument.setPrefixes(mapModel.getNsPrefixMap()); + } + } + + boolean contains(LinkedList logicalTableMappings, String uri) { + for (LogicalTableMapping logicalTableMapping : logicalTableMappings) { + if (StringUtils.equals(logicalTableMapping.getUri(), uri)) return true; + } + return false; + } + + public String getPropertiesFilename() { + return propertiesFilename; + } + + public void setPropertiesFilename(String propertiesFilename) { + this.propertiesFilename = propertiesFilename; + } + + public Database getDb() { + return db; + } + + public void setDb(Database db) { + this.db = db; + } + + public Util getUtil() { + return util; + } + + public void setUtil(Util util) { + this.util = util; + } + + public Model getResultModel() { + return resultModel; + } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/beans/Util.java b/src/main/java/gr/seab/r2rml/beans/Util.java similarity index 85% rename from src/main/java/gr/ekt/r2rml/beans/Util.java rename to src/main/java/gr/seab/r2rml/beans/Util.java index 97c31bd..b18af67 100644 --- a/src/main/java/gr/ekt/r2rml/beans/Util.java +++ b/src/main/java/gr/seab/r2rml/beans/Util.java @@ -9,11 +9,11 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. */ -package gr.ekt.r2rml.beans; +package gr.seab.r2rml.beans; -import gr.ekt.r2rml.entities.LogicalTableMapping; -import gr.ekt.r2rml.entities.Template; -import gr.ekt.r2rml.entities.sparql.LocalResultSet; +import gr.seab.r2rml.entities.LogicalTableMapping; +import gr.seab.r2rml.entities.Template; +import gr.seab.r2rml.entities.sparql.LocalResultSet; import java.sql.ResultSet; diff --git a/src/main/java/gr/ekt/r2rml/beans/UtilImpl.java b/src/main/java/gr/seab/r2rml/beans/UtilImpl.java similarity index 97% rename from src/main/java/gr/ekt/r2rml/beans/UtilImpl.java rename to src/main/java/gr/seab/r2rml/beans/UtilImpl.java index 7ae7a9d..5c56a71 100644 --- a/src/main/java/gr/ekt/r2rml/beans/UtilImpl.java +++ b/src/main/java/gr/seab/r2rml/beans/UtilImpl.java @@ -9,15 +9,15 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. */ -package gr.ekt.r2rml.beans; +package gr.seab.r2rml.beans; -import gr.ekt.r2rml.entities.LogicalTableMapping; -import gr.ekt.r2rml.entities.PredicateObjectMap; -import gr.ekt.r2rml.entities.Template; -import gr.ekt.r2rml.entities.TermType; -import gr.ekt.r2rml.entities.sparql.LocalResource; -import gr.ekt.r2rml.entities.sparql.LocalResultRow; -import gr.ekt.r2rml.entities.sparql.LocalResultSet; +import gr.seab.r2rml.entities.LogicalTableMapping; +import gr.seab.r2rml.entities.PredicateObjectMap; +import gr.seab.r2rml.entities.Template; +import gr.seab.r2rml.entities.TermType; +import gr.seab.r2rml.entities.sparql.LocalResource; +import gr.seab.r2rml.entities.sparql.LocalResultRow; +import gr.seab.r2rml.entities.sparql.LocalResultSet; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; diff --git a/src/main/java/gr/ekt/r2rml/beans/util/LogicalTableMappingComparator.java b/src/main/java/gr/seab/r2rml/beans/util/LogicalTableMappingComparator.java similarity index 81% rename from src/main/java/gr/ekt/r2rml/beans/util/LogicalTableMappingComparator.java rename to src/main/java/gr/seab/r2rml/beans/util/LogicalTableMappingComparator.java index b2bfa06..2e504ea 100644 --- a/src/main/java/gr/ekt/r2rml/beans/util/LogicalTableMappingComparator.java +++ b/src/main/java/gr/seab/r2rml/beans/util/LogicalTableMappingComparator.java @@ -1,28 +1,28 @@ -package gr.ekt.r2rml.beans.util; - -import gr.ekt.r2rml.entities.LogicalTableMapping; -import gr.ekt.r2rml.entities.PredicateObjectMap; - -import java.util.Comparator; - -import org.apache.commons.lang.StringUtils; - -public class LogicalTableMappingComparator implements Comparator { - - public int compare(LogicalTableMapping logicalTableMapping0, LogicalTableMapping logicalTableMapping1) { - - for (PredicateObjectMap p : logicalTableMapping0.getPredicateObjectMaps()) { - try { - if (StringUtils.isNotBlank(p.getRefObjectMap().getParentTriplesMapUri())) return 1; - } catch (Exception e) { } - } - - for (PredicateObjectMap p : logicalTableMapping1.getPredicateObjectMaps()) { - try { - if (StringUtils.isNotBlank(p.getRefObjectMap().getParentTriplesMapUri())) return -1; - } catch (Exception e) { } - } - - return 0; - } -} +package gr.seab.r2rml.beans.util; + +import gr.seab.r2rml.entities.LogicalTableMapping; +import gr.seab.r2rml.entities.PredicateObjectMap; + +import java.util.Comparator; + +import org.apache.commons.lang.StringUtils; + +public class LogicalTableMappingComparator implements Comparator { + + public int compare(LogicalTableMapping logicalTableMapping0, LogicalTableMapping logicalTableMapping1) { + + for (PredicateObjectMap p : logicalTableMapping0.getPredicateObjectMaps()) { + try { + if (StringUtils.isNotBlank(p.getRefObjectMap().getParentTriplesMapUri())) return 1; + } catch (Exception e) { } + } + + for (PredicateObjectMap p : logicalTableMapping1.getPredicateObjectMaps()) { + try { + if (StringUtils.isNotBlank(p.getRefObjectMap().getParentTriplesMapUri())) return -1; + } catch (Exception e) { } + } + + return 0; + } +} diff --git a/src/main/java/gr/ekt/r2rml/entities/LogicalTableMapping.java b/src/main/java/gr/seab/r2rml/entities/LogicalTableMapping.java similarity index 94% rename from src/main/java/gr/ekt/r2rml/entities/LogicalTableMapping.java rename to src/main/java/gr/seab/r2rml/entities/LogicalTableMapping.java index 86e9fdb..d7c1ef8 100644 --- a/src/main/java/gr/ekt/r2rml/entities/LogicalTableMapping.java +++ b/src/main/java/gr/seab/r2rml/entities/LogicalTableMapping.java @@ -1,115 +1,115 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -import java.util.ArrayList; - -import com.hp.hpl.jena.rdf.model.Statement; - -/** - * Logical table mappings can either map an arbitrary query or a single table name - * @author nkons - * - */ -public class LogicalTableMapping { - - /** - * The uri of the mapping in the mapping document - */ - private String uri; - - - /** - * The view that is associated with this mapping - */ - private LogicalTableView view; - - /** - * The subject mapping - */ - private SubjectMap subjectMap = new SubjectMap(); - - /** - * The list of the mappings that hold the respective predicate and object values for this mapping's subject - */ - private ArrayList predicateObjectMaps = new ArrayList(); - - /** - * TODO Hold the resulting triples - */ - private ArrayList triples; - - /** - * Default no-argument constructor - */ - public LogicalTableMapping() { - } - - /** - * @return the subjectMap - */ - public SubjectMap getSubjectMap() { - return subjectMap; - } - /** - * @param subjectMap the subjectMap to set - */ - public void setSubjectMap(SubjectMap subjectMap) { - this.subjectMap = subjectMap; - } - - /** - * @return the predicateObjectMaps - */ - public ArrayList getPredicateObjectMaps() { - return predicateObjectMaps; - } - /** - * @param predicateObjectMaps the predicateObjectMaps to set - */ - public void setPredicateObjectMaps( - ArrayList predicateObjectMaps) { - this.predicateObjectMaps = predicateObjectMaps; - } - /** - * @return the uri - */ - public String getUri() { - return uri; - } - /** - * @param uri the uri to set - */ - public void setUri(String uri) { - this.uri = uri; - } - /** - * @return the view - */ - public LogicalTableView getView() { - return view; - } - /** - * @param view the view to set - */ - public void setView(LogicalTableView view) { - this.view = view; - } - - public ArrayList getTriples() { - return triples; - } - - public void setTriples(ArrayList triples) { - this.triples = triples; - } -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +import java.util.ArrayList; + +import com.hp.hpl.jena.rdf.model.Statement; + +/** + * Logical table mappings can either map an arbitrary query or a single table name + * @author nkons + * + */ +public class LogicalTableMapping { + + /** + * The uri of the mapping in the mapping document + */ + private String uri; + + + /** + * The view that is associated with this mapping + */ + private LogicalTableView view; + + /** + * The subject mapping + */ + private SubjectMap subjectMap = new SubjectMap(); + + /** + * The list of the mappings that hold the respective predicate and object values for this mapping's subject + */ + private ArrayList predicateObjectMaps = new ArrayList(); + + /** + * TODO Hold the resulting triples + */ + private ArrayList triples; + + /** + * Default no-argument constructor + */ + public LogicalTableMapping() { + } + + /** + * @return the subjectMap + */ + public SubjectMap getSubjectMap() { + return subjectMap; + } + /** + * @param subjectMap the subjectMap to set + */ + public void setSubjectMap(SubjectMap subjectMap) { + this.subjectMap = subjectMap; + } + + /** + * @return the predicateObjectMaps + */ + public ArrayList getPredicateObjectMaps() { + return predicateObjectMaps; + } + /** + * @param predicateObjectMaps the predicateObjectMaps to set + */ + public void setPredicateObjectMaps( + ArrayList predicateObjectMaps) { + this.predicateObjectMaps = predicateObjectMaps; + } + /** + * @return the uri + */ + public String getUri() { + return uri; + } + /** + * @param uri the uri to set + */ + public void setUri(String uri) { + this.uri = uri; + } + /** + * @return the view + */ + public LogicalTableView getView() { + return view; + } + /** + * @param view the view to set + */ + public void setView(LogicalTableView view) { + this.view = view; + } + + public ArrayList getTriples() { + return triples; + } + + public void setTriples(ArrayList triples) { + this.triples = triples; + } +} diff --git a/src/main/java/gr/ekt/r2rml/entities/LogicalTableView.java b/src/main/java/gr/seab/r2rml/entities/LogicalTableView.java similarity index 90% rename from src/main/java/gr/ekt/r2rml/entities/LogicalTableView.java rename to src/main/java/gr/seab/r2rml/entities/LogicalTableView.java index 64c08c1..37c37c0 100644 --- a/src/main/java/gr/ekt/r2rml/entities/LogicalTableView.java +++ b/src/main/java/gr/seab/r2rml/entities/LogicalTableView.java @@ -1,65 +1,65 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -import gr.ekt.r2rml.entities.sql.SelectQuery; - -/** - * The logical view of the mapping can be either the fields of a table or an arbitrary sql query. - * @author nkons - * - */ -public class LogicalTableView { - - /** - * The uri of this view. It has a value for declared sql queries and is null when the mapping is on a table - */ - private String uri; - - /** - * The SQL Select query that is associated with this view - */ - private SelectQuery selectQuery; - - /** - * Default no-argument constructor - */ - public LogicalTableView() { - } - - /** - * @return the uri - */ - public String getUri() { - return uri; - } - /** - * @param uri the uri to set - */ - public void setUri(String uri) { - this.uri = uri; - } - /** - * @return the selectQuery - */ - public SelectQuery getSelectQuery() { - return selectQuery; - } - /** - * @param query the selectQuery to set - */ - public void setSelectQuery(SelectQuery selectQuery) { - this.selectQuery = selectQuery; - } - - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +import gr.seab.r2rml.entities.sql.SelectQuery; + +/** + * The logical view of the mapping can be either the fields of a table or an arbitrary sql query. + * @author nkons + * + */ +public class LogicalTableView { + + /** + * The uri of this view. It has a value for declared sql queries and is null when the mapping is on a table + */ + private String uri; + + /** + * The SQL Select query that is associated with this view + */ + private SelectQuery selectQuery; + + /** + * Default no-argument constructor + */ + public LogicalTableView() { + } + + /** + * @return the uri + */ + public String getUri() { + return uri; + } + /** + * @param uri the uri to set + */ + public void setUri(String uri) { + this.uri = uri; + } + /** + * @return the selectQuery + */ + public SelectQuery getSelectQuery() { + return selectQuery; + } + /** + * @param query the selectQuery to set + */ + public void setSelectQuery(SelectQuery selectQuery) { + this.selectQuery = selectQuery; + } + + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/MappingDocument.java b/src/main/java/gr/seab/r2rml/entities/MappingDocument.java similarity index 95% rename from src/main/java/gr/ekt/r2rml/entities/MappingDocument.java rename to src/main/java/gr/seab/r2rml/entities/MappingDocument.java index 188872f..a0cdd2a 100644 --- a/src/main/java/gr/ekt/r2rml/entities/MappingDocument.java +++ b/src/main/java/gr/seab/r2rml/entities/MappingDocument.java @@ -1,109 +1,109 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Holds the information of the mapping file. It is useful for retrieving values on demand. - * @author nkons - * - */ -public class MappingDocument { - private static final Logger log = LoggerFactory.getLogger(MappingDocument.class); - - private LinkedList logicalTableViews; - private LinkedList logicalTableMappings; - private Map prefixes; - private ArrayList timestamps = new ArrayList(); - - /** - * - */ - public MappingDocument() { - } - - public LogicalTableView findLogicalTableViewByUri(String uri) { - for (LogicalTableView logicalTableView : logicalTableViews) { - if (uri.equalsIgnoreCase(logicalTableView.getUri())) { - return logicalTableView; - } - } - return null; - } - - public LogicalTableMapping findLogicalTableMappingByUri(String uri) { - for (LogicalTableMapping logicalTableMapping : logicalTableMappings) { - if (uri.equalsIgnoreCase(logicalTableMapping.getUri())) { - return logicalTableMapping; - } - } - return null; - } - - public LogicalTableView findLogicalTableViewByQuery(String query) { - log.info("Searching for query " + query); - for (LogicalTableView logicalTableView : logicalTableViews) { - if (query.equalsIgnoreCase(logicalTableView.getSelectQuery().getQuery())) { - log.info("Found match"); - return logicalTableView; - } - } - return null; - } - - /** - * @return the logicalTableViews - */ - public LinkedList getLogicalTableViews() { - return logicalTableViews; - } - /** - * @param logicalTableViews the logicalTableViews to set - */ - public void setLogicalTableViews(LinkedList logicalTableViews) { - this.logicalTableViews = logicalTableViews; - } - /** - * @return the logicalTableMappings - */ - public LinkedList getLogicalTableMappings() { - return logicalTableMappings; - } - /** - * @param logicalTableMappings the logicalTableMappings to set - */ - public void setLogicalTableMappings(LinkedList logicalTableMappings) { - this.logicalTableMappings = logicalTableMappings; - } - - public Map getPrefixes() { - return prefixes; - } - - public void setPrefixes(Map prefixes) { - this.prefixes = prefixes; - } - - public ArrayList getTimestamps() { - return timestamps; - } - - public void setTimestamps(ArrayList timestamps) { - this.timestamps = timestamps; - } -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Holds the information of the mapping file. It is useful for retrieving values on demand. + * @author nkons + * + */ +public class MappingDocument { + private static final Logger log = LoggerFactory.getLogger(MappingDocument.class); + + private LinkedList logicalTableViews; + private LinkedList logicalTableMappings; + private Map prefixes; + private ArrayList timestamps = new ArrayList(); + + /** + * + */ + public MappingDocument() { + } + + public LogicalTableView findLogicalTableViewByUri(String uri) { + for (LogicalTableView logicalTableView : logicalTableViews) { + if (uri.equalsIgnoreCase(logicalTableView.getUri())) { + return logicalTableView; + } + } + return null; + } + + public LogicalTableMapping findLogicalTableMappingByUri(String uri) { + for (LogicalTableMapping logicalTableMapping : logicalTableMappings) { + if (uri.equalsIgnoreCase(logicalTableMapping.getUri())) { + return logicalTableMapping; + } + } + return null; + } + + public LogicalTableView findLogicalTableViewByQuery(String query) { + log.info("Searching for query " + query); + for (LogicalTableView logicalTableView : logicalTableViews) { + if (query.equalsIgnoreCase(logicalTableView.getSelectQuery().getQuery())) { + log.info("Found match"); + return logicalTableView; + } + } + return null; + } + + /** + * @return the logicalTableViews + */ + public LinkedList getLogicalTableViews() { + return logicalTableViews; + } + /** + * @param logicalTableViews the logicalTableViews to set + */ + public void setLogicalTableViews(LinkedList logicalTableViews) { + this.logicalTableViews = logicalTableViews; + } + /** + * @return the logicalTableMappings + */ + public LinkedList getLogicalTableMappings() { + return logicalTableMappings; + } + /** + * @param logicalTableMappings the logicalTableMappings to set + */ + public void setLogicalTableMappings(LinkedList logicalTableMappings) { + this.logicalTableMappings = logicalTableMappings; + } + + public Map getPrefixes() { + return prefixes; + } + + public void setPrefixes(Map prefixes) { + this.prefixes = prefixes; + } + + public ArrayList getTimestamps() { + return timestamps; + } + + public void setTimestamps(ArrayList timestamps) { + this.timestamps = timestamps; + } +} diff --git a/src/main/java/gr/ekt/r2rml/entities/PredicateObjectMap.java b/src/main/java/gr/seab/r2rml/entities/PredicateObjectMap.java similarity index 94% rename from src/main/java/gr/ekt/r2rml/entities/PredicateObjectMap.java rename to src/main/java/gr/seab/r2rml/entities/PredicateObjectMap.java index 05b8b0a..94871eb 100644 --- a/src/main/java/gr/ekt/r2rml/entities/PredicateObjectMap.java +++ b/src/main/java/gr/seab/r2rml/entities/PredicateObjectMap.java @@ -1,122 +1,122 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -import java.util.ArrayList; - -import com.hp.hpl.jena.datatypes.BaseDatatype; - -/** - * The mapping of predicates and respective objects for a specific subject. Objects come either from a column or from a template. - * @author nkons - * - */ -public class PredicateObjectMap { - - /** - * The predicate value(s). - */ - private ArrayList predicates; - - /** - * Holds the template through which the objects will be generated. - */ - private Template objectTemplate; - - /** - * Holds the value of an rr:column definition. It is null in the case of templates. - */ - private String objectColumn; - - /** - * The language of the literals to be created. - */ - private Template language; - - /** - * One of the XSD datatypes. - */ - private BaseDatatype dataType; - - /** - * If the predicateObjectMap is a rr:RefObjectMap, i.e. references a rr:parentTriplesMap, store the referenced uri, child and parent fields in an object. - */ - private RefObjectMap refObjectMap; - - /** - * - */ - public PredicateObjectMap() { - } - - /** - * @return the predicates - */ - public ArrayList getPredicates() { - return predicates; - } - /** - * @param predicates the predicates to set - */ - public void setPredicates(ArrayList predicates) { - this.predicates = predicates; - } - /** - * @return the objectTemplate - */ - public Template getObjectTemplate() { - return objectTemplate; - } - /** - * @param objectTemplate the objectTemplate to set - */ - public void setObjectTemplate(Template objectTemplate) { - this.objectTemplate = objectTemplate; - } - /** - * @return the objectColumn - */ - public String getObjectColumn() { - return objectColumn; - } - /** - * @param objectColumn the objectColumn to set - */ - public void setObjectColumn(String objectColumn) { - this.objectColumn = objectColumn; - } - - public Template getLanguage() { - return language; - } - - public void setLanguage(Template language) { - this.language = language; - } - - public BaseDatatype getDataType() { - return dataType; - } - - public void setDataType(BaseDatatype dataType) { - this.dataType = dataType; - } - - public RefObjectMap getRefObjectMap() { - return refObjectMap; - } - - public void setRefObjectMap(RefObjectMap refObjectMap) { - this.refObjectMap = refObjectMap; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +import java.util.ArrayList; + +import com.hp.hpl.jena.datatypes.BaseDatatype; + +/** + * The mapping of predicates and respective objects for a specific subject. Objects come either from a column or from a template. + * @author nkons + * + */ +public class PredicateObjectMap { + + /** + * The predicate value(s). + */ + private ArrayList predicates; + + /** + * Holds the template through which the objects will be generated. + */ + private Template objectTemplate; + + /** + * Holds the value of an rr:column definition. It is null in the case of templates. + */ + private String objectColumn; + + /** + * The language of the literals to be created. + */ + private Template language; + + /** + * One of the XSD datatypes. + */ + private BaseDatatype dataType; + + /** + * If the predicateObjectMap is a rr:RefObjectMap, i.e. references a rr:parentTriplesMap, store the referenced uri, child and parent fields in an object. + */ + private RefObjectMap refObjectMap; + + /** + * + */ + public PredicateObjectMap() { + } + + /** + * @return the predicates + */ + public ArrayList getPredicates() { + return predicates; + } + /** + * @param predicates the predicates to set + */ + public void setPredicates(ArrayList predicates) { + this.predicates = predicates; + } + /** + * @return the objectTemplate + */ + public Template getObjectTemplate() { + return objectTemplate; + } + /** + * @param objectTemplate the objectTemplate to set + */ + public void setObjectTemplate(Template objectTemplate) { + this.objectTemplate = objectTemplate; + } + /** + * @return the objectColumn + */ + public String getObjectColumn() { + return objectColumn; + } + /** + * @param objectColumn the objectColumn to set + */ + public void setObjectColumn(String objectColumn) { + this.objectColumn = objectColumn; + } + + public Template getLanguage() { + return language; + } + + public void setLanguage(Template language) { + this.language = language; + } + + public BaseDatatype getDataType() { + return dataType; + } + + public void setDataType(BaseDatatype dataType) { + this.dataType = dataType; + } + + public RefObjectMap getRefObjectMap() { + return refObjectMap; + } + + public void setRefObjectMap(RefObjectMap refObjectMap) { + this.refObjectMap = refObjectMap; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/RefObjectMap.java b/src/main/java/gr/seab/r2rml/entities/RefObjectMap.java similarity index 89% rename from src/main/java/gr/ekt/r2rml/entities/RefObjectMap.java rename to src/main/java/gr/seab/r2rml/entities/RefObjectMap.java index b77dc15..766f5ea 100644 --- a/src/main/java/gr/ekt/r2rml/entities/RefObjectMap.java +++ b/src/main/java/gr/seab/r2rml/entities/RefObjectMap.java @@ -1,39 +1,39 @@ -package gr.ekt.r2rml.entities; - -public class RefObjectMap { - - private String parentTriplesMapUri; - - private String child; - - private String parent; - - public RefObjectMap() { - } - - public String getParentTriplesMapUri() { - return parentTriplesMapUri; - } - - public void setParentTriplesMapUri(String parentTriplesMapUri) { - this.parentTriplesMapUri = parentTriplesMapUri; - } - - public String getChild() { - return child; - } - - public void setChild(String child) { - this.child = child; - } - - public String getParent() { - return parent; - } - - public void setParent(String parent) { - this.parent = parent; - } - - -} +package gr.seab.r2rml.entities; + +public class RefObjectMap { + + private String parentTriplesMapUri; + + private String child; + + private String parent; + + public RefObjectMap() { + } + + public String getParentTriplesMapUri() { + return parentTriplesMapUri; + } + + public void setParentTriplesMapUri(String parentTriplesMapUri) { + this.parentTriplesMapUri = parentTriplesMapUri; + } + + public String getChild() { + return child; + } + + public void setChild(String child) { + this.child = child; + } + + public String getParent() { + return parent; + } + + public void setParent(String parent) { + this.parent = parent; + } + + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/SubjectMap.java b/src/main/java/gr/seab/r2rml/entities/SubjectMap.java similarity index 91% rename from src/main/java/gr/ekt/r2rml/entities/SubjectMap.java rename to src/main/java/gr/seab/r2rml/entities/SubjectMap.java index b547f86..e1b7e8a 100644 --- a/src/main/java/gr/ekt/r2rml/entities/SubjectMap.java +++ b/src/main/java/gr/seab/r2rml/entities/SubjectMap.java @@ -1,72 +1,72 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -import gr.ekt.r2rml.entities.sql.SelectQuery; - -import java.util.ArrayList; - -/** - * The subject map holds information about how to create the subjects of the mappings. - * @see LogicalTableMapping - * @author nkons - * - */ -public class SubjectMap { - - private Template template; - private SelectQuery selectQuery; - private ArrayList classUris; - - /** - * Default constructor. - */ - public SubjectMap() { - } - - /** - * @return the template - */ - public Template getTemplate() { - return template; - } - /** - * @param template the template to set - */ - public void setTemplate(Template template) { - this.template = template; - } - /** - * @return the selectQuery - */ - public SelectQuery getSelectQuery() { - return selectQuery; - } - /** - * @param selectQuery the selectQuery to set - */ - public void setSelectQuery(SelectQuery selectQuery) { - this.selectQuery = selectQuery; - } - /** - * @return the classUris - */ - public ArrayList getClassUris() { - return classUris; - } - /** - * @param classUris the classUris to set - */ - public void setClassUris(ArrayList classUris) { - this.classUris = classUris; - } -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +import gr.seab.r2rml.entities.sql.SelectQuery; + +import java.util.ArrayList; + +/** + * The subject map holds information about how to create the subjects of the mappings. + * @see LogicalTableMapping + * @author nkons + * + */ +public class SubjectMap { + + private Template template; + private SelectQuery selectQuery; + private ArrayList classUris; + + /** + * Default constructor. + */ + public SubjectMap() { + } + + /** + * @return the template + */ + public Template getTemplate() { + return template; + } + /** + * @param template the template to set + */ + public void setTemplate(Template template) { + this.template = template; + } + /** + * @return the selectQuery + */ + public SelectQuery getSelectQuery() { + return selectQuery; + } + /** + * @param selectQuery the selectQuery to set + */ + public void setSelectQuery(SelectQuery selectQuery) { + this.selectQuery = selectQuery; + } + /** + * @return the classUris + */ + public ArrayList getClassUris() { + return classUris; + } + /** + * @param classUris the classUris to set + */ + public void setClassUris(ArrayList classUris) { + this.classUris = classUris; + } +} diff --git a/src/main/java/gr/ekt/r2rml/entities/Template.java b/src/main/java/gr/seab/r2rml/entities/Template.java similarity index 95% rename from src/main/java/gr/ekt/r2rml/entities/Template.java rename to src/main/java/gr/seab/r2rml/entities/Template.java index 1503255..319c8ee 100644 --- a/src/main/java/gr/ekt/r2rml/entities/Template.java +++ b/src/main/java/gr/seab/r2rml/entities/Template.java @@ -1,155 +1,155 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -import java.util.ArrayList; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.hp.hpl.jena.rdf.model.Model; - -/** - * The template is a component of the form http://data.example.com/film/{film_id}. Encloses fields in brackets. - * @see SubjectMap - * @see PredicateObjectMap - * @author nkons - * - */ -public class Template { - private static final Logger log = LoggerFactory.getLogger(Template.class); - /** - * The initial template text - */ - private String text; - - /** - * The fields, enclosed in brackets. Must correspond to database fields - */ - private ArrayList fields; - - /** - * Returns literal, blank node or iri. - */ - private TermType termType; - - /** - * Is used as a prefix, in case of IRIs. - */ - private String namespace; - - /** - * The model. - */ - private Model model; - - /** - * Default constructor. Once called, it finds the included fields. - */ - public Template(String text, TermType termType, String namespace, Model model) { - this.text = text; - this.fields = createTemplateFields(); - this.termType = termType; - this.namespace = namespace; - this.model = model; - - String msg = "Template has " + fields.size() + ((fields.size() == 1)? " field: " : " fields: "); - for (String f : fields) { - msg += f + ", "; - } - if (msg.lastIndexOf(',') > 0) { - msg = msg.substring(0, msg.lastIndexOf(',')); - } - //log.info(msg); - } - - public ArrayList createTemplateFields() { - ArrayList results = new ArrayList(); - String template = this.text; - //log.info("\\{"); - template = template.replaceAll("\\\\\\{", " "); //remove escaped braces \{ that could mix things - //log.info("new template " + template); - while (template.indexOf('{') != -1) { - int from = template.indexOf('{') + 1; - int to = template.indexOf('}'); - results.add(template.substring(from, to)); - //log.info("adding variable '" + template.substring(from, to) + "'"); - template = template.substring(to + 1, template.length()); - } - return results; - } - - public boolean isUri() { - String s = text; - - for (String key : model.getNsPrefixMap().keySet()) { - //log.info("checking if " + s + " contains '" + key + ":' or " + model.getNsPrefixMap().get(key)); - if (s.contains(key + ":") || s.contains(model.getNsPrefixMap().get(key))) return true; - } - - if (s.contains("http")) { - return true; - } else { - return false; - } - } - - /** - * @return the text - */ - public String getText() { - return text; - } - /** - * @param text the text to set - */ - public void setText(String text) { - this.text = text; - } - /** - * @return the fields - */ - public ArrayList getFields() { - return fields; - } - /** - * @param fields the fields to set - */ - public void setFields(ArrayList fields) { - this.fields = fields; - } - - public TermType getTermType() { - return termType; - } - - public void setTermType(TermType termType) { - this.termType = termType; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - public Model getModel() { - return model; - } - - public void setModel(Model model) { - this.model = model; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +import java.util.ArrayList; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.hp.hpl.jena.rdf.model.Model; + +/** + * The template is a component of the form http://data.example.com/film/{film_id}. Encloses fields in brackets. + * @see SubjectMap + * @see PredicateObjectMap + * @author nkons + * + */ +public class Template { + private static final Logger log = LoggerFactory.getLogger(Template.class); + /** + * The initial template text + */ + private String text; + + /** + * The fields, enclosed in brackets. Must correspond to database fields + */ + private ArrayList fields; + + /** + * Returns literal, blank node or iri. + */ + private TermType termType; + + /** + * Is used as a prefix, in case of IRIs. + */ + private String namespace; + + /** + * The model. + */ + private Model model; + + /** + * Default constructor. Once called, it finds the included fields. + */ + public Template(String text, TermType termType, String namespace, Model model) { + this.text = text; + this.fields = createTemplateFields(); + this.termType = termType; + this.namespace = namespace; + this.model = model; + + String msg = "Template has " + fields.size() + ((fields.size() == 1)? " field: " : " fields: "); + for (String f : fields) { + msg += f + ", "; + } + if (msg.lastIndexOf(',') > 0) { + msg = msg.substring(0, msg.lastIndexOf(',')); + } + //log.info(msg); + } + + public ArrayList createTemplateFields() { + ArrayList results = new ArrayList(); + String template = this.text; + //log.info("\\{"); + template = template.replaceAll("\\\\\\{", " "); //remove escaped braces \{ that could mix things + //log.info("new template " + template); + while (template.indexOf('{') != -1) { + int from = template.indexOf('{') + 1; + int to = template.indexOf('}'); + results.add(template.substring(from, to)); + //log.info("adding variable '" + template.substring(from, to) + "'"); + template = template.substring(to + 1, template.length()); + } + return results; + } + + public boolean isUri() { + String s = text; + + for (String key : model.getNsPrefixMap().keySet()) { + //log.info("checking if " + s + " contains '" + key + ":' or " + model.getNsPrefixMap().get(key)); + if (s.contains(key + ":") || s.contains(model.getNsPrefixMap().get(key))) return true; + } + + if (s.contains("http")) { + return true; + } else { + return false; + } + } + + /** + * @return the text + */ + public String getText() { + return text; + } + /** + * @param text the text to set + */ + public void setText(String text) { + this.text = text; + } + /** + * @return the fields + */ + public ArrayList getFields() { + return fields; + } + /** + * @param fields the fields to set + */ + public void setFields(ArrayList fields) { + this.fields = fields; + } + + public TermType getTermType() { + return termType; + } + + public void setTermType(TermType termType) { + this.termType = termType; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public Model getModel() { + return model; + } + + public void setModel(Model model) { + this.model = model; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/TermType.java b/src/main/java/gr/seab/r2rml/entities/TermType.java similarity index 91% rename from src/main/java/gr/ekt/r2rml/entities/TermType.java rename to src/main/java/gr/seab/r2rml/entities/TermType.java index fc7973d..ec2423b 100644 --- a/src/main/java/gr/ekt/r2rml/entities/TermType.java +++ b/src/main/java/gr/seab/r2rml/entities/TermType.java @@ -1,26 +1,26 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities; - -/** - * RDF resource types - * @author nkons - * - */ -public enum TermType { - - IRI, - - BLANKNODE, - - LITERAL; -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities; + +/** + * RDF resource types + * @author nkons + * + */ +public enum TermType { + + IRI, + + BLANKNODE, + + LITERAL; +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalResource.java b/src/main/java/gr/seab/r2rml/entities/sparql/LocalResource.java similarity index 94% rename from src/main/java/gr/ekt/r2rml/entities/sparql/LocalResource.java rename to src/main/java/gr/seab/r2rml/entities/sparql/LocalResource.java index ba162ea..667801e 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalResource.java +++ b/src/main/java/gr/seab/r2rml/entities/sparql/LocalResource.java @@ -1,215 +1,215 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sparql; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.rdf.model.Resource; - -/** - * A local resource, and associated information. Is contained in rows of a Sparql query resultset - * @see LocalResultRow - * @author nkons - * - */ -public class LocalResource { - private static final Logger log = LoggerFactory.getLogger(LocalResource.class); - private Map prefixMap; - - private String uri; - private String uriEncoded; - private String localName; - private String namespace; - private boolean resource = Boolean.FALSE; - private boolean literal = Boolean.FALSE; - private boolean blankNode = Boolean.FALSE; - - /** - * Create a local resource based on a jena model resource - */ - public LocalResource(Resource r) { - //log.info("About to create " + r.getURI()); - if (r.isAnon()) { - log.info("Found a blank node"); - this.blankNode = Boolean.TRUE; - this.uri = null; - this.uriEncoded = null; - this.localName = r.getId().getLabelString(); - this.namespace = null; - } else { - //log.info("Node URI: " + r.getURI()); - this.prefixMap = r.getModel().getNsPrefixMap(); - - if (r.isLiteral()) { - this.literal = Boolean.TRUE; - //log.info("Found a literal with URI: " + r.getURI()); - } else if (r.isResource()) { - //log.info("Found a resource with URI: " + r.getURI()); - this.resource = Boolean.TRUE; - this.uri = r.getURI(); - try { - uriEncoded = URLEncoder.encode(r.getURI(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - namespace = r.getNameSpace(); - - if (namespace.contains("#")) { - String prefix = findPrefixByUri(namespace); - if (prefix == null) { - this.localName = r.getURI(); - } else { - this.localName = findPrefixByUri(namespace) + ":" + r.getLocalName(); - } - - } else { - this.localName = r.getURI(); - } - log.info("Local resource: " + localName); - } - } - } - - /** - * Non-argument constructor. - */ - public LocalResource() { - } - - /** - * Create a literal resource - */ - public LocalResource(Literal l) { - this.literal = Boolean.TRUE; - - this.localName = l.getLexicalForm(); - //System.out.println("f from l" + localName); - } - - public LocalResource(String s) { - //log.info("Creating literal resource " + s); - this.literal = Boolean.TRUE; - - this.localName = s; - } - - public String findPrefixByUri(String uri) { - if (!prefixMap.isEmpty()) { - for (String prefix : prefixMap.keySet()) { - if (prefixMap.get(prefix).equals(uri)) { - return prefix; - } - } - return null; - } else { - return null; - } - } - - /** - * @return the uri - */ - public String getUri() { - return uri; - } - /** - * @param uri the uri to set - */ - public void setUri(String uri) { - this.uri = uri; - } - /** - * @return the uriEncoded - */ - public String getUriEncoded() { - return uriEncoded; - } - /** - * @param uriEncoded the uriEncoded to set - */ - public void setUriEncoded(String uriEncoded) { - this.uriEncoded = uriEncoded; - } - /** - * @return the localName - */ - public String getLocalName() { - return localName; - } - /** - * @param localName the localName to set - */ - public void setLocalName(String localName) { - this.localName = localName; - } - /** - * @return the namespace - */ - public String getNamespace() { - return namespace; - } - /** - * @param namespace the namespace to set - */ - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - /** - * @return the resource - */ - public boolean getResource() { - return resource; - } - - /** - * @param resource the resource to set - */ - public void setResource(boolean resource) { - this.resource = resource; - } - - /** - * @return the literal - */ - public boolean getLiteral() { - return literal; - } - - /** - * @param literal the literal to set - */ - public void setLiteral(boolean literal) { - this.literal = literal; - } - - /** - * @return the blankNode - */ - public boolean getBlankNode() { - return blankNode; - } - - /** - * @param blankNode the blankNode to set - */ - public void setBlankNode(boolean blankNode) { - this.blankNode = blankNode; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sparql; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.rdf.model.Resource; + +/** + * A local resource, and associated information. Is contained in rows of a Sparql query resultset + * @see LocalResultRow + * @author nkons + * + */ +public class LocalResource { + private static final Logger log = LoggerFactory.getLogger(LocalResource.class); + private Map prefixMap; + + private String uri; + private String uriEncoded; + private String localName; + private String namespace; + private boolean resource = Boolean.FALSE; + private boolean literal = Boolean.FALSE; + private boolean blankNode = Boolean.FALSE; + + /** + * Create a local resource based on a jena model resource + */ + public LocalResource(Resource r) { + //log.info("About to create " + r.getURI()); + if (r.isAnon()) { + log.info("Found a blank node"); + this.blankNode = Boolean.TRUE; + this.uri = null; + this.uriEncoded = null; + this.localName = r.getId().getLabelString(); + this.namespace = null; + } else { + //log.info("Node URI: " + r.getURI()); + this.prefixMap = r.getModel().getNsPrefixMap(); + + if (r.isLiteral()) { + this.literal = Boolean.TRUE; + //log.info("Found a literal with URI: " + r.getURI()); + } else if (r.isResource()) { + //log.info("Found a resource with URI: " + r.getURI()); + this.resource = Boolean.TRUE; + this.uri = r.getURI(); + try { + uriEncoded = URLEncoder.encode(r.getURI(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + namespace = r.getNameSpace(); + + if (namespace.contains("#")) { + String prefix = findPrefixByUri(namespace); + if (prefix == null) { + this.localName = r.getURI(); + } else { + this.localName = findPrefixByUri(namespace) + ":" + r.getLocalName(); + } + + } else { + this.localName = r.getURI(); + } + log.info("Local resource: " + localName); + } + } + } + + /** + * Non-argument constructor. + */ + public LocalResource() { + } + + /** + * Create a literal resource + */ + public LocalResource(Literal l) { + this.literal = Boolean.TRUE; + + this.localName = l.getLexicalForm(); + //System.out.println("f from l" + localName); + } + + public LocalResource(String s) { + //log.info("Creating literal resource " + s); + this.literal = Boolean.TRUE; + + this.localName = s; + } + + public String findPrefixByUri(String uri) { + if (!prefixMap.isEmpty()) { + for (String prefix : prefixMap.keySet()) { + if (prefixMap.get(prefix).equals(uri)) { + return prefix; + } + } + return null; + } else { + return null; + } + } + + /** + * @return the uri + */ + public String getUri() { + return uri; + } + /** + * @param uri the uri to set + */ + public void setUri(String uri) { + this.uri = uri; + } + /** + * @return the uriEncoded + */ + public String getUriEncoded() { + return uriEncoded; + } + /** + * @param uriEncoded the uriEncoded to set + */ + public void setUriEncoded(String uriEncoded) { + this.uriEncoded = uriEncoded; + } + /** + * @return the localName + */ + public String getLocalName() { + return localName; + } + /** + * @param localName the localName to set + */ + public void setLocalName(String localName) { + this.localName = localName; + } + /** + * @return the namespace + */ + public String getNamespace() { + return namespace; + } + /** + * @param namespace the namespace to set + */ + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + /** + * @return the resource + */ + public boolean getResource() { + return resource; + } + + /** + * @param resource the resource to set + */ + public void setResource(boolean resource) { + this.resource = resource; + } + + /** + * @return the literal + */ + public boolean getLiteral() { + return literal; + } + + /** + * @param literal the literal to set + */ + public void setLiteral(boolean literal) { + this.literal = literal; + } + + /** + * @return the blankNode + */ + public boolean getBlankNode() { + return blankNode; + } + + /** + * @param blankNode the blankNode to set + */ + public void setBlankNode(boolean blankNode) { + this.blankNode = blankNode; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalResultRow.java b/src/main/java/gr/seab/r2rml/entities/sparql/LocalResultRow.java similarity index 92% rename from src/main/java/gr/ekt/r2rml/entities/sparql/LocalResultRow.java rename to src/main/java/gr/seab/r2rml/entities/sparql/LocalResultRow.java index 4802c8c..6d5ae02 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalResultRow.java +++ b/src/main/java/gr/seab/r2rml/entities/sparql/LocalResultRow.java @@ -1,43 +1,43 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sparql; - -import java.util.ArrayList; - -/** - * Holds information about a row in a SPARQL query resultset - * @author nkons - * - */ -public class LocalResultRow { - - private ArrayList resources; - - /** - * - */ - public LocalResultRow() { - } - - /** - * @return the resources - */ - public ArrayList getResources() { - return resources; - } - /** - * @param resources the resources to set - */ - public void setResources(ArrayList resources) { - this.resources = resources; - } -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sparql; + +import java.util.ArrayList; + +/** + * Holds information about a row in a SPARQL query resultset + * @author nkons + * + */ +public class LocalResultRow { + + private ArrayList resources; + + /** + * + */ + public LocalResultRow() { + } + + /** + * @return the resources + */ + public ArrayList getResources() { + return resources; + } + /** + * @param resources the resources to set + */ + public void setResources(ArrayList resources) { + this.resources = resources; + } +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalResultSet.java b/src/main/java/gr/seab/r2rml/entities/sparql/LocalResultSet.java similarity index 92% rename from src/main/java/gr/ekt/r2rml/entities/sparql/LocalResultSet.java rename to src/main/java/gr/seab/r2rml/entities/sparql/LocalResultSet.java index 1d1aa07..6922879 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalResultSet.java +++ b/src/main/java/gr/seab/r2rml/entities/sparql/LocalResultSet.java @@ -1,59 +1,59 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sparql; - -import java.util.ArrayList; -import java.util.List; - -/** - * Holds information about a Sparql query results - * @see LocalResultRow - * @author nkons - * - */ -public class LocalResultSet { - - private List variables; - private ArrayList rows; - - /** - * - */ - public LocalResultSet() { - } - - /** - * @return the variables - */ - public List getVariables() { - return variables; - } - /** - * @param variables the variables to set - */ - public void setVariables(List variables) { - this.variables = variables; - } - /** - * @return the rows - */ - public ArrayList getRows() { - return rows; - } - /** - * @param rows the rows to set - */ - public void setRows(ArrayList rows) { - this.rows = rows; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sparql; + +import java.util.ArrayList; +import java.util.List; + +/** + * Holds information about a Sparql query results + * @see LocalResultRow + * @author nkons + * + */ +public class LocalResultSet { + + private List variables; + private ArrayList rows; + + /** + * + */ + public LocalResultSet() { + } + + /** + * @return the variables + */ + public List getVariables() { + return variables; + } + /** + * @param variables the variables to set + */ + public void setVariables(List variables) { + this.variables = variables; + } + /** + * @return the rows + */ + public ArrayList getRows() { + return rows; + } + /** + * @param rows the rows to set + */ + public void setRows(ArrayList rows) { + this.rows = rows; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalTriple.java b/src/main/java/gr/seab/r2rml/entities/sparql/LocalTriple.java similarity index 93% rename from src/main/java/gr/ekt/r2rml/entities/sparql/LocalTriple.java rename to src/main/java/gr/seab/r2rml/entities/sparql/LocalTriple.java index 0ece9c8..9e6e08b 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sparql/LocalTriple.java +++ b/src/main/java/gr/seab/r2rml/entities/sparql/LocalTriple.java @@ -1,70 +1,70 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sparql; - -/** - * @author nkons - * - */ -public class LocalTriple { - - private LocalResource subject; - private LocalResource property; - private LocalResource object; - - /** - * - */ - public LocalTriple(LocalResource subject, LocalResource property, LocalResource object) { - this.subject = subject; - this.property = property; - this.object = object; - } - - /** - * @return the subject - */ - public LocalResource getSubject() { - return subject; - } - /** - * @param subject the subject to set - */ - public void setSubject(LocalResource subject) { - this.subject = subject; - } - /** - * @return the property - */ - public LocalResource getProperty() { - return property; - } - /** - * @param property the property to set - */ - public void setProperty(LocalResource property) { - this.property = property; - } - /** - * @return the object - */ - public LocalResource getObject() { - return object; - } - /** - * @param object the object to set - */ - public void setObject(LocalResource object) { - this.object = object; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sparql; + +/** + * @author nkons + * + */ +public class LocalTriple { + + private LocalResource subject; + private LocalResource property; + private LocalResource object; + + /** + * + */ + public LocalTriple(LocalResource subject, LocalResource property, LocalResource object) { + this.subject = subject; + this.property = property; + this.object = object; + } + + /** + * @return the subject + */ + public LocalResource getSubject() { + return subject; + } + /** + * @param subject the subject to set + */ + public void setSubject(LocalResource subject) { + this.subject = subject; + } + /** + * @return the property + */ + public LocalResource getProperty() { + return property; + } + /** + * @param property the property to set + */ + public void setProperty(LocalResource property) { + this.property = property; + } + /** + * @return the object + */ + public LocalResource getObject() { + return object; + } + /** + * @param object the object to set + */ + public void setObject(LocalResource object) { + this.object = object; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sparql/SparqlQuery.java b/src/main/java/gr/seab/r2rml/entities/sparql/SparqlQuery.java similarity index 92% rename from src/main/java/gr/ekt/r2rml/entities/sparql/SparqlQuery.java rename to src/main/java/gr/seab/r2rml/entities/sparql/SparqlQuery.java index db507a8..6ff26af 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sparql/SparqlQuery.java +++ b/src/main/java/gr/seab/r2rml/entities/sparql/SparqlQuery.java @@ -1,65 +1,65 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sparql; - - -import java.io.Serializable; - -/** - * Holds information about a Sparql query and the resultset it returns - * @see LocalResultSet - * @author nkons - * - */ -public class SparqlQuery implements Serializable { - - private static final long serialVersionUID = 7934008387554302L; - - private String query; - private LocalResultSet resultSet; - - /** - * - */ - public SparqlQuery() { - } - - - /** - * @return the query - */ - public String getQuery() { - return query; - } - - /** - * @param query the query to set - */ - public void setQuery(String query) { - this.query = query; - } - - /** - * @return the resultSet - */ - public LocalResultSet getResultSet() { - return resultSet; - } - - /** - * @param resultSet the resultSet to set - */ - public void setResultSet(LocalResultSet resultSet) { - this.resultSet = resultSet; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sparql; + + +import java.io.Serializable; + +/** + * Holds information about a Sparql query and the resultset it returns + * @see LocalResultSet + * @author nkons + * + */ +public class SparqlQuery implements Serializable { + + private static final long serialVersionUID = 7934008387554302L; + + private String query; + private LocalResultSet resultSet; + + /** + * + */ + public SparqlQuery() { + } + + + /** + * @return the query + */ + public String getQuery() { + return query; + } + + /** + * @param query the query to set + */ + public void setQuery(String query) { + this.query = query; + } + + /** + * @return the resultSet + */ + public LocalResultSet getResultSet() { + return resultSet; + } + + /** + * @param resultSet the resultSet to set + */ + public void setResultSet(LocalResultSet resultSet) { + this.resultSet = resultSet; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sql/SelectField.java b/src/main/java/gr/seab/r2rml/entities/sql/SelectField.java similarity index 92% rename from src/main/java/gr/ekt/r2rml/entities/sql/SelectField.java rename to src/main/java/gr/seab/r2rml/entities/sql/SelectField.java index e12ac38..cbd2e01 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sql/SelectField.java +++ b/src/main/java/gr/seab/r2rml/entities/sql/SelectField.java @@ -1,75 +1,75 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sql; - - -/** - * A select field in an sql select query - * @see SelectQuery - * @author nkons - * - */ -public class SelectField { - - private String name; - private String alias; - - private SelectTable table; - - /** - * Default no-argument constructor. - */ - public SelectField() { - } - - /** - * @return the name - */ - public String getName() { - return name; - } - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the alias - */ - public String getAlias() { - return alias; - } - - /** - * @param alias the alias to set - */ - public void setAlias(String alias) { - this.alias = alias; - } - - /** - * @return the table - */ - public SelectTable getTable() { - return table; - } - - /** - * @param table the table to set - */ - public void setTable(SelectTable table) { - this.table = table; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sql; + + +/** + * A select field in an sql select query + * @see SelectQuery + * @author nkons + * + */ +public class SelectField { + + private String name; + private String alias; + + private SelectTable table; + + /** + * Default no-argument constructor. + */ + public SelectField() { + } + + /** + * @return the name + */ + public String getName() { + return name; + } + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the alias + */ + public String getAlias() { + return alias; + } + + /** + * @param alias the alias to set + */ + public void setAlias(String alias) { + this.alias = alias; + } + + /** + * @return the table + */ + public SelectTable getTable() { + return table; + } + + /** + * @param table the table to set + */ + public void setTable(SelectTable table) { + this.table = table; + } + +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sql/SelectQuery.java b/src/main/java/gr/seab/r2rml/entities/sql/SelectQuery.java similarity index 96% rename from src/main/java/gr/ekt/r2rml/entities/sql/SelectQuery.java rename to src/main/java/gr/seab/r2rml/entities/sql/SelectQuery.java index 668af61..63f1e80 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sql/SelectQuery.java +++ b/src/main/java/gr/seab/r2rml/entities/sql/SelectQuery.java @@ -1,207 +1,207 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sql; - - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Properties; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Represents an sql select query - * @author nkons - * - */ -public class SelectQuery { - private static final Logger log = LoggerFactory.getLogger(SelectQuery.class); - - private String query; - private ArrayList fields; - private ArrayList tables; - - private final Properties p; - /** - * - */ - public SelectQuery(String query, Properties p) { - this.p = p; - log.info("Processing query: '" + query + "'"); - if (query.indexOf(';') != -1) query = query.replace(';', ' '); - - this.query = query; - this.tables = createSelectTables(query); //tables must be created first, they are needed to create the fields - this.fields = createSelectFields(query); - } - - public SelectField findFieldByNameOrAlias(String field) { - for (SelectField f : fields) { - if (field.equalsIgnoreCase(f.getName()) || field.equalsIgnoreCase(f.getAlias())) { - return f; - } - } - return null; - } - - public SelectTable findTableByNameOrAlias(String search) { - for (SelectTable table : tables) { - if (search.equalsIgnoreCase(table.getName()) || search.equalsIgnoreCase(table.getAlias()) ) { - return table; - } - } - return null; - } - - public ArrayList createSelectFields(String q) { - ArrayList results = new ArrayList(); - - int start = q.toUpperCase().indexOf("SELECT") + 7; - int end = q.toUpperCase().indexOf("FROM"); - - ArrayList fields = new ArrayList(Arrays.asList(q.substring(start, end).split(","))); - ArrayList processedFields = new ArrayList(); - - for (int i = 0; i < fields.size(); i++) { - if (createSelectFieldTable(fields.get(i).trim()) != null) { - processedFields.add(fields.get(i)); - } - } - - for (String field : processedFields) { - SelectField f = new SelectField(); - f.setName(field.trim()); - f.setTable(createSelectFieldTable(field.trim())); - f.setAlias(createAlias(field).trim()); - log.info("Adding field with: name '" + f.getName() + "', table '" + f.getTable().getName() + "', alias '" + f.getAlias() + "'"); - results.add(f); - } - return results; - } - - public ArrayList createSelectTables(String q) { - ArrayList results = new ArrayList(); - - int start = q.toUpperCase().indexOf("FROM") + 4; - ArrayList tables = new ArrayList(); - - //split in spaces - String tokens[] = q.substring(start).split("\\s+"); - - //if there are aliases - if (StringUtils.containsIgnoreCase(q.substring(start), "AS")) { - for (int i = 0; i < tokens.length; i++) { - if ("AS".equalsIgnoreCase(tokens[i])) { - if (tokens[i+1].endsWith(",")) { - tokens[i+1] = tokens[i+1].substring(0, tokens[i+1].indexOf(",")); - } - tables.add(tokens[i-1] + " " + tokens[i] + " " + tokens[i+1]); - } - } - } else { - //otherwise, split in commas - int end = StringUtils.indexOfIgnoreCase(q, "WHERE"); - if (end == -1) end = q.length(); - tables = new ArrayList(Arrays.asList(q.substring(start, end).split(","))); - } - - for (String table : tables) { - SelectTable selectTable = new SelectTable(); - selectTable.setName(table.trim()); - selectTable.setAlias(createAlias(selectTable.getName()).trim()); - log.info("Adding table with: name '" + selectTable.getName() + "', alias '" + selectTable.getAlias() + "'"); - results.add(selectTable); - } - return results; - } - - public SelectTable createSelectFieldTable(String field) { - int dot = field.indexOf('.'); - if (dot == -1) { - return tables.get(0); //If no table specified for the field, there should be only one table in the query - } else { - //if it is an SQL function, the table name should start after the parenthesis. - int start = (field.indexOf("(") > -1? field.indexOf("(") + 1: 0); - String table = field.substring(start, dot); - return findTableByNameOrAlias(table); - } - } - - public String createAlias(String field) { - String result = ""; - - int as = field.toUpperCase().indexOf("AS"); - if (as == -1) { - //When there is no aliases, Postgres returns the column, named after the function alone while Mysql will - //name the column after the function plus the argument. i.e. in the case of sqrt(i), postgres will return sqrt - //while mysql will return sqrt(i). - String driver = p.getProperty("db.driver"); - - if (driver.contains("postgres")) { - int parenthesis = field.indexOf("("); - if (parenthesis > -1) { - return field.substring(0, parenthesis); - } else { - return result; - } - } else { - return field.trim(); - } - } else { - return field.substring(as + 2, field.length()); - } - } - - /** - * @return the query - */ - public String getQuery() { - return query; - } - - /** - * @param query the query to set - */ - public void setQuery(String query) { - this.query = query; - } - - /** - * @return the fields - */ - public ArrayList getFields() { - return fields; - } - - /** - * @param fields the fields to set - */ - public void setFields(ArrayList fields) { - this.fields = fields; - } - - /** - * @return the tables - */ - public ArrayList getTables() { - return tables; - } - /** - * @param tables the tables to set - */ - public void setTables(ArrayList tables) { - this.tables = tables; - } -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sql; + + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Represents an sql select query + * @author nkons + * + */ +public class SelectQuery { + private static final Logger log = LoggerFactory.getLogger(SelectQuery.class); + + private String query; + private ArrayList fields; + private ArrayList tables; + + private final Properties p; + /** + * + */ + public SelectQuery(String query, Properties p) { + this.p = p; + log.info("Processing query: '" + query + "'"); + if (query.indexOf(';') != -1) query = query.replace(';', ' '); + + this.query = query; + this.tables = createSelectTables(query); //tables must be created first, they are needed to create the fields + this.fields = createSelectFields(query); + } + + public SelectField findFieldByNameOrAlias(String field) { + for (SelectField f : fields) { + if (field.equalsIgnoreCase(f.getName()) || field.equalsIgnoreCase(f.getAlias())) { + return f; + } + } + return null; + } + + public SelectTable findTableByNameOrAlias(String search) { + for (SelectTable table : tables) { + if (search.equalsIgnoreCase(table.getName()) || search.equalsIgnoreCase(table.getAlias()) ) { + return table; + } + } + return null; + } + + public ArrayList createSelectFields(String q) { + ArrayList results = new ArrayList(); + + int start = q.toUpperCase().indexOf("SELECT") + 7; + int end = q.toUpperCase().indexOf("FROM"); + + ArrayList fields = new ArrayList(Arrays.asList(q.substring(start, end).split(","))); + ArrayList processedFields = new ArrayList(); + + for (int i = 0; i < fields.size(); i++) { + if (createSelectFieldTable(fields.get(i).trim()) != null) { + processedFields.add(fields.get(i)); + } + } + + for (String field : processedFields) { + SelectField f = new SelectField(); + f.setName(field.trim()); + f.setTable(createSelectFieldTable(field.trim())); + f.setAlias(createAlias(field).trim()); + log.info("Adding field with: name '" + f.getName() + "', table '" + f.getTable().getName() + "', alias '" + f.getAlias() + "'"); + results.add(f); + } + return results; + } + + public ArrayList createSelectTables(String q) { + ArrayList results = new ArrayList(); + + int start = q.toUpperCase().indexOf("FROM") + 4; + ArrayList tables = new ArrayList(); + + //split in spaces + String tokens[] = q.substring(start).split("\\s+"); + + //if there are aliases + if (StringUtils.containsIgnoreCase(q.substring(start), "AS")) { + for (int i = 0; i < tokens.length; i++) { + if ("AS".equalsIgnoreCase(tokens[i])) { + if (tokens[i+1].endsWith(",")) { + tokens[i+1] = tokens[i+1].substring(0, tokens[i+1].indexOf(",")); + } + tables.add(tokens[i-1] + " " + tokens[i] + " " + tokens[i+1]); + } + } + } else { + //otherwise, split in commas + int end = StringUtils.indexOfIgnoreCase(q, "WHERE"); + if (end == -1) end = q.length(); + tables = new ArrayList(Arrays.asList(q.substring(start, end).split(","))); + } + + for (String table : tables) { + SelectTable selectTable = new SelectTable(); + selectTable.setName(table.trim()); + selectTable.setAlias(createAlias(selectTable.getName()).trim()); + log.info("Adding table with: name '" + selectTable.getName() + "', alias '" + selectTable.getAlias() + "'"); + results.add(selectTable); + } + return results; + } + + public SelectTable createSelectFieldTable(String field) { + int dot = field.indexOf('.'); + if (dot == -1) { + return tables.get(0); //If no table specified for the field, there should be only one table in the query + } else { + //if it is an SQL function, the table name should start after the parenthesis. + int start = (field.indexOf("(") > -1? field.indexOf("(") + 1: 0); + String table = field.substring(start, dot); + return findTableByNameOrAlias(table); + } + } + + public String createAlias(String field) { + String result = ""; + + int as = field.toUpperCase().indexOf("AS"); + if (as == -1) { + //When there is no aliases, Postgres returns the column, named after the function alone while Mysql will + //name the column after the function plus the argument. i.e. in the case of sqrt(i), postgres will return sqrt + //while mysql will return sqrt(i). + String driver = p.getProperty("db.driver"); + + if (driver.contains("postgres")) { + int parenthesis = field.indexOf("("); + if (parenthesis > -1) { + return field.substring(0, parenthesis); + } else { + return result; + } + } else { + return field.trim(); + } + } else { + return field.substring(as + 2, field.length()); + } + } + + /** + * @return the query + */ + public String getQuery() { + return query; + } + + /** + * @param query the query to set + */ + public void setQuery(String query) { + this.query = query; + } + + /** + * @return the fields + */ + public ArrayList getFields() { + return fields; + } + + /** + * @param fields the fields to set + */ + public void setFields(ArrayList fields) { + this.fields = fields; + } + + /** + * @return the tables + */ + public ArrayList getTables() { + return tables; + } + /** + * @param tables the tables to set + */ + public void setTables(ArrayList tables) { + this.tables = tables; + } +} diff --git a/src/main/java/gr/ekt/r2rml/entities/sql/SelectTable.java b/src/main/java/gr/seab/r2rml/entities/sql/SelectTable.java similarity index 92% rename from src/main/java/gr/ekt/r2rml/entities/sql/SelectTable.java rename to src/main/java/gr/seab/r2rml/entities/sql/SelectTable.java index 12eab63..b4ee328 100644 --- a/src/main/java/gr/ekt/r2rml/entities/sql/SelectTable.java +++ b/src/main/java/gr/seab/r2rml/entities/sql/SelectTable.java @@ -1,69 +1,69 @@ -/** - * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported - * License (the "License"). You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * - * http://creativecommons.org/licenses/by-nc/3.0/ - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ -package gr.ekt.r2rml.entities.sql; - - -import java.util.Date; - -/** - * A table in an sql select query - * @see SelectQuery - * @author nkons - * - */ -public class SelectTable { - - private String name; - private String alias; - private Date lastModified; - - /** - * - */ - public SelectTable() { - - } - - /** - * @return the name - */ - public String getName() { - return name; - } - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - /** - * @return the alias - */ - public String getAlias() { - return alias; - } - /** - * @param alias the alias to set - */ - public void setAlias(String alias) { - this.alias = alias; - } - - public Date getLastModified() { - return lastModified; - } - - public void setLastModified(Date lastModified) { - this.lastModified = lastModified; - } - -} +/** + * Licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported + * License (the "License"). You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * + * http://creativecommons.org/licenses/by-nc/3.0/ + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +package gr.seab.r2rml.entities.sql; + + +import java.util.Date; + +/** + * A table in an sql select query + * @see SelectQuery + * @author nkons + * + */ +public class SelectTable { + + private String name; + private String alias; + private Date lastModified; + + /** + * + */ + public SelectTable() { + + } + + /** + * @return the name + */ + public String getName() { + return name; + } + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + /** + * @return the alias + */ + public String getAlias() { + return alias; + } + /** + * @param alias the alias to set + */ + public void setAlias(String alias) { + this.alias = alias; + } + + public Date getLastModified() { + return lastModified; + } + + public void setLastModified(Date lastModified) { + this.lastModified = lastModified; + } + +} diff --git a/src/main/resources/app-context.xml b/src/main/resources/app-context.xml index 7453af0..2f21917 100644 --- a/src/main/resources/app-context.xml +++ b/src/main/resources/app-context.xml @@ -10,22 +10,22 @@ Spring configuration. - + - + - + - + - + diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index 0a2ce63..d69f250 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -10,8 +10,17 @@ + + + + + + + + + - + @@ -35,7 +44,8 @@ - + + diff --git a/src/test/java/gr/ekt/r2rml/test/ComplianceTests.java b/src/test/java/gr/seab/r2rml/test/ComplianceTests.java similarity index 100% rename from src/test/java/gr/ekt/r2rml/test/ComplianceTests.java rename to src/test/java/gr/seab/r2rml/test/ComplianceTests.java diff --git a/src/test/resources/log4j.xml b/src/test/resources/log4j.xml index 9ad0dc7..6c2ea46 100644 --- a/src/test/resources/log4j.xml +++ b/src/test/resources/log4j.xml @@ -11,7 +11,7 @@ - + diff --git a/src/test/resources/test-context.xml b/src/test/resources/test-context.xml index b123d5d..68582be 100644 --- a/src/test/resources/test-context.xml +++ b/src/test/resources/test-context.xml @@ -10,22 +10,22 @@ Spring configuration. - + - + - + - + - +