OMD Documentation

omdNode

The abstract base class for all nodes in the mathematical expression tree. It provides the core functionality for tree structure (parent-child relationships), layout calculations, rendering, and provenance tracking. All specific mathematical elements (constants, variables, operators, functions, equations) extend this class.

Class Definition

export class omdNode extends omdMetaExpression

This class is not meant to be instantiated directly. Instead, you should use one of its concrete subclasses (e.g., omdConstantNode, omdBinaryExpressionNode).

Key Concepts

Public Properties

Public Methods

initialize()

Initializes the node by recursively computing its dimensions and updating its layout. This method should be called after a node and its children have been fully constructed to ensure proper sizing and positioning.

clone()

Creates a deep, structural clone of the node. The new node will have a new id, and its provenance will link back to the original node's id, establishing a historical connection.

replaceWith(newNode, options)

Replaces this node with a newNode in the expression tree. This updates all parent and child references and triggers a re-layout and re-rendering of the affected parts of the SVG.

simplify()

Asynchronously attempts to simplify the expression rooted at this node by invoking the central simplification engine (simplifyStep).

toMathJSNode()

Converts the omdNode and its children back into a math.js-compatible AST object. This method must be implemented by all concrete subclasses.

toString()

Converts the node into a human-readable string representation. It typically uses the toMathJSNode() method internally to leverage math.js's string conversion capabilities.

render()

Generates or retrieves the SVG representation of the node. This method calls renderSelf() if the SVG element has not been created yet.

renderSelf()

Abstract method that must be implemented by subclasses. This method is responsible for creating the specific SVG elements and structure for the node's visual representation.

setFontSize(size)

Sets the base font size for this node and recursively propagates the new font size to all its children.

moveTo(x, y)

Moves the node to a new absolute position (x, y) relative to its parent. It also recursively moves all children by the same delta.

show()

Makes the node and its SVG representation visible.

hide()

Hides the node and its SVG representation.

getDepth()

Calculates the depth of the node in the expression tree (0 for the root node).

findParentOfType(type)

Traverses up the tree to find the nearest parent node of a specific type.

validateProvenance(nodeMap)

Validates the provenance integrity of this node and all its descendants. It checks for duplicate IDs, invalid references, and self-references in the provenance arrays.

setHighlight(highlightOn, color)

Applies or removes a highlight from the node's background. If isExplainHighlighted is true, this method will not override the existing explanation highlight.

lowlight()

Reduces the opacity of the node's background. Similar to setHighlight, it respects the isExplainHighlighted lock.

setFillColor(color)

Sets the fill color of the node's background rectangle. This method also respects the isExplainHighlighted lock.

Abstract Methods (to be implemented by subclasses)

Internal Methods

Example

// omdNode is an abstract class - use concrete subclasses
import { omdConstantNode } from './omdConstantNode.js';
import { omdBinaryExpressionNode } from './omdBinaryExpressionNode.js';
import { omdDisplay } from '../display/omdDisplay.js';

// Create a simple expression tree
const two = new omdConstantNode({ value: 2 });
const x = new omdVariableNode({ name: 'x' });
const twoX = new omdBinaryExpressionNode({
    type: 'OperatorNode', op: '*', fn: 'multiply',
    args: [two.astNodeData, x.astNodeData],
    implicit: true
});

// Initialize the root node (this will recursively initialize children)
twoX.initialize();

// Render it using omdDisplay
const container = document.getElementById('math-container');
const display = new omdDisplay(container);
display.render(twoX);

console.log(twoX.toString()); // Output: 2x

See Also

↑ Top