Skip to content

Commit

Permalink
continued major changes to the MGraph Json provider
Browse files Browse the repository at this point in the history
extracted all classes into it's own file
  • Loading branch information
DinisCruz committed Jan 15, 2025
1 parent 363553a commit c536424
Show file tree
Hide file tree
Showing 34 changed files with 733 additions and 372 deletions.
98 changes: 94 additions & 4 deletions mgraph_ai/providers/json/domain/Domain__MGraph__Json__Graph.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,98 @@
from mgraph_ai.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Types import Domain__MGraph__Json__Types
from mgraph_ai.providers.json.models.Model__MGraph__Json__Graph import Model__MGraph__Json__Graph
from typing import Any, Optional
from mgraph_ai.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Types import Domain__MGraph__Json__Types
from mgraph_ai.providers.json.models.Model__MGraph__Json__Graph import Model__MGraph__Json__Graph
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Dict import Domain__MGraph__Json__Node__Dict
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__List import Domain__MGraph__Json__Node__List
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Value import Domain__MGraph__Json__Node__Value
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node


class Domain__MGraph__Json__Graph(Domain__MGraph__Graph):
domain_types : Domain__MGraph__Json__Types
model : Model__MGraph__Json__Graph
model : Model__MGraph__Json__Graph

def root(self) -> Domain__MGraph__Json__Node: # Get root node
"""Get the root node, creating it if it doesn't exist"""
if not self.model.data.graph_data.root_id:
schema_node = Schema__MGraph__Json__Node() # Create basic root node
node = self.model.add_node(schema_node)
self.model.data.graph_data.root_id = node.node_id
return Domain__MGraph__Json__Node(node=node, graph=self.model)

root_model = self.model.node(self.model.data.graph_data.root_id)
if not root_model:
raise ValueError("Root node ID exists but node not found")

return Domain__MGraph__Json__Node(node=root_model, graph=self.model)

def root_content(self) -> Optional[Domain__MGraph__Json__Node]: # Get the content node
"""Get the content node attached to root (if any)"""
root = self.root()
edges = self.model.node__from_edges(root.node_id)
if not edges:
return None

content_node = self.model.node(edges[0].to_node_id()) # Get first child

# Return appropriate domain node type
if isinstance(content_node.data, Schema__MGraph__Json__Node__Dict):
return Domain__MGraph__Json__Node__Dict(node=content_node, graph=self.model)
elif isinstance(content_node.data, Schema__MGraph__Json__Node__List):
return Domain__MGraph__Json__Node__List(node=content_node, graph=self.model)
elif isinstance(content_node.data, Schema__MGraph__Json__Node__Value):
return Domain__MGraph__Json__Node__Value(node=content_node, graph=self.model)

return None

def set_root_content(self, data: Any) -> Domain__MGraph__Json__Node: # Set content node
"""Set the JSON content, creating appropriate node type"""
root = self.root()

# Remove any existing content
edges = self.model.node__from_edges(root.node_id)
for edge in edges:
self.model.delete_edge(edge.edge_id())
self.model.delete_node(edge.to_node_id())

# Create appropriate node type for new content
if isinstance(data, dict):
content = self.new_dict_node(data)
elif isinstance(data, (list, tuple)):
content = self.new_list_node(data)
else:
content = self.new_value_node(data)

# Link content to root
self.model.new_edge(from_node_id=root.node_id, to_node_id=content.node_id)
return content

def new_dict_node(self, properties=None) -> Domain__MGraph__Json__Node__Dict: # Create dictionary node
"""Create a new dictionary node with optional initial properties"""
schema_node = Schema__MGraph__Json__Node__Dict()
node = self.model.add_node(schema_node)
dict_node = Domain__MGraph__Json__Node__Dict(node=node, graph=self.model)

if properties:
dict_node.update(properties)

return dict_node

def new_list_node(self, items=None) -> Domain__MGraph__Json__Node__List: # Create list node
"""Create a new list node with optional initial items"""
schema_node = Schema__MGraph__Json__Node__List()
node = self.model.add_node(schema_node)
list_node = Domain__MGraph__Json__Node__List(node=node, graph=self.model)

if items:
list_node.extend(items)

return list_node

def new_value_node(self, value: Any) -> Domain__MGraph__Json__Node__Value: # Create value node
"""Create a new value node with the given value"""
node_data = Schema__MGraph__Json__Node__Value__Data(value=value, value_type=type(value))
schema_node = Schema__MGraph__Json__Node__Value(node_data=node_data)
node = self.model.add_node(schema_node)
return Domain__MGraph__Json__Node__Value(node=node, graph=self.model)
21 changes: 18 additions & 3 deletions mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node
from typing import Optional
from mgraph_ai.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value


