Search in sources :

Example 1 with TriplePattern

use of org.apache.clerezza.rdf.core.sparql.query.TriplePattern 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

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Expression (org.apache.clerezza.rdf.core.sparql.query.Expression)1 TriplePattern (org.apache.clerezza.rdf.core.sparql.query.TriplePattern)1 SimpleConstructQuery (org.apache.clerezza.rdf.core.sparql.query.impl.SimpleConstructQuery)1 SimpleGroupGraphPattern (org.apache.clerezza.rdf.core.sparql.query.impl.SimpleGroupGraphPattern)1 RuleAtom (org.apache.stanbol.rules.base.api.RuleAtom)1