Search in sources :

Example 21 with UnsupportedTypeForExportException

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

the class ClerezzaAdpterTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    try {
        List<ConstructQuery> constructQueries = (List<ConstructQuery>) ruleAdapter.adaptTo(recipeGood, ConstructQuery.class);
        for (ConstructQuery constructQuery : constructQueries) {
            ConstructQuery cq = (ConstructQuery) QueryParser.getInstance().parse(constructQuery.toString());
            System.out.println(cq.toString());
        }
        Assert.assertTrue(true);
    } catch (UnavailableRuleObjectException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedTypeForExportException e) {
        Assert.fail(e.getMessage());
    } catch (RuleAtomCallExeption e) {
        Assert.fail(e.getMessage());
    } catch (ParseException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) List(java.util.List) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) Test(org.junit.Test)

Example 22 with UnsupportedTypeForExportException

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

the class JenaAdapter method adaptRecipeTo.

@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRecipeTo(Recipe recipe, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
    List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = null;
    if (type == com.hp.hpl.jena.reasoner.rulesys.Rule.class) {
        RuleList ruleList = recipe.getRuleList();
        Iterator<Rule> ruleIterator = ruleList.iterator();
        jenaRules = new ArrayList<com.hp.hpl.jena.reasoner.rulesys.Rule>();
        for (int i = 0; ruleIterator.hasNext(); i++) {
            jenaRules.add((com.hp.hpl.jena.reasoner.rulesys.Rule) adaptRuleTo(ruleIterator.next(), type));
        }
    } else {
        throw new UnsupportedTypeForExportException("The Jena Adapter does not support the selected serialization : " + type.getCanonicalName());
    }
    return (T) jenaRules;
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) Rule(org.apache.stanbol.rules.base.api.Rule)

Example 23 with UnsupportedTypeForExportException

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

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

the class SWRLAdapter method adaptRuleTo.

@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
    if (type == SWRLRule.class) {
        OWLDataFactory factory = OWLManager.getOWLDataFactory();
        Set<SWRLAtom> bodyAtoms = new HashSet<SWRLAtom>();
        Set<SWRLAtom> headAtoms = new HashSet<SWRLAtom>();
        for (RuleAtom atom : rule.getBody()) {
            bodyAtoms.add((SWRLAtom) adaptRuleAtomTo(atom, SWRLRule.class));
        }
        for (RuleAtom atom : rule.getHead()) {
            headAtoms.add((SWRLAtom) adaptRuleAtomTo(atom, SWRLRule.class));
        }
        return (T) factory.getSWRLRule(bodyAtoms, headAtoms);
    } else {
        throw new UnsupportedTypeForExportException("The adapter " + getClass() + " does not support type : " + type.getCanonicalName());
    }
}
Also used : SWRLAtom(org.semanticweb.owlapi.model.SWRLAtom) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) RuleAtom(org.apache.stanbol.rules.base.api.RuleAtom) HashSet(java.util.HashSet)

Example 25 with UnsupportedTypeForExportException

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

the class SWRLAdpterTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    try {
        List<SWRLRule> rules = (List<SWRLRule>) ruleAdapter.adaptTo(recipeGood, SWRLRule.class);
        StringBuilder sb = new StringBuilder();
        for (SWRLRule rule : rules) {
            sb.append(rule.toString());
        }
        Assert.assertNotSame(sb.toString(), "");
    } catch (UnavailableRuleObjectException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedTypeForExportException e) {
        Assert.fail(e.getMessage());
    } catch (RuleAtomCallExeption e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) List(java.util.List) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) Test(org.junit.Test)

Aggregations

UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)29 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)21 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)21 SPARQLObject (org.apache.stanbol.rules.base.api.SPARQLObject)11 List (java.util.List)10 Test (org.junit.Test)7 ConstructQuery (org.apache.clerezza.rdf.core.sparql.query.ConstructQuery)6 SPARQLComparison (org.apache.stanbol.rules.adapters.sparql.SPARQLComparison)5 Rule (org.apache.stanbol.rules.base.api.Rule)5 RuleAdapter (org.apache.stanbol.rules.base.api.RuleAdapter)5 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)5 SPARQLFunction (org.apache.stanbol.rules.adapters.sparql.SPARQLFunction)4 Recipe (org.apache.stanbol.rules.base.api.Recipe)4 StringFunctionAtom (org.apache.stanbol.rules.manager.atoms.StringFunctionAtom)4 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)3 IRI (org.apache.clerezza.commons.rdf.IRI)3 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)3 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)3 RuleAtom (org.apache.stanbol.rules.base.api.RuleAtom)3 NumericFunctionAtom (org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom)3