Home

Awesome

SPARQL Template

Build status

Small library for traversing an RDF store using automatic mapping of triples to annotated POJOs.

Highlights

Examples

Assume we want to retrieve some information about a person from the DBPedia using the SPARQL endpoint. We annotate our domain POJO as following.

// marks this as an RDF entity
@Rdf
public class Person {

    // will be mapped from the value of http://dbpedia.org/ontology/birthName
    @Predicate(DBP_NS)
    private String birthName;

    // will be mapped from the value of http://www.w3.org/2000/01/rdf-schema#label for the Russian language
    @Predicate(value = RDFS_NS, language = "ru")
    private String label;

    // will be mapped from the value of http://dbpedia.org/property/birthDate, automatic conversion to java.time.ZonedDateTime
    @Predicate(DBP_NS)
    private ZonedDateTime birthDate;

    // will be mapped from the values of http://dbpedia.org/property/spouse, lazy load of relationships
    @Predicate(DBP_NS)
    @Relation
    private Collection<Person> spouse;
}

Then we can just use ch.unil.sparql.template.SparqlTemplate to load the triples from the DBPedia converting them automatically to the required Java instance.

    // get the default SPARQL template
    final SparqlTemplate sparqlTemplate = new SparqlTemplate("https://dbpedia.org/sparql");

    // load information about Angelina Jolie
    final Person person = sparqlTemplate.load(DBR_NS + "Angelina_Jolie", Person.class);

    System.out.println(person.getBirthName());
    // Angelina Jolie Voight

    System.out.println(person.getLabel());
    // Джоли, Анджелина

    System.out.println(person.getBirthDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy (EEE)", Locale.ENGLISH)));
    // 04/06/1975 (Wed)

    System.out.println(person.getSpouse().stream()
            .filter(p -> p.getBirthName() != null && p.getBirthName().contains("Pitt"))
            .findAny().get().getBirthName());
    // William Bradley Pitt