kbss-cvut / jopa-examples

Usage examples for the JOPA framework.
GNU Lesser General Public License v3.0
11 stars 5 forks source link

JsonLdModule error #7

Open SiR0N opened 5 years ago

SiR0N commented 5 years ago

Hi,

I am working with json-ld in android and I want to transform a Java class to a json-dl file, I try to create the ObjectMapper with the JsonLdModule but I face a problem I do not understand problem, I know that there is not a problem with the definition of the class as the error comes from this code:

User u = new User("Paco");

ObjectMapper mapper = new ObjectMapper();

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

JsonLdModule module = new JsonLdModule();         //ERROR in this 

linemodule.configure(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jopa.jsonld");  

mapper.registerModule(module);

I followed that: https://github.com/kbss-cvut/jopa-examples/blob/master/jsonld/src/main/java/cz/cvut/kbss/jopa/jsonld/config/RestConfig.java#L37 The code runs in a Thread inside to a Service. This is the error:

E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
    Process: myprocess, PID: 6355
    java.lang.BootstrapMethodError: Exception from call site #177 bootstrap method
        at cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor.<clinit>(BeanAnnotationProcessor.java:31)
        at cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor.setPropertyAccessResolver(BeanAnnotationProcessor.java:45)
        at cz.cvut.kbss.jsonld.jackson.serialization.JsonLdSerializerModifier.<init>(JsonLdSerializerModifier.java:32)
        at cz.cvut.kbss.jsonld.jackson.JsonLdModule.init(JsonLdModule.java:38)
        at cz.cvut.kbss.jsonld.jackson.JsonLdModule.<init>(JsonLdModule.java:33)
        at com.client.Service.myService$2.run(myService.java:325)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.ClassCastException: Bootstrap method returned null
        at cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor.<clinit>(BeanAnnotationProcessor.java:31) 
        at cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor.setPropertyAccessResolver(BeanAnnotationProcessor.java:45) 
        at cz.cvut.kbss.jsonld.jackson.serialization.JsonLdSerializerModifier.<init>(JsonLdSerializerModifier.java:32) 
        at cz.cvut.kbss.jsonld.jackson.JsonLdModule.init(JsonLdModule.java:38) 
        at cz.cvut.kbss.jsonld.jackson.JsonLdModule.<init>(JsonLdModule.java:33) 
        at com.client.Service.myService$2.run(myService.java:325) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764)

Any ideas of the problem? and any sugestions of my code?

I try to do something similar to this example: https://github.com/kbss-cvut/jopa-examples/tree/master/jsonld

ledsoft commented 5 years ago

Hi,

I don't really have much experience with Android development, but from what I've gathered, this could be an issue with Java version. You need to use Java 8 with JB4JSON-LD. According to [1], [2], [3] and other Google results, you should add the following into your Gradle config:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
SiR0N commented 5 years ago

It worked, thanks! I just added these lines in the gradle. I just want to mention that it does not work if I include final as it is done in here: https://github.com/kbss-cvut/jopa-examples/blob/master/jsonld/src/main/java/cz/cvut/kbss/jopa/jsonld/config/RestConfig.java#L42

now I face another problem, I defined my User class as:

@OWLClass(iri = "http://onto.fel.cvut.cz/ontologies/ufo/Person")
public class User implements Serializable {

    @Id
    public URI uri;

    @OWLDataProperty(iri = "http://xmlns.com/foaf/0.1/firstName")
    private String firstName;

    @OWLDataProperty(iri = "http://xmlns.com/foaf/0.1/lastName")
    private String lastName;

    @OWLDataProperty(iri = "http://xmlns.com/foaf/0.1/accountName")
    private String username;

    @Properties
    private Map<String, Set<String>> properties;

    // Getters and setters follow

but when I print the json-dl it just prints a normal json file, I guess that the problem is how I configure the JsonLdModule but I am not sure how I can do it, can you share any documentation or example code? it seems that it does not recognise the Annotations in the class.

This is my code:

User u = new User("Paco");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(u));
 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

