Search in sources :

Example 11 with ClauseEntry

use of com.hp.hpl.jena.reasoner.rulesys.ClauseEntry 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 12 with ClauseEntry

use of com.hp.hpl.jena.reasoner.rulesys.ClauseEntry in project stanbol by apache.

the class MultiplicationAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    String div_result = "mul_result" + System.currentTimeMillis();
    Node arg1Node = null;
    Node arg2Node = null;
    Node arg3Node = Node_RuleVariable.createVariable(div_result);
    org.apache.stanbol.rules.manager.atoms.MultiplicationAtom tmp = (org.apache.stanbol.rules.manager.atoms.MultiplicationAtom) ruleAtom;
    NumericFunctionAtom numericFunctionAtom1 = tmp.getNumericFunctionAtom1();
    NumericFunctionAtom numericFunctionAtom2 = tmp.getNumericFunctionAtom2();
    ClauseEntry clauseEntry1 = adapter.adaptTo(numericFunctionAtom1, Rule.class);
    ClauseEntry clauseEntry2 = adapter.adaptTo(numericFunctionAtom2, Rule.class);
    List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>();
    if (clauseEntry1 instanceof HigherOrderClauseEntry) {
        arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries());
    } else if (clauseEntry1 instanceof NodeClauseEntry) {
        arg1Node = ((NodeClauseEntry) clauseEntry1).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (clauseEntry2 instanceof HigherOrderClauseEntry) {
        arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries());
    } else if (clauseEntry2 instanceof NodeClauseEntry) {
        arg2Node = ((NodeClauseEntry) clauseEntry2).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    java.util.List<Node> nodes = new ArrayList<Node>();
    nodes.add(arg1Node);
    nodes.add(arg2Node);
    nodes.add(arg3Node);
    ClauseEntry clauseEntry = new Functor("product", nodes, BuiltinRegistry.theRegistry);
    clauseEntries.add(clauseEntry);
    return (T) new HigherOrderClauseEntry(arg3Node, clauseEntries);
}
Also used : Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) Functor(com.hp.hpl.jena.reasoner.rulesys.Functor) NumericFunctionAtom(org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)

Example 13 with ClauseEntry

use of com.hp.hpl.jena.reasoner.rulesys.ClauseEntry in project stanbol by apache.

the class LetAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.LetAtom tmp = (org.apache.stanbol.rules.manager.atoms.LetAtom) ruleAtom;
    StringFunctionAtom parameterFunctionAtom = tmp.getParameterFunctionAtom();
    IObjectAtom variableIObjectAtom = tmp.getVariable();
    ClauseEntry parameterClauseEntry = adapter.adaptTo(parameterFunctionAtom, Rule.class);
    ClauseEntry variableClauseEntry = adapter.adaptTo(variableIObjectAtom, Rule.class);
    List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>();
    Node parameterNode;
    Node variableNode;
    if (parameterClauseEntry instanceof HigherOrderClauseEntry) {
        parameterNode = ((HigherOrderClauseEntry) parameterClauseEntry).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) parameterClauseEntry).getClauseEntries());
    } else if (parameterClauseEntry instanceof NodeClauseEntry) {
        parameterNode = ((NodeClauseEntry) parameterClauseEntry).getNode();
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    if (variableClauseEntry instanceof NodeClauseEntry) {
        variableNode = ((NodeClauseEntry) variableClauseEntry).getNode();
    } else {
        throw new RuleAtomCallExeption(getClass());
    }
    java.util.List<Node> nodes = new ArrayList<Node>();
    nodes.add(variableNode);
    nodes.add(parameterNode);
    ClauseEntry clauseEntry = new Functor("makeSkolem", nodes, BuiltinRegistry.theRegistry);
    clauseEntries.add(clauseEntry);
    return (T) new HigherOrderClauseEntry(variableNode, clauseEntries);
}
Also used : Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) Functor(com.hp.hpl.jena.reasoner.rulesys.Functor) IObjectAtom(org.apache.stanbol.rules.manager.atoms.IObjectAtom) StringFunctionAtom(org.apache.stanbol.rules.manager.atoms.StringFunctionAtom) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)

Example 14 with ClauseEntry

use of com.hp.hpl.jena.reasoner.rulesys.ClauseEntry in project stanbol by apache.

