Creating Java Classes from Ontology - java

I have an ontology file (in RDF) which expresses the entities in the app that I am writing. I am using Jena, and can access the Concepts and create/access the resources by directly manipulating triples.
To make things easier, I created a set of Java classes, one for each type of resource in my ontology, keeping in mind their inheritances, and properties. E.g.
public class Agent{
}
and
public class Person extends Agent{
private String name;
}
If the ontology contains two types of resources, Agent and Person, with the latter being a subclass of the former.
I realized that the process of creating these classes (and the methods to extract them) are very monotonous. If I was writing an application on databases, I would have used a config file for Hibernate and let it take care of the details.
My question is: Is there a tool available that will take an ontology (in an RDF file) as input, and create a set of Java files representing the Concepts in the ontology, as in the example above? (or if Jena itself can do this, and I am missing something)
Thanks.

There are tools such as Bouml and Andromda with which you may generate Java from XMI UML serialization.
Furthermore, work has been done towards integrating UML with RDF: http://infolab.stanford.edu/~melnik/rdf/uml/.
So I guess you could find a way to transform your RDF to XMI then to generate Java from XMI, given you first map your initial RDF schema to RDF/UML.
Also I just found this paper: Automatic Mapping of OWL Ontologies into Java which I did not read so I cannot comment.

Protege supports RDF import and java export. I've used it in the past for getting ontology data into java programs and it's worked reasonably.

While going through the links provided above, I chanced upon the Trispresso Project, which provides a nice summary of the relevant tools and their features, including multiple inheritance and code generation. Thought it would make a good answer to my own question.

Did u checked JRDF? There are other code generation tools available too.. Check here.

Related

XSD for testdata generation

