https://github.com/konradreiche/jtriple
A Java object model binding for RDF.
https://github.com/konradreiche/jtriple
Last synced: 3 months ago
JSON representation
A Java object model binding for RDF.
- Host: GitHub
- URL: https://github.com/konradreiche/jtriple
- Owner: konradreiche
- License: mit
- Created: 2012-07-23T18:58:43.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T17:51:38.000Z (about 5 years ago)
- Last Synced: 2024-05-13T14:31:35.868Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 106 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-semantic-web - jtriple - A Java object model binding for RDF. (Mapping / Object to RDF Mapping)
- awesome-semantic-web - jtriple - A Java object model binding for RDF. (Mapping / Object to RDF Mapping)
README
# JTriple
JTriple is a Java tool which creates a RDF data model out of a Java object model by making use of reflection, a small set of annotations and Jena's flexible RDF/OWL API.
### Why another RDF binding for Java?
The most popular tool for persisting Java objects to RDF is [JenaBean]. JTriple was developed, respectively JenaBean was not modified due to the following reasons:
* JenaBean aims for a persistence layer (object serialization). This fact is often expressed by missing confguration, for instance a field cannot be declared as transient.
* Not the whole functionality of JenaBean is required. Additional data is serialized, for instance the serialization of the package names. Package names are vital for deserialization but for the pure data translation (one-way) it only interferes.
* Data (RDF) and schema (OWL) should be translated into two separate RDF graphs. JenaBean creates only one graph.
## Getting Started
JTriple can be deployed through Maven. Before, the following repository has to be added to your pom.xml
```xml
berlin.reiche.jtriple
https://github.com/platzhirsch/jtriple/raw/master/repository/releases
```
Then it can be added with this dependency
```xml
berlin.reiche.jtriple
jtriple
0.1-RELEASE
compile
```
Not using Maven? You can also get the [JAR] directly.
### Example
Considering the following example. A class Philosopher
```java
public class Philosopher {
@RdfIdentifier
String name;
String nationality;
List interests;
}
```
with an enum type Branch
```java
public enum Branch {
EPISTEMOLOGY("Epistemology"),
MATHEMATIC("Mathematic"),
METAPHYSISC("Metaphysic"),
PHILOSOPHY_OF_MIND("Philosophy of Mind");
String name;
Branch(String name) {
this.name = name;
}
}
```
The only requirement is to annotate one field or method of a class with `@RdfIdentifier`. Binding objects to RDF is as easy as follows
```java
// create data
Philosopher locke = new Philosopher();
locke.setName("John Locke");
locke.setNationality("English");
List branches = new ArrayList<>();
branches.add(METAPHYSISC);
branches.add(EPISTEMOLOGY);
branches.add(PHILOSOPHY_OF_MIND);
locke.setInterests(branches);
// bind object
Binding binding = new Binding(DEFAULT_NAMESPACE);
Model model = binding.getModel();
model.setNsPrefix("philosophy", NAMESPACE);
binding.bind(locke);
// output RDF
model.write(System.out, "TURTLE");
```
It is sufficient to produce this RDF
```
@prefix philosophy: .
a ;
philosophy:interests
,
,
;
philosophy:name "John Locke"^^ ;
philosophy:nationality
"English"^^ .
a philosophy:branch ;
philosophy:name "Epistemology"^^ .
a philosophy:branch ;
philosophy:name "Metaphysic"^^ .
a philosophy:branch ;
philosophy:name "Philosophy of Mind"^^ .
```
Now, to get more sophisticated results, annotations help to provide neccessary information
```java
@RdfType("http://dbpedia.org/page/Philosopher")
public class Philosopher {
@Label
@RdfIdentifier
String name;
@RdfProperty("http://www.foafrealm.org/xfoaf/0.1/nationality")
String nationality;
List interests;
}
```
```java
public enum Branch {
@SameAs({ "http://dbpedia.org/resource/Epistemology" })
EPISTEMOLOGY("Epistemology"),
@SameAs({ "http://dbpedia.org/resource/Mathematic" })
MATHEMATIC("Mathematic"),
@SameAs({ "http://dbpedia.org/resource/Metaphysic" })
METAPHYSISC("Metaphysic"),
@SameAs({ "http://dbpedia.org/resource/Philosophy_of_mind" })
PHILOSOPHY_OF_MIND("Philosophy of Mind");
@Label
String name;
Branch(String name) {
this.name = name;
}
}
```
Leading to this RDF:
```
@prefix rdfs: .
@prefix xfoaf: .
@prefix philosophy: .
@prefix dbpedia: .
a ;
rdfs:label "John Locke"^^ ;
philosophy:interests
,
,
;
xfoaf:nationality "English"^^ .
a philosophy:branch ;
rdfs:label "Metaphysic"^^ ;
dbpedia:Metaphysic .
a philosophy:branch ;
rdfs:label "Philosophy of Mind"^^ ;
dbpedia:Philosophy_of_mind .
a philosophy:branch ;
rdfs:label "Epistemology"^^ ;
dbpedia:Epistemology .
```
### Annotations
What annotations are there and how can they be used?
NameUseEffect
@RdfIdentifierFields, MethodsValue to be used for constructing the resource URI
@RdfPropertyFields, MethodsValue to define another property URI
@RdfTypeClassesValue to define a rdfs:type property on the resource
@TransientFieldsIndicate that this field must not be converted
@SameAsEnum ConstantsValue to define a owl:sameAs property on the resource
@LabelFields, MethodsValue to define a rdfs:label property on the resource
## Future Work
Some ideas for the future development:
* Implement OWL binding
* Increase the configuration flexibility
If something is amiss, feel free to open an issue or make a pull request. The implementation is lightweight and allows to change the functionality very quickly.
[JenaBean]: http://code.google.com/p/jenabean/
[Jena API]: http://jena.apache.org/
[JAR]: https://github.com/platzhirsch/jtriple/raw/master/repository/releases/berlin/reiche/jtriple/jtriple/0.1/jtriple-0.1.jar