Java Code Examples for org.apache.jena.ontology.OntModel#add()

The following examples show how to use org.apache.jena.ontology.OntModel#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Validator.java    From Processor with Apache License 2.0 6 votes vote down vote up
public OntModel fixOntModel(OntModel ontModel)
    {
        if (ontModel == null) throw new IllegalArgumentException("Model cannot be null");
        
        OntModel fixedModel = ModelFactory.createOntologyModel(ontModel.getSpecification());
        Query fix = QueryFactory.create("CONSTRUCT\n" +
"{\n" +
"  ?s ?p ?o\n" +
"}\n" +
"WHERE\n" +
"{\n" +
"  ?s ?p ?o\n" +
"  FILTER (!(?p = <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> && ?o = <https://www.w3.org/ns/ldt#Constraint>))\n" +
"}");
        
        try (QueryExecution qex = QueryExecutionFactory.create(fix, ontModel))
        {
            fixedModel.add(qex.execConstruct());
        }
        
        return fixedModel;
    }
 
Example 2
Source File: OntologyProvider.java    From Processor with Apache License 2.0 5 votes vote down vote up
public OntologyProvider(final OntDocumentManager ontDocumentManager, final String ontologyURI,
        final OntModelSpec materializationSpec, final boolean materialize)
{
    super(Ontology.class);
    
    if (ontDocumentManager == null) throw new IllegalArgumentException("OntDocumentManager cannot be null");
    if (ontologyURI == null) throw new IllegalArgumentException("URI cannot be null");
    if (materializationSpec == null) throw new IllegalArgumentException("OntModelSpec cannot be null");
    
    this.ontDocumentManager = ontDocumentManager;
    this.ontologyURI = ontologyURI;
    
    // materialize OntModel inferences to avoid invoking rules engine on every request
    if (materialize && materializationSpec.getReasoner() != null)
    {
        OntModel ontModel = getOntModel(ontDocumentManager, ontologyURI, materializationSpec);
        Ontology ontology = ontModel.getOntology(ontologyURI);

        ImportCycleChecker checker = new ImportCycleChecker();
        checker.check(ontology);
        if (checker.getCycleOntology() != null)
        {
            if (log.isErrorEnabled()) log.error("Sitemap contains an ontology which forms an import cycle: {}", checker.getCycleOntology());
            throw new OntologyException("Sitemap contains an ontology which forms an import cycle: " + checker.getCycleOntology().getURI());
        }
        
        OntModel materializedModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
        materializedModel.add(ontModel);
        ontDocumentManager.addModel(ontologyURI, materializedModel, true);
    }
}
 
Example 3
Source File: DumpTestSource.java    From RDFUnit with Apache License 2.0 5 votes vote down vote up
@Override
protected QueryExecutionFactory initQueryFactory() {

    // When we load the referenced schemata we do rdfs reasoning to avoid false errors
    // Many ontologies skip some domain / range statements and this way we add them
    OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF, ModelFactory.createDefaultModel());
    try {

        // load the data only when the model is empty in case it is initialized with the "copy constructor"
        // This sppeds up the process on very big in-memory datasets
        if (dumpModel.isEmpty()) {
            // load the data only when the model is empty in case it is initialized with the "copy constructor"
            dumpReader.read(dumpModel);
        }

        //if (dumpModel.isEmpty()) {
        //    throw new IllegalArgumentException("Dump is empty");
        //}
        //Load all the related ontologies as well (for more consistent querying
        for (SchemaSource src : getReferencesSchemata()) {
            ontModel.add(src.getModel());
        }
        // Here we add the ontologies in the dump mode
        // Note that the ontologies have reasoning enabled but not the dump source
        dumpModel.add(ontModel);
    } catch (Exception e) {
        log.error("Cannot read dump URI: " + getUri() + " Reason: " + e.getMessage());
        throw new IllegalArgumentException("Cannot read dump URI: " + getUri() + " Reason: " + e.getMessage(), e);
    }
    return masqueradeQEF(new QueryExecutionFactoryModel(dumpModel), this);
}
 
Example 4
Source File: DatasetTestSource.java    From RDFUnit with Apache License 2.0 5 votes vote down vote up
@Override
protected QueryExecutionFactory initQueryFactory() {

    // When we load the referenced schemata we do rdfs reasoning to avoid false errors
    // Many ontologies skip some domain / range statements and this way we add them
    OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF, ModelFactory.createDefaultModel());
    try {

        // load the data only when the model is empty in case it is initialized with the "copy constructor"
        // This sppeds up the process on very big in-memory datasets
        if (dumpDataset.getDefaultModel().isEmpty() && !dumpDataset.listNames().hasNext() ) {
            // load the data only when the model is empty in case it is initialized with the "copy constructor"
            dumpReader.readDataset(dumpDataset);
        }

        //if (dumpModel.isEmpty()) {
        //    throw new IllegalArgumentException("Dump is empty");
        //}
        //Load all the related ontologies as well (for more consistent querying
        for (SchemaSource src : getReferencesSchemata()) {
            ontModel.add(src.getModel());
        }
        // Here we add the ontologies in the dump mode
        // Note that the ontologies have reasoning enabled but not the dump source
        dumpDataset.setDefaultModel(ontModel.union(dumpDataset.getDefaultModel()));
    } catch (Exception e) {
        log.error("Cannot read dump URI: " + getUri() + " Reason: " + e.getMessage());
        throw new IllegalArgumentException("Cannot read dump URI: " + getUri() + " Reason: " + e.getMessage(), e);
    }
    return masqueradeQEF(new QueryExecutionFactoryDataset(dumpDataset), this);
}