I'm trying to generate flat file for test data using JAVA. Flat file has own mapping document which describes all fields of each line.
I was suggested to use XSD for mapping and I did some research on XSD. As I understood that XSD is for only validation of XML. In this case I have to randomly generate XML file based on XSD and convert it to txt or other format. Because as an output I need flat file, not XML.
It seems like using XSD i'm adding extra steps in creating the file as first create XML, validate with XSD and convert it expected format.
What would be your recommendation in my situation for following the mapping document?
Thanks in advance.
I have seen your kind of setup before. The reasons may be different than your particular scenario, but nonetheless it made sense. One thing to consider has to do with the skills and tools people have available and so whatever makes the job done quick and well, goes.
You seem to describe an offset based, "flat" data structure. In my case, people used COBOL copybooks which are very good at describing this. IBM Rational Developer had a built-in wizard which allows the creation of Java Data Bindings from a COBOL copybook. This is to say that within a minute one gets a Java class which can create a record for your flat file in no time (it comes with all the logic required to do the padding, etc.)
To get the data generated, there are tools capable of generating XML files which cover all constraints defined by an XSD (e.g. alternate content i.e. xsd:choice, enumerated values, etc.) Now, assuming you have a proper XSD describing your logical model of your flat file, one can get 10s, 100s, even 100K XML generated from an XSD spec. It takes a click, plus the time spent by the tool to create those files.
Next, to get the XML files in your generated Java class, and so avoid going through XSLT or whatever (many shops don't have the skills) it may be as simple as writing Java mapping code between a JAXB generated class and the one created above, or if matching is possible, simply annotate the generated class to support JAXB unmarshalling. This last step may take longer to code, but it would be trivial code any Java developer would know how to do it.
This could possibly give you a view into why someone may have recommended Java and XSD for this task. XSD is a modeling language with built in support for constraints, which may prove helpful in generating test data through combinatorial techniques.

AST for UML Activity diagram

I am currently doing a project to convert uml diagrams to java code,in one of the step I have to create an ast for general activity diagram and write a java code for the ast. The problem is I am not sure how to represent it in java code to accept any activity diagram as an input. Please need some help.
This is the meta-model of a general activity diagram, I want to represent this diagram as a java code to accept inputs.
Activity Diagram Metamodel
The first diagram on the second page of the pdf
What tool are you using to create your activity diagrams? Most will provide an API to query models and/or an XMI export*. In either case you don't get an AST as such, more an Object structure of your model. But it's still pretty easy to iterate that structure and generate code.
One option worth considering is the Eclipse Modeling Framework (EMF). There are tools available for both creating diagrams (e.g. Papyrus) and a multitude of options for generating code from models (e.g. Acceleo) - have a look at the 'Model to Text' (M2T) sub-project.
Even if you decide to use another tool for diagram creation, it's possible to use Eclipse M2T for code generation; most tools will export XMI in the eclipse dialect.
hth.
--
*XMI is the 'standard' for UML model interchange. It was plagued with inconsistency in interpretation originally, and there are still some problems among tools today. However: it is an XML dialect so even if tool B can't directly import XMI from tool A, it's usually possible to convert between the two.

How to generate XMI file for the java source code?

I'm writing a program in java language which takes the java source code as an input and the output is xmi file which contains the class diagram of the source code.
I have researched for xmi structure but I was not able to find an adequate resource which explain the structure and order of tags.
First question is how can I found out what is the order of the tags and definition of each tag? for instance, what is the meaning of "isActive = false" etc?
Secondly, is there any suggestion of libraries or anything in java language which can help me to finish this project?
I think the tag order doesn't matter in XMI. In general, XMI just defines how to map MOF to XML. So basically you build the M2 model (=UML) from Java and and then map this to XML with XMI if I understand this correctly (and I think the double indirection is why you don't find good resources for the mapping from UML to XMI directly).
For many people, examples may be more accessible than the chain of transformation rules defined by the various OMG standards. So I'd just draw a minimal UML example diagram in my favorite UML tool for the export aspect I am interested in and look at the exported XMI. Then add features as needed...
P.S.: You may want to use the Java reflection classes (package java.lang.reflect) instead of parsing the the Java source yourself to generate XMI (if you don't need to preserve method argument names).

Java library to assist in XSD creation?

Is anyone aware of a library that makes the process of creating XSDs in Java a little easier than using something like a DocumentBuilder? Something like a helper or utility class. I came across the org.eclipse.xsd maven jar but I'm having ClassNotFoundException issues when working with it in Eclipse and I'm not entirely sure it's meant to be used as a standalone kind of thing. This is a bit difficult to Google for as well since there are lot of search results around automatic generation/translation from Java to XSD and vice versa.
Essentially what I need to do is to programmatically create an XSD from a certain source of data -- not Java classes.
Apache XMLSchema is a lightweight Java object model that can be used to manipulate and generate XML schema representations. You can use it to read XML Schema (xsd) files into memory and analyze or modify them, or to create entirely new schemas from scratch.
The fact that with this API one can create an XSD from scratch, it sounds as a starting point to achieve the ask; as to the fitness, it depends on what that "certain source of data" is.

Automatically build resource class based of XML in Java

In Android applications, resources are specified in xml documents, which automatically are built into the R class, readily accessible within the source code as strongly typed.
Is there any way I could use a similar approach for a regular Java desktop application?
What I'd like to accomplish, is both the removal of strings from the code (as a separation of "layers", more or less) and to make it easy to add support for localization, by simply telling the program to choose the xml file corresponding to the desired language.
I've googled around a bit, but the things I'm looking for seem to be drowning in results about parsing or outputting xml, rather than tools utilizing xml to generate code.
Eclipse's message bundle implementation (used by plugins for example) integrates with the Externalize Strings feature and generates both a static class and a resource properties file for your strings:
http://www.eclipse.org/eclipse/platform-core/documents/3.1/message_bundles.html
For this integration to work Eclipse needs to see org.eclipse.osgi.util.NLS on the class path. From memory, the dependencies of the libraries it was available in were a little tricky for the project I used this approach in, so I just got the source and have it as a stand-alone class in my core module (see the comments for more on that).
It provides the type safety you're looking for and the IDE features save a lot of time. I've found no downsides to the approach so far.
Edit: this is actually what ghostbust555 mentioned in the comments, but not clear in that article that this isn't limited to Eclipse plugins and you refer to your resources via static members of a messages class.
I haven't seen any mention of others using this approach with their own applications, but to me it makes complete sense given the IDE integration and type safety.
I'm not sure if this is what you mean but check out internationalization- http://netbeans.org/kb/docs/java/gui-automatic-i18n.html
Are you looking for something that parses XML files and generates Java instances of similar "struct-like" objects, like JAXP, and JAXB?
I came across ResGen which, given resource bundle XML files generates Java files that can be used to access the resources in a type-safe way.
http://eigenbase.sourceforge.net/resgen/

Categories