Search in sources :

Example 16 with Expression

use of org.apache.clerezza.rdf.core.sparql.query.Expression in project stanbol by apache.

the class StringAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
    org.apache.stanbol.rules.manager.atoms.StringAtom tmp = (org.apache.stanbol.rules.manager.atoms.StringAtom) ruleAtom;
    String string = tmp.getString();
    Expression exp = null;
    if (string.startsWith("\"") && string.endsWith("\"")) {
        string = string.substring(1, string.length() - 1);
    }
    if (string.startsWith(Symbols.variablesPrefix)) {
        exp = new Variable(string.replace(Symbols.variablesPrefix, ""));
    } else {
        exp = new LiteralExpression(new PlainLiteralImpl(string));
    }
    return (T) new ClerezzaSparqlObject(exp);
}
Also used : Variable(org.apache.clerezza.rdf.core.sparql.query.Variable) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) LiteralExpression(org.apache.clerezza.rdf.core.sparql.query.LiteralExpression) Expression(org.apache.clerezza.rdf.core.sparql.query.Expression) LiteralExpression(org.apache.clerezza.rdf.core.sparql.query.LiteralExpression) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject)

Example 17 with Expression

use of org.apache.clerezza.rdf.core.sparql.query.Expression in project stanbol by apache.

the class SubstringAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.SubstringAtom tmp = (org.apache.stanbol.rules.manager.atoms.SubstringAtom) ruleAtom;
    StringFunctionAtom argument = tmp.getStringFunctionAtom();
    NumericFunctionAtom start = tmp.getStart();
    NumericFunctionAtom length = tmp.getLength();
    ClerezzaSparqlObject clerezzaArgument = (ClerezzaSparqlObject) adapter.adaptTo(argument, ConstructQuery.class);
    ClerezzaSparqlObject clerezzaStart = (ClerezzaSparqlObject) adapter.adaptTo(start, ConstructQuery.class);
    ClerezzaSparqlObject clerezzaLength = (ClerezzaSparqlObject) adapter.adaptTo(length, ConstructQuery.class);
    List<Expression> argumentExpressions = new ArrayList<Expression>();
    argumentExpressions.add((Expression) clerezzaArgument.getClerezzaObject());
    argumentExpressions.add((Expression) clerezzaStart.getClerezzaObject());
    argumentExpressions.add((Expression) clerezzaLength.getClerezzaObject());
    FunctionCall functionCall = new FunctionCall(new IRI("<http://www.w3.org/2005/xpath-functions#substring>"), argumentExpressions);
    return (T) new ClerezzaSparqlObject(functionCall);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ArrayList(java.util.ArrayList) NumericFunctionAtom(org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) StringFunctionAtom(org.apache.stanbol.rules.manager.atoms.StringFunctionAtom) Expression(org.apache.clerezza.rdf.core.sparql.query.Expression) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject) FunctionCall(org.apache.clerezza.rdf.core.sparql.query.FunctionCall)

Example 18 with Expression

use of org.apache.clerezza.rdf.core.sparql.query.Expression in project stanbol by apache.

the class SubtractionAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.SubtractionAtom tmp = (org.apache.stanbol.rules.manager.atoms.SubtractionAtom) ruleAtom;
    NumericFunctionAtom numericFunctionAtom1 = tmp.getNumericFunctionAtom1();
    NumericFunctionAtom numericFunctionAtom2 = tmp.getNumericFunctionAtom2();
    ClerezzaSparqlObject argument1 = (ClerezzaSparqlObject) adapter.adaptTo(numericFunctionAtom1, ConstructQuery.class);
    ClerezzaSparqlObject argument2 = (ClerezzaSparqlObject) adapter.adaptTo(numericFunctionAtom2, ConstructQuery.class);
    Expression lhsOperand = (Expression) argument1.getClerezzaObject();
    Expression rhsOperand = (Expression) argument2.getClerezzaObject();
    BinaryOperation binaryOperation = new BinaryOperation("-", lhsOperand, rhsOperand);
    return (T) new ClerezzaSparqlObject(binaryOperation);
}
Also used : BinaryOperation(org.apache.clerezza.rdf.core.sparql.query.BinaryOperation) NumericFunctionAtom(org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) Expression(org.apache.clerezza.rdf.core.sparql.query.Expression) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject)

Example 19 with Expression

use of org.apache.clerezza.rdf.core.sparql.query.Expression in project stanbol by apache.

