@teachinglab/omd 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -0
- package/canvas/core/canvasConfig.js +203 -0
- package/canvas/core/omdCanvas.js +475 -0
- package/canvas/drawing/segment.js +168 -0
- package/canvas/drawing/stroke.js +386 -0
- package/canvas/events/eventManager.js +435 -0
- package/canvas/events/pointerEventHandler.js +263 -0
- package/canvas/features/focusFrameManager.js +287 -0
- package/canvas/index.js +49 -0
- package/canvas/tools/eraserTool.js +322 -0
- package/canvas/tools/pencilTool.js +319 -0
- package/canvas/tools/selectTool.js +457 -0
- package/canvas/tools/tool.js +223 -0
- package/canvas/tools/toolManager.js +394 -0
- package/canvas/ui/cursor.js +438 -0
- package/canvas/ui/toolbar.js +304 -0
- package/canvas/utils/boundingBox.js +378 -0
- package/canvas/utils/mathUtils.js +259 -0
- package/docs/api/configuration-options.md +104 -0
- package/docs/api/eventManager.md +68 -0
- package/docs/api/focusFrameManager.md +150 -0
- package/docs/api/index.md +91 -0
- package/docs/api/main.md +58 -0
- package/docs/api/omdBinaryExpressionNode.md +227 -0
- package/docs/api/omdCanvas.md +142 -0
- package/docs/api/omdConfigManager.md +192 -0
- package/docs/api/omdConstantNode.md +117 -0
- package/docs/api/omdDisplay.md +121 -0
- package/docs/api/omdEquationNode.md +161 -0
- package/docs/api/omdEquationSequenceNode.md +301 -0
- package/docs/api/omdEquationStack.md +139 -0
- package/docs/api/omdFunctionNode.md +141 -0
- package/docs/api/omdGroupNode.md +182 -0
- package/docs/api/omdHelpers.md +96 -0
- package/docs/api/omdLeafNode.md +163 -0
- package/docs/api/omdNode.md +101 -0
- package/docs/api/omdOperationDisplayNode.md +139 -0
- package/docs/api/omdOperatorNode.md +127 -0
- package/docs/api/omdParenthesisNode.md +122 -0
- package/docs/api/omdPopup.md +117 -0
- package/docs/api/omdPowerNode.md +127 -0
- package/docs/api/omdRationalNode.md +128 -0
- package/docs/api/omdSequenceNode.md +128 -0
- package/docs/api/omdSimplification.md +110 -0
- package/docs/api/omdSqrtNode.md +79 -0
- package/docs/api/omdStepVisualizer.md +115 -0
- package/docs/api/omdStepVisualizerHighlighting.md +61 -0
- package/docs/api/omdStepVisualizerInteractiveSteps.md +129 -0
- package/docs/api/omdStepVisualizerLayout.md +60 -0
- package/docs/api/omdStepVisualizerNodeUtils.md +140 -0
- package/docs/api/omdStepVisualizerTextBoxes.md +68 -0
- package/docs/api/omdToolbar.md +102 -0
- package/docs/api/omdTranscriptionService.md +76 -0
- package/docs/api/omdTreeDiff.md +134 -0
- package/docs/api/omdUnaryExpressionNode.md +174 -0
- package/docs/api/omdUtilities.md +70 -0
- package/docs/api/omdVariableNode.md +148 -0
- package/docs/api/selectTool.md +74 -0
- package/docs/api/simplificationEngine.md +98 -0
- package/docs/api/simplificationRules.md +77 -0
- package/docs/api/simplificationUtils.md +64 -0
- package/docs/api/transcribe.md +43 -0
- package/docs/api-reference.md +85 -0
- package/docs/index.html +454 -0
- package/docs/user-guide.md +9 -0
- package/index.js +67 -0
- package/omd/config/omdConfigManager.js +267 -0
- package/omd/core/index.js +150 -0
- package/omd/core/omdEquationStack.js +347 -0
- package/omd/core/omdUtilities.js +115 -0
- package/omd/display/omdDisplay.js +443 -0
- package/omd/display/omdToolbar.js +502 -0
- package/omd/nodes/omdBinaryExpressionNode.js +460 -0
- package/omd/nodes/omdConstantNode.js +142 -0
- package/omd/nodes/omdEquationNode.js +1223 -0
- package/omd/nodes/omdEquationSequenceNode.js +1273 -0
- package/omd/nodes/omdFunctionNode.js +352 -0
- package/omd/nodes/omdGroupNode.js +68 -0
- package/omd/nodes/omdLeafNode.js +77 -0
- package/omd/nodes/omdNode.js +557 -0
- package/omd/nodes/omdOperationDisplayNode.js +322 -0
- package/omd/nodes/omdOperatorNode.js +109 -0
- package/omd/nodes/omdParenthesisNode.js +293 -0
- package/omd/nodes/omdPowerNode.js +236 -0
- package/omd/nodes/omdRationalNode.js +295 -0
- package/omd/nodes/omdSqrtNode.js +308 -0
- package/omd/nodes/omdUnaryExpressionNode.js +178 -0
- package/omd/nodes/omdVariableNode.js +123 -0
- package/omd/simplification/omdSimplification.js +171 -0
- package/omd/simplification/omdSimplificationEngine.js +886 -0
- package/omd/simplification/package.json +6 -0
- package/omd/simplification/rules/binaryRules.js +1037 -0
- package/omd/simplification/rules/functionRules.js +111 -0
- package/omd/simplification/rules/index.js +48 -0
- package/omd/simplification/rules/parenthesisRules.js +19 -0
- package/omd/simplification/rules/powerRules.js +143 -0
- package/omd/simplification/rules/rationalRules.js +475 -0
- package/omd/simplification/rules/sqrtRules.js +48 -0
- package/omd/simplification/rules/unaryRules.js +37 -0
- package/omd/simplification/simplificationRules.js +32 -0
- package/omd/simplification/simplificationUtils.js +1056 -0
- package/omd/step-visualizer/omdStepVisualizer.js +597 -0
- package/omd/step-visualizer/omdStepVisualizerHighlighting.js +206 -0
- package/omd/step-visualizer/omdStepVisualizerLayout.js +245 -0
- package/omd/step-visualizer/omdStepVisualizerTextBoxes.js +163 -0
- package/omd/utils/omdNodeOverlay.js +638 -0
- package/omd/utils/omdPopup.js +1084 -0
- package/omd/utils/omdStepVisualizerInteractiveSteps.js +491 -0
- package/omd/utils/omdStepVisualizerNodeUtils.js +268 -0
- package/omd/utils/omdTranscriptionService.js +125 -0
- package/omd/utils/omdTreeDiff.js +734 -0
- package/package.json +46 -0
- package/src/index.js +62 -0
- package/src/json-schemas.md +109 -0
- package/src/omd-json-samples.js +115 -0
- package/src/omd.js +109 -0
- package/src/omdApp.js +391 -0
- package/src/omdAppCanvas.js +336 -0
- package/src/omdBalanceHanger.js +172 -0
- package/src/omdColor.js +13 -0
- package/src/omdCoordinatePlane.js +467 -0
- package/src/omdEquation.js +125 -0
- package/src/omdExpression.js +104 -0
- package/src/omdFunction.js +113 -0
- package/src/omdMetaExpression.js +287 -0
- package/src/omdNaturalExpression.js +564 -0
- package/src/omdNode.js +384 -0
- package/src/omdNumber.js +53 -0
- package/src/omdNumberLine.js +107 -0
- package/src/omdNumberTile.js +119 -0
- package/src/omdOperator.js +73 -0
- package/src/omdPowerExpression.js +92 -0
- package/src/omdProblem.js +55 -0
- package/src/omdRatioChart.js +232 -0
- package/src/omdRationalExpression.js +115 -0
- package/src/omdSampleData.js +215 -0
- package/src/omdShapes.js +476 -0
- package/src/omdSpinner.js +148 -0
- package/src/omdString.js +39 -0
- package/src/omdTable.js +369 -0
- package/src/omdTapeDiagram.js +245 -0
- package/src/omdTerm.js +92 -0
- package/src/omdTileEquation.js +349 -0
- package/src/omdVariable.js +51 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { omdNode } from "./omdNode.js";
|
|
2
|
+
import { getNodeForAST } from "../core/omdUtilities.js";
|
|
3
|
+
import { omdOperatorNode } from "./omdOperatorNode.js";
|
|
4
|
+
import { omdConstantNode } from "./omdConstantNode.js";
|
|
5
|
+
import { omdBinaryExpressionNode } from "./omdBinaryExpressionNode.js";
|
|
6
|
+
import { simplifyStep } from "../simplification/omdSimplification.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Represents a unary expression, like negation (-(x+y)).
|
|
10
|
+
* @extends omdNode
|
|
11
|
+
*/
|
|
12
|
+
export class omdUnaryExpressionNode extends omdNode {
|
|
13
|
+
/**
|
|
14
|
+
* @param {Object} ast - The AST node from math.js.
|
|
15
|
+
*/
|
|
16
|
+
constructor(ast) {
|
|
17
|
+
super(ast);
|
|
18
|
+
this.type = "omdUnaryExpressionNode";
|
|
19
|
+
if (!ast.args || ast.args.length !== 1) {
|
|
20
|
+
console.error("omdUnaryExpressionNode requires an AST node with exactly 1 argument", ast);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
this.op = this.createOperatorNode(ast);
|
|
25
|
+
this.operand = this.createExpressionNode(ast.args[0]);
|
|
26
|
+
|
|
27
|
+
// Set the operation property for compatibility with SimplificationEngine
|
|
28
|
+
this.operation = ast.fn || ast.op;
|
|
29
|
+
|
|
30
|
+
// Populate the argumentNodeList for mathematical child nodes
|
|
31
|
+
this.argumentNodeList.operand = this.operand;
|
|
32
|
+
|
|
33
|
+
this.addChild(this.op);
|
|
34
|
+
this.addChild(this.operand);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
createOperatorNode(ast) {
|
|
38
|
+
const opNode = new omdOperatorNode({ op: ast.op, fn: ast.fn });
|
|
39
|
+
opNode.parent = this;
|
|
40
|
+
return opNode;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
createExpressionNode(ast) {
|
|
44
|
+
const NodeClass = getNodeForAST(ast);
|
|
45
|
+
const node = new NodeClass(ast);
|
|
46
|
+
node.parent = this;
|
|
47
|
+
return node;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
computeDimensions() {
|
|
51
|
+
this.op.computeDimensions();
|
|
52
|
+
this.operand.computeDimensions();
|
|
53
|
+
|
|
54
|
+
const opWidth = this.op.width;
|
|
55
|
+
// No extra spacing for unary minus.
|
|
56
|
+
const argWidth = this.operand.width;
|
|
57
|
+
|
|
58
|
+
const totalWidth = opWidth + argWidth;
|
|
59
|
+
const totalHeight = Math.max(this.op.height, this.operand.height);
|
|
60
|
+
|
|
61
|
+
this.setWidthAndHeight(totalWidth, totalHeight);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
updateLayout() {
|
|
65
|
+
this.op.updateLayout();
|
|
66
|
+
this.operand.updateLayout();
|
|
67
|
+
|
|
68
|
+
const opY = (this.height - this.op.height) / 2;
|
|
69
|
+
const argY = (this.height - this.operand.height) / 2;
|
|
70
|
+
|
|
71
|
+
this.op.setPosition(0, opY);
|
|
72
|
+
this.operand.setPosition(this.op.width, argY);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
clone() {
|
|
76
|
+
// Create a new node. The constructor will add a backRect and temporary children.
|
|
77
|
+
const tempAst = { type: 'OperatorNode', op: '-', args: [{type: 'ConstantNode', value: 1}] };
|
|
78
|
+
const clone = new omdUnaryExpressionNode(tempAst);
|
|
79
|
+
|
|
80
|
+
// Keep the backRect, but get rid of the temporary children.
|
|
81
|
+
const backRect = clone.backRect;
|
|
82
|
+
clone.removeAllChildren();
|
|
83
|
+
clone.addChild(backRect);
|
|
84
|
+
|
|
85
|
+
// Manually clone the real children to ensure the entire tree has correct provenance.
|
|
86
|
+
clone.op = this.op.clone();
|
|
87
|
+
clone.addChild(clone.op);
|
|
88
|
+
|
|
89
|
+
clone.operand = this.operand.clone();
|
|
90
|
+
clone.addChild(clone.operand);
|
|
91
|
+
|
|
92
|
+
// Rebuild the argument list and copy AST data.
|
|
93
|
+
clone.argumentNodeList = { operand: clone.operand };
|
|
94
|
+
clone.astNodeData = JSON.parse(JSON.stringify(this.astNodeData));
|
|
95
|
+
|
|
96
|
+
// The crucial step: link the clone to its origin.
|
|
97
|
+
clone.provenance.push(this.id);
|
|
98
|
+
|
|
99
|
+
return clone;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Converts the omdUnaryExpressionNode to a math.js AST node.
|
|
104
|
+
* @returns {Object} A math.js-compatible AST node.
|
|
105
|
+
*/
|
|
106
|
+
toMathJSNode() {
|
|
107
|
+
const astNode = {
|
|
108
|
+
type: 'OperatorNode',
|
|
109
|
+
op: this.op.opName,
|
|
110
|
+
fn: this.operation,
|
|
111
|
+
args: [this.operand.toMathJSNode()],
|
|
112
|
+
id: this.id,
|
|
113
|
+
provenance: this.provenance
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Add a clone method to maintain compatibility with math.js's expectations.
|
|
117
|
+
astNode.clone = function() {
|
|
118
|
+
const clonedNode = { ...this };
|
|
119
|
+
clonedNode.args = this.args.map(arg => arg.clone());
|
|
120
|
+
return clonedNode;
|
|
121
|
+
};
|
|
122
|
+
return astNode;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Converts the unary expression node to a string.
|
|
127
|
+
* @returns {string} The string representation of the expression.
|
|
128
|
+
*/
|
|
129
|
+
toString() {
|
|
130
|
+
const operandStr = this.operand.toString();
|
|
131
|
+
if (this.needsParentheses()) {
|
|
132
|
+
return `${this.op.opName}(${operandStr})`;
|
|
133
|
+
}
|
|
134
|
+
return `${this.op.opName}${operandStr}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Check if the operand needs parentheses.
|
|
139
|
+
* @returns {boolean} Whether parentheses are required
|
|
140
|
+
*/
|
|
141
|
+
needsParentheses() {
|
|
142
|
+
// Parentheses are needed if the operand is a binary expression.
|
|
143
|
+
return this.operand.type === 'omdBinaryExpressionNode';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Evaluate the negation.
|
|
148
|
+
* @param {Object} variables - Variable name to value mapping
|
|
149
|
+
* @returns {number} The negated result
|
|
150
|
+
*/
|
|
151
|
+
evaluate(variables = {}) {
|
|
152
|
+
if (!this.operand.evaluate) return NaN;
|
|
153
|
+
const value = this.operand.evaluate(variables);
|
|
154
|
+
if (this.op.opName === '-') {
|
|
155
|
+
return -value;
|
|
156
|
+
}
|
|
157
|
+
return value; // For other potential unary ops like '+'
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Create a negation node from a string.
|
|
162
|
+
* @static
|
|
163
|
+
* @param {string} expressionString - Expression with negation
|
|
164
|
+
* @returns {omdUnaryExpressionNode}
|
|
165
|
+
*/
|
|
166
|
+
static fromString(expressionString) {
|
|
167
|
+
try {
|
|
168
|
+
const ast = window.math.parse(expressionString);
|
|
169
|
+
if (ast.type === 'OperatorNode' && ast.fn === 'unaryMinus') {
|
|
170
|
+
return new omdUnaryExpressionNode(ast);
|
|
171
|
+
}
|
|
172
|
+
throw new Error("Expression is not a unary minus operation.");
|
|
173
|
+
} catch (error) {
|
|
174
|
+
console.error("Failed to create unary expression node from string:", error);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { omdLeafNode } from "./omdLeafNode.js";
|
|
2
|
+
import { omdColor } from "../../src/omdColor.js";
|
|
3
|
+
import { jsvgTextLine } from '@teachinglab/jsvg';
|
|
4
|
+
/**
|
|
5
|
+
* Leaf node that represents a variable.
|
|
6
|
+
* @extends omdLeafNode
|
|
7
|
+
*/
|
|
8
|
+
export class omdVariableNode extends omdLeafNode {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a leaf node from the AST data.
|
|
11
|
+
* @param {Object} astNodeData - The AST node containing leaf information.
|
|
12
|
+
*/
|
|
13
|
+
constructor(nodeData) {
|
|
14
|
+
super(nodeData);
|
|
15
|
+
this.type = "omdVariableNode";
|
|
16
|
+
|
|
17
|
+
this.name = this.parseName(nodeData);
|
|
18
|
+
|
|
19
|
+
this.textElement = super.createTextElement(this.name);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
parseName(nodeData) {
|
|
23
|
+
if (typeof nodeData === "string")
|
|
24
|
+
return nodeData;
|
|
25
|
+
|
|
26
|
+
return nodeData.name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
parseType() {
|
|
30
|
+
return "variable";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Calculates the dimensions of the node.
|
|
35
|
+
* Adds padding around the node.
|
|
36
|
+
* @override
|
|
37
|
+
*/
|
|
38
|
+
computeDimensions() {
|
|
39
|
+
super.computeDimensions();
|
|
40
|
+
|
|
41
|
+
const ratio = this.getFontSize() / this.getRootFontSize();
|
|
42
|
+
const padding = 4 * ratio;
|
|
43
|
+
let paddedWidth = this.width + padding;
|
|
44
|
+
let paddedHeight = this.height + padding;
|
|
45
|
+
this.setWidthAndHeight(paddedWidth, paddedHeight);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Updates the layout of the node.
|
|
50
|
+
* @override
|
|
51
|
+
*/
|
|
52
|
+
updateLayout() {
|
|
53
|
+
super.updateLayout();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Converts the omdVariableNode to a math.js AST node.
|
|
58
|
+
* @returns {Object} A math.js-compatible AST node.
|
|
59
|
+
*/
|
|
60
|
+
toMathJSNode() {
|
|
61
|
+
const astNode = {
|
|
62
|
+
type: 'SymbolNode',
|
|
63
|
+
name: this.name,
|
|
64
|
+
id: this.id,
|
|
65
|
+
provenance: this.provenance
|
|
66
|
+
};
|
|
67
|
+
astNode.clone = function() {
|
|
68
|
+
return { ...this };
|
|
69
|
+
};
|
|
70
|
+
return astNode;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
highlight(color) {
|
|
74
|
+
super.highlight(color);
|
|
75
|
+
if (this.textElement) {
|
|
76
|
+
this.textElement.setFillColor(omdColor.white);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
clearProvenanceHighlights() {
|
|
81
|
+
super.clearProvenanceHighlights();
|
|
82
|
+
if (this.textElement) {
|
|
83
|
+
this.textElement.setFillColor(omdColor.text);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Converts the variable node to a string.
|
|
89
|
+
* @returns {string} The name of the variable.
|
|
90
|
+
*/
|
|
91
|
+
toString() {
|
|
92
|
+
return this.name;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Evaluates the variable by looking up its value in a map.
|
|
97
|
+
* @param {Object} variables - A map of variable names to their numeric values.
|
|
98
|
+
* @returns {number} The value of the variable.
|
|
99
|
+
*/
|
|
100
|
+
evaluate(variables = {}) {
|
|
101
|
+
if (Object.prototype.hasOwnProperty.call(variables, this.name)) {
|
|
102
|
+
return variables[this.name];
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`Variable '${this.name}' is not defined.`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Create a variable node from a name.
|
|
111
|
+
* @param {string} name - The variable name
|
|
112
|
+
* @returns {omdVariableNode}
|
|
113
|
+
* @static
|
|
114
|
+
*/
|
|
115
|
+
static fromName(name) {
|
|
116
|
+
// Create a minimal AST-like object for the constructor
|
|
117
|
+
const astNodeData = {
|
|
118
|
+
type: 'SymbolNode',
|
|
119
|
+
name: name
|
|
120
|
+
};
|
|
121
|
+
return new omdVariableNode(astNodeData);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { getRulesForNode } from './simplificationRules.js';
|
|
2
|
+
import { setSimplifyStep } from '../nodes/omdNode.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Finds all simplification opportunities within an expression tree.
|
|
6
|
+
* Now much faster - only checks relevant rules for each node type!
|
|
7
|
+
* @param {omdNode} rootNode - The root of the expression tree to search.
|
|
8
|
+
* @returns {Array<Object>} Array of simplification opportunities.
|
|
9
|
+
*/
|
|
10
|
+
export function findSimplificationOpportunities(rootNode) {
|
|
11
|
+
const opportunities = [];
|
|
12
|
+
const visitedNodes = new Set();
|
|
13
|
+
|
|
14
|
+
function traverse(node) {
|
|
15
|
+
if (!node || visitedNodes.has(node)) return;
|
|
16
|
+
visitedNodes.add(node);
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if (node.type === 'omdEquationNode') {
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Traverse children first (depth-first)
|
|
24
|
+
if (node.childList && node.childList.length > 0) {
|
|
25
|
+
|
|
26
|
+
for (const child of node.childList) {
|
|
27
|
+
traverse(child);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Only check rules that apply to this node type
|
|
32
|
+
const relevantRules = getRulesForNode(node);
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
for (const rule of relevantRules) {
|
|
36
|
+
const ruleData = rule.canApply(node);
|
|
37
|
+
if (ruleData) {
|
|
38
|
+
|
|
39
|
+
opportunities.push({ node, rule, ruleData });
|
|
40
|
+
break; // Only apply the first matching rule
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
traverse(rootNode);
|
|
46
|
+
console.log(`📊 Total opportunities found: ${opportunities.length}`);
|
|
47
|
+
return opportunities;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function simplifyStep(rootNode) {
|
|
51
|
+
console.log(`🚀 simplifyStep called for root node:`, {
|
|
52
|
+
nodeType: rootNode.type,
|
|
53
|
+
constructorName: rootNode.type,
|
|
54
|
+
nodeId: rootNode.id
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
let foldedCount = 0;
|
|
58
|
+
let newRoot = rootNode;
|
|
59
|
+
let historyEntry = null;
|
|
60
|
+
|
|
61
|
+
// Find all simplification opportunities
|
|
62
|
+
const opportunities = findSimplificationOpportunities(rootNode);
|
|
63
|
+
console.log(`📊 Found ${opportunities.length} simplification opportunities`);
|
|
64
|
+
|
|
65
|
+
if (opportunities.length > 0) {
|
|
66
|
+
// Apply the first opportunity
|
|
67
|
+
const opportunity = opportunities[0];
|
|
68
|
+
console.log(`🔧 Applying simplification rule:`, opportunity.rule.name);
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
const result = opportunity.rule.apply(opportunity.node, opportunity.ruleData, rootNode);
|
|
72
|
+
console.log(`✅ Rule "${opportunity.rule.name}" applied successfully:`, result);
|
|
73
|
+
|
|
74
|
+
if (result && result.success && result.newRoot) {
|
|
75
|
+
newRoot = result.newRoot;
|
|
76
|
+
foldedCount = 1;
|
|
77
|
+
historyEntry = result.historyEntry || {
|
|
78
|
+
name: opportunity.rule.name,
|
|
79
|
+
affectedNodes: [rootNode.id],
|
|
80
|
+
message: `Applied ${opportunity.rule.name}`
|
|
81
|
+
};
|
|
82
|
+
console.log(`✅ Simplification successful! New root:`, {
|
|
83
|
+
nodeType: newRoot.type,
|
|
84
|
+
nodeId: newRoot.id
|
|
85
|
+
});
|
|
86
|
+
} else {
|
|
87
|
+
console.log(`❌ Rule "${opportunity.rule.name}" returned no result`);
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error(`❌ Error applying rule "${opportunity.rule.name}":`, error);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
console.log(`❌ No simplification opportunities found`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
console.log(`📊 simplifyStep result:`, {
|
|
97
|
+
foldedCount,
|
|
98
|
+
hasNewRoot: newRoot !== rootNode,
|
|
99
|
+
newRootType: newRoot.type,
|
|
100
|
+
newRootId: newRoot.id
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return { newRoot, foldedCount, historyEntry };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Simplifies an entire mathematical expression tree by repeatedly applying rules.
|
|
108
|
+
* @param {omdNode} rootNode - The root node of the expression to simplify.
|
|
109
|
+
* @returns {{foldedCount: number, newRoot: omdNode}}
|
|
110
|
+
*/
|
|
111
|
+
export function simplifyExpression(rootNode) {
|
|
112
|
+
let totalFolded = 0;
|
|
113
|
+
let currentRoot = rootNode;
|
|
114
|
+
let iterations = 0;
|
|
115
|
+
const maxIterations = 50; // Prevent infinite loops
|
|
116
|
+
|
|
117
|
+
while (iterations < maxIterations) {
|
|
118
|
+
const { foldedCount, newRoot } = simplifyStep(currentRoot);
|
|
119
|
+
|
|
120
|
+
if (foldedCount === 0) {
|
|
121
|
+
break; // No more simplifications possible
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
currentRoot = newRoot;
|
|
125
|
+
totalFolded += foldedCount;
|
|
126
|
+
iterations++;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Return a fresh clone if any simplifications occurred
|
|
130
|
+
if (totalFolded > 0) {
|
|
131
|
+
const cleanRoot = currentRoot.clone();
|
|
132
|
+
cleanRoot.setFontSize(rootNode.getFontSize());
|
|
133
|
+
return { foldedCount: totalFolded, newRoot: cleanRoot };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { foldedCount: totalFolded, newRoot: currentRoot };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Debug function to show which rules are available for a node
|
|
141
|
+
* @param {omdNode} node - The node to check
|
|
142
|
+
* @returns {Array<string>} Array of rule names
|
|
143
|
+
*/
|
|
144
|
+
export function getAvailableRulesForNode(node) {
|
|
145
|
+
const rules = getRulesForNode(node);
|
|
146
|
+
return rules.map(rule => rule.name);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Debug function to show which rules can be applied to a node
|
|
151
|
+
* @param {omdNode} node - The node to check
|
|
152
|
+
* @returns {Array<string>} Array of applicable rule names
|
|
153
|
+
*/
|
|
154
|
+
export function getApplicableRulesForNode(node) {
|
|
155
|
+
const rules = getRulesForNode(node);
|
|
156
|
+
const applicable = [];
|
|
157
|
+
|
|
158
|
+
for (const rule of rules) {
|
|
159
|
+
try {
|
|
160
|
+
const ruleData = rule.canApply(node);
|
|
161
|
+
if (ruleData) {
|
|
162
|
+
applicable.push(rule.name);
|
|
163
|
+
}
|
|
164
|
+
} catch (error) {
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return applicable;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
setSimplifyStep(simplifyStep);
|