You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by an...@apache.org on 2013/10/01 01:48:13 UTC
[21/51] [partial] Revert "Merge branch 'master' of
https://git-wip-us.apache.org/repos/asf/incubator-marmotta"
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/4aa87dbe/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
deleted file mode 100644
index 61eedd2..0000000
--- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/persistence/KiWiSparqlConnection.java
+++ /dev/null
@@ -1,706 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.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.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.marmotta.kiwi.sparql.persistence;
-
-import com.google.common.base.Preconditions;
-import info.aduna.iteration.CloseableIteration;
-import info.aduna.iteration.CloseableIteratorIteration;
-import info.aduna.iteration.Iterations;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.marmotta.commons.sesame.model.Namespaces;
-import org.apache.marmotta.commons.util.DateUtils;
-import org.apache.marmotta.kiwi.model.rdf.KiWiNode;
-import org.apache.marmotta.kiwi.persistence.KiWiConnection;
-import org.apache.marmotta.kiwi.persistence.util.ResultSetIteration;
-import org.apache.marmotta.kiwi.persistence.util.ResultTransformerFunction;
-import org.apache.marmotta.kiwi.sail.KiWiValueFactory;
-import org.openrdf.model.BNode;
-import org.openrdf.model.Literal;
-import org.openrdf.model.URI;
-import org.openrdf.model.Value;
-import org.openrdf.query.Binding;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.algebra.*;
-import org.openrdf.query.impl.MapBindingSet;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.concurrent.*;
-import java.util.regex.Pattern;
-
-/**
- * Provide improved SPARQL support by evaluating certain common compley SPARQL constructs directly on the
- * database (e.g. JOIN over pattern queries).
- * <p/>
- * Implemented using a decorator pattern (i.e. wrapping the KiWiConnection).
- *
- * @author Sebastian Schaffert (sschaffert@apache.org)
- */
-public class KiWiSparqlConnection {
-
- private static Logger log = LoggerFactory.getLogger(KiWiSparqlConnection.class);
-
- private static DateFormat sqlDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
-
- private KiWiConnection parent;
- private KiWiValueFactory valueFactory;
-
- private ExecutorService executorService;
-
- public KiWiSparqlConnection(KiWiConnection parent, KiWiValueFactory valueFactory) throws SQLException {
- this.parent = parent;
- this.valueFactory = valueFactory;
-
- // interruptible queries run in a separate thread
- this.executorService = Executors.newCachedThreadPool();
- }
-
- /**
- * Evaluate a statement pattern join or filter on the database by translating it into an appropriate SQL statement.
- * Copied and adapted from KiWiReasoningConnection.query()
- * @param join
- * @return
- */
- public CloseableIteration<BindingSet, SQLException> evaluateJoin(TupleExpr join, final BindingSet bindings) throws SQLException, InterruptedException {
- Preconditions.checkArgument(join instanceof Join || join instanceof Filter || join instanceof StatementPattern);
-
- // some definitions
- String[] positions = new String[] {"subject","predicate","object","context"};
-
- // collect all patterns in a list, using depth-first search over the join
- List<StatementPattern> patterns = new ArrayList<StatementPattern>();
- collectPatterns(join, patterns);
-
- // associate a name with each pattern; the names are used in the database query to refer to the triple
- // that matched this pattern and in the construction of variable names for the HQL query
- int patternCount = 0;
- final Map<StatementPattern,String> patternNames = new HashMap<StatementPattern, String>();
- for(StatementPattern p : patterns) {
- patternNames.put(p,"P"+ (++patternCount));
- }
-
- // find all variables occurring in the patterns and create a map to map them to
- // field names in the database query; each variable will have one or several field names,
- // one for each pattern it occurs in; field names are constructed automatically by a counter
- // and the pattern name to ensure the name is a valid HQL identifier
- int variableCount = 0;
-
- // a map for the variable names; will look like { ?x -> "V1", ?y -> "V2", ... }
- final Map<Var,String> variableNames = new HashMap<Var, String>();
-
- // a map for mapping variables to field names; each variable might have one or more field names,
- // depending on the number of patterns it occurs in; will look like
- // { ?x -> ["P1_V1", "P2_V1"], ?y -> ["P2_V2"], ... }
- Map<Var,List<String>> queryVariables = new HashMap<Var, List<String>>();
- for(StatementPattern p : patterns) {
- Var[] fields = new Var[] {
- p.getSubjectVar(),
- p.getPredicateVar(),
- p.getObjectVar(),
- p.getContextVar()
- };
- for(int i = 0; i<fields.length; i++) {
- if(fields[i] != null && !fields[i].hasValue()) {
- Var v = fields[i];
- if(variableNames.get(v) == null) {
- variableNames.put(v,"V"+ (++variableCount));
- queryVariables.put(v,new LinkedList<String>());
- }
- String pName = patternNames.get(p);
- String vName = variableNames.get(v);
- queryVariables.get(v).add(pName + "_" + positions[i] + "_" + vName);
- }
- }
- }
-
- // build the select clause by projecting for each query variable the first name
- StringBuilder selectClause = new StringBuilder();
- final List<Var> selectVariables = new LinkedList<Var>();
- for(Iterator<Var> it = queryVariables.keySet().iterator(); it.hasNext(); ) {
- Var v = it.next();
- String projectedName = variableNames.get(v);
- String fromName = queryVariables.get(v).get(0);
- selectClause.append(fromName);
- selectClause.append(".id as ");
- selectClause.append(projectedName);
- if(it.hasNext()) {
- selectClause.append(", ");
- }
- selectVariables.add(v);
- }
-
-
- // build the from-clause of the query; the from clause is constructed as follows:
- // 1. for each pattern P, there will be a "KiWiTriple P" in the from clause
- // 2. for each variable V in P occurring in
- // - subject, there will be a "inner join P.subject as P_S_V" or "left outer join P.subject as P_S_V",
- // depending on whether the "optional" parameter is false or true
- // - property, there will be a "inner join P.property as P_P_V" or "left outer join p.property as P_P_V"
- // - object, there will be a "inner join P.object as P_O_V" or "left outer join p.object as P_O_V"
- // - context, there will be a "inner join P.context as P_C_V" or "left outer join p.context as P_C_V"
- StringBuilder fromClause = new StringBuilder();
- for(Iterator<StatementPattern> it = patterns.iterator(); it.hasNext(); ) {
- StatementPattern p = it.next();
- String pName = patternNames.get(p);
- fromClause.append("triples "+pName);
-
- Var[] fields = new Var[] {
- p.getSubjectVar(),
- p.getPredicateVar(),
- p.getObjectVar(),
- p.getContextVar()
- };
- for(int i = 0; i<fields.length; i++) {
- if(fields[i] != null && !fields[i].hasValue()) {
- String vName = variableNames.get(fields[i]);
- fromClause.append(" INNER JOIN nodes AS ");
- fromClause.append(pName + "_"+positions[i]+"_" + vName);
- fromClause.append(" ON " + pName + "." + positions[i] + " = ");
- fromClause.append(pName + "_"+positions[i]+"_" + vName + ".id ");
- }
- }
-
- if(it.hasNext()) {
- fromClause.append(",\n ");
- }
- }
-
-
- // build the where clause as follows:
- // 1. iterate over all patterns and for each resource and literal field in subject,
- // property, object, or context, and set a query condition according to the
- // nodes given in the pattern
- // 2. for each variable that has more than one occurrences, add a join condition
- // 3. for each variable in the initialBindings, add a condition to the where clause
-
- // list of where conditions that will later be connected by AND
- List<String> whereConditions = new LinkedList<String>();
-
-
- // 1. iterate over all patterns and for each resource and literal field in subject,
- // property, object, or context, and set a query condition according to the
- // nodes given in the pattern
- for(StatementPattern p : patterns) {
- String pName = patternNames.get(p);
- Var[] fields = new Var[] {
- p.getSubjectVar(),
- p.getPredicateVar(),
- p.getObjectVar(),
- p.getContextVar()
- };
- for(int i = 0; i<fields.length; i++) {
- // find node id of the resource or literal field and use it in the where clause
- // in this way we can avoid setting too many query parameters
- Long nodeId = null;
- if(fields[i] != null && fields[i].hasValue()) {
- Value v = valueFactory.convert(fields[i].getValue());
- if(v instanceof KiWiNode) {
- nodeId = ((KiWiNode) v).getId();
- } else {
- throw new IllegalArgumentException("the values in this query have not been created by the KiWi value factory");
- }
-
- if(nodeId != null) {
- String condition = pName+"."+positions[i]+" = " + nodeId;
- whereConditions.add(condition);
- }
- }
- }
- }
-
- // 2. for each variable that has more than one occurrences, add a join condition
- for(Var v : queryVariables.keySet()) {
- List<String> vNames = queryVariables.get(v);
- for(int i = 1; i < vNames.size(); i++) {
- String vName1 = vNames.get(i-1);
- String vName2 = vNames.get(i);
- whereConditions.add(vName1 + ".id = " + vName2 + ".id");
- }
- }
-
- // 3. for each variable in the initialBindings, add a condition to the where clause setting it
- // to the node given as binding
- if(bindings != null) {
- for(String v : bindings.getBindingNames()) {
- for(Map.Entry<Var,List<String>> entry : queryVariables.entrySet()) {
- if(entry.getKey().getName() != null && entry.getKey().getName().equals(v) &&
- entry.getValue() != null && entry.getValue().size() > 0) {
- List<String> vNames = entry.getValue();
- String vName = vNames.get(0);
- Value binding = valueFactory.convert(bindings.getValue(v));
- if(binding instanceof KiWiNode) {
- whereConditions.add(vName+".id = "+((KiWiNode)binding).getId());
- } else {
- throw new IllegalArgumentException("the values in this binding have not been created by the KiWi value factory");
- }
- }
- }
- }
- }
-
- // 4. for each pattern, ensure that the matched triple is not marked as deleted
- for(StatementPattern p : patterns) {
- String pName = patternNames.get(p);
- whereConditions.add(pName+".deleted = false");
- }
-
-
- // 5. for each filter condition, add a statement to the where clause
- List<ValueExpr> filters = new ArrayList<ValueExpr>();
- collectFilters(join, filters);
- for(ValueExpr expr : filters) {
- whereConditions.add(evaluateExpression(expr,queryVariables, null));
- }
-
-
- // construct the where clause
- StringBuilder whereClause = new StringBuilder();
- for(Iterator<String> it = whereConditions.iterator(); it.hasNext(); ) {
- whereClause.append(it.next());
- whereClause.append("\n ");
- if(it.hasNext()) {
- whereClause.append("AND ");
- }
- }
-
-
- // build the query string
- String queryString =
- "SELECT " + selectClause + "\n " +
- "FROM " + fromClause + "\n " +
- "WHERE " + whereClause;
-
- log.debug("original SPARQL syntax tree:\n {}", join);
- log.debug("constructed SQL query string:\n {}",queryString);
- log.debug("SPARQL -> SQL variable mappings:\n {}", queryVariables);
-
- final PreparedStatement queryStatement = parent.getJDBCConnection().prepareStatement(queryString);
- if(parent.getDialect().isCursorSupported()) {
- queryStatement.setFetchSize(parent.getConfiguration().getCursorSize());
- }
-
- Future<ResultSet> queryFuture =
- executorService.submit(new Callable<ResultSet>() {
- @Override
- public ResultSet call() throws Exception {
- try {
- return queryStatement.executeQuery();
- } catch (SQLException ex) {
- if(Thread.interrupted()) {
- log.info("SQL query execution cancelled; not returning result (Thread={})", Thread.currentThread());
- throw new InterruptedException("SPARQL query execution cancelled");
- } else {
- throw ex;
- }
- }
- }
- }
- );
-
- try {
- ResultSet result = queryFuture.get();
-
- ResultSetIteration<BindingSet> it = new ResultSetIteration<BindingSet>(result, true, new ResultTransformerFunction<BindingSet>() {
- @Override
- public BindingSet apply(ResultSet row) throws SQLException {
- MapBindingSet resultRow = new MapBindingSet();
-
- for(Var v : selectVariables) {
- resultRow.addBinding(v.getName(), parent.loadNodeById(row.getLong(variableNames.get(v))));
- }
-
-
- if(bindings != null) {
- for(Binding binding : bindings) {
- resultRow.addBinding(binding);
- }
- }
- return resultRow;
- }
- });
-
- // materialize result to avoid having more than one result set open at the same time
- return new CloseableIteratorIteration<BindingSet, SQLException>(Iterations.asList(it).iterator());
- } catch (InterruptedException | CancellationException e) {
- log.info("SPARQL query execution cancelled");
- queryFuture.cancel(true);
- queryStatement.cancel();
- queryStatement.close();
-
- throw new InterruptedException("SPARQL query execution cancelled");
- } catch (ExecutionException e) {
- log.error("error executing SPARQL query",e.getCause());
- if(e.getCause() instanceof SQLException) {
- throw (SQLException)e.getCause();
- } else if(e.getCause() instanceof InterruptedException) {
- throw (InterruptedException)e.getCause();
- } else {
- throw new SQLException("error executing SPARQL query",e);
- }
- }
- }
-
- private String evaluateExpression(ValueExpr expr, Map<Var, List<String>> queryVariables, OPTypes optype) {
- if(expr instanceof And) {
- return "(" + evaluateExpression(((And) expr).getLeftArg(), queryVariables, optype) + " AND " + evaluateExpression(((And) expr).getRightArg(),queryVariables, optype) + ")";
- } else if(expr instanceof Or) {
- return "(" + evaluateExpression(((Or) expr).getLeftArg(), queryVariables, optype) + " OR " + evaluateExpression(((Or) expr).getRightArg(),queryVariables, optype) + ")";
- } else if(expr instanceof Not) {
- return "NOT (" + evaluateExpression(((Not) expr).getArg(), queryVariables, optype) + ")";
- } else if(expr instanceof Str) {
- Str str = (Str)expr;
- // get value of argument and express it as string
- if(str.getArg() instanceof Var) {
- return queryVariables.get(str.getArg()).get(0) + ".svalue";
- } else if(str.getArg() instanceof ValueConstant) {
- return "'" + ((ValueConstant) str.getArg()).getValue().stringValue() + "'";
- }
- } else if(expr instanceof Label) {
- Label str = (Label)expr;
- // get value of argument and express it as string
- if(str.getArg() instanceof Var) {
- return queryVariables.get(str.getArg()).get(0) + ".svalue";
- } else if(str.getArg() instanceof ValueConstant) {
- return "'" + ((ValueConstant) str.getArg()).getValue().stringValue() + "'";
- }
- } else if(expr instanceof Lang) {
- Lang lang = (Lang)expr;
-
- if(lang.getArg() instanceof Var) {
- return queryVariables.get(lang.getArg()).get(0) + ".lang";
- }
- } else if(expr instanceof Compare) {
- Compare cmp = (Compare)expr;
-
- OPTypes ot = determineOpType(cmp.getLeftArg(), cmp.getRightArg());
-
- return evaluateExpression(cmp.getLeftArg(),queryVariables, ot) + getSQLOperator(cmp.getOperator()) + evaluateExpression(cmp.getRightArg(),queryVariables, ot);
- } else if(expr instanceof MathExpr) {
- MathExpr cmp = (MathExpr)expr;
-
- OPTypes ot = determineOpType(cmp.getLeftArg(), cmp.getRightArg());
-
- if(ot == OPTypes.STRING) {
- if(cmp.getOperator() == MathExpr.MathOp.PLUS) {
- return parent.getDialect().getConcat(evaluateExpression(cmp.getLeftArg(),queryVariables, ot), evaluateExpression(cmp.getRightArg(),queryVariables, ot));
- } else {
- throw new IllegalArgumentException("operation "+cmp.getOperator()+" is not supported on strings");
- }
- } else {
- return evaluateExpression(cmp.getLeftArg(),queryVariables, ot) + getSQLOperator(cmp.getOperator()) + evaluateExpression(cmp.getRightArg(),queryVariables, ot);
- }
- } else if(expr instanceof Regex) {
- Regex re = (Regex)expr;
-
- return optimizeRegexp(evaluateExpression(re.getArg(),queryVariables, optype), evaluateExpression(re.getPatternArg(), queryVariables, OPTypes.STRING));
- } else if(expr instanceof LangMatches) {
- LangMatches lm = (LangMatches)expr;
- String value = evaluateExpression(lm.getLeftArg(), queryVariables, optype);
- ValueConstant pattern = (ValueConstant) lm.getRightArg();
-
- if(pattern.getValue().stringValue().equals("*")) {
- return value + " LIKE '%'";
- } else if(pattern.getValue().stringValue().equals("")) {
- return value + " IS NULL";
- } else {
- return "(" + value + " = '"+pattern.getValue().stringValue()+"' OR " + parent.getDialect().getILike(value, "'" + pattern.getValue().stringValue() + "-%' )");
- }
- } else if(expr instanceof IsResource) {
- ValueExpr arg = ((UnaryValueOperator)expr).getArg();
-
- // operator must be a variable or a constant
- if(arg instanceof ValueConstant) {
- return Boolean.toString(((ValueConstant) arg).getValue() instanceof URI || ((ValueConstant) arg).getValue() instanceof BNode);
- } else if(arg instanceof Var) {
- String var = queryVariables.get(arg).get(0);
-
- return "(" + var + ".ntype = 'uri' OR " + var + ".ntype = 'bnode')";
- }
- } else if(expr instanceof IsURI) {
- ValueExpr arg = ((UnaryValueOperator)expr).getArg();
-
- // operator must be a variable or a constant
- if(arg instanceof ValueConstant) {
- return Boolean.toString(((ValueConstant) arg).getValue() instanceof URI);
- } else if(arg instanceof Var) {
- String var = queryVariables.get(arg).get(0);
-
- return var + ".ntype = 'uri'";
- }
- } else if(expr instanceof IsBNode) {
- ValueExpr arg = ((UnaryValueOperator)expr).getArg();
-
- // operator must be a variable or a constant
- if(arg instanceof ValueConstant) {
- return Boolean.toString(((ValueConstant) arg).getValue() instanceof BNode);
- } else if(arg instanceof Var) {
- String var = queryVariables.get(arg).get(0);
-
- return var + ".ntype = 'bnode'";
- }
- } else if(expr instanceof IsLiteral) {
- ValueExpr arg = ((UnaryValueOperator)expr).getArg();
-
- // operator must be a variable or a constant
- if(arg instanceof ValueConstant) {
- return Boolean.toString(((ValueConstant) arg).getValue() instanceof Literal);
- } else if(arg instanceof Var) {
- String var = queryVariables.get(arg).get(0);
-
- return "(" + var + ".ntype = 'string' OR " + var + ".ntype = 'int' OR " + var + ".ntype = 'double' OR " + var + ".ntype = 'date' OR " + var + ".ntype = 'boolean')";
- }
- } else if(expr instanceof Var) {
- String var = queryVariables.get(expr).get(0);
-
- if(optype == null) {
- return var + ".svalue";
- } else {
- switch (optype) {
- case STRING: return var + ".svalue";
- case INT: return var + ".ivalue";
- case DOUBLE: return var + ".dvalue";
- case DATE: return var + ".tvalue";
- case ANY: return var + ".id";
- }
- }
- } else if(expr instanceof ValueConstant) {
- String val = ((ValueConstant) expr).getValue().stringValue();
-
- if(optype == null) {
- return "'" + val + "'";
- } else {
- switch (optype) {
- case STRING: return "'" + val + "'";
- case INT: return "" + Integer.parseInt(val);
- case DOUBLE: return "" + Double.parseDouble(val);
- case DATE: return "'" + sqlDateFormat.format(DateUtils.parseDate(val)) + "'";
- default: throw new IllegalArgumentException("unsupported value type: " + optype);
- }
- }
- }
-
-
- throw new IllegalArgumentException("unsupported value expression: "+expr);
- }
-
-
- /**
- * Collect all statement patterns in a tuple expression in depth-first order. The tuple expression may only
- * contain Join or StatementPattern expressions, otherwise an IllegalArgumentException is thrown.
- * @param expr
- * @param patterns
- */
- private void collectPatterns(TupleExpr expr, List<StatementPattern> patterns) {
- if(expr instanceof Join) {
- collectPatterns(((Join) expr).getLeftArg(), patterns);
- collectPatterns(((Join) expr).getRightArg(), patterns);
- } else if(expr instanceof Filter) {
- collectPatterns(((Filter) expr).getArg(), patterns);
- } else if(expr instanceof StatementPattern) {
- patterns.add((StatementPattern)expr);
- } else {
- throw new IllegalArgumentException("the tuple expression was neither a join nor a statement pattern: "+expr);
- }
-
- }
-
-
- /**
- * Collect all filter conditions in a tuple expression in depth-first order. The tuple expression may only
- * contain Join or StatementPattern expressions, otherwise an IllegalArgumentException is thrown.
- * @param expr
- * @param filters
- */
- private void collectFilters(TupleExpr expr, List<ValueExpr> filters) {
- if(expr instanceof Join) {
- collectFilters(((Join) expr).getLeftArg(), filters);
- collectFilters(((Join) expr).getRightArg(), filters);
- } else if(expr instanceof Filter) {
- filters.add(((Filter) expr).getCondition());
- } else if(expr instanceof StatementPattern) {
- // do nothing
- } else {
- throw new IllegalArgumentException("the tuple expression was neither a join nor a statement pattern: "+expr);
- }
-
- }
-
-
- private String getSQLOperator(Compare.CompareOp op) {
- switch (op) {
- case EQ: return " = ";
- case GE: return " >= ";
- case GT: return " > ";
- case LE: return " <= ";
- case LT: return " < ";
- case NE: return " <> ";
- }
- throw new IllegalArgumentException("unsupported operator type for comparison: "+op);
- }
-
-
- private String getSQLOperator(MathExpr.MathOp op) {
- switch (op) {
- case PLUS: return " + ";
- case MINUS: return " - ";
- case DIVIDE: return " / ";
- case MULTIPLY: return " / ";
- }
- throw new IllegalArgumentException("unsupported operator type for math expression: "+op);
- }
-
-
-
- private OPTypes determineOpType(ValueExpr expr) {
- if(expr instanceof ValueConstant) {
- if(((ValueConstant) expr).getValue() instanceof Literal) {
- Literal l = (Literal)((ValueConstant) expr).getValue();
- String type = l.getDatatype() != null ? l.getDatatype().stringValue() : null;
-
- if(StringUtils.equals(Namespaces.NS_XSD + "double", type)
- || StringUtils.equals(Namespaces.NS_XSD + "float", type)
- || StringUtils.equals(Namespaces.NS_XSD + "decimal", type)) {
- return OPTypes.DOUBLE;
- } else if(StringUtils.equals(Namespaces.NS_XSD + "integer", type)
- || StringUtils.equals(Namespaces.NS_XSD + "long", type)
- || StringUtils.equals(Namespaces.NS_XSD + "int", type)
- || StringUtils.equals(Namespaces.NS_XSD + "short", type)
- || StringUtils.equals(Namespaces.NS_XSD + "nonNegativeInteger", type)
- || StringUtils.equals(Namespaces.NS_XSD + "nonPositiveInteger", type)
- || StringUtils.equals(Namespaces.NS_XSD + "negativeInteger", type)
- || StringUtils.equals(Namespaces.NS_XSD + "positiveInteger", type)
- || StringUtils.equals(Namespaces.NS_XSD + "unsignedLong", type)
- || StringUtils.equals(Namespaces.NS_XSD + "unsignedShort", type)
- || StringUtils.equals(Namespaces.NS_XSD + "byte", type)
- || StringUtils.equals(Namespaces.NS_XSD + "unsignedByte", type)) {
- return OPTypes.INT;
- } else if(StringUtils.equals(Namespaces.NS_XSD + "dateTime", type)
- || StringUtils.equals(Namespaces.NS_XSD + "date", type)
- || StringUtils.equals(Namespaces.NS_XSD + "time", type)) {
- return OPTypes.DATE;
- } else {
- return OPTypes.STRING;
- }
- } else {
- return OPTypes.STRING;
- }
- } else if(expr instanceof Str) {
- return OPTypes.STRING;
- } else if(expr instanceof Lang) {
- return OPTypes.STRING;
- } else if(expr instanceof LocalName) {
- return OPTypes.STRING;
- } else if(expr instanceof Label) {
- return OPTypes.STRING;
- } else if(expr instanceof MathExpr) {
- return determineOpType(((MathExpr) expr).getLeftArg(), ((MathExpr) expr).getRightArg());
- } else if(expr instanceof Var) {
- return OPTypes.ANY;
- } else {
- throw new IllegalArgumentException("unsupported expression: "+expr);
- }
- }
-
- private OPTypes determineOpType(ValueExpr expr1, ValueExpr expr2) {
- OPTypes left = determineOpType(expr1);
- OPTypes right = determineOpType(expr2);
-
- if(left == OPTypes.ANY) {
- return right;
- } else if(right == OPTypes.ANY) {
- return left;
- } else if(left == right) {
- return left;
- } else if( (left == OPTypes.INT && right == OPTypes.DOUBLE) || (left == OPTypes.DOUBLE && right == OPTypes.INT)) {
- return OPTypes.DOUBLE;
- } else if( (left == OPTypes.STRING) || (right == OPTypes.STRING)) {
- return OPTypes.STRING;
- } else {
- throw new IllegalArgumentException("unsupported type coercion: " + left + " and " + right);
- }
- }
-
-
- /**
- * Test if the regular expression given in the pattern can be simplified to a LIKE SQL statement; these are
- * considerably more efficient to evaluate in most databases, so in case we can simplify, we return a LIKE.
- *
- * @param value
- * @param pattern
- * @return
- */
- private String optimizeRegexp(String value, String pattern) {
- String simplified = pattern;
-
- // apply simplifications
-
- // remove SQL quotes at beginning and end
- simplified = simplified.replaceFirst("^'","");
- simplified = simplified.replaceFirst("'$","");
-
-
- // remove .* at beginning and end, they are the default anyways
- simplified = simplified.replaceFirst("^\\.\\*","");
- simplified = simplified.replaceFirst("\\.\\*$","");
-
- // replace all occurrences of % with \% and _ with \_, as they are special characters in SQL
- simplified = simplified.replaceAll("%","\\%");
- simplified = simplified.replaceAll("_","\\_");
-
- // if pattern now does not start with a ^, we put a "%" in front
- if(!simplified.startsWith("^")) {
- simplified = "%" + simplified;
- } else {
- simplified = simplified.substring(1);
- }
-
- // if pattern does not end with a "$", we put a "%" at the end
- if(!simplified.endsWith("$")) {
- simplified = simplified + "%";
- } else {
- simplified = simplified.substring(0,simplified.length()-2);
- }
-
- // replace all non-escaped occurrences of .* with %
- simplified = simplified.replaceAll("(?<!\\\\)\\.\\*","%");
-
- // replace all non-escaped occurrences of .+ with _%
- simplified = simplified.replaceAll("(?<!\\\\)\\.\\+","_%");
-
- // the pattern is not simplifiable if the simplification still contains unescaped regular expression constructs
- Pattern notSimplifiable = Pattern.compile("(?<!\\\\)[\\.\\*\\+\\{\\}\\[\\]\\|]");
-
- if(notSimplifiable.matcher(simplified).find()) {
- return parent.getDialect().getRegexp(value, pattern);
- } else {
- return value + " LIKE '"+simplified+"'";
- }
-
- }
-
-
- private static enum OPTypes {
- STRING, DOUBLE, INT, DATE, ANY
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/4aa87dbe/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSail.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSail.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSail.java
deleted file mode 100644
index eb1b810..0000000
--- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSail.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.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.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.marmotta.kiwi.sparql.sail;
-
-import org.apache.marmotta.kiwi.sail.KiWiSailConnection;
-import org.apache.marmotta.kiwi.sail.KiWiStore;
-import org.apache.marmotta.kiwi.sparql.persistence.KiWiSparqlConnection;
-import org.openrdf.sail.*;
-import org.openrdf.sail.helpers.NotifyingSailWrapper;
-import org.openrdf.sail.helpers.SailConnectionWrapper;
-import org.openrdf.sail.helpers.SailWrapper;
-
-import java.sql.SQLException;
-
-/**
- * Add file description here!
- *
- * @author Sebastian Schaffert (sschaffert@apache.org)
- */
-public class KiWiSparqlSail extends NotifyingSailWrapper {
-
- private KiWiStore parent;
-
- public KiWiSparqlSail(NotifyingSail baseSail) {
- super(baseSail);
-
- this.parent = getRootSail(baseSail);
-
- }
-
- /**
- * Get the root sail in the wrapped sail stack
- * @param sail
- * @return
- */
- private KiWiStore getRootSail(Sail sail) {
- if(sail instanceof KiWiStore) {
- return (KiWiStore) sail;
- } else if(sail instanceof SailWrapper) {
- return getRootSail(((SailWrapper) sail).getBaseSail());
- } else {
- throw new IllegalArgumentException("root sail is not a KiWiStore or could not be found");
- }
- }
-
- /**
- * Get the root connection in a wrapped sail connection stack
- * @param connection
- * @return
- */
- private KiWiSailConnection getRootConnection(SailConnection connection) {
- if(connection instanceof KiWiSailConnection) {
- return (KiWiSailConnection) connection;
- } else if(connection instanceof SailConnectionWrapper) {
- return getRootConnection(((SailConnectionWrapper) connection).getWrappedConnection());
- } else {
- throw new IllegalArgumentException("root connection is not a KiWiSailConnection or could not be found");
- }
- }
-
- @Override
- public NotifyingSailConnection getConnection() throws SailException {
- NotifyingSailConnection connection = super.getConnection();
- KiWiSailConnection root = getRootConnection(connection);
-
- try {
- return new KiWiSparqlSailConnection(connection, new KiWiSparqlConnection(root.getDatabaseConnection(), root.getValueFactory()), root.getValueFactory());
- } catch (SQLException e) {
- throw new SailException(e);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/4aa87dbe/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSailConnection.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSailConnection.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSailConnection.java
deleted file mode 100644
index d92808f..0000000
--- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sail/KiWiSparqlSailConnection.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.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.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.marmotta.kiwi.sparql.sail;
-
-import info.aduna.iteration.CloseableIteration;
-import org.apache.marmotta.kiwi.sail.KiWiSailConnection;
-import org.apache.marmotta.kiwi.sail.KiWiValueFactory;
-import org.apache.marmotta.kiwi.sparql.evaluation.KiWiEvaluationStatistics;
-import org.apache.marmotta.kiwi.sparql.evaluation.KiWiEvaluationStrategyImpl;
-import org.apache.marmotta.kiwi.sparql.evaluation.KiWiTripleSource;
-import org.apache.marmotta.kiwi.sparql.persistence.KiWiSparqlConnection;
-import org.openrdf.model.ValueFactory;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.Dataset;
-import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.algebra.QueryRoot;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
-import org.openrdf.query.algebra.evaluation.impl.*;
-import org.openrdf.query.impl.EmptyBindingSet;
-import org.openrdf.sail.NotifyingSailConnection;
-import org.openrdf.sail.SailConnection;
-import org.openrdf.sail.SailException;
-import org.openrdf.sail.helpers.NotifyingSailConnectionWrapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Add file description here!
- *
- * @author Sebastian Schaffert (sschaffert@apache.org)
- */
-public class KiWiSparqlSailConnection extends NotifyingSailConnectionWrapper {
-
- private static Logger log = LoggerFactory.getLogger(KiWiSparqlSailConnection.class);
-
- private KiWiSparqlConnection connection;
- private KiWiValueFactory valueFactory;
-
- public KiWiSparqlSailConnection(NotifyingSailConnection parent, KiWiSparqlConnection connection, KiWiValueFactory valueFactory) {
- super(parent);
- this.connection = connection;
- this.valueFactory = valueFactory;
- }
-
- @Override
- public CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) throws SailException {
- // Clone the tuple expression to allow for more aggressive optimizations
- tupleExpr = tupleExpr.clone();
-
- if (!(tupleExpr instanceof QueryRoot)) {
- // Add a dummy root node to the tuple expressions to allow the
- // optimizers to modify the actual root node
- tupleExpr = new QueryRoot(tupleExpr);
- }
-
- try {
- KiWiTripleSource tripleSource = new KiWiTripleSource(this,valueFactory,includeInferred);
- EvaluationStrategy strategy = new KiWiEvaluationStrategyImpl(tripleSource, dataset, connection);
-
- new BindingAssigner().optimize(tupleExpr, dataset, bindings);
- new ConstantOptimizer(strategy).optimize(tupleExpr, dataset, bindings);
- new CompareOptimizer().optimize(tupleExpr, dataset, bindings);
- new ConjunctiveConstraintSplitter().optimize(tupleExpr, dataset, bindings);
- new DisjunctiveConstraintOptimizer().optimize(tupleExpr, dataset, bindings);
- new SameTermFilterOptimizer().optimize(tupleExpr, dataset, bindings);
- new QueryModelNormalizer().optimize(tupleExpr, dataset, bindings);
- new QueryJoinOptimizer(new KiWiEvaluationStatistics()).optimize(tupleExpr, dataset, bindings);
- new IterativeEvaluationOptimizer().optimize(tupleExpr, dataset, bindings);
- new FilterOptimizer().optimize(tupleExpr, dataset, bindings);
- new OrderLimitOptimizer().optimize(tupleExpr, dataset, bindings);
-
- log.debug("evaluating SPARQL query:\n {}", tupleExpr);
-
- return strategy.evaluate(tupleExpr, EmptyBindingSet.getInstance());
-
- } catch (QueryEvaluationException e) {
- throw new SailException(e.getMessage(),e);
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/4aa87dbe/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/JoinTable.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/JoinTable.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/JoinTable.java
new file mode 100644
index 0000000..c40fdca
--- /dev/null
+++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/JoinTable.java
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.marmotta.platform.sparql.services.evaluation.sql;
+
+import org.jooq.Condition;
+import org.jooq.Table;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Wrapper for representing information about a table to join with. Holds a reference to the table and a
+ * list of conditions to be used as join conditions.
+ * <p/>
+ * Author: Sebastian Schaffert
+ */
+public class JoinTable {
+
+ private Table table;
+
+ private Set<Condition> conditions;
+
+ // remember conditions referring to tables not created in the current statement; they will also be contained
+ // in conditions, this set is just for checking in special cases
+ private Set<Condition> backConditions;
+
+
+ public JoinTable(Table table) {
+ this.table = table;
+
+ conditions = new HashSet<Condition>();
+ backConditions = new HashSet<Condition>();
+ }
+
+ public void setTable(Table table) {
+ this.table = table;
+ }
+
+ public Table getTable() {
+ return table;
+ }
+
+ public Set<Condition> getConditions() {
+ return conditions;
+ }
+
+ public void addCondition(Condition condition) {
+ conditions.add(condition);
+ }
+
+ public Set<Condition> getBackConditions() {
+ return backConditions;
+ }
+
+ public void addBackCondition(Condition condition) {
+ backConditions.add(condition);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/4aa87dbe/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/LMFNativeEvaluationStrategy.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/LMFNativeEvaluationStrategy.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/LMFNativeEvaluationStrategy.java
new file mode 100644
index 0000000..7135d9f
--- /dev/null
+++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/sqlmapper/LMFNativeEvaluationStrategy.java
@@ -0,0 +1,479 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.marmotta.platform.sparql.services.evaluation.sql;
+
+import at.newmedialab.sesame.commons.model.LiteralCommons;
+import info.aduna.iteration.CloseableIteration;
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.persistence.PersistenceService;
+import org.apache.marmotta.platform.core.api.triplestore.SesameService;
+import org.apache.marmotta.platform.core.events.DBInitialisationEvent;
+import org.apache.marmotta.platform.core.model.rdf.KiWiNode;
+import org.apache.marmotta.platform.core.services.sail.LMFEvaluationStrategy;
+import org.apache.commons.validator.routines.UrlValidator;
+import org.jooq.Cursor;
+import org.jooq.Record;
+import org.jooq.SQLDialect;
+import org.jooq.Select;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.algebra.TupleExpr;
+import org.openrdf.query.algebra.ValueExpr;
+import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
+import org.openrdf.query.impl.MapBindingSet;
+import org.slf4j.Logger;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * An implementation of a Sesame evaluation strategy that translates SPARQL queries into native SQL queries
+ * on the LMF triple store for improved performance. This implementation is based on the jOOQ Java-to-SQL library
+ * to achieve a certain level of database independence.
+ *
+ * TODO: this class should somehow move to the lmf-sparql package
+ * <p/>
+ * Author: Sebastian Schaffert
+ */
+@ApplicationScoped
+public class LMFNativeEvaluationStrategy implements LMFEvaluationStrategy {
+
+ @Inject
+ private Logger log;
+
+ @Inject
+ private PersistenceService persistenceService;
+
+ @Inject
+ private ConfigurationService configurationService;
+
+
+ @Inject
+ private SesameService sesameService;
+
+
+ @PostConstruct
+ public void initialise() {
+ log.info("initialising LMF native query evaluation strategy (SPARQL -> SQL mapper)");
+
+ }
+
+
+ public void onDatabaseInitialisation(@Observes DBInitialisationEvent event) {
+ log.debug("Received DB-initialisation event, checking existance of SPARQL query indexes");
+
+ if(!"off".equals(configurationService.getStringConfiguration("database.mode")) && "native".equals(configurationService.getStringConfiguration("sparql.strategy"))) {
+ checkIndexes();
+ }
+ }
+
+
+ /**
+ * Depending on the database dialect, check for the existance of appropriate indexes to speed up
+ * typical SPARQL queries
+ */
+ private void checkIndexes() {
+ switch (getSQLDialect()) {
+ case POSTGRES:
+ log.info("SQL Dialect is PostgreSQL, creating advanced indexes");
+ createIndexesPostgres();
+ break;
+ }
+
+ }
+
+ private void createIndexesPostgres() {
+ Connection connection = persistenceService.getJDBCConnection();
+ try {
+ // index for language matches
+ String idx_sparql_lang = "create index idx_sparql_lang on kiwinode ( coalesce(locale,''));";
+ connection.createStatement().executeUpdate("drop index if exists idx_sparql_lang");
+ connection.createStatement().executeUpdate(idx_sparql_lang);
+
+ // index for string value matches
+ //String idx_sparql_content = "create index idx_sparql_content on kiwinode ( coalesce(content,uri,anonid,'') text_pattern_ops);";
+ String idx_sparql_content = "create index idx_sparql_content on kiwinode using hash( coalesce(content,uri,anonid,''));";
+ connection.createStatement().executeUpdate("drop index if exists idx_sparql_content");
+ connection.createStatement().executeUpdate(idx_sparql_content);
+
+ // index for combined URI/ID matches (typical join in triple patterns)
+ String idx_sparql_uri = "create unique index idx_sparql_uri on kiwinode (id, uri);";
+ connection.createStatement().executeUpdate("drop index if exists idx_sparql_uri");
+ connection.createStatement().executeUpdate(idx_sparql_uri);
+
+ if(!connection.getAutoCommit())
+ connection.commit();
+ } catch (SQLException e) {
+ log.error("Postgres: error while trying to create indexes!",e);
+ } finally {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ log.error("could not close SQL connection",e);
+ }
+ }
+
+ }
+
+ /**
+ * Return the SQL Dialect used in this LMF instance. Returns null for unknown dialects.
+ *
+ * @return
+ */
+ private SQLDialect getSQLDialect() {
+ String db_type = configurationService.getStringConfiguration("database.type","h2");
+
+ SQLDialect dialect = null;
+ if(db_type.equals("h2")) {
+ dialect = SQLDialect.H2;
+ } else if(db_type.equals("postgres")) {
+ dialect = SQLDialect.POSTGRES;
+ } else if(db_type.equals("oracle")) {
+ dialect = SQLDialect.ORACLE;
+ } else if(db_type.equals("mysql")) {
+ dialect = SQLDialect.MYSQL;
+ }
+ return dialect;
+ }
+
+ /**
+ * Get the unique identifier for the evaluation strategy (e.g. "sesame", "hql", "native")
+ *
+ * @return
+ */
+ @Override
+ public String getIdentifier() {
+ return "native";
+ }
+
+ /**
+ * Return true if this evaluation strategy is available for use. Evaluation strategies can e.g. disable
+ * themselves in case the underlying database system or persistence layer is not supported.
+ *
+ * @return
+ */
+ @Override
+ public boolean isEnabled() {
+ String db_type = configurationService.getStringConfiguration("database.type","h2");
+
+ return "h2".equals(db_type) || "postgres".equals(db_type) || "mysql".equals(db_type) || "oracle".equals(db_type);
+ }
+
+ /**
+ * Evaluates the tuple expression against the supplied triple source with the
+ * specified set of variable bindings as input.
+ *
+ * @param expr The Tuple Expression to evaluate
+ * @param bindings The variables bindings to use for evaluating the expression, if
+ * applicable.
+ * @return A closeable iterator over the variable binding sets that match the
+ * tuple expression.
+ */
+ @Override
+ public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final TupleExpr expr, final BindingSet bindings) throws QueryEvaluationException {
+
+
+ String db_type = configurationService.getStringConfiguration("database.type","h2");
+
+ SQLDialect dialect = null;
+ if(db_type.equals("h2")) {
+ dialect = SQLDialect.H2;
+ } else if(db_type.equals("postgres")) {
+ dialect = SQLDialect.POSTGRES;
+ } else if(db_type.equals("oracle")) {
+ dialect = SQLDialect.ORACLE;
+ } else if(db_type.equals("mysql")) {
+ dialect = SQLDialect.MYSQL;
+ }
+
+ SparqlToSQLMapper.CastType castType = SparqlToSQLMapper.CastType.STRICT;
+ String castCfg = configurationService.getStringConfiguration("sparql.native.casttype","strict");
+ if("strict".equals(castCfg)) {
+ castType = SparqlToSQLMapper.CastType.STRICT;
+ } else if("loose".equals(castCfg)) {
+ castType = SparqlToSQLMapper.CastType.LOOSE;
+ } else if("none".equals(castCfg)) {
+ castType = SparqlToSQLMapper.CastType.NONE;
+ }
+
+ if(dialect != null) {
+
+ // rearrange the SPARQL abstract syntax tree so things are more suitable for SQL
+
+ // no unions as subselects in joins, we move the joins inside the union instead
+ new UnionOptimizer().optimize(expr);
+
+ final Connection connection = persistenceService.getJDBCConnection();
+
+ try {
+ connection.setAutoCommit(false);
+
+ final SparqlToSQLMapper mapper = new SparqlToSQLMapper(connection,dialect,expr, castType);
+
+ final Select query = mapper.getQuery();
+
+ if(log.isDebugEnabled() || configurationService.getBooleanConfiguration("sparql.native.logsql",true)) {
+ log.info("SQL query from SPARQL transformation:\n{}", query.getSQL());
+ }
+
+
+ final EntityManager em = persistenceService.getEntityManager();
+
+ return new CloseableIteration<BindingSet, QueryEvaluationException>() {
+
+ private Cursor cursor = query.fetchLazy(50);
+
+ private UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES | UrlValidator.ALLOW_LOCAL_URLS);
+
+ // in case we need to split one SQL query row into several SPARQL query rows, we do it by creating
+ // a temporary iterator
+ private Iterator<BindingSet> splitBindings = null;
+
+ @Override
+ public void close() throws QueryEvaluationException {
+ if(!cursor.isClosed()) {
+ cursor.close();
+ }
+ try {
+ if(!connection.getAutoCommit()) {
+ connection.commit();
+ }
+ connection.close();
+ } catch (SQLException e) {
+ log.error("SQL exception while closing JDBC connection",e);
+ }
+ em.close();
+ }
+
+ @Override
+ public boolean hasNext() throws QueryEvaluationException {
+ return (splitBindings !=null && splitBindings.hasNext()) || cursor.hasNext();
+ }
+
+ @Override
+ public BindingSet next() throws QueryEvaluationException {
+ if(splitBindings != null) {
+ return splitBindings.next();
+ } else {
+ Record record = cursor.fetchOne();
+
+ // check if the result contains multiple SPARQL bindings in a single row or
+ // only one SPARQL row; we can do this by checking whether the first result starts
+ // with _multi
+ if(record.getField(0).getName().startsWith("_multi")) {
+ log.info("transforming single-row SQL result into multi-row SPARQL result");
+ List<BindingSet> results = new ArrayList<BindingSet>();
+ for(int i=1; true; i++) {
+ MapBindingSet result = new MapBindingSet();
+ for(String var : mapper.getProjectedVariables()) {
+ if(var.startsWith("_multi") && var.endsWith("_"+i)) {
+ Long nodeId = record.getValue(var, Long.class);
+
+ if(nodeId != null) {
+ Value value = em.find(KiWiNode.class, nodeId);
+
+ result.addBinding(var.substring(var.indexOf('_',1)+1,var.lastIndexOf('_')),value);
+ }
+ }
+ }
+ for(Map.Entry<String,Class> ext : mapper.getExtensionVariables().entrySet()) {
+ String var = ext.getKey();
+ if(var.startsWith("_multi") && var.endsWith("_"+i)) {
+ Object val = record.getValue(ext.getKey(),ext.getValue());
+
+ // this is truly a hack: we check whether the string is a URI, and if yes create a URI resource...
+ // it would be better to carry over this information from the value constants
+ if(urlValidator.isValid(val.toString())) {
+ URI value = new URIImpl(val.toString());
+ result.addBinding(var.substring(var.indexOf('_',1)+1,var.lastIndexOf('_')),value);
+ } else {
+ String type = LiteralCommons.getXSDType(ext.getValue());
+
+ // we only create an in-memory representation of the value, the LMF methods
+ // would automatically persist it, so we create a Sesame value
+ Value value = new LiteralImpl(val.toString(),sesameService.getValueFactory().createURI(type));
+ result.addBinding(var.substring(var.indexOf('_',1)+1,var.lastIndexOf('_')),value);
+ }
+ }
+ }
+ if(result.size() == 0) {
+ break;
+ } else {
+ results.add(result);
+ }
+ }
+ em.clear();
+ splitBindings = results.iterator();
+ return splitBindings.next();
+ } else {
+
+ MapBindingSet result = new MapBindingSet();
+ for(String var : mapper.getProjectedVariables()) {
+ Long nodeId = record.getValue(var, Long.class);
+
+ if(nodeId != null) {
+ Value value = em.find(KiWiNode.class, nodeId);
+ result.addBinding(var,value);
+ }
+ }
+ for(Map.Entry<String,Class> ext : mapper.getExtensionVariables().entrySet()) {
+ Object val = record.getValue(ext.getKey(),ext.getValue());
+
+ // this is truly a hack: we check whether the string is a URI, and if yes create a URI resource...
+ // it would be better to carry over this information from the value constants
+ if(urlValidator.isValid(val.toString())) {
+ URI value = new URIImpl(val.toString());
+ result.addBinding(ext.getKey(),value);
+ } else {
+ String type = LiteralCommons.getXSDType(ext.getValue());
+
+ // we only create an in-memory representation of the value, the LMF methods
+ // would automatically persist it, so we create a Sesame value
+ Value value = new LiteralImpl(val.toString(),sesameService.getValueFactory().createURI(type));
+ result.addBinding(ext.getKey(),value);
+ }
+ }
+
+ em.clear();
+
+ return result;
+ }
+ }
+ }
+
+ @Override
+ public void remove() throws QueryEvaluationException {
+ throw new UnsupportedOperationException("removing not supported");
+ }
+ };
+
+ } catch(Exception ex) {
+ log.error("exception while translating SPARQL query",ex);
+ log.error("abstract query tree was: {}",expr);
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ log.error("error while trying to close JDBC connection",e);
+ }
+ }
+
+ throw new UnsupportedOperationException("not yet implemented");
+ }
+
+ throw new UnsupportedOperationException("the database "+db_type+" is not yet supported for SPARQL->SQL mapping");
+ }
+
+ /**
+ * Gets the value of this expression.
+ *
+ * @param bindings The variables bindings to use for evaluating the expression, if
+ * applicable.
+ * @return The Value that this expression evaluates to, or <tt>null</tt> if
+ * the expression could not be evaluated.
+ */
+ @Override
+ public Value evaluate(ValueExpr expr, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
+ String db_type = configurationService.getStringConfiguration("database.type","h2");
+ /*
+ SQLDialect dialect = null;
+ if(db_type.equals("h2")) {
+ dialect = SQLDialect.H2;
+ } else if(db_type.equals("postgres")) {
+ dialect = SQLDialect.POSTGRES;
+ } else if(db_type.equals("oracle")) {
+ dialect = SQLDialect.ORACLE;
+ } else if(db_type.equals("mysql")) {
+ dialect = SQLDialect.MYSQL;
+ }
+
+ SparqlToSQLMapper.CastType castType = SparqlToSQLMapper.CastType.STRICT;
+ String castCfg = configurationService.getStringConfiguration("sparql.native.casttype","strict");
+ if("strict".equals(castCfg)) {
+ castType = SparqlToSQLMapper.CastType.STRICT;
+ } else if("loose".equals(castCfg)) {
+ castType = SparqlToSQLMapper.CastType.LOOSE;
+ } else if("none".equals(castCfg)) {
+ castType = SparqlToSQLMapper.CastType.NONE;
+ }
+
+ if(dialect != null) {
+ final Connection connection = persistenceService.getJDBCConnection();
+
+ try {
+ final SparqlToSQLMapper mapper = new SparqlToSQLMapper(connection,dialect,expr,castType);
+
+ final Select query = mapper.getQuery();
+
+ log.info("SQL query would be: {}", query.getSQL());
+
+
+ Object value = query.fetchOne(0);
+
+
+ connection.commit();
+ connection.close();
+
+ throw new UnsupportedOperationException("not yet implemented");
+ } catch (SQLException e) {
+ log.error("error while evaluating SQL query",e);
+ } finally {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ log.error("error while trying to close JDBC connection",e);
+ }
+ }
+
+ }
+*/
+ throw new UnsupportedOperationException("the database "+db_type+" is not yet supported for SPARQL->SQL mapping");
+ }
+
+ /**
+ * Evaluates the boolean expression on the supplied TripleSource object.
+ *
+ * @param bindings The variables bindings to use for evaluating the expression, if
+ * applicable.
+ * @return The result of the evaluation.
+ * @throws org.openrdf.query.algebra.evaluation.ValueExprEvaluationException
+ * If the value expression could not be evaluated, for example when
+ * comparing two incompatible operands. When thrown, the result of
+ * the boolean expression is neither <tt>true</tt> nor
+ * <tt>false</tt>, but unknown.
+ */
+ @Override
+ public boolean isTrue(ValueExpr expr, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+
+
+}