Search in sources :

Example 21 with SPARQLObject

use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.

the class SumAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
    org.apache.stanbol.rules.manager.atoms.SumAtom tmp = (org.apache.stanbol.rules.manager.atoms.SumAtom) ruleAtom;
    NumericFunctionAtom argument1 = tmp.getNumericFunctionAtom1();
    NumericFunctionAtom argument2 = tmp.getNumericFunctionAtom2();
    try {
        SPARQLObject sparqlArgument1 = adapter.adaptTo(argument1, SPARQLObject.class);
        SPARQLObject sparqlArgument2 = adapter.adaptTo(argument2, SPARQLObject.class);
        String sparqlFunction1 = sparqlArgument1.getObject();
        String sparqlFunction2 = sparqlArgument2.getObject();
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        sb.append(sparqlFunction1);
        sb.append(" + ");
        sb.append(sparqlFunction2);
        sb.append(")");
        return (T) new SPARQLComparison(sb.toString());
    } catch (UnsupportedTypeForExportException e) {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    } catch (UnavailableRuleObjectException e) {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
}
Also used : SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) SPARQLComparison(org.apache.stanbol.rules.adapters.sparql.SPARQLComparison) NumericFunctionAtom(org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)

Example 22 with SPARQLObject

use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.

the class SPARQLAdapter method adaptRuleTo.

/*
     * public <T> T exportRecipe(String recipeID, Class<T> type) throws UnsupportedTypeForExportException,
     * UnavailableRuleObjectException, NoSuchRecipeException {
     * 
     * 
     * Recipe recipe; try { recipe = ruleStore.getRecipe(IRI.create(recipeID));
     * 
     * return exportRecipe(recipe, type); } catch (NoSuchRecipeException e) { throw e; }
     * 
     * 
     * }
     */
@SuppressWarnings("unchecked")
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws UnsupportedTypeForExportException, UnavailableRuleObjectException {
    String sparql = "CONSTRUCT {";
    boolean firstIte = true;
    for (RuleAtom ruleAtom : rule.getHead()) {
        if (!firstIte) {
            sparql += " . ";
        }
        firstIte = false;
        sparql += ((SPARQLObject) adaptRuleAtomTo(ruleAtom, type)).getObject();
    }
    sparql += "} ";
    sparql += "WHERE {";
    firstIte = true;
    ArrayList<SPARQLObject> sparqlObjects = new ArrayList<SPARQLObject>();
    for (RuleAtom ruleAtom : rule.getBody()) {
        SPARQLObject tmp = ((SPARQLObject) adaptRuleAtomTo(ruleAtom, type));
        if (tmp instanceof SPARQLNot) {
            sparqlObjects.add(tmp);
        } else if (tmp instanceof SPARQLComparison) {
            sparqlObjects.add(tmp);
        } else {
            if (!firstIte) {
                sparql += " . ";
            } else {
                firstIte = false;
            }
            sparql += tmp.getObject();
        }
    }
    firstIte = true;
    String optional = "";
    String filter = "";
    for (SPARQLObject sparqlObj : sparqlObjects) {
        if (sparqlObj instanceof SPARQLNot) {
            SPARQLNot sparqlNot = (SPARQLNot) sparqlObj;
            if (!firstIte) {
                optional += " . ";
            } else {
                firstIte = false;
            }
            optional += sparqlNot.getObject();
            String[] filters = sparqlNot.getFilters();
            for (String theFilter : filters) {
                if (!filter.isEmpty()) {
                    filter += " && ";
                }
                filter += theFilter;
            }
        } else if (sparqlObj instanceof SPARQLComparison) {
            SPARQLComparison sparqlDifferent = (SPARQLComparison) sparqlObj;
            String theFilter = sparqlDifferent.getObject();
            if (!filter.isEmpty()) {
                filter += " && ";
            }
            filter += theFilter;
        }
    }
    if (!optional.isEmpty()) {
        sparql += " . OPTIONAL { " + optional + " } ";
    }
    if (!filter.isEmpty()) {
        sparql += " . FILTER ( " + filter + " ) ";
    }
    sparql += "}";
    return (T) new SPARQLQuery(sparql);
}
Also used : SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) ArrayList(java.util.ArrayList) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 23 with SPARQLObject

