Class QueryService

java.lang.Object
ai.chatur.cortex.core.query.QueryService

public class QueryService extends Object
Answers questions about the knowledge graph.

All lookups run against the inference dataset, so results include both approved assertions and statements derived from them by the reasoner. Provenance, which lives in the provenance graph of the assertions dataset and is excluded from inference, is looked up there.

  • Constructor Summary

    Constructors
    Constructor
    Description
    QueryService(org.apache.jena.query.Dataset inferences, org.apache.jena.query.Dataset assertions, org.apache.jena.ontapi.model.OntModel ontModel, UsageService usageService)
    Creates the service.
  • Method Summary

    Modifier and Type
    Method
    Description
    Counts the instances of every class over the inference dataset, so a resource typed only by a subclass still counts towards the superclasses the reasoner derives for it.
    List<ai.chatur.cortex.ProvenancedStatement>
    Returns everything known about a resource, with the origin of each statement and the creation timestamp of those where provenance was recorded.
    List<ai.chatur.cortex.Term>
    Returns the known instances of an ontology class.
    query(String sparql)
    Runs a SPARQL query against the knowledge graph.
    search(String text)
    Finds resources by full-text search over their labels, comments, and SKOS annotations.
    List<ai.chatur.cortex.SearchResult>
    Searches the full-text index and returns the matching subjects.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • QueryService

      public QueryService(org.apache.jena.query.Dataset inferences, org.apache.jena.query.Dataset assertions, org.apache.jena.ontapi.model.OntModel ontModel, UsageService usageService)
      Creates the service.
      Parameters:
      inferences - the dataset holding the assertions enriched by inference
      assertions - the dataset holding the approved assertions and their provenance
      ontModel - the ontology model, used to resolve classes and shorten URIs
      usageService - view counts, which weight the ranking of search results
  • Method Details

    • getInstances

      public List<ai.chatur.cortex.Term> getInstances(String type)
      Returns the known instances of an ontology class.
      Parameters:
      type - the local name of the ontology class
      Returns:
      the instance identifiers sorted alphabetically, empty if the class is unknown
    • countInstances

      public Map<String,Long> countInstances()
      Counts the instances of every class over the inference dataset, so a resource typed only by a subclass still counts towards the superclasses the reasoner derives for it.
      Returns:
      the number of distinct instances keyed by class URI; classes with no instances are absent
    • describe

      public List<ai.chatur.cortex.ProvenancedStatement> describe(String id)
      Returns everything known about a resource, with the origin of each statement and the creation timestamp of those where provenance was recorded.

      The statements come from the inference dataset; their creation timestamps come from the provenance graph of the assertions dataset. Each statement is returned once: statements carrying several provenance records — for example because they were asserted by more than one ingestion — report their earliest creation timestamp.

      The inference dataset holds more than the reasoner's conclusions: the reasoner is bound to the ontology as its schema, so the ontology's own axioms are materialized alongside them. Those have no provenance either, so absence of a timestamp alone does not make a statement inferred — the origin distinguishes them by looking the statement up in the ontology.

      Parameters:
      id - the identifier of the resource within the Cortex namespace, or a full URI
      Returns:
      the statements about the resource, sorted by predicate
    • query

      public String query(String sparql)
      Runs a SPARQL query against the knowledge graph.
      Parameters:
      sparql - a SPARQL SELECT, ASK, or DESCRIBE query
      Returns:
      SELECT and ASK results formatted as text, DESCRIBE results serialized in Turtle syntax, or null for other query types
    • search

      public String search(String text)
      Finds resources by full-text search over their labels, comments, and SKOS annotations.

      Every term of the input is required and matched exactly after analysis, so recall comes from the analyzer's stemming (reports finds report) rather than edit-distance fuzz.

      Parameters:
      text - the text to search for
      Returns:
      the matches with their relevance scores, formatted as text and ranked best first
    • searchSubjects

      public List<ai.chatur.cortex.SearchResult> searchSubjects(String text)
      Searches the full-text index and returns the matching subjects.

      Every term is required and must occur in the same indexed literal, so adding a word narrows the results. Terms are matched exactly after analysis; the analyzer's stemming is what lets spelling variations of the same word still find their target.

      The index holds one document per literal, so a resource matching on both its label and its comment produces several hits; each resource is reported once, keeping its best-scoring match.

      Parameters:
      text - the text to search for
      Returns:
      the matching subjects ranked best first, empty if nothing matches