private void convertNotEquals(E_NotEquals expr) { logger.debug("convertNotEquals " + expr.toString()); expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); // TODO Expression.FALSE and Expression.TRUE are not constants if (e1.equals(Expression.FALSE)) e1 = CONSTANT_FALSE; else if (e1.equals(Expression.TRUE)) e1 = CONSTANT_TRUE; if (e2.equals(Expression.FALSE)) e2 = CONSTANT_FALSE; else if (e2.equals(Expression.TRUE)) e2 = CONSTANT_TRUE; if (e1 instanceof AttributeExprEx && e2 instanceof Constant || e2 instanceof AttributeExprEx && e1 instanceof Constant) { AttributeExprEx variable; ConstantEx constant; if (e1 instanceof AttributeExprEx) { variable = (AttributeExprEx) e1; constant = (ConstantEx) e2; } else { variable = (AttributeExprEx) e2; constant = (ConstantEx) e1; } logger.debug("isNotEqual(" + variable + ", " + constant + ")"); NodeMaker nm = variable.getNodeMaker(); if (nm instanceof TypedNodeMaker) { ValueMaker vm = ((TypedNodeMaker) nm).valueMaker(); Node node = constant.getNode(); logger.debug("checking " + node + " with " + nm); if (XSD.isNumeric(node)) { DetermineNodeType filter = new DetermineNodeType(); nm.describeSelf(filter); RDFDatatype datatype = filter.getDatatype(); if (datatype != null && XSD.isNumeric(datatype)) { RDFDatatype numericType = XSD.getNumericType(datatype, node.getLiteralDatatype()); nm = cast(nm, numericType); node = XSD.cast(node, numericType); } } boolean empty = nm.selectNode(node, RelationalOperators.DUMMY).equals(NodeMaker.EMPTY); logger.debug("result " + new Boolean(empty)); if (!empty) { if (node.isURI()) expression.push(new Negation(vm.valueExpression(node.getURI()))); else if (node.isLiteral()) { if (XSD.isSupported(node.getLiteralDatatype())) expression.push(new Negation(vm.valueExpression(constant.value()))); else // type = boolean or an unknown type conversionFailed("cannot compare values of type " + node.getLiteralDatatypeURI(), expr); } else conversionFailed(expr); // TODO blank nodes? return; } else { expression.push(Expression.TRUE); return; } } } else if (e1 instanceof ConstantEx && e2 instanceof ConstantEx) { logger.debug("isNotEqual(" + e1 + ", " + e2 + ")"); Node c1 = ((ConstantEx) e1).getNode(); Node c2 = ((ConstantEx) e2).getNode(); boolean equals; if (XSD.isNumeric(c1) && XSD.isNumeric(c2)) { RDFDatatype datatype = XSD.getNumericType(c1.getLiteralDatatype(), c2.getLiteralDatatype()); equals = XSD.cast(c1, datatype).equals(XSD.cast(c2, datatype)); } else if (isSimpleLiteral(c1) && isSimpleLiteral(c2)) { equals = c1.getLiteralValue().equals(c2.getLiteralValue()); } else if (XSD.isString(c1) && XSD.isString(c2)) { equals = c1.getLiteralValue().equals(c2.getLiteralValue()); } else { try { equals = NodeFunctions.rdfTermEquals(c1, c2); } catch (ExprEvalException e) { equals = false; } } logger.debug("constants equal? " + new Boolean(equals)); expression.push(equals ? Expression.FALSE : Expression.TRUE); return; } else if (e1 instanceof AttributeExprEx && e2 instanceof AttributeExprEx) { logger.debug("isNotEqual(" + e1 + ", " + e2 + ")"); AttributeExprEx variable1 = (AttributeExprEx) e1; AttributeExprEx variable2 = (AttributeExprEx) e2; NodeMaker nm1 = variable1.getNodeMaker(); NodeMaker nm2 = variable2.getNodeMaker(); DetermineNodeType filter1 = new DetermineNodeType(); nm1.describeSelf(filter1); RDFDatatype datatype1 = filter1.getDatatype(); DetermineNodeType filter2 = new DetermineNodeType(); nm2.describeSelf(filter2); RDFDatatype datatype2 = filter2.getDatatype(); if (datatype1 != null && XSD.isNumeric(datatype1) && datatype2 != null && XSD.isNumeric(datatype2)) { RDFDatatype numericType = XSD.getNumericType(filter1.getDatatype(), filter2.getDatatype()); nm1 = cast(nm1, numericType); nm2 = cast(nm2, numericType); } NodeSetConstraintBuilder nodeSet = new NodeSetConstraintBuilder(); nm1.describeSelf(nodeSet); nm2.describeSelf(nodeSet); if (nodeSet.isEmpty()) { logger.debug("nodes " + nm1 + " " + nm2 + " incompatible"); expression.push(Expression.TRUE); return; } } expression.push(new Negation(Equality.create(e1, e2))); }
/* * See http://www.w3.org/TR/rdf-sparql-query paragraph 11.4.11 * * "(SAMETERM) Returns TRUE if term1 and term2 are the same RDF term as defined * in Resource Description Framework (RDF): Concepts and Abstract Syntax [CONCEPTS]; returns FALSE otherwise." * * @see http://www.w3.org/TR/rdf-sparql-query * @see http://www.w3.org/TR/rdf-concepts/ */ private void convertSameTerm(E_SameTerm expr) { logger.debug("convertSameTerm " + expr.toString()); expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); // TODO Expression.FALSE and Expression.TRUE are not constants if (e1.equals(Expression.FALSE)) e1 = CONSTANT_FALSE; else if (e1.equals(Expression.TRUE)) e1 = CONSTANT_TRUE; if (e2.equals(Expression.FALSE)) e2 = CONSTANT_FALSE; else if (e2.equals(Expression.TRUE)) e2 = CONSTANT_TRUE; if (e1 instanceof AttributeExprEx && e2 instanceof Constant || e2 instanceof AttributeExprEx && e1 instanceof Constant) { AttributeExprEx variable; ConstantEx constant; if (e1 instanceof AttributeExprEx) { variable = (AttributeExprEx) e1; constant = (ConstantEx) e2; } else { variable = (AttributeExprEx) e2; constant = (ConstantEx) e1; } logger.debug("isEqual(" + variable + ", " + constant + ")"); NodeMaker nm = variable.getNodeMaker(); if (nm instanceof TypedNodeMaker) { ValueMaker vm = ((TypedNodeMaker) nm).valueMaker(); Node node = constant.getNode(); logger.debug("checking " + node + " with " + nm); boolean empty = nm.selectNode(node, RelationalOperators.DUMMY).equals(NodeMaker.EMPTY); logger.debug("result " + new Boolean(empty)); if (!empty) { if (node.isURI()) expression.push(vm.valueExpression(node.getURI())); else if (node.isLiteral()) expression.push(vm.valueExpression(constant.value())); else conversionFailed(expr); // TODO blank nodes? return; } else { expression.push(Expression.FALSE); return; } } else { logger.warn("nm is not a TypedNodemaker"); } } else if (e1 instanceof ConstantEx && e2 instanceof ConstantEx) { logger.debug("isEqual(" + e1 + ", " + e2 + ")"); ConstantEx constant1 = (ConstantEx) e1; ConstantEx constant2 = (ConstantEx) e2; boolean equals = NodeFunctions.sameTerm(constant1.getNode(), constant2.getNode()); logger.debug("constants same? " + new Boolean(equals)); expression.push(equals ? Expression.TRUE : Expression.FALSE); return; } else if (e1 instanceof AttributeExprEx && e2 instanceof AttributeExprEx) { logger.debug("isEqual(" + e1 + ", " + e2 + ")"); AttributeExprEx variable1 = (AttributeExprEx) e1; AttributeExprEx variable2 = (AttributeExprEx) e2; NodeMaker nm1 = variable1.getNodeMaker(); NodeMaker nm2 = variable2.getNodeMaker(); NodeSetConstraintBuilder nodeSet = new NodeSetConstraintBuilder(); nm1.describeSelf(nodeSet); nm2.describeSelf(nodeSet); if (nodeSet.isEmpty()) { logger.debug("nodes " + nm1 + " " + nm2 + " incompatible"); expression.push(Expression.FALSE); return; } } expression.push(Equality.create(e1, e2)); }
private void convertFunction(ExprFunction2 expr) { logger.debug("convertFunction " + expr.toString()); if (expr instanceof E_LogicalOr) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(e1.or(e2)); } else if (expr instanceof E_LessThan) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new LessThan(e1, e2)); } else if (expr instanceof E_LessThanOrEqual) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new LessThanOrEqual(e1, e2)); } else if (expr instanceof E_GreaterThan) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new GreaterThan(e1, e2)); } else if (expr instanceof E_GreaterThanOrEqual) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new GreaterThanOrEqual(e1, e2)); } else if (expr instanceof E_Add) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new Add(e1, e2)); } else if (expr instanceof E_Subtract) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new Subtract(e1, e2)); } else if (expr instanceof E_Multiply) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new Multiply(e1, e2)); } else if (expr instanceof E_Divide) { expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); expression.push(new Divide(e1, e2)); } else if (expr instanceof E_Equals) { convertEquals((E_Equals) expr); } else if (expr instanceof E_NotEquals) { convertNotEquals((E_NotEquals) expr); } else if (expr instanceof E_LangMatches) { convertLangMatches((E_LangMatches) expr); } else if (expr instanceof E_SameTerm) { convertSameTerm((E_SameTerm) expr); } else if (extensionSupports(expr)) { expr.getArg(1).visit(this); expr.getArg(2).visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); List<Expression> args = new ArrayList<Expression>(2); args.add(e1); args.add(e2); extensionConvert(expr, args); } else { conversionFailed(expr); } }