Search in sources :

Example 1 with Property

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

the class JenaReasoningServiceTest method testRunWithRules.

/**
     * Tests the run(Model,List<Rule>) method of a service
     * 
     * @param service
     */
private void testRunWithRules(JenaReasoningService service) {
    // Prepare the rule set
    String source = "" + "\n@prefix foaf: <" + TestData.FOAF_NS + ">." + "\n@prefix ex: <" + TestData.TEST_NS + ">." + "\n[rule1: (?a foaf:knows ?b) (?a foaf:workplaceHomepage ?w) (?b foaf:workplaceHomepage ?w) -> (?a ex:collegueOf ?b)] " + "\n[rule2: (?b foaf:knows ?a) -> (?a foaf:knows ?b)] " + "\n[rule3: (?a ex:collegueOf ?b) -> (?b ex:collegueOf ?a)] ";
    // log.info("This is the ruleset: \n {}", source);
    List<Rule> rules = TestUtils.parseRuleStringAsFile(source);
    log.info("Loaded {} rules", rules.size());
    // Clean data
    TestData.alexdma.removeProperties();
    TestData.enridaga.removeProperties();
    Resource wphomepage = TestData.model.createResource("http://stlab.istc.cnr.it");
    // Prepare data
    TestData.alexdma.addProperty(TestData.foaf_knows, TestData.enridaga);
    TestData.alexdma.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
    TestData.enridaga.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.enridaga.getModel(), TestData.alexdma.getModel());
    input = ModelFactory.createUnion(input, TestData.foaf);
    // Run the method
    Set<Statement> inferred = service.run(input, rules).listStatements().toSet();
    // Expected statements
    Property collegueOf = TestData.model.createProperty(TestData.TEST_NS + "collegueOf");
    Set<Statement> expected = new HashSet<Statement>();
    expected.add(TestData.model.createStatement(TestData.alexdma, collegueOf, TestData.enridaga));
    expected.add(TestData.model.createStatement(TestData.enridaga, collegueOf, TestData.alexdma));
    log.info("All the expected statements must be in the inferred output");
    Set<Statement> notInOutput = TestUtils.expectedStatementsCheck(inferred, expected);
    log.info("Are all expected statements in the inferred set (true)? {}", notInOutput.isEmpty());
    if (!notInOutput.isEmpty()) {
        for (Statement bad : notInOutput) {
            log.error("The following statement is not included in the reasoner output: {}", bad);
        }
    }
    assertTrue(notInOutput.isEmpty());
    // Clean data
    TestData.alexdma.removeProperties();
    TestData.enridaga.removeProperties();
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) Resource(com.hp.hpl.jena.rdf.model.Resource) Model(com.hp.hpl.jena.rdf.model.Model) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule) Property(com.hp.hpl.jena.rdf.model.Property) HashSet(java.util.HashSet)

Example 2 with Property

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

the class JenaToClerezzaConverterTest method setupClass.

