Bases: TripleStoreManager
Filesystem-based implementation of triple store management.
This class provides a concrete implementation of triple store management
using the local filesystem for storage. It reads and writes ontologies
and facts as Turtle (.ttl) files in specified directories.
The manager supports:
- Loading ontologies from a dedicated ontology directory
- Storing ontologies with versioned filenames
- Storing facts with customizable filenames based on specifications
- Error handling for file operations
Attributes:
| Name |
Type |
Description |
working_directory |
Path | None
|
Path to the working directory for storing data.
|
ontology_path |
Path | None
|
Optional path to the ontology directory for loading ontologies.
|
Source code in ontocast/tool/triple_manager/filesystem_manager.py
| class FilesystemTripleStoreManager(TripleStoreManager):
"""Filesystem-based implementation of triple store management.
This class provides a concrete implementation of triple store management
using the local filesystem for storage. It reads and writes ontologies
and facts as Turtle (.ttl) files in specified directories.
The manager supports:
- Loading ontologies from a dedicated ontology directory
- Storing ontologies with versioned filenames
- Storing facts with customizable filenames based on specifications
- Error handling for file operations
Attributes:
working_directory: Path to the working directory for storing data.
ontology_path: Optional path to the ontology directory for loading ontologies.
"""
working_directory: pathlib.Path | None
ontology_path: pathlib.Path | None
def __init__(self, **kwargs):
"""Initialize the filesystem triple store manager.
This method sets up the filesystem manager with the specified
working and ontology directories.
Args:
**kwargs: Additional keyword arguments passed to the parent class.
working_directory: Path to the working directory for storing data.
ontology_path: Path to the ontology directory for loading ontologies.
Example:
>>> manager = FilesystemTripleStoreManager(
... working_directory="/path/to/work",
... ontology_path="/path/to/ontologies"
... )
"""
super().__init__(**kwargs)
def fetch_ontologies(self) -> list[Ontology]:
"""Fetch all available ontologies from the filesystem.
This method scans the ontology directory for Turtle (.ttl) files
and loads each one as an Ontology object. Files are processed
in sorted order for consistent results.
Returns:
list[Ontology]: List of all ontologies found in the ontology directory.
Example:
>>> ontologies = manager.fetch_ontologies()
>>> for onto in ontologies:
... print(f"Loaded ontology: {onto.ontology_id}")
"""
ontologies = []
if self.ontology_path is not None:
sorted_files = sorted(self.ontology_path.glob("*.ttl"))
for fname in sorted_files:
try:
ontology = Ontology.from_file(fname)
ontologies.append(ontology)
logger.debug(f"Successfully loaded ontology from {fname}")
except Exception as e:
logger.error(f"Failed to load ontology {fname}: {str(e)}")
return ontologies
def serialize_graph(self, graph: Graph, **kwargs) -> bool | None:
"""Store an RDF graph in the filesystem.
This method stores the given RDF graph as a Turtle file in the
working directory. The filename is generated based on the graph_uri
parameter or defaults to "current.ttl".
Args:
graph: The RDF graph to store.
fname: str
Example:
>>> graph = RDFGraph()
>>> manager.serialize_graph(graph)
# Creates: working_directory/current.ttl
>>> manager.serialize_graph(graph, fname="facts_abc.ttl")
"""
if self.working_directory is None:
return
fname: str = kwargs.pop("fname")
dump_clean_graph: bool = kwargs.pop("dump_clean_graph", False)
output_path = self.working_directory / fname
self._write_turtle(graph, output_path)
logger.info(f"Graph saved to {output_path}")
if dump_clean_graph:
clean_graph = self.strip_provenance(graph)
clean_output_path = self.working_directory / self._with_suffix(
fname, "clean"
)
self._write_turtle(
clean_graph,
clean_output_path,
serialization_format="turtle",
)
logger.info(f"Graph without provenance saved to {clean_output_path}")
@staticmethod
def _with_suffix(fname: str, suffix: str) -> str:
path = pathlib.Path(fname)
return f"{path.stem}_{suffix}{path.suffix}"
@staticmethod
def _write_turtle(
graph: Graph,
output_path: pathlib.Path,
*,
serialization_format: str | None = None,
) -> None:
# Use longturtle where available (non-oxigraph) for easier local inspection.
if serialization_format is None:
is_oxigraph = (
isinstance(graph, RDFGraph)
and type(graph.store).__name__ == "OxigraphStore"
)
serialization_format = "turtle" if is_oxigraph else "longturtle"
graph.serialize(format=serialization_format, destination=output_path)
def serialize(self, o: Ontology | RDFGraph, graph_uri: str | None = None): # type: ignore[override]
if isinstance(o, Ontology):
graph = o.graph
fname = f"ontology_{o.ontology_id}_{o.version}.ttl"
dump_clean_graph = False
elif isinstance(o, RDFGraph):
graph = o
if graph_uri:
s = graph_uri.split("/")[-2:]
s = "_".join([x for x in s if x])
fname = f"facts_{s}.ttl"
else:
fname = "facts_default.ttl"
dump_clean_graph = True
else:
raise TypeError(f"unsupported obj of type {type(o)} received")
self.serialize_graph(
graph=graph,
fname=fname,
dump_clean_graph=dump_clean_graph,
)
async def clean(self) -> None:
"""Clean/flush all data from the filesystem triple store.
This method deletes all Turtle (.ttl) files from both the working
directory and the ontology directory.
Warning: This operation is irreversible and will delete all data.
Raises:
Exception: If the cleanup operation fails.
"""
logger.warning("clean method not implemented for FilesystemTripleStoreManager")
|
Initialize the filesystem triple store manager.
This method sets up the filesystem manager with the specified
working and ontology directories.
Parameters:
| Name |
Type |
Description |
Default |
**kwargs
|
|
Additional keyword arguments passed to the parent class.
working_directory: Path to the working directory for storing data.
ontology_path: Path to the ontology directory for loading ontologies.
|
{}
|
Example
manager = FilesystemTripleStoreManager(
... working_directory="/path/to/work",
... ontology_path="/path/to/ontologies"
... )
Source code in ontocast/tool/triple_manager/filesystem_manager.py
| def __init__(self, **kwargs):
"""Initialize the filesystem triple store manager.
This method sets up the filesystem manager with the specified
working and ontology directories.
Args:
**kwargs: Additional keyword arguments passed to the parent class.
working_directory: Path to the working directory for storing data.
ontology_path: Path to the ontology directory for loading ontologies.
Example:
>>> manager = FilesystemTripleStoreManager(
... working_directory="/path/to/work",
... ontology_path="/path/to/ontologies"
... )
"""
super().__init__(**kwargs)
|
Clean/flush all data from the filesystem triple store.
This method deletes all Turtle (.ttl) files from both the working
directory and the ontology directory.
Warning: This operation is irreversible and will delete all data.
Raises:
| Type |
Description |
Exception
|
If the cleanup operation fails.
|
Source code in ontocast/tool/triple_manager/filesystem_manager.py
| async def clean(self) -> None:
"""Clean/flush all data from the filesystem triple store.
This method deletes all Turtle (.ttl) files from both the working
directory and the ontology directory.
Warning: This operation is irreversible and will delete all data.
Raises:
Exception: If the cleanup operation fails.
"""
logger.warning("clean method not implemented for FilesystemTripleStoreManager")
|
Fetch all available ontologies from the filesystem.
This method scans the ontology directory for Turtle (.ttl) files
and loads each one as an Ontology object. Files are processed
in sorted order for consistent results.
Returns:
| Type |
Description |
list[Ontology]
|
list[Ontology]: List of all ontologies found in the ontology directory.
|
Example
ontologies = manager.fetch_ontologies()
for onto in ontologies:
... print(f"Loaded ontology: {onto.ontology_id}")
Source code in ontocast/tool/triple_manager/filesystem_manager.py
| def fetch_ontologies(self) -> list[Ontology]:
"""Fetch all available ontologies from the filesystem.
This method scans the ontology directory for Turtle (.ttl) files
and loads each one as an Ontology object. Files are processed
in sorted order for consistent results.
Returns:
list[Ontology]: List of all ontologies found in the ontology directory.
Example:
>>> ontologies = manager.fetch_ontologies()
>>> for onto in ontologies:
... print(f"Loaded ontology: {onto.ontology_id}")
"""
ontologies = []
if self.ontology_path is not None:
sorted_files = sorted(self.ontology_path.glob("*.ttl"))
for fname in sorted_files:
try:
ontology = Ontology.from_file(fname)
ontologies.append(ontology)
logger.debug(f"Successfully loaded ontology from {fname}")
except Exception as e:
logger.error(f"Failed to load ontology {fname}: {str(e)}")
return ontologies
|
Store an RDF graph in the filesystem.
This method stores the given RDF graph as a Turtle file in the
working directory. The filename is generated based on the graph_uri
parameter or defaults to "current.ttl".
Parameters:
| Name |
Type |
Description |
Default |
graph
|
Graph
|
|
required
|
fname
|
|
|
required
|
Example
graph = RDFGraph()
manager.serialize_graph(graph)
manager.serialize_graph(graph, fname="facts_abc.ttl")
Source code in ontocast/tool/triple_manager/filesystem_manager.py
| def serialize_graph(self, graph: Graph, **kwargs) -> bool | None:
"""Store an RDF graph in the filesystem.
This method stores the given RDF graph as a Turtle file in the
working directory. The filename is generated based on the graph_uri
parameter or defaults to "current.ttl".
Args:
graph: The RDF graph to store.
fname: str
Example:
>>> graph = RDFGraph()
>>> manager.serialize_graph(graph)
# Creates: working_directory/current.ttl
>>> manager.serialize_graph(graph, fname="facts_abc.ttl")
"""
if self.working_directory is None:
return
fname: str = kwargs.pop("fname")
dump_clean_graph: bool = kwargs.pop("dump_clean_graph", False)
output_path = self.working_directory / fname
self._write_turtle(graph, output_path)
logger.info(f"Graph saved to {output_path}")
if dump_clean_graph:
clean_graph = self.strip_provenance(graph)
clean_output_path = self.working_directory / self._with_suffix(
fname, "clean"
)
self._write_turtle(
clean_graph,
clean_output_path,
serialization_format="turtle",
)
logger.info(f"Graph without provenance saved to {clean_output_path}")
|