|
| 1 | +--- |
| 2 | +name: fbp-evaluator |
| 3 | +description: Lazy graph evaluator for Flow-Based Programming. Use when evaluating FBP graphs, running dataflow computations, or working with the @fbp/evaluator package. |
| 4 | +--- |
| 5 | + |
| 6 | +Lazy graph evaluator for Flow-Based Programming. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +pnpm add @fbp/evaluator |
| 12 | +``` |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +`@fbp/evaluator` provides a lazy evaluation engine for FBP graphs. It only evaluates nodes that are needed for the requested output, making it efficient for large graphs where only a subset of nodes contribute to the result. |
| 17 | + |
| 18 | +## Basic Usage |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { evaluate } from '@fbp/evaluator'; |
| 22 | +import type { Graph } from '@fbp/types'; |
| 23 | +import type { NodeDefinitionWithImpl } from '@fbp/evaluator'; |
| 24 | + |
| 25 | +// Define node implementations |
| 26 | +const addDef: NodeDefinitionWithImpl = { |
| 27 | + context: 'js', |
| 28 | + category: 'math', |
| 29 | + type: 'js/math/add', |
| 30 | + inputs: [ |
| 31 | + { name: 'a', type: 'number' }, |
| 32 | + { name: 'b', type: 'number' } |
| 33 | + ], |
| 34 | + outputs: [{ name: 'sum', type: 'number' }], |
| 35 | + impl: (inputs) => ({ |
| 36 | + sum: (inputs.a ?? 0) + (inputs.b ?? 0) |
| 37 | + }) |
| 38 | +}; |
| 39 | + |
| 40 | +const constNumberDef: NodeDefinitionWithImpl = { |
| 41 | + context: 'js', |
| 42 | + category: 'const', |
| 43 | + type: 'js/const/number', |
| 44 | + props: [{ name: 'value', type: 'number' }], |
| 45 | + outputs: [{ name: 'value', type: 'number' }], |
| 46 | + impl: (inputs, props) => ({ |
| 47 | + value: props.value ?? 0 |
| 48 | + }) |
| 49 | +}; |
| 50 | + |
| 51 | +// Create a graph |
| 52 | +const graph: Graph = { |
| 53 | + name: 'simple-add', |
| 54 | + nodes: [ |
| 55 | + { name: 'num1', type: 'js/const/number', props: [{ name: 'value', value: 5 }] }, |
| 56 | + { name: 'num2', type: 'js/const/number', props: [{ name: 'value', value: 3 }] }, |
| 57 | + { name: 'add', type: 'js/math/add' } |
| 58 | + ], |
| 59 | + edges: [ |
| 60 | + { src: { node: 'num1', port: 'value' }, dst: { node: 'add', port: 'a' } }, |
| 61 | + { src: { node: 'num2', port: 'value' }, dst: { node: 'add', port: 'b' } } |
| 62 | + ] |
| 63 | +}; |
| 64 | + |
| 65 | +// Evaluate the graph |
| 66 | +const result = evaluate(graph, { |
| 67 | + definitions: [constNumberDef, addDef], |
| 68 | + outputNode: 'add', |
| 69 | + outputPort: 'sum' |
| 70 | +}); |
| 71 | + |
| 72 | +console.log(result); // 8 |
| 73 | +``` |
| 74 | + |
| 75 | +## Node Definition with Implementation |
| 76 | + |
| 77 | +```typescript |
| 78 | +interface NodeDefinitionWithImpl extends NodeDefinition { |
| 79 | + impl: ( |
| 80 | + inputs: Record<string, any>, |
| 81 | + props: Record<string, any> |
| 82 | + ) => Record<string, any>; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +The `impl` function receives: |
| 87 | +- `inputs`: Values from connected input ports |
| 88 | +- `props`: Property values set on the node instance |
| 89 | + |
| 90 | +It returns an object with output port names as keys. |
| 91 | + |
| 92 | +## API |
| 93 | + |
| 94 | +### `evaluate(graph, options)` |
| 95 | + |
| 96 | +Evaluates a graph starting from the specified output node/port. |
| 97 | + |
| 98 | +```typescript |
| 99 | +const result = evaluate(graph, { |
| 100 | + definitions: NodeDefinitionWithImpl[], // Node definitions with implementations |
| 101 | + outputNode: string, // Node to get output from |
| 102 | + outputPort: string, // Port to get output from |
| 103 | + inputs?: Record<string, any>, // External inputs for graphInput nodes |
| 104 | + props?: Record<string, any> // Props for graphProp nodes |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +## Features |
| 109 | + |
| 110 | +### Lazy Evaluation |
| 111 | + |
| 112 | +Only evaluates nodes that are needed for the output. If a node's output isn't connected to the requested output path, it won't be evaluated. |
| 113 | + |
| 114 | +### Multi-Input Ports |
| 115 | + |
| 116 | +Supports ports that accept multiple incoming edges. Values are collected in edge array order: |
| 117 | + |
| 118 | +```typescript |
| 119 | +const mergeDef: NodeDefinitionWithImpl = { |
| 120 | + type: 'js/array/merge', |
| 121 | + inputs: [{ name: 'items', type: 'any', multi: true }], |
| 122 | + outputs: [{ name: 'array', type: 'any[]' }], |
| 123 | + impl: (inputs) => ({ |
| 124 | + array: inputs.items // Array of all connected values |
| 125 | + }) |
| 126 | +}; |
| 127 | +``` |
| 128 | + |
| 129 | +### Boundary Nodes |
| 130 | + |
| 131 | +Supports `graphInput`, `graphOutput`, and `graphProp` boundary nodes for graph inputs/outputs/props: |
| 132 | + |
| 133 | +```typescript |
| 134 | +// Provide external inputs |
| 135 | +const result = evaluate(graph, { |
| 136 | + definitions, |
| 137 | + outputNode: 'output_result', |
| 138 | + outputPort: 'value', |
| 139 | + inputs: { a: 10, b: 20 }, // Keyed by portName |
| 140 | + props: { scale: 2.0 } // Keyed by propName |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +## Example: Building a Node Library |
| 145 | + |
| 146 | +```typescript |
| 147 | +const mathNodes: NodeDefinitionWithImpl[] = [ |
| 148 | + { |
| 149 | + context: 'js', |
| 150 | + category: 'math', |
| 151 | + type: 'js/math/add', |
| 152 | + inputs: [ |
| 153 | + { name: 'a', type: 'number' }, |
| 154 | + { name: 'b', type: 'number' } |
| 155 | + ], |
| 156 | + outputs: [{ name: 'sum', type: 'number' }], |
| 157 | + impl: (inputs) => ({ sum: (inputs.a ?? 0) + (inputs.b ?? 0) }) |
| 158 | + }, |
| 159 | + { |
| 160 | + context: 'js', |
| 161 | + category: 'math', |
| 162 | + type: 'js/math/multiply', |
| 163 | + inputs: [ |
| 164 | + { name: 'a', type: 'number' }, |
| 165 | + { name: 'b', type: 'number' } |
| 166 | + ], |
| 167 | + outputs: [{ name: 'product', type: 'number' }], |
| 168 | + impl: (inputs) => ({ product: (inputs.a ?? 1) * (inputs.b ?? 1) }) |
| 169 | + }, |
| 170 | + { |
| 171 | + context: 'js', |
| 172 | + category: 'math', |
| 173 | + type: 'js/math/negate', |
| 174 | + inputs: [{ name: 'value', type: 'number' }], |
| 175 | + outputs: [{ name: 'negated', type: 'number' }], |
| 176 | + impl: (inputs) => ({ negated: -(inputs.value ?? 0) }) |
| 177 | + } |
| 178 | +]; |
| 179 | +``` |
| 180 | + |
| 181 | +## Best Practices |
| 182 | + |
| 183 | +1. Use descriptive type paths like `context/category/name` (e.g., `js/math/add`) |
| 184 | +2. Always provide default values in `impl` functions for missing inputs |
| 185 | +3. Keep node implementations pure — no side effects |
| 186 | +4. Use `multi: true` for ports that should accept multiple connections |
0 commit comments