Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing ontology file (.owl, serialized in RDF/XML) to JSON-LD #3

Closed
luispcosta opened this issue Nov 3, 2015 · 6 comments
Closed
Assignees

Comments

@luispcosta
Copy link

Hello.

I'm trying to create a simple program like the one presented here: https://github.com/jsonld-java/jsonld-java-rdf2go/blob/master/src/test/java/com/github/jsonldjava/rdf2go/RDF2GoRDFParserTest.java

If I understood correctly, RDF/XML is also a valid syntax to be transformed to JSON-LD (correct if I'm wrong) and so this is the code I wrote so far:

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.ontoware.rdf2go.RDF2Go;
import org.ontoware.rdf2go.exception.ModelRuntimeException;
import org.ontoware.rdf2go.model.Model;
import org.ontoware.rdf2go.model.Syntax;

import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.rdf2go.RDF2GoRDFParser;
import com.github.jsonldjava.utils.JsonUtils;

public class Populate {

    public static void main(String[] args) throws JsonLdError, ModelRuntimeException, IOException {

            final String rdfxml = 
                    "<?xml version='1.0'?>" +
                    "<rdf:RDF xmlns='http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#'" +
                    "xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'" +
                    "xmlns:owl='http://www.w3.org/2002/07/owl#'" +
                    "xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'>" +
                    " <owl:Ontology rdf:about='http://protege.stanford.edu/plugins/owl/owl-library/koala.owl'/>" + 

                    "<owl:ObjectProperty rdf:about='http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#hasChildren'>" +
                    "<rdfs:range rdf:resource='http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#Animal'/>" +
                    "<rdfs:domain rdf:resource='http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#Animal'/>" +
                    "</owl:ObjectProperty>" +
                    "</rdf:RDF>";

            final RDF2GoRDFParser parser = new RDF2GoRDFParser();
            final Model modelResult = RDF2Go.getModelFactory().createModel().open();
            modelResult.readFrom(new ByteArrayInputStream(rdfxml.getBytes()), Syntax.RdfXml);
            final Object jsonld = JsonLdProcessor.fromRDF(modelResult, parser);
            String finalObj = JsonUtils.toPrettyString(jsonld);
            System.out.println(finalObj);
    }
}

I'm sure that the Ontology encoded by the the string RdfXml is a valid one.
However when I try to run this code, I always get:

Model runtime exception org.ontoware.rdf2go.exception.ModelRuntimeException: org.openrdf.rio.RDFParseException: Element type "rdf:RDF" must be followed by either attribute specifications, ">" or "/>". [line 1, column 101]

So two questions:

  1. Is OWL files (encoded in RDF/XML syntax) accepted?
  2. If it is, what's wrong with the code?

Thank you!

Edit: If this actually works and I can solve the problem, I'll gladly open a PR with some tests of RDF/XML to JSON-LD transformations!

@ansell
Copy link
Member

ansell commented Nov 3, 2015

You may be missing some whitespace between the "xmlns" attributes inside of the rdf:RDF element declaration.

@luispcosta
Copy link
Author

You were right! Thank you for the help. One last question: If, instead I wanted to read the RDF/XML from a .owl file instead directly from a string, how could I do it?

This is what I tried:

final RDF2GoRDFParser parser = new RDF2GoRDFParser();
            final Model modelResult = RDF2Go.getModelFactory().createModel().open();
            modelResult.readFrom(new FileInputStream(ontologyFile), Syntax.RdfXml);
            final Object jsonld = JsonLdProcessor.fromRDF(modelResult, parser);

Map con = new HashMap();
            JsonLdOptions opt = new JsonLdOptions();
            Object compact = JsonLdProcessor.compact(jsonld, con, opt);
            String finalObj = JsonUtils.toPrettyString(jsonld);

System.out.println(finalObj);

The ontology file I'm using is this: http://pastebin.com/BhHHx4RM

I'm getting this error:

[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: Cannot cast a BlankNode to a URI
    at org.ontoware.rdf2go.model.node.impl.AbstractBlankNodeImpl.asURI(AbstractBlankNodeImpl.java:49)
    at com.github.jsonldjava.rdf2go.RDF2GoRDFParser.handleStatement(RDF2GoRDFParser.java:70)
    at com.github.jsonldjava.rdf2go.RDF2GoRDFParser.importModel(RDF2GoRDFParser.java:40)
    at com.github.jsonldjava.rdf2go.RDF2GoRDFParser.parse(RDF2GoRDFParser.java:127)
    at com.github.jsonldjava.core.JsonLdProcessor.fromRDF(JsonLdProcessor.java:420)
    at com.github.jsonldjava.core.JsonLdProcessor.fromRDF(JsonLdProcessor.java:456)
    at luispcosta.db.Populate.storeOntology(Populate.java:85)
    at luispcosta.db.Populate.main(Populate.java:61)

I'm not sure if I need a special configuration on json-ld to read this ontology, or if is a specific problem in the ontology. I'm guessing it is a error in the ontology because of: Caused by: java.lang.ClassCastException: Cannot cast a BlankNode to a URI at
but I'm not sure.

@ansell
Copy link
Member

ansell commented Nov 4, 2015

That looks like a bug here as not all non-literal cases are URIs, but I am not familiar with the RDF2Go API:

https://github.com/jsonld-java/jsonld-java-rdf2go/blob/master/src/main/java/com/github/jsonldjava/rdf2go/RDF2GoRDFParser.java#L70

@ismriv may be able to help further with a fix for that.

@luispcosta
Copy link
Author

Great! I'll be glad to help out fixing the issue (if it is confirmed as one) as well. Will be waiting

@ismriv
Copy link
Member

ismriv commented Jan 24, 2016

Hi @luispcosta, sorry I've just seen this now, I'm not actively involve in this anymore. Are you still having the same problem? There seems to be a bug in L70, can you try checking if the object is an instance of BlankNode and call toString() in that case?

@duy-tran
Copy link

duy-tran commented Mar 22, 2019

I'm having the same problem while parsing an RDF graph containing cardinality restriction.
From @luispcosta 's file, the error comes from this block:

 <owl:Class rdf:about="http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#Animal">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#hasHabitat"/>
                <owl:minCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:minCardinality>
            </owl:Restriction>
        </rdfs:subClassOf>
    </owl:Class>

L70 treats <owl:Restriction> as a blank node. Here is the screenshot of debugging
bug
@ismriv could you take a look at this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants