Schema Operations
This section documents the schema operations functions used throughout the NDF Studio backend.
backend.core.schema_ops
NDF Studio Schema Operations
This module provides functions for managing schema definitions in the NDF Studio backend. It handles attribute types, relation types, and node types, including loading, saving, validation, and CNL (Controlled Natural Language) parsing for schema definitions.
Functions:
| Name | Description |
|---|---|
ordered_schema_dict |
Create an ordered dictionary with specified key order |
ensure_schema_file |
Ensure a schema file exists with default data |
load_schema |
Load schema data from file with default fallback |
validate_schema_entry |
Validate that a schema entry has required keys |
save_schema |
Save schema data to file with proper formatting |
load_schema_json |
Load JSON schema with default data creation |
create_attribute_type_from_dict |
Create attribute type from dictionary data |
create_relation_type_from_dict |
Create relation type from dictionary data |
parse_cnl_block |
Parse CNL block for schema definitions |
filter_used_schema |
Filter schema to only include used types |
Functions
ordered_schema_dict(entry: dict, key_order: list[str]) -> OrderedDict
Create an ordered dictionary with specified key order.
This function creates an OrderedDict from a regular dictionary, ensuring that keys appear in the specified order. Keys not in the order list are appended at the end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
dict
|
Input dictionary to reorder |
required |
key_order
|
list[str]
|
List of keys in desired order |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OrderedDict |
OrderedDict
|
Dictionary with keys in specified order |
Example
entry = {"description": "A type", "name": "MyType", "extra": "value"} ordered = ordered_schema_dict(entry, ["name", "description"]) list(ordered.keys()) ['name', 'description', 'extra']
Source code in backend/core/schema_ops.py
ensure_schema_file(file_name, default_data)
Ensure a schema file exists with default data.
This function checks if a schema file exists and creates it with default data if it doesn't exist. It also ensures the schema directory exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Name of the schema file |
required |
default_data
|
Default data to write if file doesn't exist |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
Path to the schema file |
Example
ensure_schema_file("attribute_types.json", []) 'graph_data/global/attribute_types.json'
Source code in backend/core/schema_ops.py
load_schema(file_name, default_data)
Load schema data from file with default fallback.
This function loads schema data from a file. If the file doesn't exist, it creates it with the default data and returns the default data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Name of the schema file |
required |
default_data
|
Default data to use if file doesn't exist |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
Schema data from file or default data |
Example
schema = load_schema("attribute_types.json", []) isinstance(schema, list) True
Source code in backend/core/schema_ops.py
validate_schema_entry(entry: dict, required_keys: list[str], file_name: str) -> None
Validate that a schema entry has required keys.
This function checks if a schema entry contains all required keys. If any required keys are missing, it raises a ValueError with details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
dict
|
Schema entry to validate |
required |
required_keys
|
list[str]
|
List of required keys |
required |
file_name
|
str
|
Name of the schema file for error reporting |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required keys are missing |
Example
entry = {"name": "MyType"} validate_schema_entry(entry, ["name", "description"], "test.json") Traceback (most recent call last): ValueError: Missing keys in test.json entry: ['description'] → {'name': 'MyType'}
Source code in backend/core/schema_ops.py
save_schema(file_name, data: list[dict])
Save schema data to file with proper formatting.
This function saves schema data to a file with proper key ordering and validation. It determines the appropriate key order based on the file name and validates each entry before saving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Name of the schema file |
required |
data
|
list[dict]
|
List of schema entries to save |
required |
Example
data = [{"name": "MyType", "description": "A type"}] save_schema("attribute_types.json", data)
Source code in backend/core/schema_ops.py
load_schema_json(file_name: str, default_data: list)
Load JSON schema with default data creation.
This function loads JSON schema data from a file. If the file is empty or contains None, it writes the default data and returns it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Name of the schema file |
required |
default_data
|
list
|
Default data to use if file is empty |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
Schema data from file or default data |
Example
schema = load_schema_json("relation_types.json", []) isinstance(schema, list) True
Source code in backend/core/schema_ops.py
create_attribute_type_from_dict(data: dict)
Create attribute type from dictionary data.
This function creates a new attribute type from dictionary data and adds it to the attribute types schema. If an attribute type with the same name already exists, the function returns without making changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Attribute type data with keys: name, data_type, unit, applicable_classes |
required |
Example
data = { ... "name": "mass", ... "data_type": "float", ... "unit": "kg", ... "applicable_classes": ["atom", "molecule"] ... } create_attribute_type_from_dict(data)
Source code in backend/core/schema_ops.py
create_relation_type_from_dict(data: dict)
Create relation type from dictionary data.
This function creates a new relation type from dictionary data and adds it to the relation types schema. If a relation type with the same name already exists, the function returns without making changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Relation type data with keys: name, inverse, domain, range |
required |
Example
data = { ... "name": "bonds_with", ... "inverse": "bonded_by", ... "domain": "atom", ... "range": "atom" ... } create_relation_type_from_dict(data)
Source code in backend/core/schema_ops.py
parse_cnl_block(block: str) -> list[dict]
Parse CNL block for schema definitions.
This function parses a Controlled Natural Language (CNL) block to extract schema definitions for attributes and relations. It supports the following CNL patterns: - "define attribute 'name' as a type with unit 'unit' applicable to classes." - "define relation 'name' with inverse 'inverse' between 'domain' and 'range'."
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
str
|
CNL block containing schema definitions |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: List of parsed schema statements |
Example
cnl = ''' ... define attribute 'mass' as a float with unit 'kg' applicable to atom, molecule. ... define relation 'bonds_with' with inverse 'bonded_by' between 'atom' and 'atom'. ... ''' statements = parse_cnl_block(cnl) len(statements) 2
Source code in backend/core/schema_ops.py
filter_used_schema(parsed_json_path, relation_schema_path, attribute_schema_path, output_path)
Filters only the used relation and attribute types from the global schema and writes them into used_schema.json.
This function analyzes a parsed graph to identify which relation and attribute types are actually used, then creates a filtered schema containing only those types. This is useful for creating lightweight schemas for specific graphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parsed_json_path
|
str
|
Path to the parsed graph JSON file |
required |
relation_schema_path
|
str
|
Path to the global relation schema file |
required |
attribute_schema_path
|
str
|
Path to the global attribute schema file |
required |
output_path
|
str
|
Path where the filtered schema will be written |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
The filtered schema containing only used types |
Example
filter_used_schema( ... "parsed_graph.json", ... "relation_types.json", ... "attribute_types.json", ... "used_schema.json" ... ) {'relation_types': [...], 'attribute_types': [...]}