the class SameAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    Node arg1Node = null;
    Node arg2Node = null;
    org.apache.stanbol.rules.manager.atoms.SameAtom tmp = (org.apache.stanbol.rules.manager.atoms.SameAtom) ruleAtom;
    ExpressionAtom argument1 = tmp.getStringFunctionAtom1();
    ExpressionAtom argument2 = tmp.getStringFunctionAtom2();
    ClauseEntry clauseEntry1 = adapter.adaptTo(argument1, Rule.class);
    ClauseEntry clauseEntry2 = adapter.adaptTo(argument2, Rule.class);
    List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>();
    if (clauseEntry1 instanceof HigherOrderClauseEntry) {
        arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries());
    } else if (clauseEntry1 instanceof NodeClauseEntry) {
        arg1Node = ((NodeClauseEntry) clauseEntry1).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (clauseEntry2 instanceof HigherOrderClauseEntry) {
        arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries());
    } else if (clauseEntry2 instanceof NodeClauseEntry) {
        arg2Node = ((NodeClauseEntry) clauseEntry2).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    java.util.List<Node> nodes = new ArrayList<Node>();
    nodes.add(arg1Node);
    nodes.add(arg2Node);
    return (T) new Functor("equal", nodes, new BuiltinRegistry());
}
Also used : Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) Functor(com.hp.hpl.jena.reasoner.rulesys.Functor) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) BuiltinRegistry(com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry)

Example 15 with ClauseEntry

use of com.hp.hpl.jena.reasoner.rulesys.ClauseEntry in project stanbol by apache.

the class StartsWithAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    Node arg1Node = null;
    Node arg2Node = null;
    org.apache.stanbol.rules.manager.atoms.EndsWithAtom tmp = (org.apache.stanbol.rules.manager.atoms.EndsWithAtom) ruleAtom;
    ExpressionAtom argument = tmp.getArgument();
    ExpressionAtom term = tmp.getTerm();
    ClauseEntry clauseEntry1 = adapter.adaptTo(argument, Rule.class);
    ClauseEntry clauseEntry2 = adapter.adaptTo(term, Rule.class);
    List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>();
    if (clauseEntry1 instanceof HigherOrderClauseEntry) {
        arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries());
    } else if (clauseEntry1 instanceof NodeClauseEntry) {
        arg1Node = ((NodeClauseEntry) clauseEntry1).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (clauseEntry2 instanceof HigherOrderClauseEntry) {
        arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries());
    } else if (clauseEntry2 instanceof NodeClauseEntry) {
        Node tmpNode = ((NodeClauseEntry) clauseEntry2).getNode();
        LiteralLabel literalLabel = tmpNode.getLiteral();
        String lexicalForm = literalLabel.getLexicalForm();
        StringBuilder sb = new StringBuilder();
        sb.append("^");
        sb.append(lexicalForm);
        String regex = sb.toString();
        arg2Node = Node_RuleVariable.createLiteral(regex);
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    java.util.List<Node> nodes = new ArrayList<Node>();
    nodes.add(arg1Node);
    nodes.add(arg2Node);
    return (T) new Functor("regex", nodes, new BuiltinRegistry());
}
Also used : Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) BuiltinRegistry(com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry) LiteralLabel(com.hp.hpl.jena.graph.impl.LiteralLabel) Functor(com.hp.hpl.jena.reasoner.rulesys.Functor) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)

Aggregations

ClauseEntry (com.hp.hpl.jena.reasoner.rulesys.ClauseEntry)21 Node (com.hp.hpl.jena.graph.Node)20 NodeClauseEntry (org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)20 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)20 ArrayList (java.util.ArrayList)17 Functor (com.hp.hpl.jena.reasoner.rulesys.Functor)16 HigherOrderClauseEntry (org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry)15 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)9 IObjectAtom (org.apache.stanbol.rules.manager.atoms.IObjectAtom)7 TriplePattern (com.hp.hpl.jena.reasoner.TriplePattern)4 NumericFunctionAtom (org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom)4 BuiltinRegistry (com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry)3 LiteralLabel (com.hp.hpl.jena.graph.impl.LiteralLabel)2 RuleAtom (org.apache.stanbol.rules.base.api.RuleAtom)2 Builtin (com.hp.hpl.jena.reasoner.rulesys.Builtin)1 VariableClauseEntry (org.apache.stanbol.rules.adapters.jena.VariableClauseEntry)1 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)1 AtomList (org.apache.stanbol.rules.base.api.util.AtomList)1 StringFunctionAtom (org.apache.stanbol.rules.manager.atoms.StringFunctionAtom)1