use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.

the class PropStringAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
    org.apache.stanbol.rules.manager.atoms.PropStringAtom tmp = (org.apache.stanbol.rules.manager.atoms.PropStringAtom) ruleAtom;
    StringFunctionAtom namespaceArg = tmp.getNamespaceArg();
    StringFunctionAtom labelArg = tmp.getLabelArg();
    try {
        SPARQLObject namespaceSparqlAtom = adapter.adaptTo(namespaceArg, SPARQLObject.class);
        SPARQLObject labelSparqlAtom = adapter.adaptTo(labelArg, SPARQLObject.class);
        String ns = namespaceSparqlAtom.getObject();
        String label = labelSparqlAtom.getObject();
        String sparql = "<http://www.stlab.istc.cnr.it/semion/function#propString>(" + ns + ", " + label + ")";
        return (T) new SPARQLFunction(sparql);
    } catch (UnsupportedTypeForExportException e) {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    } catch (UnavailableRuleObjectException e) {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
}
Also used : SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) SPARQLFunction(org.apache.stanbol.rules.adapters.sparql.SPARQLFunction) StringFunctionAtom(org.apache.stanbol.rules.manager.atoms.StringFunctionAtom) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)

Example 24 with SPARQLObject

use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.

the class StrAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.StrAtom tmp = (org.apache.stanbol.rules.manager.atoms.StrAtom) ruleAtom;
    IObjectAtom argumentAtom = tmp.getUriResource();
    SPARQLObject sparqlObject = adapter.adaptTo(argumentAtom, SPARQLObject.class);
    String sparql = "str(" + sparqlObject.getObject() + ")";
    return (T) new SPARQLFunction(sparql);
}
Also used : SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) IObjectAtom(org.apache.stanbol.rules.manager.atoms.IObjectAtom) SPARQLFunction(org.apache.stanbol.rules.adapters.sparql.SPARQLFunction)

Example 25 with SPARQLObject

use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.

the class TypedLiteralAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.TypedLiteralAtom tmp = (org.apache.stanbol.rules.manager.atoms.TypedLiteralAtom) ruleAtom;
    ExpressionAtom valueExpression = tmp.getValue();
    URIResource xsdTypeResource = tmp.getXsdType();
    SPARQLObject sparqlObject = null;
    sparqlObject = adapter.adaptTo(valueExpression, SPARQLObject.class);
    String value = sparqlObject.getObject();
    if (!value.startsWith("\"")) {
        value = "\"" + value;
    }
    if (!value.endsWith("\"")) {
        value += "\"";
    }
    String xsdType = "<" + xsdTypeResource.getURI().toString() + ">";
    return (T) new SPARQLFunction(value + "^^" + xsdType);
}
Also used : URIResource(org.apache.stanbol.rules.base.api.URIResource) SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) SPARQLFunction(org.apache.stanbol.rules.adapters.sparql.SPARQLFunction)

Aggregations

SPARQLObject (org.apache.stanbol.rules.base.api.SPARQLObject)35 SPARQLFunction (org.apache.stanbol.rules.adapters.sparql.SPARQLFunction)16 SPARQLComparison (org.apache.stanbol.rules.adapters.sparql.SPARQLComparison)13 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)11 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)10 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)10 StringFunctionAtom (org.apache.stanbol.rules.manager.atoms.StringFunctionAtom)10 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)7 NumericFunctionAtom (org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom)5 IObjectAtom (org.apache.stanbol.rules.manager.atoms.IObjectAtom)4 SPARQLTriple (org.apache.stanbol.rules.adapters.sparql.SPARQLTriple)3 ArrayList (java.util.ArrayList)2 RuleAtom (org.apache.stanbol.rules.base.api.RuleAtom)2 List (java.util.List)1 SPARQLNot (org.apache.stanbol.rules.adapters.sparql.SPARQLNot)1 Rule (org.apache.stanbol.rules.base.api.Rule)1 URIResource (org.apache.stanbol.rules.base.api.URIResource)1 AtomList (org.apache.stanbol.rules.base.api.util.AtomList)1 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)1 Test (org.junit.Test)1