feat: added reduce and connection logic

A rule still needs to be added to the inferred belief handles to prevent cyclical inferredBeliefs, but aside from that logic ifs finished.

ref: N25B-433
This commit is contained in:
JGerla
2026-01-12 16:44:25 +01:00
parent 0a4940bdd0
commit b869f7c071
5 changed files with 70 additions and 31 deletions

View File

@@ -0,0 +1,12 @@
import { type Node } from '@xyflow/react';
import {BasicBeliefReduce} from "./BasicBeliefNode.tsx";
import {InferredBeliefReduce} from "./InferredBeliefNode.tsx";
export function BeliefReduce(beliefNode: Node, nodes: Node[]) {
switch (beliefNode.type) {
case 'basic_belief':
return BasicBeliefReduce(beliefNode, nodes);
case 'inferred_belief':
return InferredBeliefReduce(beliefNode, nodes);
}
}

View File

@@ -8,9 +8,8 @@ export const InferredBeliefNodeDefaults: InferredBeliefNodeData = {
label: "Inferred Belief",
droppable: true,
inferredBelief: {
id: "",
left: undefined,
operator: "OR",
operator: true,
right: undefined
},
hasReduce: true,

View File

@@ -2,10 +2,12 @@ import {
type NodeProps,
Position,
type Node,
getConnectedEdges
} from '@xyflow/react';
import {useState} from "react";
import { Toolbar } from '../components/NodeComponents.tsx';
import styles from '../../VisProg.module.css';
import {BeliefReduce} from "./BeliefReduce.ts";
import switchStyles from './InferredBeliefNode.module.css';
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
import {allowOnlyConnectionsFromType} from "../HandleRules.ts";
@@ -20,18 +22,26 @@ import type {BasicBeliefType} from "./BasicBeliefNode.tsx";
export type InferredBeliefNodeData = {
label: string;
droppable: boolean;
inferredBelief: Belief;
inferredBelief: InferredBelief;
hasReduce: boolean;
};
type Belief = InferredBelief | BasicBeliefType;
type Belief = ReducedInferredBelief | BasicBeliefType;
type ReducedInferredBelief = {
id: string,
left: Belief,
operator: "AND" | "OR"
right: Belief
};
type InferredBelief = {
id: string;
left: Belief | undefined,
operator: "AND" | "OR"
right: Belief | undefined
};
left: string | undefined,
operator: boolean,
right: string | undefined,
}
// helper validation function for InferredBelief objects
// const isValidInferredBelief = (inferredBelief: InferredBelief) : boolean => {
@@ -47,7 +57,17 @@ export type InferredBeliefNode = Node<InferredBeliefNodeData>;
* @param _sourceNodeId the source of the received connection
*/
export function InferredBeliefConnectionTarget(_thisNode: Node, _sourceNodeId: string) {
// no additional connection logic exists yet
const data = _thisNode.data as InferredBeliefNodeData;
if ((useFlowStore.getState().nodes.find((node) => node.id === _sourceNodeId
&& ['basic_belief', 'inferred_belief'].includes(node.type!)))
) {
const connectedEdges = getConnectedEdges([_thisNode], useFlowStore.getState().edges);
switch(connectedEdges.find(edge => edge.source === _sourceNodeId)?.targetHandle){
case 'beliefLeft': data.inferredBelief.left = _sourceNodeId; break;
case 'beliefRight': data.inferredBelief.right = _sourceNodeId; break;
}
}
}
/**
@@ -65,7 +85,10 @@ export function InferredBeliefConnectionSource(_thisNode: Node, _targetNodeId: s
* @param _sourceNodeId the source of the disconnected connection
*/
export function InferredBeliefDisconnectionTarget(_thisNode: Node, _sourceNodeId: string) {
// no additional connection logic exists yet
const data = _thisNode.data as InferredBeliefNodeData;
if (_sourceNodeId === data.inferredBelief.left) data.inferredBelief.left = undefined;
if (_sourceNodeId === data.inferredBelief.right) data.inferredBelief.right = undefined;
}
/**
@@ -87,19 +110,18 @@ export default function InferredBeliefNode(props: NodeProps<InferredBeliefNode>)
const { updateNodeData } = useFlowStore();
// start of as an AND operator, true: "AND", false: "OR"
const [enforceAllBeliefs, setEnforceAllBeliefs] = useState(true);
function onToggle() {
setEnforceAllBeliefs(!enforceAllBeliefs);
updateNodeData(props.id, {
...data,
belief: {
inferredBelief: {
...data.inferredBelief,
operator: enforceAllBeliefs ? "AND" : "OR",
operator: enforceAllBeliefs,
}
});
}
// TODO define node
return (
<>
<Toolbar nodeId={props.id} allowDelete={true}/>
@@ -108,7 +130,7 @@ export default function InferredBeliefNode(props: NodeProps<InferredBeliefNode>)
<label className={switchStyles.operatorSwitch}>
<input
type="checkbox"
checked={enforceAllBeliefs}
checked={data.inferredBelief.operator}
onChange={onToggle}
/>
<div className={switchStyles.switchVisual}></div>
@@ -139,15 +161,22 @@ export default function InferredBeliefNode(props: NodeProps<InferredBeliefNode>)
/**
* Reduces each BasicBelief, including its children down into its core data.
* @param node - The BasicBelief node to reduce.
* @param _nodes - The list of all nodes in the current flow graph.
* @param nodes - The list of all nodes in the current flow graph.
* @returns A simplified object containing the node label and its list of BasicBeliefs.
*/
export function InferredBeliefReduce(node: Node, _nodes: Node[]) {
//const data = node.data as InferredBeliefNodeData;
const result: Record<string, unknown> = {
id: node.id,
};
export function InferredBeliefReduce(node: Node, nodes: Node[]) {
const data = node.data as InferredBeliefNodeData;
const leftBelief = nodes.find((node) => node.id === data.inferredBelief.left);
const rightBelief = nodes.find((node) => node.id === data.inferredBelief.right);
// TODO define reduce
if (!leftBelief) { throw new Error("No Left belief found")}
if (!rightBelief) { throw new Error("No Right Belief found")}
const result: Record<string, unknown> = {
id: node.id,
left: BeliefReduce(leftBelief, nodes),
operator: data.inferredBelief.operator ? "AND" : "OR",
right: BeliefReduce(rightBelief, nodes),
};
return result
}

View File

@@ -9,7 +9,7 @@ import { TextField } from '../../../../components/TextField';
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../HandleRules.ts";
import useFlowStore from '../VisProgStores';
import { BasicBeliefReduce } from './BasicBeliefNode.tsx';
import {BeliefReduce} from "./BeliefReduce.ts";
/**
* The default data dot a phase node
@@ -81,7 +81,7 @@ export default function NormNode(props: NodeProps<NormNode>) {
allowOnlyConnectionsFromHandle([{nodeType:"phase",handleId:"data"}])
]}/>
<SingleConnectionHandle type="target" position={Position.Bottom} id="beliefs" rules={[
allowOnlyConnectionsFromType(["basic_belief, inferred_belief"])
allowOnlyConnectionsFromType(["basic_belief", "inferred_belief"])
]}/>
</div>
</>;
@@ -105,11 +105,10 @@ export function NormReduce(node: Node, nodes: Node[]) {
};
if (data.condition) {
const reducer = BasicBeliefReduce; // TODO: also add inferred.
const conditionNode = nodes.find((node) => node.id === data.condition);
// In case something went wrong, and our condition doesn't actually exist;
if (conditionNode == undefined) return result;
result["condition"] = reducer(conditionNode, nodes)
result["condition"] = BeliefReduce(conditionNode, nodes)
}
return result
}

View File

@@ -11,8 +11,8 @@ import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleB
import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../HandleRules.ts";
import useFlowStore from '../VisProgStores';
import { PlanReduce, type Plan } from '../components/Plan';
import PlanEditorDialog from '../components/PlanEditor';
import { BasicBeliefReduce } from './BasicBeliefNode.tsx';
import PlanEditorDialog from '../components/PlanEditor';
import {BeliefReduce} from "./BeliefReduce.ts";
/**
* The default data structure for a Trigger node
@@ -92,7 +92,7 @@ export default function TriggerNode(props: NodeProps<TriggerNode>) {
export function TriggerReduce(node: Node, nodes: Node[]) {
const data = node.data as TriggerNodeData;
const conditionNode = data.condition ? nodes.find((n)=>n.id===data.condition) : undefined
const conditionData = conditionNode ? BasicBeliefReduce(conditionNode, nodes) : ""
const conditionData = conditionNode ? BeliefReduce(conditionNode, nodes) : ""
return {
id: node.id,
condition: conditionData, // Make sure we have a condition before reducing, or default to ""