class Domain__MGraph__Json__Node(Domain__MGraph__Node):
node: Model__MGraph__Json__Node # Reference to node model
node: Model__MGraph__Json__Node # Reference to node model

def get_type(self) -> Optional[str]: # Get node type
"""Get the type of this JSON node (dict, list, or value)"""
node_type = self.node.data.node_type
if node_type == Schema__MGraph__Json__Node__Dict:
return 'dict'
elif node_type == Schema__MGraph__Json__Node__List:
return 'list'
elif node_type == Schema__MGraph__Json__Node__Value:
return 'value'
return None
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Optional, Dict, Any
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node__Dict, Model__MGraph__Json__Node__Property, Model__MGraph__Json__Node__Value
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node__Value, Schema__MGraph__Json__Node__Value__Data, Schema__MGraph__Json__Node__Property, Schema__MGraph__Json__Node__Property__Data
from typing import Optional, Dict, Any
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Property import Schema__MGraph__Json__Node__Property
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Property__Data import Schema__MGraph__Json__Node__Property__Data
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value__Data import Schema__MGraph__Json__Node__Value__Data


class Domain__MGraph__Json__Node__Dict(Domain__MGraph__Json__Node):
Expand Down
89 changes: 67 additions & 22 deletions mgraph_ai/providers/json/domain/Domain__MGraph__Json__Node__List.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,82 @@
from typing import List, Any
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node__List, Model__MGraph__Json__Node__Value
from typing import List, Any, Union
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Dict import Domain__MGraph__Json__Node__Dict
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__List import Model__MGraph__Json__Node__List
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__List import Schema__MGraph__Json__Node__List
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value__Data import \
Schema__MGraph__Json__Node__Value__Data


class Domain__MGraph__Json__Node__List(Domain__MGraph__Json__Node):
node: Model__MGraph__Json__Node__List # Reference to list node model
node: Model__MGraph__Json__Node__List # Reference to list node model

def items(self) -> List[Any]: # Get all items in the list
def items(self) -> List[Any]: # Get all items in the list
items = []
for edge in self.models__from_edges():
value_node = self.graph.node(edge.to_node_id())
if isinstance(value_node, Model__MGraph__Json__Node__Value):
items.append(value_node.value)
target_node = self.model__node_from_edge(edge)

# Handle different node types
if target_node.data.node_type == Schema__MGraph__Json__Node__Value:
items.append(target_node.data.node_data.value)
elif target_node.data.node_type == Schema__MGraph__Json__Node__Dict:
# For dict nodes, get their properties
dict_node = Domain__MGraph__Json__Node__Dict(node=target_node, graph=self.graph)
items.append(dict_node.properties())
elif target_node.data.node_type == Schema__MGraph__Json__Node__List:
# For list nodes, recursively get their items
list_node = Domain__MGraph__Json__Node__List(node=target_node, graph=self.graph)
items.append(list_node.items())
return items

def add(self, value: Any) -> None: # Add an item to the list
value_node = self.graph.new_node(value=value, value_type=type(value))
self.graph.new_edge(from_node_id=self.node_id(), to_node_id=value_node.node_id())
def add(self, value: Any) -> None:
"""Add an item to the list. The item can be a primitive value, dict, or list."""
if isinstance(value, (str, int, float, bool)) or value is None:
# Handle primitive values
node_data = Schema__MGraph__Json__Node__Value__Data(value=value, value_type=type(value))
schema_node = Schema__MGraph__Json__Node__Value (node_data=node_data)
value_node = self.graph.add_node(schema_node)

elif isinstance(value, dict):
# Handle dictionaries
dict_node = Schema__MGraph__Json__Node__Dict()
value_node = self.graph.add_node(dict_node)
domain_dict = Domain__MGraph__Json__Node__Dict(node=value_node, graph=self.graph)
domain_dict.update(value)

elif isinstance(value, (list, tuple)):
# Handle lists
list_node = Schema__MGraph__Json__Node__List()
value_node = self.graph.add_node(list_node)
domain_list = Domain__MGraph__Json__Node__List(node=value_node, graph=self.graph)
for item in value:
domain_list.add(item)
else:
raise ValueError(f"Unsupported value type: {type(value)}")

print()
print("from_node_id = ", self.node.node_id, " | to_node_id=", value_node.node_id)
# Connect the new node to this list
#self.graph.new_edge(from_node_id=self.node.node_id, to_node_id=value_node.node_id)

