Class: RDFGraph

RDFGraph()

Wrap an rdflib graph store so Resource Map code can read and change RDF statements through one small API. Direct rdflib access stays in this class.

Constructor

new RDFGraph()

Since:
  • 2.39.0
Source:

Methods

addStatement(statement) → {RDFGraph}

Add a statement to the graph.
Parameters:
Name Type Description
statement RDFGraphStatement The statement to add.
Source:
Returns:
This graph
Type
RDFGraph

addStatementIfMissing(statement) → {RDFGraph}

Add a statement only when the exact statement is missing.
Parameters:
Name Type Description
statement RDFGraphStatement The statement to add.
Source:
Returns:
This graph
Type
RDFGraph

createIndex() → {Object}

Build generic term indexes from statement subjects and objects.
Source:
Returns:
Generic graph indexes.
Type
Object

dedupeStatements() → {RDFGraph}

Remove duplicate RDF statements from the graph.
Source:
Returns:
This graph
Type
RDFGraph

findStatements(patternopt) → {Array.<RDFGraphStatement>}

Find statements matching the provided pattern.
Parameters:
Name Type Attributes Description
pattern PartialRDFGraphStatement <optional>
Subject, predicate, and/or object values to match.
Source:
Returns:
Matching rdflib statements.
Type
Array.<RDFGraphStatement>

getStatements(optionsopt) → {Array.<RDFGraphStatement>}

Get all statements.
Parameters:
Name Type Attributes Description
options object <optional>
Read options.
Properties
Name Type Attributes Description
copy boolean <optional>
Return a shallow copy.
Source:
Returns:
The graph statements.
Type
Array.<RDFGraphStatement>

hasStatement(patternopt) → {boolean}

Check whether a matching statement exists.
Parameters:
Name Type Attributes Description
pattern PartialRDFGraphStatement <optional>
Subject, predicate, and/or object values to match.
Source:
Returns:
True if at least one statement matches.
Type
boolean

parseXml(xml, baseUri) → {RDFGraph}

Parse RDF/XML into this graph.
Parameters:
Name Type Description
xml string RDF/XML string
baseUri string Parser base URI
Source:
Returns:
This graph
Type
RDFGraph

removeNodeReferences(node) → {RDFGraph}

Remove every statement where the node is subject or object.
Parameters:
Name Type Description
node object The node to remove.
Source:
Returns:
This graph
Type
RDFGraph

removeStatement(statement) → {RDFGraph}

Remove a statement from the graph.
Parameters:
Name Type Description
statement RDFGraphStatement The rdflib statement to remove.
Source:
Returns:
This graph
Type
RDFGraph

removeStatementsMatching(patternopt) → {RDFGraph}

Remove all statements matching the provided pattern.
Parameters:
Name Type Attributes Description
pattern PartialRDFGraphStatement <optional>
Subject, predicate, and/or object values to match.
Source:
Returns:
This graph
Type
RDFGraph

replaceNodeValue(oldValue, newValue) → {RDFGraph}

Replace all references to one URI node with another URI node.
Parameters:
Name Type Description
oldValue string The URI to replace.
newValue string The replacement URI.
Source:
Returns:
This graph
Type
RDFGraph

replaceNodeValues(replacements, optionsopt) → {RDFGraph}

Replace URI subjects and objects, including collection entries, from a replacement map. Predicate URIs stay unchanged.
Parameters:
Name Type Attributes Description
replacements Map.<string, string> Old URI to new URI
options object <optional>
Replacement options
Properties
Name Type Attributes Description
preserveObjectPredicates Array.<string> <optional>
Predicate URIs whose objects stay unchanged, e.g. prov:atLocation. Subjects are still replaced
Source:
Returns:
This graph
Type
RDFGraph

restoreStatements(snapshot) → {RDFGraph}

Replace current statements with a previous statement snapshot.
Parameters:
Name Type Description
snapshot Array.<RDFGraphStatement> Statements to restore.
Source:
Returns:
This graph
Type
RDFGraph

serializeStatementsToXml(statements) → {string}

Serialize statements to RDF/XML.
Parameters:
Name Type Description
statements Array.<RDFGraphStatement> Statements to serialize
Source:
Returns:
RDF/XML
Type
string

buildKey(parts) → {string}

Build a stable serialized key from ordered values converted to strings.
Parameters:
Name Type Description
parts Array.<*> Ordered key parts
Source:
Returns:
Stable serialized key
Type
string

buildStatementKey(statement) → {string}

Build a stable identity key for one RDF statement or statement pattern.
Parameters:
Name Type Description
statement Object RDF statement
Source:
Returns:
Stable statement key
Type
string

buildTermKey(term) → {string}

Build a stable identity key for one RDF term, including literal type.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
Stable term key
Type
string

createBlankNode(idopt) → {object}

Create an RDF blank node. Blank nodes are unnamed resources whose identity only matters inside this graph, often used for nested structures that do not have their own URI or PID.
Parameters:
Name Type Attributes Description
id string <optional>
Optional local blank node identifier
Source:
Returns:
RDF blank node
Type
object

