简体   繁体   中英

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

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. eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM