src.dackar.knowledge_graph.KGconstruction

Created on September, 2025

@author: mandd, wangc

Attributes

currentDir

Classes

KG

Class designed to automate and check knowledge graph construction

Functions

stringToDatetimeConverterFlexible(dateString[, formatCode])

Method that convert a string into datetime according to specific format

Module Contents

src.dackar.knowledge_graph.KGconstruction.currentDir[source]
class src.dackar.knowledge_graph.KGconstruction.KG(configFilePath, importFolderPath, uri, pwd, user)[source]

Class designed to automate and check knowledge graph construction

datatypes = ['string', 'integer', 'float', 'floating', 'boolean', 'datetime', 'enum', 'array', 'json_string'][source]
py2neo[source]
graphSchemas[source]
graphMetadata[source]
entityLibrary[source]
predefinedGraphSchemas[source]
resetGraph()[source]

Method designed to reset knowledge graph @ In, None @ Out, None

_crossSchemasCheck(graphSchemas=None)[source]

Method designed to perform a series of checks across the defined schemas @ In, graphSchemas, dict, optional registry of schemas to check; defaults

to the committed session registry (self.graphSchemas)

@ Out, None

_checkSchemaStructure(importedSchema)[source]

Method designed to check importedSchema against self.baseSchema @ In, importedSchema, dict, schema parsed by tomllib from .toml file @ Out, None

_loadAndValidateSchemaFile(tomlFilename)[source]

Parse one TOML schema file and run the per-file checks (base-schema structure and allowed data types). Does not mutate session state. @ In, tomlFilename, string, .toml file containing the schema @ Out, configData, dict, parsed and per-file-validated schema

importGraphSchemas(schemaFiles, crossCheck=True, replace=False)[source]

Method that imports a batch of schemas atomically. Each file is parsed and per-file-validated into a temporary candidate registry; the whole candidate is cross-checked; session state (self.graphSchemas) is replaced only once every check passes, so a failure anywhere leaves the existing registry untouched (no partial load). @ In, schemaFiles, dict, mapping {schemaName: tomlFilename} @ In, crossCheck, bool, run whole-set cross-schema integrity (duplicate

nodes/relation-triples and endpoint resolution) on the candidate before committing. Pass False for a known-partial batch whose endpoints resolve in schemas loaded elsewhere (default True).

@ In, replace, bool, build the candidate from an empty registry rather

than layering on the committed set, so re-loading the same set is idempotent instead of failing on existing names (default False).

@ Out, None

importGraphSchema(graphSchemaName, tomlFilename)[source]

Method that imports new schema contained in a .toml file @ In, graphSchemaName, string, name of the schema to be imported @ In, tomlFilename, string, .toml file contained the new schema @ Out, None

loadPredefinedGraphSchemas()[source]

Method that loads the full curated set of predefined DACKAR schemas and validates cross-schema integrity once the whole set is in memory. The load is atomic (session state is replaced only after the whole set validates) and idempotent (a repeat load rebuilds the same set rather than failing on existing names). @ In, None @ Out, None

removeGraphSchema(graphSchemaName)[source]

Method that removes a single schema from the session registry and revalidates the cross-schema references among the schemas that remain, so the registry is never left with a relation pointing at a node label the removal just deleted. The removal is atomic and symmetric with the import path: session state (self.graphSchemas) is replaced only after the remaining set validates, so a removal that would strand a reference raises and leaves the existing registry untouched. @ In, graphSchemaName, string, name of the schema to remove @ Out, None

_checkSchemaDataTypes(schema)[source]

Method that checks that the datatypes defined in the new schema are part of the allowed data types contained in self.datatypes @ In, schema, dict, schema parsed by tomllib from .toml file @ Out, None

_schemaReturnNodeProperties(nodeLabel)[source]

Method that returns the properties of the node nodeLabel @ In, nodeLabel, string, ID of the node label @ Out, propdf, dataframe, dataframe containing nodeLabel properties

_schemaReturnRelationProperties(relation)[source]

Method that returns the properties of the selected relation @ In, relation, string, ID of the node label @ Out, propdf, dataframe, dataframe containing relation properties

_constructionSchemaStructureValidation(constructionSchema)[source]

Method that validates the structure of constructionSchema @ In, constructionSchema, dict, construction schema @ Out, None

_constructionSchemaValidation(constructionSchema)[source]

Method that validates the constructionSchema against defined schemas @ In, constructionSchema, dict, construction schema @ Out, None

genericWorkflow(data, constructionSchema)[source]

Method designed to importa data into knowledge graph according to constructionSchema @ In, data, pd.dataframe, pandas dataframe containing data to be imported in the knowledge graph @ Out, constructionSchema, dict, dataframe containing relation properties. A construction schema is defined as follows:

constructionSchema = {‘nodes’nodeConstructionSchema,

‘relations’: edgeConstructionSchema}

nodeConstructionSchema = {‘nodeLabel1’: {‘property1’: ‘dataframe.colA’, ‘property2’: ‘dataframe.colB’},

‘nodeLabel2’: {‘property1’: ‘dataframe.colC’}}

edgeConstructionSchema = [{‘source’: {‘nodeLabel1.property1’:’dataframe.col1’},

‘target’: {‘nodeLabel2.property1’:’dataframe.col2’}, ‘type’ : ‘edgeType’, ‘properties’: {‘property1’: ‘dataframe.colAlpha’, ‘property2’: ‘dataframe.colBeta’}}]

_checkDataframeDatatypes(data, constructionSchema)[source]

Method that checks that data elements in data match format specified in the graph schemas @ In, data, pd.dataframe, pandas dataframe containing data to be imported in the knowledge graph @ In, constructionSchema, dict, dataframe containing relation properties @ Out, None

_returnNodePropertyDatatype(nodeID, propID)[source]

Method that returns the allowed type of a specified node property @ In, nodeID, string, specific node label @ In, propID, string, specific node property @ Out, allowedType, string, allowed type of the specified node property

_returnRelationPropertyDatatype(relID, propID)[source]

Method that returns the allowed type of a specified relation property. @ In, relID, string, specific relation @ In, propID, string, specific node property @ Out, allowedType, string, allowed type of the specified relation property

_returnPropertyEnumValues(labelID, propID, isRelation=False)[source]

Method that returns the declared enum_values of a node/relation property, used for enum-membership validation. Returns None when the property declares none (membership then cannot be checked). @ In, labelID, string, node label or relation name @ In, propID, string, property name @ In, isRelation, bool, look up a relation property when True, else a node property @ Out, enumValues, list|None, declared allowed values, or None if absent

_createIteractivePlot(output_file='knowledge_graph_schema_interactive.html')[source]

Method that renders the currently loaded schemas as an interactive HTML graph. @ In, output_file, string, path of the HTML file to write @ Out, None

src.dackar.knowledge_graph.KGconstruction.stringToDatetimeConverterFlexible(dateString, formatCode=None)[source]

Method that convert a string into datetime according to specific format @ In, dateString, string, string containing date @ In, formatCode, string, datetime specific format @ Out, datetimeObject, datetime, datetime object