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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user