Search in sources :

Example 11 with RuleAtomCallExeption

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

the class LessThanAtom 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.LessThanAtom tmp = (org.apache.stanbol.rules.manager.atoms.LessThanAtom) ruleAtom;
    ExpressionAtom argument1 = tmp.getArgument1();
    ExpressionAtom argument2 = tmp.getArgument2();
    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);
    Functor functor = new Functor("lessThan", nodes, BuiltinRegistry.theRegistry);
    clauseEntries.add(functor);
    return (T) new HigherOrderClauseEntry(arg1Node, clauseEntries);
}
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)

Example 12 with RuleAtomCallExeption

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

the class SameAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
    org.apache.stanbol.rules.manager.atoms.SameAtom tmp = (org.apache.stanbol.rules.manager.atoms.SameAtom) ruleAtom;
    ExpressionAtom argument1 = tmp.getStringFunctionAtom1();
    ExpressionAtom argument2 = tmp.getStringFunctionAtom2();
    try {
        SPARQLObject sparqlArgument1 = adapter.adaptTo(argument1, SPARQLObject.class);
        SPARQLObject sparqlArgument2 = adapter.adaptTo(argument2, SPARQLObject.class);
        ;
        String arg1 = sparqlArgument1.getObject();
        String arg2 = sparqlArgument2.getObject();
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        sb.append(arg1);
        sb.append(" = ");
        sb.append(arg2);
        sb.append(")");
        return (T) new SPARQLComparison(sb.toString());
    } catch (UnsupportedTypeForExportException e) {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    } catch (UnavailableRuleObjectException e) {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
}
Also used : SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) SPARQLComparison(org.apache.stanbol.rules.adapters.sparql.SPARQLComparison) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)

Example 13 with RuleAtomCallExeption

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

the class RecipeInputProvider method getInput.

