package de.fuberlin.wiwiss.d2rq.optimizer.expr;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.expr.E_Add;
import com.hp.hpl.jena.sparql.expr.E_Datatype;
import com.hp.hpl.jena.sparql.expr.E_Divide;
import com.hp.hpl.jena.sparql.expr.E_Equals;
import com.hp.hpl.jena.sparql.expr.E_GreaterThan;
import com.hp.hpl.jena.sparql.expr.E_GreaterThanOrEqual;
import com.hp.hpl.jena.sparql.expr.E_IsBlank;
import com.hp.hpl.jena.sparql.expr.E_IsIRI;
import com.hp.hpl.jena.sparql.expr.E_IsLiteral;
import com.hp.hpl.jena.sparql.expr.E_Lang;
import com.hp.hpl.jena.sparql.expr.E_LangMatches;
import com.hp.hpl.jena.sparql.expr.E_LessThan;
import com.hp.hpl.jena.sparql.expr.E_LessThanOrEqual;
import com.hp.hpl.jena.sparql.expr.E_LogicalNot;
import com.hp.hpl.jena.sparql.expr.E_LogicalOr;
import com.hp.hpl.jena.sparql.expr.E_Multiply;
import com.hp.hpl.jena.sparql.expr.E_NotEquals;
import com.hp.hpl.jena.sparql.expr.E_SameTerm;
import com.hp.hpl.jena.sparql.expr.E_Str;
import com.hp.hpl.jena.sparql.expr.E_Subtract;
import com.hp.hpl.jena.sparql.expr.E_UnaryMinus;
import com.hp.hpl.jena.sparql.expr.E_UnaryPlus;
import com.hp.hpl.jena.sparql.expr.Expr;
import com.hp.hpl.jena.sparql.expr.ExprAggregator;
import com.hp.hpl.jena.sparql.expr.ExprEvalException;
import com.hp.hpl.jena.sparql.expr.ExprFunction;
import com.hp.hpl.jena.sparql.expr.ExprFunction0;
import com.hp.hpl.jena.sparql.expr.ExprFunction1;
import com.hp.hpl.jena.sparql.expr.ExprFunction2;
import com.hp.hpl.jena.sparql.expr.ExprFunction3;
import com.hp.hpl.jena.sparql.expr.ExprFunctionN;
import com.hp.hpl.jena.sparql.expr.ExprFunctionOp;
import com.hp.hpl.jena.sparql.expr.ExprVar;
import com.hp.hpl.jena.sparql.expr.ExprVisitor;
import com.hp.hpl.jena.sparql.expr.NodeValue;
import com.hp.hpl.jena.sparql.expr.nodevalue.NodeFunctions;
import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueBoolean;
import de.fuberlin.wiwiss.d2rq.algebra.Attribute;
import de.fuberlin.wiwiss.d2rq.algebra.ExpressionProjectionSpec;
import de.fuberlin.wiwiss.d2rq.algebra.NodeRelation;
import de.fuberlin.wiwiss.d2rq.algebra.ProjectionSpec;
import de.fuberlin.wiwiss.d2rq.algebra.RelationalOperators;
import de.fuberlin.wiwiss.d2rq.expr.Add;
import de.fuberlin.wiwiss.d2rq.expr.Constant;
import de.fuberlin.wiwiss.d2rq.expr.Divide;
import de.fuberlin.wiwiss.d2rq.expr.Equality;
import de.fuberlin.wiwiss.d2rq.expr.Expression;
import de.fuberlin.wiwiss.d2rq.expr.GreaterThan;
import de.fuberlin.wiwiss.d2rq.expr.GreaterThanOrEqual;
import de.fuberlin.wiwiss.d2rq.expr.LessThan;
import de.fuberlin.wiwiss.d2rq.expr.LessThanOrEqual;
import de.fuberlin.wiwiss.d2rq.expr.Multiply;
import de.fuberlin.wiwiss.d2rq.expr.Negation;
import de.fuberlin.wiwiss.d2rq.expr.SQLExpression;
import de.fuberlin.wiwiss.d2rq.expr.Subtract;
import de.fuberlin.wiwiss.d2rq.expr.UnaryMinus;
import de.fuberlin.wiwiss.d2rq.nodes.DetermineNodeType;
import de.fuberlin.wiwiss.d2rq.nodes.FixedNodeMaker;
import de.fuberlin.wiwiss.d2rq.nodes.NodeMaker;
import de.fuberlin.wiwiss.d2rq.nodes.NodeSetConstraintBuilder;
import de.fuberlin.wiwiss.d2rq.nodes.TypedNodeMaker;
import de.fuberlin.wiwiss.d2rq.values.ValueMaker;
/**
* Attempts to transform a SPARQL FILTER Expr to a SQL Expression
*
* Notes:
* <ul>
* <li>Literals using d2rq:pattern cannot be compared.</li>
* <li>No XSD type checking/conversion or constructor functions yet.</li>
* </ul>
*
* @author Herwig Leimer
* @author Giovanni Mels
*/
public final class TransformExprToSQLApplyer implements ExprVisitor {
private static final Log logger = LogFactory.getLog(TransformExprToSQLApplyer.class);
/**
* Converts a SPARQL filter expression to an SQL expression
*
* @param expr The root node of an {@link Expr Expr} tree, contains the SPARQL filter.
* @param nodeRelation The relation supplying the values to apply the filter on.
* @return The root node of an {@link Expression Expression} tree, if conversion was successful, <code>null</code> otherwise.
*/
public static Expression convert(final Expr expr, final NodeRelation nodeRelation) {
TransformExprToSQLApplyer transformer = new TransformExprToSQLApplyer(nodeRelation);
expr.visit(transformer);
return transformer.result();
}
// TODO Expression.FALSE and Expression.TRUE are not constants
private static final Expression CONSTANT_FALSE = new ConstantEx("false", NodeValueBoolean.FALSE.asNode());
private static final Expression CONSTANT_TRUE = new ConstantEx("true", NodeValueBoolean.TRUE.asNode());
private final NodeRelation nodeRelation;
private final Stack<Expression> expression = new Stack<Expression>();
private boolean convertable; // flag if converting was possible
private String reason = null; // reason why converting failed
/**
* Creates an expression transformer.
*
* @param nodeRelation
*/
public TransformExprToSQLApplyer(NodeRelation nodeRelation)
{
this.convertable = true;
this.nodeRelation = nodeRelation;
}
/**
* Returns the sql Expression
*
* @return the transformed sql expression if converting was possible, otherwise null.
*/
public Expression result()
{
if (!convertable) {
logger.debug("filter conversion failed: " + reason);
return null;
}
if (expression.size() != 1)
throw new IllegalStateException("something is seriously wrong");
Expression result = expression.pop();
logger.debug("Resulting filter = " + result);
return result;
}
public void startVisit()
{
logger.debug("transform started");
}
public void finishVisit()
{
logger.debug("transform finished");
}
public void visit(ExprFunction0 func) {
visitExprFunction(func);
}
public void visit(ExprFunction1 function) {
logger.debug("visit ExprFunction " + function);
if (!convertable) {
expression.push(Expression.FALSE); // prevent stack empty exceptions when conversion
return; // fails in the middle of a multi-arg operator conversion
}
convertFunction(function);
}
public void visit(ExprFunction2 function) {
logger.debug("visit ExprFunction " + function);
if (!convertable) {
expression.push(Expression.FALSE); // prevent stack empty exceptions when conversion
return; // fails in the middle of a multi-arg operator conversion
}
convertFunction(function);
}
public void visit(ExprFunction3 func) {
visitExprFunction(func);
}
public void visit(ExprFunctionN func) {
visitExprFunction(func);
}
public void visit(ExprFunctionOp funcOp) {
visitExprFunction(funcOp);
}
public void visit(ExprAggregator eAgg) {
conversionFailed(eAgg);
}
public void visit(ExprVar var)
{
logger.debug("visit ExprVar " + var);
if (!convertable) {
expression.push(Expression.FALSE); // prevent stack empty exceptions when conversion
return; // fails in the middle of a multi-arg operator conversion
}
String varName = var.getVarName();
// if expression contains a blank node, no conversion to sql can be done
if (Var.isBlankNodeVarName(varName)) {
conversionFailed("blank nodes not supported", var);
return;
}
List<Expression> expressions = toExpression(var);
if (expressions.size() == 1) {
expression.push(expressions.get(0));
} else {
// no single sql-column for sparql-var does exist break up conversion
// (the case for Pattern ValueMakers)
conversionFailed("multi column pattern valuemakers not supported", var);
}
}
public void visit(NodeValue value)
{
logger.debug("visit NodeValue " + value);
if (!convertable) {
expression.push(Expression.FALSE); // pr
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【作品名称】: 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】:基于知识图谱的农业病虫害数据查询系统(附数据爬虫) d2rq启动流程 根据数据库表生成对应映射文件 generate-mapping -u 用户名 -p 密码 -o 映射文件名 数据库连接信息 generate-mapping -u root -p 123456 -o crop_atlas.ttl jdbc:mysql:///kg_demo_movie D2RQ由rdf映射文件生成实体rdf .\dump-rdf.bat -o crop_atlas.nt .\crop_atlas.ttl D2RQ服务器启动 d2r-server.bat crop_pedigree.ttl
资源推荐
资源详情
资源评论
收起资源包目录
基于知识图谱的农业病虫害数据查询系统(附数据爬虫) (1162个子文件)
d2r-server.bat 459B
generate-mapping.bat 360B
d2r-query.bat 349B
dump-rdf.bat 348B
mvnw.cmd 6KB
layui.css 78KB
layer.css 14KB
common.css 13KB
laydate.css 7KB
styles.css 5KB
style.css 4KB
main.css 3KB
style.css 2KB
style.css 2KB
reset.css 1KB
stylesheet.css 1KB
code.css 1KB
metadata.css 439B
translationtable.csv 17B
d2r-query 428B
d2r-server 536B
iswc.daml 35KB
userLibrary.dtc 56KB
dump-rdf 427B
generate-mapping 435B
fu-logo.gif 6KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
rdf_flyer.24.gif 330B
inherit.gif 57B
.gitignore 395B
.htaccess 81B
ISWC.html 116KB
Expression.html 102KB
D2RQ.html 92KB
ModelD2RQ.html 86KB
D2RQException.html 83KB
Attribute.html 77KB
d2rq-language.html 72KB
PushDownOpFilterVisitor.html 69KB
index-19.html 67KB
constant-values.html 60KB
index-20.html 59KB
index-7.html 58KB
index-22.html 58KB
overview-tree.html 57KB
ResourceMap.html 57KB
index-3.html 55KB
index-4.html 53KB
index-18.html 51KB
index-1.html 51KB
SQL92.html 50KB
ProjectionSpec.html 48KB
RelationName.html 46KB
ConnectedDB.html 45KB
SKOS.html 45KB
PropertyBridge.html 44KB
MappingGenerator.html 43KB
AliasMap.html 43KB
ConnectedDB.html 42KB
index-13.html 42KB
ColumnRenamer.html 42KB
index-16.html 42KB
VarCollector.html 41KB
Vendor.html 41KB
index-9.html 41KB
RelationImpl.html 39KB
AutoReloadableDataset.html 37KB
Relation.html 37KB
D2RServer.html 37KB
Database.html 37KB
Pattern.html 36KB
SystemLoader.html 35KB
package-use.html 35KB
TypedNodeMaker.html 35KB
TransformExprToSQLApplyer.html 35KB
NodeMaker.html 34KB
Oracle.html 34KB
Attribute.html 33KB
SQLServer.html 33KB
AliasMap.html 33KB
QueryIterTableSQL.html 33KB
OpUnionTableSQL.html 33KB
DetermineNodeType.html 32KB
Vendor.html 32KB
Mapping.html 32KB
NodeSetConstraintBuilder.html 32KB
Relation.html 32KB
ValueDecorator.html 31KB
TransformOpBGP.html 31KB
ClassMap.html 31KB
GraphD2RQ.html 31KB
OpTableSQL.html 31KB
MySQL.html 30KB
TransformFilterCNF.html 30KB
index-12.html 30KB
TransformFilterCNF.DistributiveLawApplyer.html 29KB
ConfigLoader.html 29KB
TransformFilterCNF.DeMorganLawApplyer.html 29KB
共 1162 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
MarcoPage
- 粉丝: 3036
- 资源: 3306
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功