Link Search Menu Expand Document
Start for Free

Model Extraction Service

This page presents a SPARQL service that extracts a data model - classes, properties, and their domains and ranges - from the local (non-virtualized) RDF data in a Stardog database.

Page Contents
  1. Overview
  2. Modes
  3. Output vocabulary
  4. Parameters
  5. Examples
    1. Storing an annotated model in a named graph
    2. Extracting the model of a single named graph
    3. Labeling the extracted model

Overview

Some Stardog databases contain data that has no explicit schema, or whose schema has drifted from what the data actually looks like. The Model Extraction Service, available as of Stardog 12.2, inspects a database and returns the model it observes: which classes are present, which properties are used, and which classes or datatypes appear in the subject and object positions of each property. The result is a (usually small) RDF graph that can be consumed by an application or loaded into a named graph.

The SERVICE IRI is <tag:stardog:api:model> and, like the Statistics Service, it returns rows with three variables that are interpreted as subject, predicate, and object of an RDF triple:

prefix stardog: <tag:stardog:api:>

CONSTRUCT { ?s ?p ?o } WHERE {
    SERVICE stardog:model {
        [] stardog:rdf:subject   ?s ;
           stardog:rdf:predicate ?p ;
           stardog:rdf:object    ?o .
    }
}

Replacing CONSTRUCT with INSERT { GRAPH <urn:model> { ?s ?p ?o } } stores the extracted model in a named graph, where it can be queried with SPARQL or used as a reasoning schema.

The extraction covers data stored in Stardog. It does not read from Virtual Graphs; to generate a model from virtual graph mappings, use stardog data model --input VIRTUAL.

Modes

Schema can be extracted either from the precomputed statistics the query optimizer maintains, or from live scans of the data. The stardog:model:mode parameter selects between them explicitly.

Mode How domains/ranges are computed Cost Supports FROM/FROM NAMED in the query
stats Precomputed statistics only; no data scans. Ranges for datatype properties are not extracted because statistics does not maintain those. Lowest, bounded latency No: statistics is for the entire database
hybrid Statistics for domains and object property ranges, plus one scan per datatype property to determine its range. Low No: statistics is for the entire database
data Everything from per-property scans of the data. Exact for small properties, sampled for large ones. Highest Yes

When stardog:model:mode is not given, the mode is resolved automatically:

  • hybrid when the query targets the entire database, since the global statistics are accurate for that dataset;
  • data when the query is restricted to a subset of graphs (via FROM, FROM NAMED, or a GRAPH block wrapping the SERVICE call), since global statistics would over-count for a subset.

Choose stats explicitly when bounded latency matters more than completeness, for example when a UI extracts the model interactively. Choose data explicitly when accuracy matters more than latency.

In stats and hybrid modes the domain and range information comes from database-wide statistics and is not restricted to the graphs in the query dataset, even when the query specifies one. Use data mode to extract a model for a subset of graphs. Sets of types and predicates will still be extracted from all graphs but domain/range information only from the query dataset

In data mode, properties whose extent exceeds stardog:model:sample:threshold (1,000,000 triples by default) are sampled rather than fully scanned, using stardog:model:sample:size triples (100,000 by default).

Class and property hierarchies (rdfs:subClassOf and rdfs:subPropertyOf) are always read from the schema graphs and are unaffected by the mode.

Output vocabulary

The extracted model uses standard rdf:, rdfs:, owl:, and schema: vocabulary so that it can be directly loaded as an ontology:

  • Each class is emitted as ?class rdf:type owl:Class.
  • Each property is emitted as ?property rdf:type owl:ObjectProperty if its objects are resources, owl:DatatypeProperty if they are literals, or rdf:Property if they are mixed or unknown.
  • Domains and ranges are emitted with schema:domainIncludes and schema:rangeIncludes. Ranges of datatype properties are datatype IRIs such as xsd:string.
  • Asserted rdfs:subClassOf and rdfs:subPropertyOf triples from the active schema are included as-is.

schema:domainIncludes / schema:rangeIncludes are used rather than rdfs:domain / rdfs:range because an extracted domain is disjunctive: subjects of a property were observed to be one of the listed classes, not all of them simultaneously, which is what rdfs:domain would entail. Set stardog:model:rdfs-domain-range to true to emit the RDFS terms instead.

With stardog:model:counts set to true, the service additionally reports how often each class, property, domain, and range was observed. Class and property counts are direct annotations; domain and range counts are attached through a blank node so that each class of a domain or range gets its own count:

