Skip to content

SPARQL based Attributes

Martin Ledvinka edited this page Jun 26, 2023 · 6 revisions

SPARQL-based entity attributes allow populating entity attributes with results of SPARQL queries without any additional effort. This can be useful, for example, to get relevant values without having to create new entities and relationships between them, or to load some aggregate data for an entity.

Example

Namespace declarations and imports are omitted for brevity.

@OWLClass(iri = "skos:Concept")
public class Concept {

    @Id
    private URI id;

    @Sparql(query = "SELECT (COUNT(?comment) as ?count) WHERE {" +
            "?comment sioc:topic ?this ." +
            "}")
    private Integer commentCount;

    // Other attributes, getters and setters
}

This will load the number of comments of a Concept instance when it is loaded from the repository.

Notice the variable ?this - its value is populated automatically with the identifier of the instance for which the query is evaluated.

See the integration tests for further examples.

Referencing other entity attributes

JOPA is able to automatically populate query variables referencing other entity attributes (besides ?this). In the following example, the entity has a key attribute which represents the key of a report documenting an event on which a particular corrective measure is based. JOPA will automatically inject the value of the event attribute into the query, replacing the ?event variable.

This feature is not strictly necessary to make the queries work (the example would work the same without it), but it allows the repository to be more efficient in evaluating the queries. To prevent unintentional query variable replacement, this feature can be disabled by setting the enableReferencingAttributes attribute of @Sparql to false.

@OWLClass(iri = "ex:report")
public class CorrectiveMeasure {

    @Id
    private URI uri;

    @OWLObjectProperty(iri = "ex:based-on")
    private Event event;

    @Sparql(query = "SELECT ?key WHERE {" +
                        "?r ex:has-key ?key ; " +
                        "   ex:documents ?event . " +
                        "?this ex:based-on ?event . }")
    private String reportKey;
}

Supported Features

  • The attributes can be singular or plural and their type can be a basic type (Integer, Date, String), another managed type (an entity) or a collection thereof.
  • The attributes can be loaded eagerly or lazily, based on configuration in the @Sparql annotation.
  • Note that SPARQL-based attributes are read only, direct changes to their values will not be reflected in the repository.
  • Variable this in the queries represents the entity for which the query is evaluated. Its value is set automatically by JOPA to be the entity's identifier.
  • Referencing other attributes from the query via variables.

TODO

  • Support for SPARQL result set mapping
  • Support for SPARQL-based updates