Models

Knowledge Graph

The KnowledgeGraph model represents a knowledge graph with high-fidelity RDF data.

See the documentation for the Knowledge Graph concept.

class KnowledgeGraph(BaseModel):
    entities: List[Entity] = Field(default_factory=list)
    relations: List[Relation] = Field(default_factory=list)
    namespaces: Dict[str, str] = Field(default_factory=dict)
    ttl_content: Optional[str] = None
    cql_content: Optional[str] = None
AttributeTypeDefaultDescription
entitiesList[Entity][]A list of Entity objects within the knowledge graph.
relationsList[Relation][]A list of Relation objects within the knowledge graph.
namespacesDict[str, str]{}A dictionary mapping namespace prefixes to their URIs.
ttl_contentOptional[str]NoneThe Turtle (TTL) string representation of the graph, if available.
cql_contentOptional[str]NoneThe Cypher Query Language (CQL) string representation of the graph, if available.

Methods

The KnowledgeGraph class provides several convenience methods for serialization, saving to databases, and interlinking. These methods are powered by the various service classes and are typically injected into the KnowledgeGraph instance upon creation.

to_ttl

Returns the Turtle (TTL) string representation of the knowledge graph.

def to_ttl() -> str:

Returns: str - The TTL string.

to_cql

Returns the Cypher (CQL) string representation of the knowledge graph.

def to_cql(strip_prefixes: bool = True) -> str:
ParameterTypeDefaultDescription
strip_prefixesboolTrueStrips namespace prefixes for a cleaner schema.

Returns: str - The CQL string.

to_cypher_statements

Returns a list of Cypher (CQL) statements that represent the knowledge graph.

def to_cypher_statements(strip_prefixes: bool = True) -> List[str]:
ParameterTypeDefaultDescription
strip_prefixesboolTrueStrips namespace prefixes for cleaner statements.

Returns: List[str] - A list of Cypher statements.

to_rdflib

Converts the knowledge graph to an rdflib.Graph object.

def to_rdflib() -> "Graph":

Returns: rdflib.Graph - The converted graph object.

to_json

Converts the knowledge graph to a JSON serializable dictionary.

def to_json() -> dict:

Returns: dict - A JSON serializable dictionary.

save_to_neo4j

Synchronously saves the CQL content of the knowledge graph to a Neo4j database.

def save_to_neo4j(strip_prefixes: bool = True):
ParameterTypeDefaultDescription
strip_prefixesboolTrueStrips namespace prefixes for a cleaner schema.

save_to_neo4j_async

Asynchronously saves the CQL content of the knowledge graph to a Neo4j database.

async def save_to_neo4j_async(strip_prefixes: bool = True):
ParameterTypeDefaultDescription
strip_prefixesboolTrueStrips namespace prefixes for a cleaner schema.

save_to_falkordb

Synchronously saves the CQL content of the knowledge graph to a FalkorDB database.

def save_to_falkordb(strip_prefixes: bool = True):
ParameterTypeDefaultDescription
strip_prefixesboolTrueStrips namespace prefixes for a cleaner schema.

save_to_falkordb_async

Asynchronously saves the CQL content of the knowledge graph to a FalkorDB database.

async def save_to_falkordb_async(strip_prefixes: bool = True):
ParameterTypeDefaultDescription
strip_prefixesboolTrueStrips namespace prefixes for a cleaner schema.

save_cql

Saves the knowledge graph to a Cypher (CQL) file.

def save_cql(file_path: str, strip_prefixes: bool = True):
ParameterTypeDefaultDescription
file_pathstrThe path to save the CQL file to.
strip_prefixesboolTrueStrips namespace prefixes for a cleaner schema.

save_ttl

Saves the knowledge graph to a Turtle (TTL) file.

def save_ttl(file_path: str):
ParameterTypeDescription
file_pathstrThe path to save the TTL file to.