createLiteral(value, languageOrDatatypeopt, datatypeopt) → {object}

Create an RDF literal. Literals are concrete values, such as strings, dates, or numbers, stored as data instead of as links to another resource.
Parameters:
Name Type Attributes Description
value * Literal value
languageOrDatatype string | object <optional>
Language tag or datatype node that describes the literal value, such as `en` or `xsd:string`.
datatype object <optional>
Datatype node
Source:
Returns:
RDF literal
Type
object

createNamedNode(uri) → {object}

Create a named RDF node from a URI. Named nodes are resources with stable identifiers, so two statements using the same URI refer to the same RDF resource.
Parameters:
Name Type Description
uri string Node URI
Source:
Returns:
RDF named node
Type
object

createNamespace(uri) → {function}

Create a namespace helper for building named RDF terms.
Parameters:
Name Type Description
uri string Namespace URI
Source:
Returns:
Namespace function
Type
function

createStatement(subject, predicate, object, whyopt) → {RDFGraphStatement}

Create an RDF statement.
Parameters:
Name Type Attributes Description
subject object Named node or blank node the statement is about
predicate object Named node that identifies the relationship
object object Named node, blank node, or literal value
why object <optional>
Optional graph/context
Source:
Returns:
RDF statement
Type
RDFGraphStatement

dedupeTerms(terms) → {Array.<(NamedNode|BlankNode|Literal)>}

Deduplicate RDF terms by exact RDF term identity.
Parameters:
Name Type Description
terms Array.<(NamedNode|BlankNode|Literal)> Candidate RDF terms
Source:
Returns:
Deduplicated terms
Type
Array.<(NamedNode|BlankNode|Literal)>

getLiteralValue(term) → {*|null}

Read the value stored in a literal term. Return null for named nodes and blank nodes because they identify resources rather than store values.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
Literal value, or null for terms that are not literals.
Type
* | null

isBlankNode(term) → {boolean}

Check whether a term is a blank node.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
True if the term is a blank node
Type
boolean

isLiteral(term) → {boolean}

Check whether a term is a literal node.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
True if the term is a literal node
Type
boolean

isNamedNode(term) → {boolean}

Check whether a term is a named node.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
True if the term is a named node
Type
boolean

isTermOfType(term, type) → {boolean}

Check whether a term is of a specific RDF term type.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
type string RDF term type
Source:
Returns:
True if the term is of the specified type
Type
boolean

RDFGraph(storeopt)

new RDFGraph(storeopt)

Create an RDF graph wrapper
Parameters:
Name Type Attributes Description
store object <optional>
An rdflib graph store
Source:
Throws:
When store does not implement the required graph methods
Type
Error

Methods

addStatement(statement) → {RDFGraph}

Add a statement to the graph.
Parameters:
Name Type Description
statement RDFGraphStatement The statement to add.
Source:
Returns:
This graph
Type
RDFGraph

addStatementIfMissing(statement) → {RDFGraph}

Add a statement only when the exact statement is missing.
Parameters:
Name Type Description
statement RDFGraphStatement The statement to add.
Source:
Returns:
This graph
Type
RDFGraph

createIndex() → {Object}

Build generic term indexes from statement subjects and objects.
Source:
Returns:
Generic graph indexes.
Type
Object

dedupeStatements() → {RDFGraph}

Remove duplicate RDF statements from the graph.
Source:
Returns:
This graph
Type
RDFGraph

findStatements(patternopt) → {Array.<RDFGraphStatement>}

Find statements matching the provided pattern.
Parameters:
Name Type Attributes Description
pattern PartialRDFGraphStatement <optional>
Subject, predicate, and/or object values to match.
Source:
Returns:
Matching rdflib statements.
Type
Array.<RDFGraphStatement>

getStatements(optionsopt) → {Array.<RDFGraphStatement>}

Get all statements.
Parameters:
Name Type Attributes Description
options object <optional>
Read options.
Properties
Name Type Attributes Description
copy boolean <optional>
Return a shallow copy.
Source:
Returns:
The graph statements.
Type
Array.<RDFGraphStatement>

hasStatement(patternopt) → {boolean}

Check whether a matching statement exists.
Parameters:
Name Type Attributes Description
pattern PartialRDFGraphStatement <optional>
Subject, predicate, and/or object values to match.
Source:
Returns:
True if at least one statement matches.
Type
boolean

parseXml(xml, baseUri) → {RDFGraph}

Parse RDF/XML into this graph.
Parameters:
Name Type Description
xml string RDF/XML string
baseUri string Parser base URI
Source:
Returns:
This graph
Type
RDFGraph

removeNodeReferences(node) → {RDFGraph}

Remove every statement where the node is subject or object.
Parameters:
Name Type Description
node object The node to remove.
Source:
Returns:
This graph
Type
RDFGraph

removeStatement(statement) → {RDFGraph}

Remove a statement from the graph.
Parameters:
Name Type Description
statement RDFGraphStatement The rdflib statement to remove.
Source:
Returns:
This graph
Type
RDFGraph

