Search in sources :

Example 1 with InfModel

use of com.hp.hpl.jena.rdf.model.InfModel 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 2 with InfModel

use of com.hp.hpl.jena.rdf.model.InfModel in project stanbol by apache.

the class JenaRDFSReasoningServiceTest method testRDFSDomain.

@Test
public void testRDFSDomain() {
    log.info("Testing rdfs:domain inference with RDFS reasoner");
    // Prepare data
    TestData.alexdma.addProperty(TestData.foaf_knows, TestData.enridaga);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
    // Is alexdma a foaf:Person?
    InfModel inferred = reasoningService.run(input);
    Statement isPerson = TestData.model.createStatement(TestData.alexdma, RDF.type, TestData.foaf_Person);
    //		log.info("Statements: {}", TestUtils.printStatements(inferred,
    //				TestData.enridaga, RDF.type));
    log.info("Is any rdfs:domain of foaf:knows a foaf:Person...(true)? {}", inferred.contains(isPerson));
    assertTrue(inferred.contains(isPerson));
    // Reset resource to be clean for other tests
    TestData.alexdma.removeProperties();
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Model(com.hp.hpl.jena.rdf.model.Model) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Test(org.junit.Test)

Example 3 with InfModel

use of com.hp.hpl.jena.rdf.model.InfModel in project stanbol by apache.

the class AbstractJenaReasoningService method run.

/**
     * Generic method to perform inferences
     */
@Override
public InfModel run(Model data) {
    log.debug(" run(Model data)");
    InfModel im = ModelFactory.createInfModel(this.reasoner, data);
    im.prepare();
    return im;
}
Also used : InfModel(com.hp.hpl.jena.rdf.model.InfModel)

Example 4 with InfModel

use of com.hp.hpl.jena.rdf.model.InfModel in project stanbol by apache.

the class AbstractJenaReasoningService method enrich.

/**
     * Enriching: 1) Perform reasoning on a reasoner customized with the given rule set 2) Returns all the
     * statements (filtered = false) or only inferred ones (filtered = true)
     * 
     * This is a default implementation of task {@see ReasoningService.Tasks.ENRICH} when a set of rules is
     * given. Subclasses may want to change it.
     * 
     * @param data
     * @param rules
     * @param filtered
     * @return
     */
protected Set<Statement> enrich(Model data, List<Rule> rules, boolean filtered) {
    log.debug(" enrich(Model data, List<Rule> rules, boolean filtered)");
    // We keep the original list to prune the data after, if necessary
    if (filtered) {
        Set<Statement> original = new HashSet<Statement>();
        original.addAll(data.listStatements().toSet());
        log.debug(" original statements are: {}", original.size());
        InfModel i = run(data, rules);
        Set<Statement> inferred = i.listStatements().toSet();
        log.debug(" inferred statements are: {}", inferred.size());
        return prune(original, inferred);
    } else {
        return run(data, rules).listStatements().toSet();
    }
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) InfModel(com.hp.hpl.jena.rdf.model.InfModel) HashSet(java.util.HashSet)

Example 5 with InfModel

use of com.hp.hpl.jena.rdf.model.InfModel in project stanbol by apache.

the class AbstractJenaReasoningService method enrich.

/**
     * Enriching: 1) Perform reasoning 2) Returns all the statements (filtered = false) or only inferred ones
     * (filtered = true)
     * 
     * This is a default implementation of task {@see ReasoningService.Tasks.ENRICH}. Subclasses may want to
     * change it.
     * 
     * @param data
     * @param rules
     * @return
     */
protected Set<Statement> enrich(Model data, boolean filtered) {
    log.debug(" enrich(Model data, boolean filtered)");
    // We keep the original list to prune the data after, if necessary
    if (filtered) {
        Set<Statement> original = new HashSet<Statement>();
        original.addAll(data.listStatements().toSet());
        log.debug(" original statements are: {}", original.size());
        InfModel i = run(data);
        Set<Statement> inferred = i.listStatements().toSet();
        log.debug(" inferred statements are: {}", inferred.size());
        return prune(original, inferred);
    } else {
        return run(data).listStatements().toSet();
    }
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) InfModel(com.hp.hpl.jena.rdf.model.InfModel) HashSet(java.util.HashSet)

Aggregations

InfModel (com.hp.hpl.jena.rdf.model.InfModel)10 Model (com.hp.hpl.jena.rdf.model.Model)7 Statement (com.hp.hpl.jena.rdf.model.Statement)7 Test (org.junit.Test)6 HashSet (java.util.HashSet)2 Query (com.hp.hpl.jena.query.Query)1 QueryExecution (com.hp.hpl.jena.query.QueryExecution)1 Property (com.hp.hpl.jena.rdf.model.Property)1 Resource (com.hp.hpl.jena.rdf.model.Resource)1 ValidityReport (com.hp.hpl.jena.reasoner.ValidityReport)1 GenericRuleReasoner (com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 AbstractRuleAdapter (org.apache.stanbol.rules.adapters.AbstractRuleAdapter)1 Recipe (org.apache.stanbol.rules.base.api.Recipe)1 Rule (org.apache.stanbol.rules.base.api.Rule)1 RuleAdapter (org.apache.stanbol.rules.base.api.RuleAdapter)1