1515import pandas as pd
1616import os , sys
1717import tomllib
18- import jsonschema
18+ from jsonschema import validate , ValidationError
1919import copy
20+ from pathlib import Path
2021
2122class 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
0 commit comments