def clear(self) -> None: # Remove all items
def clear(self) -> None: # Remove all items
for edge in self.models__from_edges():
target_node = self.model__node_from_edge(edge)
self.graph.delete_edge(edge.edge_id())
self.graph.delete_node(target_node.node_id())

def extend(self, nodes: List[Domain__MGraph__Json__Node]) -> None:
"""Add multiple nodes to the list"""
for node in nodes:
self.graph.new_edge(from_node_id = self.node_id(),
to_node_id = node.node_id())
def extend(self, items: List[Any]) -> None:
"""Add multiple items to the list"""
for item in items:
self.add(item)

def remove(self, value: Any) -> bool: # Remove first occurrence of a value
def remove(self, value: Any) -> bool: # Remove first occurrence of a value
for edge in self.models__from_edges():
value_node = self.graph.node(edge.to_node_id())
if isinstance(value_node, Model__MGraph__Json__Node__Value) and value_node.value == value:
self.graph.delete_edge(edge.edge_id())
return True
target_node = self.model__node_from_edge(edge)
if target_node.data.node_type == Schema__MGraph__Json__Node__Value:
if target_node.data.node_data.value == value:
self.graph.delete_edge(edge.edge_id())
self.graph.delete_node(target_node.node_id())
return True
return False
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from osbot_utils.type_safe.methods.type_safe_property import set_as_property
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node import Model__MGraph__Json__Node__Value

from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Value import Model__MGraph__Json__Node__Value
from osbot_utils.type_safe.methods.type_safe_property import set_as_property
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node import Domain__MGraph__Json__Node


class Domain__MGraph__Json__Node__Value(Domain__MGraph__Json__Node):
Expand Down
18 changes: 14 additions & 4 deletions mgraph_ai/providers/json/models/Model__MGraph__Json__Graph.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
from mgraph_ai.providers.json.models.Model__MGraph__Json__Types import Model__MGraph__Json__Types
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Graph import Schema__MGraph__Json__Graph
from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict
from mgraph_ai.providers.json.models.Model__MGraph__Json__Types import Model__MGraph__Json__Types
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Graph import Schema__MGraph__Json__Graph
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Dict import Schema__MGraph__Json__Node__Dict


class Model__MGraph__Json__Graph(Model__MGraph__Graph):
data : Schema__MGraph__Json__Graph
model_types: Model__MGraph__Json__Types
model_types: Model__MGraph__Json__Types

def add_node__dict(self):
data = Schema__MGraph__Json__Node__Dict()
node = Model__MGraph__Json__Node__Dict(data=data)
self.data.nodes[node.node_id] = data # for now, we need to add this directly here
# self.add_node(data) # todo: improve add_node to receive the model to create since at the not going ot create a Model__MGraph__Json__Node__Dict

return node
41 changes: 3 additions & 38 deletions mgraph_ai/providers/json/models/Model__MGraph__Json__Node.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
from typing import Any
from osbot_utils.type_safe.methods.type_safe_property import set_as_property
from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node

from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import (
Schema__MGraph__Json__Node,
Schema__MGraph__Json__Node__Value,
Schema__MGraph__Json__Node__Dict,
Schema__MGraph__Json__Node__List,
Schema__MGraph__Json__Node__Property,
)
from typing import Any
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node
from mgraph_ai.mgraph.models.Model__MGraph__Node import Model__MGraph__Node

class Model__MGraph__Json__Node(Model__MGraph__Node): # Base model class for JSON nodes
data: Schema__MGraph__Json__Node

class Model__MGraph__Json__Node__Value(Model__MGraph__Json__Node): # Model class for JSON value nodes
data: Schema__MGraph__Json__Node__Value

value_type = set_as_property('data.node_data', 'value_type')

def is_primitive(self) -> bool: # Check if the value is a primitive type
return self.value_type in (str, int, float, bool, type(None))

@property
def value(self) -> Any:
return self.data.node_data.value

@value.setter
def value(self, new_value: Any) -> None:
self.data.node_data.value = new_value
self.data.node_data.value_type = type(new_value)

class Model__MGraph__Json__Node__Dict(Model__MGraph__Json__Node): # Model class for JSON object nodes
data: Schema__MGraph__Json__Node__Dict

class Model__MGraph__Json__Node__List(Model__MGraph__Json__Node): # Model class for JSON array nodes
data: Schema__MGraph__Json__Node__List

class Model__MGraph__Json__Node__Property(Model__MGraph__Json__Node): # Model class for JSON object property nodes
data: Schema__MGraph__Json__Node__Property

name = set_as_property('data.node_data', 'name')
Loading

0 comments on commit c536424

Please sign in to comment.