JsonLdModule module = new JsonLdModule();
module.configure(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jopa.jsonld");
mapper.registerModule(module);
System.out.println(mapper.writeValueAsString(u));

and I get the same output:

I/System.out: {"firstName":"Paco","lastName":null,"properties":null,"uri":null,"username":null}
I/System.out: {"firstName":"Paco","lastName":null,"properties":null,"uri":null,"username":null}
ledsoft commented 5 years ago

Hey, this is an interesting issue. I did some experimenting and it looks like once you use the ObjectMapper instance for the first time, it ignores modules registered afterwards (or at least the JsonLdModule).

If you move the first mapper.writeValueAsString(u) call after registration of the JsonLdModule, it works ok. So, restructuring the code as follows should work:

User u = new User("Paco");
ObjectMapper mapper = new ObjectMapper();
 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

JsonLdModule module = new JsonLdModule();
module.configure(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jopa.jsonld");
mapper.registerModule(module);
System.out.println(mapper.writeValueAsString(u));

If you want to have both JSON and JSON-LD serialization, you will need two mappers.

SiR0N commented 5 years ago

Hi it worked, thanks! I spend all day with that problem, it seems like a bug.

btw, I saw that you are working a lot with semantic, I have an RDF schema and I want to create java classes automatically (similar to https://github.com/kbss-cvut/jopa-examples/blob/master/jsonld/src/main/java/cz/cvut/kbss/jopa/jsonld/model/User.java)

do you know any tool to help me with that?

ledsoft commented 5 years ago

You're welcome, glad to help.

Well, we have JOPA, which is a persistence layer over semantic data. It supports generation of Java model, but the generator is based on OWL integrity constraints. If you have just RDF Schema, you can take a look at the generator in AliBaba (section Generating Concepts), but it is (as usual) poorly documented. We actually wrote a whole paper on comparison of semantic persistence libraries and the object model generator was one of the criteria we compared, so you might find other tools there as well.

SiR0N commented 5 years ago

Thanks for your help!

I think that JOPA is a powerful tool, we are making an ontology so we will use it. I am looking at the examples and in the example01 I do not find the code to generate the Java classes in there: https://github.com/kbss-cvut/jopa-examples/tree/master/example01-jopa-sesame-owl2java/src/main/java/cz/cvut/kbss/jopa/example01/model

It says these classes are generated by OWL2Java automatically but I do not see how to use OWL2Java

btw, your paper is interesting, good job!

ledsoft commented 5 years ago

Thanks for taking interest in JOPA and in the paper.

As for the example01, the classes in the package you reference are written manually. However, if you run mvn package (actually, compile is enough), OWL2Java will be executed by the jopa-maven-plugin and the generated classes will be placed in src/main/generated-sources/cz/cvut/kbss/jopa/example01/generated. Check https://github.com/kbss-cvut/jopa-examples/blob/master/example01-jopa-sesame-owl2java/pom.xml to see the configuration. Both manual and generated model are available so that one can compare how they differ.

SiR0N commented 4 years ago

I am trying to get the classes but I get this error:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.320 s
[INFO] Finished at: 2019-10-24T09:24:10+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.11:compile (default) on project example-01: Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.11:compile failed:
 Plugin org.codehaus.mojo:aspectj-maven-plugin:1.11 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:12.0.1 at specified path C:\Program Files\Java\jdk-12.0.1/../lib/tools.jar -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

I checked maven version, I use JDK now (I used JRE before), besides I downloaded the tools.jar in the directory. Did not work :( (I use JAVA 12)

I took all the examples and I execute maven (mvn package) inside example01-jopa-sesame-owl2java folder (the pom is there) I wonder if the parent pom needs to be executed first and then this one or just running the pom in the example would work.

if I execute mvn compile it creates the .class files

may it be a missing dependency??

ledsoft commented 4 years ago

Hi, no, this is not a missing dependency. You have to use JDK 8 to build JOPA-based applications. This is because the aspectj-maven-plugin we use to weave aspects into entity classes currently does not support later versions of Java. There is an issue for this in the JOPA repository with a workaround...

SiR0N commented 4 years ago

Hi, thanks for the quick reply!

I just realized that the classes were built! (anyway I will use JAVA 8 from now). I saw in the pom:

 <configuration>
                    <package>cz.cvut.kbss.jopa.example01.generated</package>
                    <output-directory>${project.basedir}/src/main/generated-sources</output-directory>
                    <ontology-iri>http://krizik.felk.cvut.cz/ontologies/2015/jopa-example01.owl</ontology-iri>
                    <mapping-file>${project.basedir}/mapping</mapping-file>
                    <context-name>jopa-example01</context-name>
                </configuration>

if I want to use my ontology, I guess I should change this and the mapping file, right?

ledsoft commented 4 years ago

Yeah, basically, the mapping file is there to allow loading locally stored ontologies referenced by a URL. So, taking the configuration of example01, there is no ontology jopa-example01.owl at krizik.felk.cvut.cz (the server is not even running anymore). But there is a local copy in the project, and the mapping file points to it, so that OWLAPI (which is used by the generator) is able to load it.