File I/O
File-based import/export utilities for KnowledgeComplex.
Functions:
| Name | Description |
|---|---|
save_graph Serialize the instance graph to a file. |
|
load_graph Parse a file into the instance graph |
|
dump_graph Return the instance graph as a string in a given format. |
|
Design notes
- Turtle (
.ttl) is the default format — human-readable and consistent with the ontology/shapes patterns used throughout the codebase. - All load functions are additive: they call
graph.parse()which adds triples to the existing graph. Load into a freshKnowledgeComplexfor a clean restore. - No TriG (
.trig) or N-Quads (.nq) —KnowledgeComplexuses a plainrdflib.Graph, not aConjunctiveGraph.
Adapted from discourse_graph.io (multi-agent-dg).
save_graph(kc, path, format='turtle')
Serialize kc._instance_graph to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kc
|
'KnowledgeComplex'
|
The |
required |
path
|
'Path | str'
|
Destination file path. The file is created or overwritten. |
required |
format
|
str
|
rdflib serialization format string. Defaults to |
'turtle'
|
Source code in knowledgecomplex/io.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
load_graph(kc, path, format=None, validate=False)
Parse a file into kc._instance_graph (additive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kc
|
'KnowledgeComplex'
|
The |
required |
path
|
'Path | str'
|
Source file path. |
required |
format
|
Optional[str]
|
rdflib serialization format string. When |
None
|
validate
|
bool
|
If |
False
|
Notes
This operation is additive: existing triples in the instance graph
are retained. For a clean restore, load into a freshly constructed
KnowledgeComplex.
The instance graph includes TBox (ontology) triples. Loading a file that was saved from a KC with the same schema is harmless — rdflib deduplicates triples. Loading data from a different schema will merge ontologies; the caller is responsible for schema compatibility.
Source code in knowledgecomplex/io.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
dump_graph(kc, format='turtle')
Return the instance graph as a string in the requested format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kc
|
'KnowledgeComplex'
|
The |
required |
format
|
str
|
rdflib serialization format string. Defaults to |
'turtle'
|
Returns:
| Type | Description |
|---|---|
str
|
The serialized graph. |
Source code in knowledgecomplex/io.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |