Skip to content

Commit cb9421d

Browse files
committed
edits
1 parent 54cdf11 commit cb9421d

File tree

3 files changed

+93
-25
lines changed

3 files changed

+93
-25
lines changed

src/dackar/knowledge_graph/KGconstruction.py

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import pandas as pd
1616
import os, sys
1717
import tomllib
18-
import jsonschema
18+
from jsonschema import validate, ValidationError
1919
import copy
20+
from pathlib import Path
2021

2122
class KG:
2223
#def __init__(self, config_file_path, import_folder_path, uri, pwd, user, processedDataFolder):
@@ -32,6 +33,34 @@ def __init__(self, config_file_path, uri, pwd, user):
3233

3334
self.graphSchemas = {}
3435

36+
self.schemaSchema = {"type": "object",
37+
"properties": {"title" : {"type": "string", "description": "Data object that is target of the schema"},
38+
"version" : {"type": "number", "description": "Development version of the schema"},
39+
"node" : {"description": "Data element encapsulated in the node",
40+
"type": "object",
41+
"properties" : {"node_description": {"type": "string", "description": "Type of relationship encapsulated in the relation between two nodes"},
42+
"node_properties": {"type": "array",
43+
"description": "Allowed properties associate with the node",
44+
"items": {"type": "object",
45+
"properties": {"name" : {"type": "string", "description": "Name of the node property"},
46+
"type" : {"type": "string", "description": "Type of the node property"},
47+
"optional": {"type": "boolean", "description": "Specifies if this property is required or not"}},
48+
"required":["name","type","optional"],
49+
}
50+
}
51+
},
52+
"required":["node_description"]
53+
},
54+
"relation": {"description": "Data element encapsulated in the edge",
55+
"type": "object",
56+
"properties" : {"relation_description": {"type": "string", "description": "Type of relationship encapsulated in the relation between two nodes"},
57+
"from_entity": {"type": "string", "description": "Label of the departure node"},
58+
"to_entity" : {"type": "string", "description": "Label of the arrival node"}},
59+
"required":["relation_description","from_entity","to_entity"],
60+
}
61+
},
62+
"required":["title"]}
63+
3564
self.predefinedGraphSchemas = {'conditionReportSchema' : 'conditionReportSchema.toml',
3665
'customMbseSchema' : 'customMbseSchema.toml',
3766
'monitoringSystemSchema' : 'monitoringSystemSchema.toml',
@@ -40,16 +69,48 @@ def __init__(self, config_file_path, uri, pwd, user):
4069

4170
def resetGraph(self):
4271
self.py2neo.reset()
72+
73+
def checkSchemaStructure(self, importedSchema):
74+
try:
75+
validate(instance=importedSchema, schema=self.schemaSchema)
76+
print("TOML content is valid against the schema.")
77+
except tomllib.TOMLDecodeError as e:
78+
print(f"TOML syntax error: {e}")
79+
except ValidationError as e:
80+
print(f"TOML schema validation error: {e.message}")
4381

44-
def importGraphSchema(graphSchemaName, TomlFilename):
82+
def importGraphSchema(self, graphSchemaName, tomlFilename):
83+
config_path = Path(tomlFilename)
84+
if not config_path.exists():
85+
raise FileNotFoundError(f"Configuration file not found: {tomlFilename}")
86+
87+
with open(config_path, 'rb') as f:
88+
config_data = tomllib.load(f)
89+
90+
self.checkSchemaStructure(config_data)
91+
92+
# Check structure of imported graphSchema
93+
for node in config_data['node'].keys():
94+
pass
4595
# Check imported graphSchema against self.graphSchemas
96+
for node in config_data['node'].keys():
97+
for schema in self.graphSchemas:
98+
if node in schema['node'].keys():
99+
print('Node ' + str(node) + ' defined in the new schema is already defined in the exisiting schema ' + str(schema))
46100

47-
# Add graphSchema to self.graphSchemas
101+
for relation in config_data['relation'].keys():
102+
for schema in self.graphSchemas:
103+
if relation in schema['relation'].keys():
104+
print('Relation ' + str(node) + ' defined in the new schema is already defined in the exisiting schema ' + str(schema))
48105

49-
pass
106+
self.graphSchemas[graphSchemaName] = config_data
107+
return config_data
50108

51109
def schemaValidation(self, constructionSchema):
52-
pass
110+
for node in constructionSchema['nodes']:
111+
for schema in self.graphSchemas:
112+
if node in schema['node'].keys():
113+
pass
53114

54115
def genericWorkflow(self, data, constructionSchema):
55116
# Check constructionSchema against self.graphSchemas
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
title = "Condition Report Graph Schema"
2-
version = "1.0"
2+
version = 1.0
33

44
# ====================
5-
# Node Entities
5+
# Node
66
# ====================
77

8-
[entities.condition_report]
9-
description = "Represents a structured report documenting an observed abnormal event or condition within
10-
a plant or operational environment. This entity captures descriptive information provided
11-
by plant staff, including the nature, context, and potential implications of the anomaly."
12-
properties = [
13-
{ name = "date", type = "string"},
14-
{ name = "ID", type = "string"},
8+
[node.condition_report]
9+
node_description = """Represents a structured report documenting an observed abnormal event or condition within
10+
a plant or operational environment. This entity captures descriptive information provided
11+
by plant staff, including the nature, context, and potential implications of the anomaly."""
12+
node_properties = [
13+
{name = "date", type = "string", optional = false},
14+
{name = "ID", type = "string", optional = false},
15+
]
16+
17+
[node.work_order]
18+
description = """[]"""
19+
edge_properties = [
20+
{name = "date", type = "string", optional = false},
21+
{name = "ID", type = "string", optional = false},
1522
]
1623

1724
# ====================
18-
# Relationships
25+
# Relations
1926
# ====================
2027

21-
[relationships.refers]
22-
description = "Indicates that a generalized nuclear-relevant entity such as materials, chemical elements
23-
and compounds, chemical reactions, failure modes, degradation mechanisms, and components
24-
across electrical, hydraulic, and mechanical systems has been explicitly referenced within
25-
a condition report"
28+
[relation.refers]
29+
relation_description = """Indicates that a generalized nuclear-relevant entity such as materials, chemical elements
30+
and compounds, chemical reactions, failure modes, degradation mechanisms, and components
31+
across electrical, hydraulic, and mechanical systems has been explicitly referenced within
32+
a condition report"""
2633
from_entity = "condition_report"
2734
to_entity = "nuclear_entity"
2835

2936

30-
[relationships.mentions]
31-
description = "Indicates that a specific system, equipment, or component contained in the provided MBSE model
32-
has been explicitly referenced within a condition report, establishing a contextual link between
33-
the reported abnormal event or condition and the physical or functional asset involved."
37+
[relation.mentions]
38+
relation_description = """Indicates that a specific system, equipment, or component contained in the provided MBSE model
39+
has been explicitly referenced within a condition report, establishing a contextual link between
40+
the reported abnormal event or condition and the physical or functional asset involved."""
3441
from_entity = "condition_report"
3542
to_entity = "mbse_entity"
3643

src/dackar/knowledge_graph/schemas/customMbseSchema.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version = "1.0"
99
description = "Represents a specific MBSE component, equipment, or system"
1010
properties = [
1111
{ name = "label", type = "string"},
12-
{ name = "ID", type = "string", optional = true },
12+
{ name = "ID", type = "string", optional = true},
1313
]
1414

1515
# ====================

0 commit comments

Comments
 (0)