@SuppressWarnings("unchecked")
@Override
public <T> Iterator<T> getInput(Class<T> type) throws IOException {
    ReasoningProvider reasoningProvider = null;
    if (type.isAssignableFrom(SWRLRule.class)) {
        reasoningProvider = ReasoningProvider.OWL2;
    } else if (type.isAssignableFrom(Rule.class)) {
        reasoningProvider = ReasoningProvider.Jena;
    } else {
        log.error("Cannot adapt to this type {}", type.getCanonicalName());
        throw new UnsupportedOperationException("Cannot adapt to " + type.getCanonicalName());
    }
    switch(reasoningProvider) {
        case OWL2:
            List<SWRLRule> rules = null;
            if (recipeId != null) {
                long start = System.currentTimeMillis();
                log.info("[start] Prepare rules for OWLApi ");
                // If recipe exists, return it as a list of SWRL rules
                rules = new ArrayList<SWRLRule>();
                try {
                    Recipe recipe = null;
                    synchronized (store) {
                        try {
                            recipe = store.getRecipe(new IRI(recipeId));
                        } catch (RecipeConstructionException e) {
                            log.error("An error occurred while generating the recipe.", e);
                        }
                    }
                    log.debug("Recipe is: {}", recipe);
                    /*
	                 * We ask to the adapter manager to get the right adapter in order to transform
	                 * recipes into SWRLRule objects.
	                 */
                    RuleAdapter adapter;
                    try {
                        adapter = adapterManager.getAdapter(recipe, SWRLRule.class);
                        rules = (List<SWRLRule>) adapter.adaptTo(recipe, SWRLRule.class);
                    } catch (UnavailableRuleObjectException e) {
                        log.error(e.getMessage(), e);
                    } catch (RuleAtomCallExeption e) {
                        log.error(e.getMessage(), e);
                    } catch (UnsupportedTypeForExportException e) {
                        log.error(e.getMessage(), e);
                    }
                /*
	                RuleList ruleList = recipe.getRuleList();
	                log.debug("RuleList is: {}",ruleList);
	                for(org.apache.stanbol.rules.base.api.Rule r : ruleList ){
	                    SWRLRule swrl = r.toSWRL(OWLManager.getOWLDataFactory());
	                    log.debug("Prepared rule: {}",swrl);
	                    rules.add(swrl);
	                }*/
                } catch (NoSuchRecipeException e) {
                    log.error("Recipe {} does not exists", recipeId);
                    throw new IOException(e);
                }
                long end = System.currentTimeMillis();
                log.info("[end] Prepared {} rules for OWLApi in {} ms.", rules.size(), (end - start));
            }
            if (rules == null) {
                log.error("No rules have been loaded");
                throw new IOException("No rules loaded");
            }
            final Iterator<SWRLRule> iterator = Collections.unmodifiableList(rules).iterator();
            return new Iterator<T>() {

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public T next() {
                    return (T) iterator.next();
                }

                @Override
                public void remove() {
                    log.error("Cannot remove items from this iterator. This may be cused by an error in the program");
                    throw new UnsupportedOperationException("Cannot remove items from this iterator");
                }
            };
        case Jena:
            List<Rule> jenaRules = null;
            if (recipeId != null) {
                long start = System.currentTimeMillis();
                log.info("[start] Prepare rules for Jena ");
                try {
                    Recipe recipe = null;
                    synchronized (store) {
                        try {
                            recipe = store.getRecipe(new IRI(recipeId));
                        } catch (RecipeConstructionException e) {
                            log.error("An error occurred while generating the recipe.", e);
                        }
                    }
                    if (recipe != null) {
                        log.debug("Recipe is: {}", recipe);
                        /*
		                 * We ask to the adapter manager to get the right adapter in order to transform
		                 * recipes into Jena Rule objects.
		                 */
                        RuleAdapter adapter;
                        try {
                            adapter = adapterManager.getAdapter(recipe, Rule.class);
                            jenaRules = (List<Rule>) adapter.adaptTo(recipe, Rule.class);
                        } catch (UnavailableRuleObjectException e) {
                            log.error(e.getMessage(), e);
                        } catch (RuleAtomCallExeption e) {
                            log.error(e.getMessage(), e);
                        } catch (UnsupportedTypeForExportException e) {
                            log.error(e.getMessage(), e);
                        }
                    }
                //jenaRules = recipe.toJenaRules();
                } catch (NoSuchRecipeException e) {
                    log.error("Recipe {} does not exists", recipeId);
                    throw new IOException(e);
                }
                long end = System.currentTimeMillis();
                log.info("[end] Prepared {} rules for Jena in {} ms.", jenaRules.size(), (end - start));
            }
            if (jenaRules == null) {
                log.error("No rules have been loaded");
                throw new IOException("No rules loaded");
            }
            final Iterator<Rule> jRiterator = Collections.unmodifiableList(jenaRules).iterator();
            return new Iterator<T>() {

                @Override
                public boolean hasNext() {
                    return jRiterator.hasNext();
                }

                @Override
                public T next() {
                    return (T) jRiterator.next();
                }

                @Override
                public void remove() {
                    log.error("Cannot remove items from this iterator. This may be cused by an error in the program");
                    throw new UnsupportedOperationException("Cannot remove items from this iterator");
                }
            };
        default:
            return null;
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) IOException(java.io.IOException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) Iterator(java.util.Iterator) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter)

Example 14 with RuleAtomCallExeption

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

the class JenaAdapter method main.

