@rdfjs/wrapper
    Preparing search index...

    Class TermWrapper

    TermWrapper is one of the two central constructs of this library. It is the base class of all models that represent a mapping from RDF to JavaScript. It is an RDF/JS term (or node) that also has a reference to both the dataset (or graph) that is the context of (i.e. contains) the term and to a factory that can be used to create additional terms.

    This class contains all members of all types derived from Term. This is so instances of this class can be used as instances of any term type. See relevant example.

    The basic pattern of working with this class is to simply extend it and add accessors and mutators (both optional) that expose data from the underlying RDF:

    class SomeClass extends TermWrapper {
    get someProperty(): string {
    return RequiredFrom.subjectPredicate(this, "http://example.com/someProperty", LiteralAs.string)
    }

    set someProperty(value: string) {
    RequiredAs.object(this, "http://example.com/someProperty", value, LiteralFrom.string)
    }
    }

    Assume the following RDF data:

    BASE <http://example.com/>

    <someSubject> <someProperty> "some value" .

    We can work with this data in JavaScript and TypeScript as follows:

    const dataset: DatasetCore // which has the RDF above loaded
    const instance = new SomeClass("http://example.com/someSubject", dataset, DataFactory)

    const value = instance.someProperty // contains "some value"

    instance.someProperty = "some other value" // underlying RDF is now <someSubject> <someProperty> "some other value" .

    Since this class implements all members of all term types (named nodes, literals, blank nodes etc.), it can be cast to an RDF/JS Term:

    let instance: TermWrapper

    // Our instance cast as Term
    const term = instance as Term

    Instances of this class can be used anywhere an RDF/JS Term can be used, which includes creating quads:

    let instance: TermWrapper
    let factory: DataFactory
    const predicate = factory.namedNode("http://example.com/p")
    const object = factory.literal("o")

    // Our instance used as subject when creating a quad
    factory.quad(instance as Quad_Subject, predicate, object)

    Instances of this class can be used anywhere an RDF/JS Term can be used, which includes matching quads in a dataset:

    let instance: TermWrapper
    let dataset: DatasetCore

    // Our instance used as subject when matching statements in a dataset
    dataset.match(instance as Term)

    Implements

    • IRdfJsTerm
    Index

    Constructors

    Accessors

    • get "[toStringTag]"(): string

      The well-known property containing a string that represents the type of this object.

      Returns string

    • get dataset(): DatasetCore

      The dataset that contains this term.

      This accessor provides access to the underlying RDF graph that is the containing context of a node mapped to JavaScript by instances of this class.

      Returns DatasetCore

      RDF/JS, like many other RDF frameworks, keeps terms and datasets separate. This means that terms do not hold a reference to a dataset they reside in (or were found in). This, in turn, means that a dataset must always be available, separate from the term, if either changes to the underlying data or further traversal of the underlying data is called for. In an object-oriented context however, where property chaining is idiomatic (i.e. instance.property1.property2), there is no way to supply the dataset when dereferencing a link in the chain.

      This property solves the problem by keeping a reference to the dataset.

      Using the dataset to modify information related to this node in the underlying data:

      class Book extends TermWrapper {
      set author(value: string) {
      const subject = this as Quad_Subject
      const predicate = this.factory.namedNode("http://example.com/author")
      const object = this.factory.literal(value)
      const oldAuthors = this.factory.quad(subject, predicate)
      const newAuthor = this.factory.quad(subject, predicate, object)

      this.dataset.delete(oldAuthors)
      this.dataset.add(newAuthor)
      }
      }

      Note: The above example operates on a low level to explain this property. Library users are more likely to interact with OptionalAs, RequiredAs and LiteralFrom for a better experience.

      Using the dataset to modify data related to this node in the underlying data:

      class Container extends TermWrapper {
      add(something: string) {
      const subject = this as Quad_Subject
      const predicate = this.factory.namedNode("http://example.com/contains")
      const object = this.factory.literal(something)
      const quad = this.factory.quad(subject, predicate, object)

      this.dataset.add(quad)
      }
      }
    • get factory(): DataFactory

      The data factory this instance was instantiated with. A collection of methods that can be used to create terms by this or subsequent wrappers.

      Returns DataFactory

      Using the factory to create a literal term from the current date and time:

      class Calendar extends TermWrapper {
      get currentDate(): Literal {
      const date = new Date().toISOString()
      const xsdDateTime = this.factory.namedNode("http://www.w3.org/2001/XMLSchema#dateTime")

      return this.factory.literal(date, xsdDateTime)
      }
      }

      Using the factory to create a quad:

      class Container extends TermWrapper {
      add(something: string) {
      const subject = this as Quad_Subject
      const predicate = this.factory.namedNode("http://example.com/contains")
      const object = this.factory.literal(something)
      const quad = this.factory.quad(subject, predicate, object)

      this.dataset.add(quad)
      }
      }

    Implementation of RDF/JS Term

    • get termType(): | "NamedNode"
      | "BlankNode"
      | "Literal"
      | "Variable"
      | "DefaultGraph"
      | "Quad"

      Returns "NamedNode" | "BlankNode" | "Literal" | "Variable" | "DefaultGraph" | "Quad"