Open SiR0N opened 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
}
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}
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.
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?
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.
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!
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.
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??
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...
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?
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.
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: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:
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