Wolfgang-Schuetzelhofer / jcypher

Java access to Neo4J graph databases at multiple levels of abstraction
Apache License 2.0
86 stars 15 forks source link

Add annotation support #20

Closed gizmochief7 closed 6 years ago

gizmochief7 commented 8 years ago

Would it be possible to add support for adding one or more annotations to the DOClassBuilder class when constructing new classes and their fields? I wanted to create a dynamic domain model and then have a 3rd party software use that model. However, the 3rd party expects to annotations on the class and fields.

examples: addField(String fieldName, String className, String... annotations) addressBuilder.addField("street", String.class.getName(), "@PlanningVariable(...)"); or addAnnotation(String... annotations") addressBuilder.addField("street",String.class.getName()).addAnnotation("@PlanningVariable(...)"); or addAnnotationToField(String fieldName, String... annotations) addressBuilder.addField("street",String.class.getName()); addressBuilder.addAnnotationToField("street", "@PlanningVariable(...)");

Thanks for your time

Wolfgang-Schuetzelhofer commented 8 years ago

That would be a major change, not because of adding annotations when creating dynamic classes (I think javassist which I am using can do this), but because information about those annotations must also be stored together with the meta-information in the graph database. Otherwise it is not possible to recreate the dynamic classes correctly when reading from the graph database.

Maybe you are able to use statically defined classes, that is, if you know your model in advance.

You could also define your own dynamic classes, using javassist (the dependency comes with JCypher anyway) adding annotations, and then use them with JCypher as you would with statically defined classes.

Maybe you could explain your scenario in more detail to me, and we can find another working solution.

Best regards, Wolfgang

gizmochief7 commented 8 years ago

Thank you for the fast response. That does make sense about the difficulty level of changing the storing and association mechanism. Does JCypher currently discard the annotations from Static classes that it puts into the db?

I will not be able to use static classes primarily because there isn't a data-model until the user defines one. I am allowing them to create, modify and/or remove their own domain objects.

I'm writing an optimazation and scheduling application. The domains will be drasically different from nurses to marines. I will be using the Drools Rules engine from JBoss to match people with various resources. Drools uses annotation in classes to determine what objects in memory can and cant be matched to resources. There is no way for me to know what domain objects will be mapped ahead of time. This is where the user needs to create or manage what objects the Drools Rule engine is to track.

JavaAssist does look promising for this purpose. Your API seems easier though. Would it be possible to dynamically create the Class within your API and then pass it to javaassist to add the annotation and then back to your API.

Im still a little lost on how the API is able to pull an object from the db and then put it into the JVM working memory.

Wolfgang-Schuetzelhofer commented 8 years ago

When JCypher stores an Object to the graph database, it also stores information about that Object's class to the db (if not already done). The information about the class contains mostly the fully qualified class name as well as the fields and their types (but no info about annotations).

When JCypher loads an object from the db, it first looks by means of the fully qualified class name if such a class is known to the JVM. If so, the class is instantiated and filled with data. If not, javassist is used to dynamically create such a class using the class information which had previously been stored in the graph db.

Therefore, if the class is known to the JVM because it is on the classpath (the static case) annotations are preserved. But when the class is dynamically created based on the information in the db, annotations (at least for now) are not created with the class.

It could be an option for a future release of JCypher to also store info about annotations in the db and to allow specifying annotations when specifying dynamic classes.

Best regards, Wolfgang