Git Product home page Git Product logo

sparql-template's Introduction

SPARQL Template

Build status

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

Highlights

  • Support of any store exposing HTTP SPARQL endpoint
  • Uses Jena API to load and process RDF triples
  • Uses MappingContext from Spring Data Commons to process class annotations
  • On-demand (lazy) loading of relations using automatic proxying with ByteBuddy
  • Easily extended for conversion from any org.apache.jena.graph.Node to a custom Java type
  • Some useful converters are registered by default, see ch.unil.sparql.template.convert.ExtendedRdfJavaConverter
  • java.util.Date
  • java.time.ZonedDateTime
  • java.time.Duration
  • java.net.URL

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

sparql-template's People

Contributors

gushakov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.