:Person  a                      owl:Class ;
         stardog:model:count    "12000"^^xsd:integer .

:worksFor a                     owl:ObjectProperty ;
          stardog:model:count   "9500"^^xsd:integer ;
          schema:domainIncludes :Person ;
          schema:rangeIncludes  :Company ;
          stardog:model:domain  [ stardog:model:class :Person  ; stardog:model:count "9500"^^xsd:integer ] ;
          stardog:model:range   [ stardog:model:class :Company ; stardog:model:count "9500"^^xsd:integer ] .

Parameters

All parameters are optional except the three output variables, and are given in the SERVICE pattern using the stardog:model: namespace.

Parameter Default Description
stardog:rdf:subject, stardog:rdf:predicate, stardog:rdf:object - Required. The output variables bound to the triples of the extracted model.
stardog:model:mode auto stats, hybrid, or data. See Modes.
stardog:model:counts false Emit occurrence counts for classes, properties, domains, and ranges.
stardog:model:types:limit 1000 Maximum number of classes to include.
stardog:model:types:usage:min 1000 Minimum usage (incoming and outgoing edges for instances of the class) for a class to be included.
stardog:model:predicates:count:min 100 Minimum number of triples for a property to be included.
stardog:model:domain:count:min 100 :X is included as a domain class of :p if at least this number of subjects are instances of :X.
stardog:model:range:count:min 100 :X is included as a range class (datatype) of :p if at least this number of objects are instances of :X.
stardog:model:dominance 100 Drops domain and range candidates whose count is below max_count / dominance for that property. The default keeps candidates that account for at least 1% of the dominant class.
stardog:model:rdfs-domain-range false Use rdfs:domain / rdfs:range instead of schema:domainIncludes / schema:rangeIncludes.
stardog:model:rollup true Replace a property’s set of domain or range classes with their common superclass from the schema when one exists.
stardog:model:rollup:max-edges 100000 Safety cap on the number of rdfs:subClassOf axioms loaded for roll-up. Roll-up is disabled if the schema is larger.
stardog:model:schema default The reasoning schema whose graphs supply the class and property hierarchy. Use all for the union of all configured schemas.
stardog:model:sample:threshold 1000000 In data mode, the property extent above which sampling is used instead of a full scan.
stardog:model:sample:size 100000 In data mode, the number of triples sampled for such properties.

Examples

Storing an annotated model in a named graph

Extract the model with counts and save it as <urn:model>, so it can be inspected with ordinary SPARQL queries:

prefix stardog: <tag:stardog:api:>

INSERT { GRAPH <urn:model> { ?s ?p ?o } }
WHERE {
    SERVICE stardog:model {
        [] stardog:rdf:subject   ?s ;
           stardog:rdf:predicate ?p ;
           stardog:rdf:object    ?o ;
           stardog:model:counts  true ;
           stardog:model:mode    "data" .
    }
}

The properties of a class and their ranges can then be listed with:

prefix schema: <https://schema.org/>

SELECT ?property ?range FROM <urn:model> {
    ?property schema:domainIncludes <http://example.com/Person> ;
              schema:rangeIncludes  ?range .
}

Extracting the model of a single named graph

Restricting the query dataset restricts the extraction. Because the dataset is a subset of the database, the service resolves to data mode automatically:

prefix stardog: <tag:stardog:api:>

CONSTRUCT { ?s ?p ?o }
FROM <urn:my-graph>
WHERE {
    SERVICE stardog:model {
        [] stardog:rdf:subject   ?s ;
           stardog:rdf:predicate ?p ;
           stardog:rdf:object    ?o .
    }
}

Even in data mode only domains and ranges are restricted to the query dataset; the lists of classes and properties are currently database-wide. This is subject to change in the future.

Labeling the extracted model

The output of the service can be joined with other services. This query pairs each extracted class and property with a human-readable label from the Label Service:

prefix stardog: <tag:stardog:api:>

SELECT DISTINCT ?entity ?label {
    SERVICE stardog:model {
        [] stardog:rdf:subject   ?entity ;
           stardog:rdf:predicate ?p ;
           stardog:rdf:object    ?o .
    }
    SERVICE stardog:labels {
        [] stardog:entity ?entity ;
           stardog:label  ?label ;
           stardog:optional true ;
           stardog:limit  1 .
    }
}