Search in sources :

Example 1 with RuleAtom

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

the class DatavaluedPropertyAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.DatavaluedPropertyAtom tmp = (org.apache.stanbol.rules.manager.atoms.DatavaluedPropertyAtom) ruleAtom;
    IObjectAtom argument1 = tmp.getArgument1();
    IObjectAtom datatypeProperty = tmp.getDatatypeProperty();
    RuleAtom argument2 = tmp.getArgument2();
    ClauseEntry argument2ClauseEntry = adapter.adaptTo(argument2, Rule.class);
    ClauseEntry argument1ClauseEntry = adapter.adaptTo(argument1, Rule.class);
    ClauseEntry datatypePropertyClauseEntry = adapter.adaptTo(datatypeProperty, Rule.class);
    Node subjectNode = null;
    Node predicateNode = null;
    Node objectNode = null;
    if (argument1ClauseEntry instanceof NodeClauseEntry) {
        subjectNode = ((NodeClauseEntry) argument1ClauseEntry).getNode();
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    if (datatypePropertyClauseEntry instanceof NodeClauseEntry) {
        predicateNode = ((NodeClauseEntry) datatypePropertyClauseEntry).getNode();
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    if (argument2ClauseEntry instanceof NodeClauseEntry) {
        objectNode = ((NodeClauseEntry) argument2ClauseEntry).getNode();
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    return (T) new TriplePattern(subjectNode, predicateNode, objectNode);
}
Also used : Node(com.hp.hpl.jena.graph.Node) IObjectAtom(org.apache.stanbol.rules.manager.atoms.IObjectAtom) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) TriplePattern(com.hp.hpl.jena.reasoner.TriplePattern) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 2 with RuleAtom

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

the class DatavaluedPropertyAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.DatavaluedPropertyAtom tmp = (org.apache.stanbol.rules.manager.atoms.DatavaluedPropertyAtom) ruleAtom;
    IObjectAtom argument1 = tmp.getArgument1();
    IObjectAtom datatypeProperty = tmp.getDatatypeProperty();
    RuleAtom argument2 = tmp.getArgument2();
    ClerezzaSparqlObject argument1CSO = (ClerezzaSparqlObject) adapter.adaptTo(argument1, ConstructQuery.class);
    ClerezzaSparqlObject datatypePropertyCSO = (ClerezzaSparqlObject) adapter.adaptTo(datatypeProperty, ConstructQuery.class);
    ClerezzaSparqlObject argument2CSO = (ClerezzaSparqlObject) adapter.adaptTo(argument2, ConstructQuery.class);
    Object arg1 = argument1CSO.getClerezzaObject();
    Object dt = datatypePropertyCSO.getClerezzaObject();
    Object arg2 = argument2CSO.getClerezzaObject();
    UriRefOrVariable subject;
    UriRefOrVariable predicate;
    ResourceOrVariable object;
    if (arg1 instanceof Variable) {
        subject = new UriRefOrVariable((Variable) arg1);
    } else if (arg1 instanceof IRI) {
        subject = new UriRefOrVariable((IRI) arg1);
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    if (dt instanceof Variable) {
        predicate = new UriRefOrVariable((Variable) dt);
    } else if (dt instanceof IRI) {
        predicate = new UriRefOrVariable((IRI) dt);
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    if (arg2 instanceof Variable) {
        object = new ResourceOrVariable((Variable) arg2);
    } else if (arg2 instanceof LiteralExpression) {
        object = new ResourceOrVariable(((LiteralExpression) arg2).getLiteral());
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    return (T) new ClerezzaSparqlObject(new SimpleTriplePattern(subject, predicate, object));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ResourceOrVariable(org.apache.clerezza.rdf.core.sparql.query.ResourceOrVariable) Variable(org.apache.clerezza.rdf.core.sparql.query.Variable) UriRefOrVariable(org.apache.clerezza.rdf.core.sparql.query.UriRefOrVariable) ResourceOrVariable(org.apache.clerezza.rdf.core.sparql.query.ResourceOrVariable) LiteralExpression(org.apache.clerezza.rdf.core.sparql.query.LiteralExpression) UriRefOrVariable(org.apache.clerezza.rdf.core.sparql.query.UriRefOrVariable) IObjectAtom(org.apache.stanbol.rules.manager.atoms.IObjectAtom) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject) SimpleTriplePattern(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleTriplePattern) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 3 with RuleAtom

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

the class RuleImpl method prettyPrint.

@Override
public String prettyPrint() {
    String rule = null;
    String tab = "       ";
    if (head != null && body != null) {
        boolean addAnd = false;
        rule = "RULE " + ruleName + " ASSERTS THAT " + System.getProperty("line.separator");
        rule += "IF" + System.getProperty("line.separator");
        for (RuleAtom atom : body) {
            rule += tab;
            if (addAnd) {
                rule += "AND ";
            } else {
                addAnd = true;
            }
            rule += atom.toString() + System.getProperty("line.separator");
        }
        rule += "IMPLIES" + System.getProperty("line.separator");
        addAnd = false;
        for (RuleAtom atom : head) {
            rule += tab;
            if (addAnd) {
                rule += "AND ";
            } else {
                addAnd = true;
            }
            rule += atom.toString() + System.getProperty("line.separator");
        }
    }
    return rule;
}
Also used : RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 4 with RuleAtom

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

the class AtomList method remove.

public boolean remove(Object o) {
    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 removed;
}
Also used : RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom)

Example 5 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)

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