Cora Contrabida
Cora Contrabida

Reputation: 141

how to get rdf file from jena fuseki server and initialize it to my OntModel in Apache Jena?

I learn that you can extract rdf file and initialize to Model in jena using this codes:

DatasetAccessor accessor = DatasetAccessorFactory.createHTTP("http://localhost:3030/ds/data"); Model updated = accessor.getModel();


but when I try to put it in OntModel like this:

 OntModel updated = accessor.getModel();

it yields an error like this: Incompatible types: Model cannot be converted to OntModel

And also when I try to do this: OntModel model = (OntModel) accessor.getModel();

it still output an error saying: com.hp.hpl.jena.rdf.model.impl.ModelCom cannot be cast to com.hp.hpl.jena.ontology.OntModel

Upvotes: 2

Views: 586

Answers (1)

Jizheng Wan
Jizheng Wan

Reputation: 11

You probably can't get OntModel from Fuseki directly, however what you can do is save the Model in a local file and then read it into an OntModel. e.g.:

OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    DatasetAccessor myDA = DatasetAccessorFactory.createHTTP("URL");
    Model model = myDA.getModel("GRAPH");

File myFile = new File(_path);
        try
        {
            PrintStream ps_Writer = new PrintStream(myFile);
            _model.write(ps_Writer, "TTL").toString();

        }
        catch (FileNotFoundException ex)
        {

        }
ontModel.read(_path);

It is important to remember that you have to specify an OntModelSpec when you create the OntModel (in my case use OWL_MEM) and make sure you use the same spec when you save your OntModel to Fuseki. It takes me a while to find out that when you use DatasetAccessor.putModel to put the OntModel into Fuseki, it will convert it into a normal rdf Model , and if you don't specific the OntModelSpec, it will apply some reasonings when convert, and you will find out the result in Fuseki is not quite the same.

Upvotes: 1

Related Questions