public static void main(String[] args) {
    RuleAdapter ruleAdapter = new JenaAdapter();
    try {
        KB kb = RuleParserImpl.parse("http://sssw.org/2012/rules/", new FileInputStream("/Users/mac/Documents/CNR/SSSW2012/rules/exercise1"));
        System.out.println("Rules: " + kb.getRuleList().size());
        Recipe recipe = new RecipeImpl(new IRI("http://sssw.org/2012/rules/"), "Recipe", kb.getRuleList());
        List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = (List<com.hp.hpl.jena.reasoner.rulesys.Rule>) ruleAdapter.adaptTo(recipe, com.hp.hpl.jena.reasoner.rulesys.Rule.class);
        String rules = "[ Exercise1: (http://dbpedia.org/resource/Madrid http://dbpedia.org/ontology/locationOf ?location) (?location rdf:type http://dbpedia.org/ontology/Museum) (?location http://dbpedia.org/ontology/numberOfVisitors ?visitors) greaterThan(?visitors '2000000'^^http://www.w3.org/2001/XMLSchema#integer) -> (?location rdf:type http://www.mytravels.com/Itinerary/MadridItinerary) ]";
        //List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = com.hp.hpl.jena.reasoner.rulesys.Rule.parseRules(rules);
        for (com.hp.hpl.jena.reasoner.rulesys.Rule jenaRule : jenaRules) {
            System.out.println(jenaRule.toString());
        }
        Model m = ModelFactory.createDefaultModel();
        Resource configuration = m.createResource();
        configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
        //Model model = FileManager.get().loadModel("/Users/mac/Documents/workspaceMyStanbol/sssw2012/events.rdf");
        Model model = FileManager.get().loadModel("/Users/mac/Documents/CNR/SSSW2012/datasets_new/Exercise1.rdf");
        //GenericRuleReasoner reasoner = new GenericRuleReasoner(jenaRules);
        //GenericRuleReasoner reasoner = new GenericRuleReasoner(com.hp.hpl.jena.reasoner.rulesys.Rule.parseRules(rules));
        GenericRuleReasoner reasoner = new GenericRuleReasoner(jenaRules);
        // not needed in RDFS case
        reasoner.setOWLTranslation(true);
        reasoner.setTransitiveClosureCaching(true);
        InfModel infModel = ModelFactory.createInfModel(reasoner, model);
        infModel.prepare();
        infModel.getDeductionsModel().write(System.out);
        //String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/MovieCityMuseums> }";
        //String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/CityEventItinerary> }";
        String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/MadridItinerary> }";
        //String sparql = "select * where {?s a <http://linkedevents.org/ontology/cazzo> }"; 
        //String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/MovieCityItinerary> }";
        Query query = QueryFactory.create(sparql, Syntax.syntaxARQ);
        QueryExecution queryExecution = QueryExecutionFactory.create(query, infModel);
        com.hp.hpl.jena.query.ResultSet resultSet = queryExecution.execSelect();
        ResultSetFormatter.out(System.out, resultSet);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RuleAtomCallExeption e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnavailableRuleObjectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedTypeForExportException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Query(com.hp.hpl.jena.query.Query) Recipe(org.apache.stanbol.rules.base.api.Recipe) FileNotFoundException(java.io.FileNotFoundException) InfModel(com.hp.hpl.jena.rdf.model.InfModel) QueryExecution(com.hp.hpl.jena.query.QueryExecution) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) KB(org.apache.stanbol.rules.manager.KB) List(java.util.List) ArrayList(java.util.ArrayList) AtomList(org.apache.stanbol.rules.base.api.util.AtomList) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) Resource(com.hp.hpl.jena.rdf.model.Resource) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) FileInputStream(java.io.FileInputStream) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Model(com.hp.hpl.jena.rdf.model.Model) GenericRuleReasoner(com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner) Rule(org.apache.stanbol.rules.base.api.Rule) AbstractRuleAdapter(org.apache.stanbol.rules.adapters.AbstractRuleAdapter) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter)

Example 15 with RuleAtomCallExeption

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

the class IndividualPropertyAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.IndividualPropertyAtom tmp = (org.apache.stanbol.rules.manager.atoms.IndividualPropertyAtom) ruleAtom;
    IObjectAtom argument1 = tmp.getArgument1();
    IObjectAtom objectProperty = tmp.getObjectProperty();
    IObjectAtom argument2 = tmp.getArgument2();
    ClerezzaSparqlObject argument1CSO = (ClerezzaSparqlObject) adapter.adaptTo(argument1, ConstructQuery.class);
    ClerezzaSparqlObject datatypePropertyCSO = (ClerezzaSparqlObject) adapter.adaptTo(objectProperty, 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 UriRefOrVariable((Variable) arg2);
    } else if (dt instanceof IRI) {
        object = new UriRefOrVariable((IRI) arg2);
    } 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) 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)

Aggregations

RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)64 ArrayList (java.util.ArrayList)31 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)21 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)21 Node (com.hp.hpl.jena.graph.Node)20 ClauseEntry (com.hp.hpl.jena.reasoner.rulesys.ClauseEntry)20 NodeClauseEntry (org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)20 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)20 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)18 Functor (com.hp.hpl.jena.reasoner.rulesys.Functor)16 ArgumentSWRLAtom (org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom)16 SWRLAtom (org.semanticweb.owlapi.model.SWRLAtom)16 HigherOrderClauseEntry (org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry)15 IObjectAtom (org.apache.stanbol.rules.manager.atoms.IObjectAtom)15 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)15 SWRLDArgument (org.semanticweb.owlapi.model.SWRLDArgument)13 HigherOrderSWRLAtom (org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom)12 SWRLArgument (org.semanticweb.owlapi.model.SWRLArgument)12 List (java.util.List)10 SPARQLObject (org.apache.stanbol.rules.base.api.SPARQLObject)10