@BeforeClass
public static void setupClass() {
    /*
		 * Set-up the Jena model for the test.
		 * Simply add the triples:
		 * 	AndreaNuzzolese isA Person
		 * 	EnricoDaga isA Person
		 *  AndreaNuzzolese knows EnricoDaga
		 */
    model = ModelFactory.createDefaultModel();
    Resource foafPersonInJena = model.createResource(foaf + "Person");
    Property knowsInJena = model.createProperty(foaf + "knows");
    Resource andreaNuzzoleseInJena = model.createResource(ns + "AndreaNuzzolese", foafPersonInJena);
    Resource enricoDagaInJena = model.createResource(ns + "EnricoDaga", foafPersonInJena);
    andreaNuzzoleseInJena.addProperty(knowsInJena, enricoDagaInJena);
    /*
		 * Set-up the Clerezza model for the test.
		 * As before simply add the triples:
		 * 	AndreaNuzzolese isA Person
		 * 	EnricoDaga isA Person
		 *  AndreaNuzzolese knows EnricoDaga
		 */
    mGraph = new SimpleGraph();
    IRI knowsInClerezza = new IRI(ns + "knows");
    IRI rdfType = new IRI(RDF.getURI() + "type");
    IRI foafPersonInClerezza = new IRI(foaf + "Person");
    BlankNodeOrIRI andreaNuzzoleseInClerezza = new IRI(ns + "AndreaNuzzolese");
    BlankNodeOrIRI enricoDagaInClerezza = new IRI(ns + "EnricoDaga");
    Triple triple = new TripleImpl(andreaNuzzoleseInClerezza, rdfType, foafPersonInClerezza);
    mGraph.add(triple);
    triple = new TripleImpl(enricoDagaInClerezza, rdfType, foafPersonInClerezza);
    mGraph.add(triple);
    triple = new TripleImpl(andreaNuzzoleseInClerezza, knowsInClerezza, enricoDagaInClerezza);
    mGraph.add(triple);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Resource(com.hp.hpl.jena.rdf.model.Resource) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Property(com.hp.hpl.jena.rdf.model.Property) BeforeClass(org.junit.BeforeClass)

Example 3 with Property

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

the class JenaRDFSReasoningServiceTest method testRDFSSubPropertyOf.

@Test
public void testRDFSSubPropertyOf() {
    log.info("Testing rdfs:subPropertyOf inference with RDFS reasoner");
    // We invent a property to extend foaf:knows
    Property collegueOf = TestData.model.createProperty(TestData.TEST_NS + "collegueOf");
    collegueOf.addProperty(RDFS.subPropertyOf, TestData.foaf_knows);
    // Prepare data
    TestData.alexdma.addProperty(collegueOf, TestData.enridaga);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
    // Does alexdma know enridaga?
    InfModel inferred = reasoningService.run(input);
    Statement knowsHim = TestData.model.createStatement(TestData.alexdma, TestData.foaf_knows, TestData.enridaga);
    // log.info("Statements: {}", TestUtils.printStatements(inferred,
    //		TestData.enridaga, RDF.type));
    log.info("Does alexdma foaf:knows enridaga?...(true)? {}", inferred.contains(knowsHim));
    assertTrue(inferred.contains(knowsHim));
    // 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) Property(com.hp.hpl.jena.rdf.model.Property) Test(org.junit.Test)

Example 4 with Property

use of com.hp.hpl.jena.rdf.model.Property in project wikidata-query-rdf by wikimedia.

the class BigdataStatementToJenaStatementMapper method map1.

@Override
public Statement map1(final BigdataStatement blzgStmt) {
    final Resource s = convertToJenaResource(blzgStmt.getSubject());
    final Property p = convertToJenaProperty(blzgStmt.getPredicate());
    final RDFNode o = convertToJenaRDFNode(blzgStmt.getObject());
    return ResourceFactory.createStatement(s, p, o);
}
Also used : BigdataResource(com.bigdata.rdf.model.BigdataResource) Resource(com.hp.hpl.jena.rdf.model.Resource) Property(com.hp.hpl.jena.rdf.model.Property) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 5 with Property

use of com.hp.hpl.jena.rdf.model.Property in project eol-globi-data by jhpoelen.

the class StudyImporterForSaproxylic method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    try {
        final Model model = ModelFactory.createDefaultModel();
        NavigableSet<Triple<String, String, String>> triples = DBMaker.newTempTreeSet();
        Tripler add = triple -> {
            Resource resource = model.createResource("sx:" + triple.getLeft());
            Property property = model.createProperty("sx:" + triple.getMiddle());
            String obj = triple.getRight();
            if (StringUtils.startsWith(obj, "{")) {
                Resource food = model.createResource("sx:" + obj);
                model.add(resource, property, food);
            } else if (StringUtils.startsWith(obj, "http")) {
                model.add(resource, property, model.createResource(obj));
            } else if (StringUtils.isNotBlank(obj)) {
                model.add(resource, property, obj);
            }
        };
        parseReferences(add, getDataset().getResource("sx_txt/Reference.txt"));
        parseLocalities(add, getDataset().getResource("sx_txt/Locality.txt"));
        parseTaxa(add, getDataset().getResource("sx_txt/Taxon.txt"));
        parseTaxonRanks(add, getDataset().getResource("sx_txt/TaxonRank.txt"));
        parseOccurrences(add, getDataset().getResource("sx_txt/Occurrence.txt"));
        parseAssociations(add, getDataset().getResource("sx_txt/SX_Association.txt"));
        parseInteractionTypeMap(add, getDataset().getResource("interaction_type_map.tsv"));
        // associations -> occurrences, taxa, reference
        String queryString = "SELECT ?sourceTaxonName ?sourceLifeStage ?interactionTypeId ?targetTaxonName ?targetLifeStage ?referenceCitation ?localityName ?studyTitle " + "WHERE {" + "      ?interaction <sx:mentioned_by> ?studyTitle . " + "      ?studyTitle <sx:hasName> ?referenceCitation . " + "      ?sourceSpecimen <sx:participates_in> ?interaction . " + "      ?targetSpecimen <sx:participates_in> ?interaction . " + "      ?sourceSpecimen ?inter ?targetSpecimen . " + "      ?inter <sx:equivalentTo> ?interactionTypeId . " + "      ?sourceSpecimen <sx:classifiedAs> ?sourceTaxon . " + "      ?sourceTaxon <sx:hasName> ?sourceTaxonName . " + "      ?targetSpecimen <sx:classifiedAs> ?targetTaxon . " + "      ?targetTaxon <sx:hasName> ?targetTaxonName . " + "      ?sourceSpecimen <sx:inStage> ?sourceLifeStage . " + "      ?targetSpecimen <sx:inStage> ?targetLifeStage . " + "      ?targetSpecimen <sx:foundAt> ?locality . " + "      ?locality <sx:hasName> ?localityName . " + "}";
        Query query = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(query, model);
        ResultSet results = qe.execSelect();
        toInteractions(results);
        qe.close();
    } catch (IOException e) {
        throw new StudyImporterException("failed to access resource", e);
    }
}
Also used : CSVTSVUtil(org.eol.globi.util.CSVTSVUtil) Query(com.hp.hpl.jena.query.Query) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) ModelFactory(com.hp.hpl.jena.rdf.model.ModelFactory) QueryExecutionFactory(com.hp.hpl.jena.query.QueryExecutionFactory) StudyImpl(org.eol.globi.domain.StudyImpl) ArrayList(java.util.ArrayList) Property(com.hp.hpl.jena.rdf.model.Property) Map(java.util.Map) Triple(org.apache.commons.lang3.tuple.Triple) QueryFactory(com.hp.hpl.jena.query.QueryFactory) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) ResultSetFormatter(com.hp.hpl.jena.query.ResultSetFormatter) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) Iterator(java.util.Iterator) SelectorImpl(com.hp.hpl.jena.rdf.model.impl.SelectorImpl) IOException(java.io.IOException) NavigableSet(java.util.NavigableSet) ResultSet(com.hp.hpl.jena.query.ResultSet) Resource(com.hp.hpl.jena.rdf.model.Resource) File(java.io.File) List(java.util.List) QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeMap(java.util.TreeMap) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) Model(com.hp.hpl.jena.rdf.model.Model) DBMaker(org.mapdb.DBMaker) QueryExecution(com.hp.hpl.jena.query.QueryExecution) InputStream(java.io.InputStream) Query(com.hp.hpl.jena.query.Query) Resource(com.hp.hpl.jena.rdf.model.Resource) IOException(java.io.IOException) QueryExecution(com.hp.hpl.jena.query.QueryExecution) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) Model(com.hp.hpl.jena.rdf.model.Model) ResultSet(com.hp.hpl.jena.query.ResultSet) Property(com.hp.hpl.jena.rdf.model.Property)

