Skip to content

GraphSet

GraphSet is the container for all graph entities. It handles entity creation, IRI generation, counter management and entity retrieval.

from oc_ocdm.graph import GraphSet
g_set = GraphSet("https://w3id.org/oc/meta/")
GraphSet(
base_iri: str,
info_dir: str = "",
supplier_prefix: str = "",
wanted_label: bool = True,
custom_counter_handler: CounterHandler | None = None
)
ParameterPurpose
base_iriRoot IRI for all entities (e.g. https://w3id.org/oc/meta/)
info_dirDirectory for counter file persistence. When set, a FilesystemCounterHandler is used
supplier_prefixNumeric prefix identifying the data source within IRIs (e.g. "060")
wanted_labelGenerate rdfs:label triples for new entities
custom_counter_handlerOverride the default counter strategy. See Counter handlers

Each OCDM entity type has a factory method. They all share the same signature:

g_set.add_br(
resp_agent: str | None,
source: str | None = None,
res: str | None = None,
preexisting_graph: SubgraphView | None = None
)
ParameterPurpose
resp_agentIRI of the agent responsible for the change
sourceProvenance source IRI
resUse a specific IRI instead of auto-generating one
preexisting_graphPrior state for diff-based tracking (see Provenance)

When res matches an entity already in the set, the existing entity is returned.

Factory methodReturn typeOCDM class
add_br()BibliographicResourcefabio:Expression
add_ra()ResponsibleAgentfoaf:Agent
add_ar()AgentRolepro:RoleInTime
add_id()Identifierdatacite:Identifier
add_ci()Citationcito:Citation
add_be()BibliographicReferencebiro:BibliographicReference
add_re()ResourceEmbodimentfabio:Manifestation
add_de()DiscourseElementdeo:DiscourseElement
add_an()ReferenceAnnotationoa:Annotation
add_pl()PointerListc4o:SingleLocationPointerList
add_rp()ReferencePointerc4o:InTextReferencePointer

Look up a single entity by IRI:

entity = g_set.get_entity("https://w3id.org/oc/meta/br/0605")

Returns None if the IRI is not in the set.

Typed getters return tuples of all entities of a given type:

all_br: tuple[BibliographicResource, ...] = g_set.get_br()
all_ra: tuple[ResponsibleAgent, ...] = g_set.get_ra()

One getter exists per entity type: get_an(), get_ar(), get_be(), get_br(), get_ci(), get_de(), get_id(), get_pl(), get_ra(), get_re(), get_rp().

get_orphans() returns entities that no other entity in the set references (i.e. they never appear as the object of a triple):

orphans = g_set.get_orphans()

To remove orphan references from a SPARQL triplestore, call remove_orphans_from_triplestore(). It queries the triplestore for entities that reference deleted entities, imports them into the GraphSet, and removes those references locally. The triplestore itself is updated when you later call store_all() or upload_all():

g_set.remove_orphans_from_triplestore("http://localhost:9999/sparql", resp_agent)

After generating provenance and storing data, call commit_changes() to reset change tracking. See Provenance for details.

g_set.commit_changes()