fix: fixed the program reduce algorithm to be flexable and correctly use the different phase variables.

ref: N25B-294
This commit is contained in:
Björn Otgaar
2025-11-18 18:47:08 +01:00
parent 0bbb6101ae
commit bb4e9d0b26
7 changed files with 128 additions and 56 deletions

View File

@@ -93,7 +93,9 @@ export function GoalReduce(node: Node, nodes: Node[]) {
}
const data = node.data as GoalNodeData;
return {
id: node.id,
label: data.label,
description: data.description,
achieved: data.achieved,
}
}

View File

@@ -6,6 +6,6 @@ import type { NormNodeData } from "./NormNode";
export const NormNodeDefaults: NormNodeData = {
label: "Norm Node",
droppable: true,
normList: [],
norm: "",
hasReduce: true,
};

View File

@@ -9,18 +9,19 @@ import {
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
import { TextField } from '../../../../components/TextField';
import useFlowStore from '../VisProgStores';
/**
* The default data dot a phase node
* @param label: the label of this phase
* @param droppable: whether this node is droppable from the drop bar (initialized as true)
* @param normList: list of strings of norms for this node
* @param norm: list of strings of norms for this node
* @param hasReduce: whether this node has reducing functionality (true by default)
*/
export type NormNodeData = {
label: string;
droppable: boolean;
normList: string[];
norm: string;
hasReduce: boolean;
};
@@ -39,25 +40,32 @@ export function NormNodeCanConnect(connection: Connection | Edge): boolean {
* @returns React.JSX.Element
*/
export default function NormNode(props: NodeProps<Node>) {
const label_input_id = `Norm_${props.id}_label_input`;
const data = props.data as NormNodeData;
return (
<>
<Toolbar nodeId={props.id} allowDelete={true}/>
<div className={`${styles.defaultNode} ${styles.nodeNorm}`}>
<div className={"flex-row gap-sm"}>
<label htmlFor={label_input_id}></label>
{props.data.label as string}
</div>
<div>
<Norms id={props.id} list={data.normList}/>
</div>
<Handle type="target" position={Position.Right} id="phase"/>
const {updateNodeData} = useFlowStore();
const text_input_id = `norm_${props.id}_text_input`;
const setValue = (value: string) => {
updateNodeData(props.id, {norm: value});
}
return <>
<Toolbar nodeId={props.id} allowDelete={true}/>
<div className={`${styles.defaultNode} ${styles.nodeNorm}`}>
<div className={"flex-row gap-sm"}>
<label htmlFor={text_input_id}>Norm :</label>
<TextField
id={text_input_id}
value={data.norm}
setValue={(val) => setValue(val)}
placeholder={"Pepper should ..."}
/>
</div>
</>
);
}
<Handle type="source" position={Position.Right} id="norms"/>
</div>
</>;
};
/**
* Reduces each Norm, including its children down into its relevant data.
@@ -70,8 +78,9 @@ export function NormReduce(node: Node, nodes: Node[]) {
}
const data = node.data as NormNodeData;
return {
id: node.id,
label: data.label,
list: data.normList,
norm: data.norm,
}
}
@@ -80,25 +89,4 @@ export function NormConnects(thisNode: Node, otherNode: Node, isThisSource: bool
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
console.warn("Impossible node connection called in EndConnects")
}
}
function Norms(props: { id: string; list: string[] }) {
const { id, list } = props;
return (
<>
<span> The norms that the robot will uphold:</span>
{
list.map((norm, idx) => {
return (
<div key={`${id}_${idx}`} className={"flex-row gap-md"}>
<TextField
value={norm}
setValue={() => { return; }}
/>
</div>
);
})
}
</>
);
}

View File

@@ -6,7 +6,9 @@ import {
} from '@xyflow/react';
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
import { NodeDefaults, NodeReduces } from '../NodeRegistry';
import { NodeDefaults, NodeReduces, NodesInPhase, NodeTypes } from '../NodeRegistry';
import useFlowStore from '../VisProgStores';
import { TextField } from '../../../../components/TextField';
/**
* The default data dot a phase node
@@ -32,22 +34,33 @@ export type PhaseNode = Node<PhaseNodeData>
* @returns React.JSX.Element
*/
export default function PhaseNode(props: NodeProps<Node>) {
const data = props.data as PhaseNodeData;
const {updateNodeData} = useFlowStore();
const updateLabel = (value: string) => updateNodeData(props.id, {...data, label: value});
const label_input_id = `phase_${props.id}_label_input`;
return (
<>
<Toolbar nodeId={props.id} allowDelete={true}/>
<div className={`${styles.defaultNode} ${styles.nodePhase}`}>
<div className={"flex-row gap-sm"}>
<label htmlFor={label_input_id}></label>
{props.data.label as string}
<label htmlFor={label_input_id}>Name:</label>
<TextField
id={label_input_id}
value={data.label}
setValue={updateLabel}
placeholder={"Phase ..."}
/>
</div>
<Handle type="target" position={Position.Left} id="target"/>
<Handle type="target" position={Position.Bottom} id="norms"/>
<Handle type="source" position={Position.Right} id="source"/>
<Handle type="source" position={Position.Bottom} id="norms"/>
</div>
</>
);
}
};
/**
* Reduces each phase, including its children down into its relevant data.
@@ -86,10 +99,53 @@ export function PhaseReduce(node: Node, nodes: Node[]) {
}
}
/**
* Reduces each phase, including its children down into its relevant data.
* @param props: The Node Properties of this node.
*/
export function PhaseReduce2(node: Node, nodes: Node[]) {
const thisnode = node as PhaseNode;
const data = thisnode.data as PhaseNodeData;
// node typings that are not in phase
let nodesNotInPhase: string[] = Object.entries(NodesInPhase)
.filter(([, f]) => !f())
.map(([t]) => t);
// node typings that then are in phase
let nodesInPhase: string[] = Object.entries(NodeTypes)
.filter(([t]) => !nodesNotInPhase.includes(t))
.map(([t]) => t);
// children nodes
let childrenNodes = nodes.filter((node) => data.children.includes(node.id));
// Build the result object
let result: Record<string, unknown> = {
id: thisnode.id,
label: data.label,
};
nodesInPhase.forEach((type) => {
let typedChildren = childrenNodes.filter((child) => child.type == type);
const reducer = NodeReduces[type as keyof typeof NodeReduces];
if (!reducer) {
console.warn(`No reducer found for node type ${type}`);
result[type + "s"] = [];
} else {
result[type + "s"] = typedChildren.map((child) => reducer(child, nodes));
}
});
return result;
}
export function PhaseConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
console.log("Connect functionality called.")
const node = thisNode as PhaseNode
const data = node.data as PhaseNodeData
if (isThisSource)
if (!isThisSource)
data.children.push(otherNode.id)
}

View File

@@ -6,7 +6,7 @@ import type { TriggerNodeData } from "./TriggerNode";
export const TriggerNodeDefaults: TriggerNodeData = {
label: "Trigger Node",
droppable: true,
triggers: [{id: "help-trigger", keyword:"help"}],
triggers: [],
triggerType: "keywords",
hasReduce: true,
};