the class SumAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.SumAtom tmp = (org.apache.stanbol.rules.manager.atoms.SumAtom) ruleAtom;
    NumericFunctionAtom numericFunctionAtom1 = tmp.getNumericFunctionAtom1();
    NumericFunctionAtom numericFunctionAtom2 = tmp.getNumericFunctionAtom2();
    ClerezzaSparqlObject argument1 = (ClerezzaSparqlObject) adapter.adaptTo(numericFunctionAtom1, ConstructQuery.class);
    ClerezzaSparqlObject argument2 = (ClerezzaSparqlObject) adapter.adaptTo(numericFunctionAtom2, ConstructQuery.class);
    Expression lhsOperand = (Expression) argument1.getClerezzaObject();
    Expression rhsOperand = (Expression) argument2.getClerezzaObject();
    BinaryOperation binaryOperation = new BinaryOperation("+", lhsOperand, rhsOperand);
    return (T) new ClerezzaSparqlObject(binaryOperation);
}
Also used : BinaryOperation(org.apache.clerezza.rdf.core.sparql.query.BinaryOperation) NumericFunctionAtom(org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) Expression(org.apache.clerezza.rdf.core.sparql.query.Expression) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject)

Example 20 with Expression

use of org.apache.clerezza.rdf.core.sparql.query.Expression in project stanbol by apache.

the class ClerezzaAdapter method adaptRuleTo.

@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
    Set<TriplePattern> triplePatterns = new HashSet<TriplePattern>();
    List<Expression> expressions = new ArrayList<Expression>();
    Iterator<RuleAtom> it = rule.getBody().iterator();
    while (it.hasNext()) {
        RuleAtom ruleAtom = it.next();
        ClerezzaSparqlObject clerezzaSparqlObject = null;
        log.debug("Type to adapt {}", type);
        clerezzaSparqlObject = (ClerezzaSparqlObject) adaptRuleAtomTo(ruleAtom, type);
        Object clerezzaObj = clerezzaSparqlObject.getClerezzaObject();
        if (clerezzaObj instanceof TriplePattern) {
            triplePatterns.add((TriplePattern) clerezzaObj);
        } else if (clerezzaObj instanceof Expression) {
            expressions.add((Expression) clerezzaObj);
        }
    }
    SimpleGroupGraphPattern groupGraphPattern = new SimpleGroupGraphPattern();
    groupGraphPattern.addTriplePatterns(triplePatterns);
    for (Expression expression : expressions) {
        groupGraphPattern.addConstraint(expression);
    }
    triplePatterns = new HashSet<TriplePattern>();
    it = rule.getHead().iterator();
    while (it.hasNext()) {
        RuleAtom ruleAtom = it.next();
        ClerezzaSparqlObject clerezzaSparqlObject = (ClerezzaSparqlObject) adaptRuleAtomTo(ruleAtom, type);
        triplePatterns.add((TriplePattern) clerezzaSparqlObject.getClerezzaObject());
    }
    SimpleConstructQuery constructQuery = new SimpleConstructQuery(triplePatterns);
    constructQuery.setQueryPattern(groupGraphPattern);
    return (T) constructQuery;
}
Also used : SimpleConstructQuery(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleConstructQuery) ArrayList(java.util.ArrayList) SimpleGroupGraphPattern(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleGroupGraphPattern) Expression(org.apache.clerezza.rdf.core.sparql.query.Expression) TriplePattern(org.apache.clerezza.rdf.core.sparql.query.TriplePattern) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom) HashSet(java.util.HashSet)

Aggregations

Expression (org.apache.clerezza.rdf.core.sparql.query.Expression)26 ClerezzaSparqlObject (org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject)25 ConstructQuery (org.apache.clerezza.rdf.core.sparql.query.ConstructQuery)22 ArrayList (java.util.ArrayList)14 StringFunctionAtom (org.apache.stanbol.rules.manager.atoms.StringFunctionAtom)11 BinaryOperation (org.apache.clerezza.rdf.core.sparql.query.BinaryOperation)10 IRI (org.apache.clerezza.commons.rdf.IRI)8 FunctionCall (org.apache.clerezza.rdf.core.sparql.query.FunctionCall)7 BuiltInCall (org.apache.clerezza.rdf.core.sparql.query.BuiltInCall)6 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)6 NumericFunctionAtom (org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom)5 Variable (org.apache.clerezza.rdf.core.sparql.query.Variable)4 IObjectAtom (org.apache.stanbol.rules.manager.atoms.IObjectAtom)4 LiteralExpression (org.apache.clerezza.rdf.core.sparql.query.LiteralExpression)2 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)2 HashSet (java.util.HashSet)1 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)1 TriplePattern (org.apache.clerezza.rdf.core.sparql.query.TriplePattern)1 UnaryOperation (org.apache.clerezza.rdf.core.sparql.query.UnaryOperation)1 UriRefExpression (org.apache.clerezza.rdf.core.sparql.query.UriRefExpression)1