Search in sources :

Example 21 with RuleAtom

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

the class AtomList method removeAll.

public boolean removeAll(Collection<?> c) {
    if (contains(c)) {
        for (Object o : c) {
            boolean removed = false;
            for (int i = 0; i < kReSRuleAtoms.length && !removed; i++) {
                RuleAtom semionRule = kReSRuleAtoms[i];
                if (semionRule.equals(o)) {
                    RuleAtom[] semionRulesCopy = new RuleAtom[kReSRuleAtoms.length - 1];
                    System.arraycopy(kReSRuleAtoms, 0, semionRulesCopy, 0, i);
                    System.arraycopy(kReSRuleAtoms, i + 1, semionRulesCopy, 0, semionRulesCopy.length - i);
                    kReSRuleAtoms = semionRulesCopy;
                    removed = true;
                }
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 22 with RuleAtom

use of org.apache.stanbol.rules.base.api.RuleAtom 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 : ArrayList(java.util.ArrayList) SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 23 with RuleAtom

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

the class JenaAdapter method adaptRuleTo.

@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
    if (type == com.hp.hpl.jena.reasoner.rulesys.Rule.class) {
        AtomList bodyAtomList = rule.getBody();
        AtomList headAtomList = rule.getHead();
        List<ClauseEntry> headClauseEntries = new ArrayList<ClauseEntry>();
        List<ClauseEntry> bodyClauseEntries = new ArrayList<ClauseEntry>();
        variableMap = new HashMap<String, Integer>();
        Iterator<RuleAtom> it = headAtomList.iterator();
        while (it.hasNext()) {
            RuleAtom atom = it.next();
            ClauseEntry clauseEntry = adaptRuleAtomTo(atom, com.hp.hpl.jena.reasoner.rulesys.Rule.class);
            if (clauseEntry instanceof HigherOrderClauseEntry) {
                List<ClauseEntry> clauseEntries = ((HigherOrderClauseEntry) clauseEntry).getClauseEntries();
                for (ClauseEntry ce : clauseEntries) {
                    headClauseEntries.add(ce);
                }
            } else {
                headClauseEntries.add(clauseEntry);
            }
        }
        it = bodyAtomList.iterator();
        while (it.hasNext()) {
            RuleAtom atom = it.next();
            ClauseEntry clauseEntry = adaptRuleAtomTo(atom, com.hp.hpl.jena.reasoner.rulesys.Rule.class);
            if (clauseEntry instanceof HigherOrderClauseEntry) {
                List<ClauseEntry> clauseEntries = ((HigherOrderClauseEntry) clauseEntry).getClauseEntries();
                for (ClauseEntry ce : clauseEntries) {
                    bodyClauseEntries.add(ce);
                }
            } else {
                bodyClauseEntries.add(clauseEntry);
            }
        }
        return (T) new com.hp.hpl.jena.reasoner.rulesys.Rule(rule.getRuleName(), headClauseEntries, bodyClauseEntries);
    } else {
        throw new UnsupportedTypeForExportException("The adapter " + getClass() + " does not support type : " + type.getCanonicalName());
    }
}
Also used : AtomList(org.apache.stanbol.rules.base.api.util.AtomList) ArrayList(java.util.ArrayList) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 24 with RuleAtom

use of org.apache.stanbol.rules.base.api.RuleAtom 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)

Example 25 with RuleAtom

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

the class RuleParserImpl method atom.

public final RuleAtom atom() throws ParseException {
    RuleAtom ruleAtom;
    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case IS:
            ruleAtom = classAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case HAS:
            ruleAtom = individualPropertyAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case VALUES:
            ruleAtom = datavaluedPropertyAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case LET:
            ruleAtom = letAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case NEW_IRI:
            ruleAtom = newIRIAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case NEW_LITERAL:
            ruleAtom = newLiteralAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case SAME:
        case DIFFERENT:
        case LESSTHAN:
        case GREATERTHAN:
        case STARTS_WITH:
        case ENDS_WITH:
        case NOT:
        case IS_BLANK:
            ruleAtom = comparisonAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        case UNION:
            ruleAtom = unionAtom();
            {
                if (true)
                    return ruleAtom;
            }
            break;
        default:
            jj_la1[4] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
    }
    throw new Error("Missing return statement in function");
}
Also used : RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Aggregations

RuleAtom (org.apache.stanbol.rules.base.api.RuleAtom)33 Test (org.junit.Test)11 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)4 AtomList (org.apache.stanbol.rules.base.api.util.AtomList)4 IObjectAtom (org.apache.stanbol.rules.manager.atoms.IObjectAtom)4 ArrayList (java.util.ArrayList)3 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)3 ClauseEntry (com.hp.hpl.jena.reasoner.rulesys.ClauseEntry)2 HashSet (java.util.HashSet)2 SPARQLObject (org.apache.stanbol.rules.base.api.SPARQLObject)2 StringFunctionAtom (org.apache.stanbol.rules.manager.atoms.StringFunctionAtom)2 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)2 SWRLAtom (org.semanticweb.owlapi.model.SWRLAtom)2 Node (com.hp.hpl.jena.graph.Node)1 TriplePattern (com.hp.hpl.jena.reasoner.TriplePattern)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 ConstructQuery (org.apache.clerezza.rdf.core.sparql.query.ConstructQuery)1 Expression (org.apache.clerezza.rdf.core.sparql.query.Expression)1 LiteralExpression (org.apache.clerezza.rdf.core.sparql.query.LiteralExpression)1 ResourceOrVariable (org.apache.clerezza.rdf.core.sparql.query.ResourceOrVariable)1