-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e38b91
commit 1e6aa22
Showing
15 changed files
with
14,998 additions
and
1,436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
dist | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,65 @@ | ||
import { FunctionRegistry, default as buildGrammar} from "@tissue/bettermath" | ||
import { IValueType, IExpressionType, BettermathGrammarParser, ValueResolvingResult as BettermathValueResolvingResult, IBaseType} from "@tecido/bettermath" | ||
import { IDataFunction } from "../DataFunction"; | ||
import { INode } from "../../model/Node"; | ||
import { getRefIds } from "../grammar/bettermath"; | ||
|
||
class BettermathDataFunction implements IDataFunction<IValueType<any>> { | ||
export class ValueResolvingResult<T> { | ||
readonly isError: boolean; | ||
readonly value?: T; | ||
readonly error?: Error; | ||
|
||
private constructor(isError: boolean, result?: {value? : T, error?: Error}) { | ||
this.isError = isError; | ||
this.value = result?.value; | ||
this.error = result?.error; | ||
} | ||
|
||
static success = <T>(value: T) => new ValueResolvingResult(false, {value}); | ||
|
||
static error = <T>(error: Error) => new ValueResolvingResult<T>(true, {error}); | ||
|
||
static fromBettermath = <T>(valueResolvingResult: BettermathValueResolvingResult<T>) => | ||
new ValueResolvingResult(valueResolvingResult.isError, { | ||
value: valueResolvingResult.value, | ||
error: valueResolvingResult.error, | ||
}) | ||
|
||
get = () => { | ||
if (this.isError) { | ||
throw new Error( | ||
"The ValueResolvingResult is an error. Cannot get value. Inner error is: " | ||
+ this.error | ||
); | ||
} | ||
|
||
return this.value as T; | ||
} | ||
|
||
getOrElse = (elseValue: any) => this.isError ? elseValue : this.value; | ||
|
||
getError = () => this.error; | ||
|
||
toString = () => `ValueResolvingResult(${this.isError? "[ERROR]" : this.value})` | ||
} | ||
|
||
class BettermathDataFunction implements IDataFunction<any> { | ||
parsedExpression: IExpressionType<any>; | ||
dependencyIds: Set<string>; | ||
|
||
// TODO: Inject grammar | ||
constructor(grammar: P.Parser<IExpressionType<any>>, fn: string) { | ||
constructor(grammar: BettermathGrammarParser, fn: string) { | ||
// TODO: Handle parsing error | ||
this.parsedExpression = grammar.tryParse(fn) | ||
// TODO: Add REF() support to extract ids from expressions | ||
this.dependencyIds = this.parsedExpression.getRefs() | ||
this.dependencyIds = new Set(getRefIds(this.parsedExpression).toJS().map(({column, line}) => `${column}${line}`)) | ||
} | ||
|
||
compute: (dependencies: Map<string, INode<IValueType<any>> | undefined>) => { | ||
// TODO: Add support for REF() value injection. | ||
// Might be better to pass <refId, node.data> pairs directly, to avoid extra recomputation | ||
return parsedExpression.getValue(refMap) | ||
compute = (dependencies: Map<string, INode<any> | undefined>) => { | ||
// TODO: Might be worth it introducing the concept of async compute for functions | ||
// (as it can take some time to resolve REF values to compute this function value) | ||
const dependencyValues: Map<string, any> = new Map(Array.from(dependencies) | ||
.map(([refId, refNode]) => ([refId, refNode?.data.get()]))) | ||
return ValueResolvingResult.fromBettermath(this.parsedExpression.getValue(dependencyValues)) | ||
}; | ||
|
||
|
||
} | ||
|
||
export default BettermathDataFunction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { BettermathGrammarParser, FunctionRegistry, IBaseType, IExpressionType, Types, buildGrammar } from '@tecido/bettermath'; | ||
import { IRef, parseRefId } from '../../model/Ref'; | ||
import { Set, MapOf } from "immutable"; | ||
import { IRefType } from '@tecido/bettermath'; | ||
|
||
export default (): BettermathGrammarParser => { | ||
const functionRegistry: FunctionRegistry = new FunctionRegistry(); | ||
return buildGrammar(functionRegistry) | ||
} | ||
|
||
export const getRefIds = (expression: IExpressionType<any>): Set<MapOf<IRef>> => { | ||
const refIds = expression | ||
.find((elem: IBaseType<any>) => elem.type === Types.REF) | ||
.map((refNode: IBaseType<any>) => parseRefId((refNode as IRefType).value)) | ||
|
||
return Set(refIds); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,7 @@ | ||
import { DumbNumberSumDefinitionParser } from "./functions/Addition"; | ||
import { DataFunction } from "./functions/DataFunction"; | ||
import { Node } from "./model/Node"; | ||
import { NodeMesh } from "./model/NodeMesh"; | ||
import BettermathDataFunction from "./functions/adapter/bettermath"; | ||
|
||
const NodeA = new Node<number>("A", new DataFunction<number>("1+1", new DumbNumberSumDefinitionParser())) | ||
const NodeB = new Node<number>("B", new DataFunction<number>("1+A", new DumbNumberSumDefinitionParser())) | ||
const NodeC = new Node<number>("C", new DataFunction<number>("A+B", new DumbNumberSumDefinitionParser())) | ||
|
||
const grid = new NodeMesh([ | ||
NodeA, | ||
NodeB, | ||
NodeC, | ||
]) | ||
|
||
console.info("Iteration #0") | ||
grid.printNodes() | ||
|
||
console.info("Iteration #1 :: Change Add Node D") | ||
const NodeD = new Node<number>("D", new DataFunction<number>("C+E", new DumbNumberSumDefinitionParser())) | ||
grid.addNode(NodeD) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #2 :: Change Add Node E") | ||
const NodeE = new Node<number>("E", new DataFunction<number>("1+A", new DumbNumberSumDefinitionParser())) | ||
grid.addNode(NodeE) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #3 :: Change A to 0") | ||
NodeA.setDataFunction(new DataFunction<number>("0+0", new DumbNumberSumDefinitionParser())) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #4 :: Add Node F") | ||
const NodeF = new Node<number>("F", new DataFunction<number>("1+0", new DumbNumberSumDefinitionParser())) | ||
grid.addNode(NodeF) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #5 :: Change A to undefined (Simulate Error)") | ||
NodeA.setDataFunction(new DataFunction<number>("", new DumbNumberSumDefinitionParser())) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #5 :: Change A to good value, but C to undefined (Simulate Error)") | ||
NodeA.setDataFunction(new DataFunction<number>("1+1", new DumbNumberSumDefinitionParser())) | ||
NodeC.setDataFunction(new DataFunction<number>("", new DumbNumberSumDefinitionParser())) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #6 :: Revert C to A+B; Put C to sleep and change A (dependency)") | ||
NodeC.setDataFunction(new DataFunction<number>("A+B", new DumbNumberSumDefinitionParser())) | ||
grid.printNodes() | ||
|
||
NodeC.sleep() | ||
NodeA.setDataFunction(new DataFunction<number>("1+2", new DumbNumberSumDefinitionParser())) | ||
grid.printNodes() | ||
|
||
console.info("Iteration #7 :: Wake up C (should reconcile its data with the new values from A and B)") | ||
NodeC.wakeUp() | ||
grid.printNodes() | ||
|
||
export {NodeMesh, Node, DataFunction, DumbNumberSumDefinitionParser} | ||
export { NodeMesh, Node, DataFunction, BettermathDataFunction } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.