Implement MathPage evaluation logic
This commit is contained in:
parent
2aa2a5bf05
commit
be66844f17
@ -2,58 +2,77 @@ import {MathStatement} from './parse'
|
|||||||
import * as math from 'mathjs'
|
import * as math from 'mathjs'
|
||||||
import {DepGraph} from 'dependency-graph'
|
import {DepGraph} from 'dependency-graph'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
import {EvaluationResult, StatementID, VariableName} from '../types'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for a page containing multiple interrelated mathematical statements.
|
||||||
|
*/
|
||||||
export class MathPage {
|
export class MathPage {
|
||||||
protected statements: Record<string, MathStatement> = {}
|
/** The statements on the page. */
|
||||||
|
protected statements: Record<StatementID, MathStatement> = {}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
/** Unique page ID. */
|
||||||
public readonly id: string,
|
public readonly id: string,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
/** Add a statement to this page. */
|
||||||
addStatement(statement: MathStatement): this {
|
addStatement(statement: MathStatement): this {
|
||||||
this.statements[statement.id] = statement
|
this.statements[statement.id] = statement
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Parse the math expression and add it to the page as a statement. */
|
||||||
addRaw(statement: string): this {
|
addRaw(statement: string): this {
|
||||||
return this.addStatement(new MathStatement(uuidv4(), statement))
|
return this.addStatement(new MathStatement(uuidv4() as StatementID, statement))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all symbols referenced by statements on this page. */
|
||||||
symbols(): math.SymbolNode[] {
|
symbols(): math.SymbolNode[] {
|
||||||
return Object.values(this.statements)
|
return Object.values(this.statements)
|
||||||
.map(x => x.symbols())
|
.map(x => x.symbols())
|
||||||
.reduce((carry, current) => current.concat(carry), [])
|
.reduce((carry, current) => current.concat(carry), [])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all symbols defined on this page. */
|
||||||
defines(): math.SymbolNode[] {
|
defines(): math.SymbolNode[] {
|
||||||
return Object.values(this.statements)
|
return Object.values(this.statements)
|
||||||
.map(x => x.defines())
|
.map(x => x.defines())
|
||||||
.reduce((carry, current) => current.concat(carry), [])
|
.reduce((carry, current) => current.concat(carry), [])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all symbols used on this page. */
|
||||||
uses(): math.SymbolNode[] {
|
uses(): math.SymbolNode[] {
|
||||||
return Object.values(this.statements)
|
return Object.values(this.statements)
|
||||||
.map(x => x.uses())
|
.map(x => x.uses())
|
||||||
.reduce((carry, current) => current.concat(carry), [])
|
.reduce((carry, current) => current.concat(carry), [])
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies(): DepGraph<MathStatement> {
|
/** Get a mapping of symbol names to the statements where they are defined. */
|
||||||
const graph = new DepGraph<MathStatement>()
|
definers(): Record<VariableName, MathStatement> {
|
||||||
const defined: Record<string, MathStatement> = {}
|
const definers: Record<VariableName, MathStatement> = {}
|
||||||
|
|
||||||
for ( const statement of Object.values(this.statements) ) {
|
for ( const statement of Object.values(this.statements) ) {
|
||||||
for ( const symbol of statement.defines() ) {
|
for ( const symbol of statement.defines() ) {
|
||||||
defined[symbol.name] = statement
|
definers[symbol.name as VariableName] = statement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return definers
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the dependency graph of variable declarations between statements on this page. */
|
||||||
|
dependencies(): DepGraph<MathStatement> {
|
||||||
|
const graph = new DepGraph<MathStatement>()
|
||||||
|
const defined: Record<VariableName, MathStatement> = this.definers()
|
||||||
|
|
||||||
for ( const statement of Object.values(this.statements) ) {
|
for ( const statement of Object.values(this.statements) ) {
|
||||||
graph.addNode(statement.id, statement)
|
graph.addNode(statement.id, statement)
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( const statement of Object.values(this.statements) ) {
|
for ( const statement of Object.values(this.statements) ) {
|
||||||
for ( const symbol of statement.uses() ) {
|
for ( const symbol of statement.uses() ) {
|
||||||
const provider = defined[symbol.name]
|
const provider = defined[symbol.name as VariableName]
|
||||||
if ( !provider ) {
|
if ( !provider ) {
|
||||||
throw new Error('No provider for undefined symbol: ' + symbol.name)
|
throw new Error('No provider for undefined symbol: ' + symbol.name)
|
||||||
}
|
}
|
||||||
@ -64,4 +83,23 @@ export class MathPage {
|
|||||||
|
|
||||||
return graph
|
return graph
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Evaluate the current state of the page and get the result. */
|
||||||
|
evaluate(): EvaluationResult {
|
||||||
|
const evaluations: Record<StatementID, any> = {}
|
||||||
|
const scope: Record<VariableName, any> = {}
|
||||||
|
const graph = this.dependencies()
|
||||||
|
|
||||||
|
for ( const node of graph.overallOrder() ) {
|
||||||
|
const stmt = this.statements[node as StatementID]
|
||||||
|
evaluations[stmt.id] = stmt.parse()
|
||||||
|
.compile()
|
||||||
|
.evaluate(scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
variables: scope,
|
||||||
|
statements: evaluations,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import * as math from 'mathjs'
|
import * as math from 'mathjs'
|
||||||
import katex from 'katex'
|
import katex from 'katex'
|
||||||
import {HTMLString, LaTeXString} from '../types'
|
import {HTMLString, LaTeXString, StatementID} from '../types'
|
||||||
|
|
||||||
/** Base class for walks over MathNode trees. */
|
/** Base class for walks over MathNode trees. */
|
||||||
export abstract class MathNodeWalk<TReturn> {
|
export abstract class MathNodeWalk<TReturn> {
|
||||||
@ -72,6 +72,7 @@ export abstract class MathNodeWalk<TReturn> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** A walk that accumulates all different SymbolNode instances in a tree. */
|
||||||
export class SymbolWalk extends MathNodeWalk<math.SymbolNode[]> {
|
export class SymbolWalk extends MathNodeWalk<math.SymbolNode[]> {
|
||||||
walkAccessorNode(node: math.AccessorNode): math.SymbolNode[] {
|
walkAccessorNode(node: math.AccessorNode): math.SymbolNode[] {
|
||||||
return [
|
return [
|
||||||
@ -168,6 +169,7 @@ export class SymbolWalk extends MathNodeWalk<math.SymbolNode[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** A walk that accumulates all SymbolNode instances used on the RHS of expressions. */
|
||||||
export class RValSymbolWalk extends SymbolWalk {
|
export class RValSymbolWalk extends SymbolWalk {
|
||||||
walkAssignmentNode(node: math.AssignmentNode): math.SymbolNode[] {
|
walkAssignmentNode(node: math.AssignmentNode): math.SymbolNode[] {
|
||||||
return this.walk(node.value)
|
return this.walk(node.value)
|
||||||
@ -184,6 +186,7 @@ export class RValSymbolWalk extends SymbolWalk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** A walk that accumulates SymbolNode instances used on the LHS of assignments. */
|
||||||
export class LValSymbolWalk extends SymbolWalk {
|
export class LValSymbolWalk extends SymbolWalk {
|
||||||
walkAccessorNode(): math.SymbolNode[] {
|
walkAccessorNode(): math.SymbolNode[] {
|
||||||
return []
|
return []
|
||||||
@ -252,26 +255,35 @@ export class LValSymbolWalk extends SymbolWalk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** A single mathematical statement. */
|
||||||
export class MathStatement {
|
export class MathStatement {
|
||||||
constructor(
|
constructor(
|
||||||
public readonly id: string,
|
/** Unique ID of this statement. */
|
||||||
|
public readonly id: StatementID,
|
||||||
|
|
||||||
|
/** The raw statement input by the user. */
|
||||||
public readonly raw: string,
|
public readonly raw: string,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
/** Parse the raw statement to an AST. */
|
||||||
parse(): math.MathNode {
|
parse(): math.MathNode {
|
||||||
return math.parse(this.raw)
|
return math.parse(this.raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Convert the statement to its equivalent LaTeX code. */
|
||||||
toLaTeX(): LaTeXString {
|
toLaTeX(): LaTeXString {
|
||||||
return this.parse().toTex() as LaTeXString
|
return this.parse().toTex() as LaTeXString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Render the statement as HTML string. */
|
||||||
toHTMLString(): HTMLString {
|
toHTMLString(): HTMLString {
|
||||||
return katex.renderToString(this.toLaTeX(), {
|
return katex.renderToString(this.toLaTeX(), {
|
||||||
output: 'mathml',
|
output: 'mathml',
|
||||||
}) as HTMLString
|
}) as HTMLString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Render the statement to a DOM element. */
|
||||||
toDOM(): HTMLSpanElement {
|
toDOM(): HTMLSpanElement {
|
||||||
const node = document.createElement('span')
|
const node = document.createElement('span')
|
||||||
katex.render(this.toLaTeX(), node, {
|
katex.render(this.toLaTeX(), node, {
|
||||||
@ -280,14 +292,17 @@ export class MathStatement {
|
|||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all symbols referenced in this statement. */
|
||||||
symbols(): math.SymbolNode[] {
|
symbols(): math.SymbolNode[] {
|
||||||
return (new SymbolWalk()).walk(this.parse())
|
return (new SymbolWalk()).walk(this.parse())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all symbols defined on the LHS of this statement. */
|
||||||
defines(): math.SymbolNode[] {
|
defines(): math.SymbolNode[] {
|
||||||
return (new LValSymbolWalk()).walk(this.parse())
|
return (new LValSymbolWalk()).walk(this.parse())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all symbols used on the RHS of this statement. */
|
||||||
uses(): math.SymbolNode[] {
|
uses(): math.SymbolNode[] {
|
||||||
return (new RValSymbolWalk()).walk(this.parse())
|
return (new RValSymbolWalk()).walk(this.parse())
|
||||||
}
|
}
|
||||||
|
@ -83,3 +83,10 @@ export function isInteger(num: number): num is Integer {
|
|||||||
|
|
||||||
export type LaTeXString = TypeTag<'@app.LaTeXString'> & string
|
export type LaTeXString = TypeTag<'@app.LaTeXString'> & string
|
||||||
export type HTMLString = TypeTag<'@app.HTMLString'> & string
|
export type HTMLString = TypeTag<'@app.HTMLString'> & string
|
||||||
|
export type StatementID = TypeTag<'@app.StatementID'> & string
|
||||||
|
export type VariableName = TypeTag<'@app.VariableName'> & string
|
||||||
|
|
||||||
|
export interface EvaluationResult {
|
||||||
|
variables: Record<VariableName, any>
|
||||||
|
statements: Record<StatementID, any>
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user