Geometry Implementations

Geometry Implementations

The Matrix Framework provides several geometry implementations for organizing and managing service hierarchies.

Tree Geometry

The TreeGeometry class implements a hierarchical tree structure:

export class TreeGeometry extends GeometryBase {
    private nodeMap: Map<CoreElement, TreeNode> = new Map();
    private root: TreeNode;

    constructor(name: string, rootNode: CoreElement) {
        super(name);
        this.root = new TreeNode(rootNode);
        this.nodeMap.set(rootNode, this.root);
        this.nodes.add(rootNode);
    }

    public addNode(node: CoreElement, parent?: CoreElement): void {
        const parentNode = parent ? this.nodeMap.get(parent) : this.root;
        if (!parentNode) throw new Error(`Parent not found in geometry ${this.name}`);
        
        const treeNode = new TreeNode(node, parentNode);
        parentNode.children.push(treeNode);
        this.nodeMap.set(node, treeNode);
        this.nodes.add(node);
    }

    public async routeEvent(event: MiddlewareEvent): Promise<void> {
        const strategy = this.getRoutingStrategy(event.routingStrategy);
        if (!strategy) throw new Error(`Routing strategy ${event.routingStrategy} not found.`);
        await strategy.route(event, this);
    }
}

Key Features

  • Maintains parent-child relationships
  • Supports hierarchical event routing
  • Efficient node lookup via Map

Tensor Geometry

The TensorGeometry class implements a multi-dimensional array structure:

export class TensorGeometry extends GeometryBase {
    private dimensions: number[];
    private elements: Map<string, CoreElement> = new Map();

    constructor(name: string, dimensions: number[]) {
        super(name);
        this.dimensions = dimensions;
    }

    public addNode(node: CoreElement, indices: number[]): void {
        const key = indices.join(',');
        if (this.elements.has(key)) throw new Error(`Node already exists at ${key}`);
        this.elements.set(key, node);
        this.nodes.add(node);
    }
}

Key Features

  • Multi-dimensional organization
  • Index-based node access
  • Support for tensor operations

Usage in WPF

Both geometries can be represented visually in WPF:

<!-- Tree Geometry -->
<TreeGeometry x:Name="tree">
    <TreeNode x:Name="root">
        <TreeNode x:Name="child1" />
        <TreeNode x:Name="child2">
            <TreeNode x:Name="grandchild" />
        </TreeNode>
    </TreeNode>
</TreeGeometry>

<!-- Tensor Geometry -->
<TensorGeometry x:Name="tensor" Dimensions="3,3">
    <TensorNode Position="0,0" />
    <TensorNode Position="0,1" />
    <TensorNode Position="1,0" />
</TensorGeometry>

Best Practices

  1. Node Management

    • Always validate parent-child relationships
    • Use efficient data structures for lookups
    • Maintain geometry-specific constraints
  2. Event Routing

    • Implement geometry-specific routing strategies
    • Handle edge cases in node traversal
    • Support standard routing patterns (bubble, tunnel)
  3. Visual Integration

    • Provide XAML representations
    • Support data binding for state
    • Include debug visualizations