@rkmodules/rules
v0.0.103
Published
`npm install @rkmodules/rules`
Readme
Usage
npm install @rkmodules/rules
Engine
An engine creates a namespace for functions, so create an engine to start with:
const engine = new Engine()
Specifying a function
A function definition is just a json object. It can be assembled together via code or just hardcoded. It is of type GraphedFunction and at least has to have a name and body field. The latter defines a graph with calls to primitives or other graphed functions:
const testFunction: GraphedFunction = {
name: "test",
body: {
myVal: {
name: "value",
params: {
type: "string",
},
},
myLog: {
name: "log",
inputs: {
data: "<myVal.value>",
},
},
},
};We get back to function definitions later
Running the function
- build all the custom functions
- run the one you need
use in react
useFunction
editor
- Create an instance of the rules engine
- create a function to work with
- use the
<Flow>component to create the editor
const engine = new Engine({
// map of custom rules
});
const emptyFunction: GraphedFunction = {
name: "test",
body: {},
outputs: {
documents: "",
},
};
export default function MyComponent() {
const [fn, setFn] = React.useState(emptyFunction);
return (
<div>
<Flow function={fn} engine={engine} onChange={setFn} />
<div>
);
}TODO:
- [ ] filtering of lists
- document grasshopper nodes
- build them
- [ ] set value
- [ ] inspection
- run time
- running flag
- intermediate results
Components / functions
List
- [x] listItem
- [x] listLength
- [x] graftTree
- [x] trimTree
Math
- [x] lessThan
- [x] greaterThan
Text
- formatting
- text replacement
grasshopper investigation
value
using functions
in React context
- use useFunction. This will build the function, mount it if needed and provides a run function.
in Vanilla context
- use engine.build(fn) to build the function
- use engine.mount(fn) to mount the function (if it has a mount function)
- use engine.run(fn, inputs) to run the function
considerations
For Supa Lipu we have quite a lot of functions that need to be called on various events
when propobjects are updated / created / deleted.
- currently, each propobject has its own engine and 1 single rule. This is fired when the object is mutated
- all functions need to be propobjects as well, then referenced in various places
- the engine needs to be global, so that functions can reference each other
- for these references, we need propobject refs, to that dependency functions are loaded
- in useObjectMutation, we fire an event such that mounted eventlisteners can react
we need deep references. On change, oldProps and changedProps are passed in as an object. So we need
- primitives to getValue and setValue
- [x] reference strings that can directly reference deeper objects to eliminate the need for getValue
[x] also need to think about data tree handling for outputs
Entries
- in finance I had filterLine(on, match) function that filters return a boolean if any of the lines match. This is a hardcoded solition, which is ok for finance. But we might come up with something more generic