Aggregations

Property (com.hp.hpl.jena.rdf.model.Property)5 Resource (com.hp.hpl.jena.rdf.model.Resource)4 Model (com.hp.hpl.jena.rdf.model.Model)3 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)2 Statement (com.hp.hpl.jena.rdf.model.Statement)2 LabeledCSVParser (com.Ostermiller.util.LabeledCSVParser)1 BigdataResource (com.bigdata.rdf.model.BigdataResource)1 Query (com.hp.hpl.jena.query.Query)1 QueryExecution (com.hp.hpl.jena.query.QueryExecution)1 QueryExecutionFactory (com.hp.hpl.jena.query.QueryExecutionFactory)1 QueryFactory (com.hp.hpl.jena.query.QueryFactory)1 QuerySolution (com.hp.hpl.jena.query.QuerySolution)1 ResultSet (com.hp.hpl.jena.query.ResultSet)1 ResultSetFormatter (com.hp.hpl.jena.query.ResultSetFormatter)1 InfModel (com.hp.hpl.jena.rdf.model.InfModel)1 ModelFactory (com.hp.hpl.jena.rdf.model.ModelFactory)1 SelectorImpl (com.hp.hpl.jena.rdf.model.impl.SelectorImpl)1 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)1 File (java.io.File)1 IOException (java.io.IOException)1