removeStatementsMatching(patternopt) → {RDFGraph}

Remove all statements matching the provided pattern.
Parameters:
Name Type Attributes Description
pattern PartialRDFGraphStatement <optional>
Subject, predicate, and/or object values to match.
Source:
Returns:
This graph
Type
RDFGraph

replaceNodeValue(oldValue, newValue) → {RDFGraph}

Replace all references to one URI node with another URI node.
Parameters:
Name Type Description
oldValue string The URI to replace.
newValue string The replacement URI.
Source:
Returns:
This graph
Type
RDFGraph

replaceNodeValues(replacements, optionsopt) → {RDFGraph}

Replace URI subjects and objects, including collection entries, from a replacement map. Predicate URIs stay unchanged.
Parameters:
Name Type Attributes Description
replacements Map.<string, string> Old URI to new URI
options object <optional>
Replacement options
Properties
Name Type Attributes Description
preserveObjectPredicates Array.<string> <optional>
Predicate URIs whose objects stay unchanged, e.g. prov:atLocation. Subjects are still replaced
Source:
Returns:
This graph
Type
RDFGraph

restoreStatements(snapshot) → {RDFGraph}

Replace current statements with a previous statement snapshot.
Parameters:
Name Type Description
snapshot Array.<RDFGraphStatement> Statements to restore.
Source:
Returns:
This graph
Type
RDFGraph

serializeStatementsToXml(statements) → {string}

Serialize statements to RDF/XML.
Parameters:
Name Type Description
statements Array.<RDFGraphStatement> Statements to serialize
Source:
Returns:
RDF/XML
Type
string

buildKey(parts) → {string}

Build a stable serialized key from ordered values converted to strings.
Parameters:
Name Type Description
parts Array.<*> Ordered key parts
Source:
Returns:
Stable serialized key
Type
string

buildStatementKey(statement) → {string}

Build a stable identity key for one RDF statement or statement pattern.
Parameters:
Name Type Description
statement Object RDF statement
Source:
Returns:
Stable statement key
Type
string

buildTermKey(term) → {string}

Build a stable identity key for one RDF term, including literal type.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
Stable term key
Type
string

createBlankNode(idopt) → {object}

Create an RDF blank node. Blank nodes are unnamed resources whose identity only matters inside this graph, often used for nested structures that do not have their own URI or PID.
Parameters:
Name Type Attributes Description
id string <optional>
Optional local blank node identifier
Source:
Returns:
RDF blank node
Type
object

createLiteral(value, languageOrDatatypeopt, datatypeopt) → {object}

Create an RDF literal. Literals are concrete values, such as strings, dates, or numbers, stored as data instead of as links to another resource.
Parameters:
Name Type Attributes Description
value * Literal value
languageOrDatatype string | object <optional>
Language tag or datatype node that describes the literal value, such as `en` or `xsd:string`.
datatype object <optional>
Datatype node
Source:
Returns:
RDF literal
Type
object

createNamedNode(uri) → {object}

Create a named RDF node from a URI. Named nodes are resources with stable identifiers, so two statements using the same URI refer to the same RDF resource.
Parameters:
Name Type Description
uri string Node URI
Source:
Returns:
RDF named node
Type
object

createNamespace(uri) → {function}

Create a namespace helper for building named RDF terms.
Parameters:
Name Type Description
uri string Namespace URI
Source:
Returns:
Namespace function
Type
function

createStatement(subject, predicate, object, whyopt) → {RDFGraphStatement}

Create an RDF statement.
Parameters:
Name Type Attributes Description
subject object Named node or blank node the statement is about
predicate object Named node that identifies the relationship
object object Named node, blank node, or literal value
why object <optional>
Optional graph/context
Source:
Returns:
RDF statement
Type
RDFGraphStatement

dedupeTerms(terms) → {Array.<(NamedNode|BlankNode|Literal)>}

Deduplicate RDF terms by exact RDF term identity.
Parameters:
Name Type Description
terms Array.<(NamedNode|BlankNode|Literal)> Candidate RDF terms
Source:
Returns:
Deduplicated terms
Type
Array.<(NamedNode|BlankNode|Literal)>

getLiteralValue(term) → {*|null}

Read the value stored in a literal term. Return null for named nodes and blank nodes because they identify resources rather than store values.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
Literal value, or null for terms that are not literals.
Type
* | null

isBlankNode(term) → {boolean}

Check whether a term is a blank node.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
True if the term is a blank node
Type
boolean

isLiteral(term) → {boolean}

Check whether a term is a literal node.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
True if the term is a literal node
Type
boolean

isNamedNode(term) → {boolean}

Check whether a term is a named node.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
Source:
Returns:
True if the term is a named node
Type
boolean

isTermOfType(term, type) → {boolean}

Check whether a term is of a specific RDF term type.
Parameters:
Name Type Description
term NamedNode | BlankNode | Literal | null | undefined RDF term
type string RDF term type
Source:
Returns:
True if the term is of the specified type
Type
boolean