feat: The Big One UI #47
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,9 +8,8 @@ export const InferredBeliefNodeDefaults: InferredBeliefNodeData = {
|
|||||||
label: "Inferred Belief",
|
label: "Inferred Belief",
|
||||||
droppable: true,
|
droppable: true,
|
||||||
inferredBelief: {
|
inferredBelief: {
|
||||||
id: "",
|
|
||||||
left: undefined,
|
left: undefined,
|
||||||
operator: "OR",
|
operator: true,
|
||||||
right: undefined
|
right: undefined
|
||||||
},
|
},
|
||||||
hasReduce: true,
|
hasReduce: true,
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import {
|
|||||||
type NodeProps,
|
type NodeProps,
|
||||||
Position,
|
Position,
|
||||||
type Node,
|
type Node,
|
||||||
|
getConnectedEdges
|
||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import {useState} from "react";
|
import {useState} from "react";
|
||||||
import { Toolbar } from '../components/NodeComponents.tsx';
|
import { Toolbar } from '../components/NodeComponents.tsx';
|
||||||
import styles from '../../VisProg.module.css';
|
import styles from '../../VisProg.module.css';
|
||||||
|
import {BeliefReduce} from "./BeliefReduce.ts";
|
||||||
import switchStyles from './InferredBeliefNode.module.css';
|
import switchStyles from './InferredBeliefNode.module.css';
|
||||||
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
|
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
|
||||||
import {allowOnlyConnectionsFromType} from "../HandleRules.ts";
|
import {allowOnlyConnectionsFromType} from "../HandleRules.ts";
|
||||||
@@ -20,18 +22,26 @@ import type {BasicBeliefType} from "./BasicBeliefNode.tsx";
|
|||||||
export type InferredBeliefNodeData = {
|
export type InferredBeliefNodeData = {
|
||||||
label: string;
|
label: string;
|
||||||
droppable: boolean;
|
droppable: boolean;
|
||||||
inferredBelief: Belief;
|
inferredBelief: InferredBelief;
|
||||||
hasReduce: boolean;
|
hasReduce: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Belief = InferredBelief | BasicBeliefType;
|
|
||||||
|
|
||||||
|
type Belief = ReducedInferredBelief | BasicBeliefType;
|
||||||
|
|
||||||
|
type ReducedInferredBelief = {
|
||||||
|
id: string,
|
||||||
|
left: Belief,
|
||||||
|
operator: "AND" | "OR"
|
||||||
|
right: Belief
|
||||||
|
};
|
||||||
|
|
||||||
type InferredBelief = {
|
type InferredBelief = {
|
||||||
id: string;
|
left: string | undefined,
|
||||||
left: Belief | undefined,
|
operator: boolean,
|
||||||
operator: "AND" | "OR"
|
right: string | undefined,
|
||||||
right: Belief | undefined
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// helper validation function for InferredBelief objects
|
// helper validation function for InferredBelief objects
|
||||||
// const isValidInferredBelief = (inferredBelief: InferredBelief) : boolean => {
|
// const isValidInferredBelief = (inferredBelief: InferredBelief) : boolean => {
|
||||||
@@ -47,7 +57,17 @@ export type InferredBeliefNode = Node<InferredBeliefNodeData>;
|
|||||||
* @param _sourceNodeId the source of the received connection
|
* @param _sourceNodeId the source of the received connection
|
||||||
*/
|
*/
|
||||||
export function InferredBeliefConnectionTarget(_thisNode: Node, _sourceNodeId: string) {
|
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
|
* @param _sourceNodeId the source of the disconnected connection
|
||||||
*/
|
*/
|
||||||
export function InferredBeliefDisconnectionTarget(_thisNode: Node, _sourceNodeId: string) {
|
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();
|
const { updateNodeData } = useFlowStore();
|
||||||
// start of as an AND operator, true: "AND", false: "OR"
|
// start of as an AND operator, true: "AND", false: "OR"
|
||||||
const [enforceAllBeliefs, setEnforceAllBeliefs] = useState(true);
|
const [enforceAllBeliefs, setEnforceAllBeliefs] = useState(true);
|
||||||
|
|
||||||
function onToggle() {
|
function onToggle() {
|
||||||
setEnforceAllBeliefs(!enforceAllBeliefs);
|
setEnforceAllBeliefs(!enforceAllBeliefs);
|
||||||
|
|
||||||
updateNodeData(props.id, {
|
updateNodeData(props.id, {
|
||||||
...data,
|
...data,
|
||||||
belief: {
|
inferredBelief: {
|
||||||
...data.inferredBelief,
|
...data.inferredBelief,
|
||||||
operator: enforceAllBeliefs ? "AND" : "OR",
|
operator: enforceAllBeliefs,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO define node
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
<Toolbar nodeId={props.id} allowDelete={true}/>
|
||||||
@@ -108,7 +130,7 @@ export default function InferredBeliefNode(props: NodeProps<InferredBeliefNode>)
|
|||||||
<label className={switchStyles.operatorSwitch}>
|
<label className={switchStyles.operatorSwitch}>
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
checked={enforceAllBeliefs}
|
checked={data.inferredBelief.operator}
|
||||||
onChange={onToggle}
|
onChange={onToggle}
|
||||||
/>
|
/>
|
||||||
<div className={switchStyles.switchVisual}></div>
|
<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.
|
* Reduces each BasicBelief, including its children down into its core data.
|
||||||
* @param node - The BasicBelief node to reduce.
|
* @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.
|
* @returns A simplified object containing the node label and its list of BasicBeliefs.
|
||||||
*/
|
*/
|
||||||
export function InferredBeliefReduce(node: Node, _nodes: Node[]) {
|
export function InferredBeliefReduce(node: Node, nodes: Node[]) {
|
||||||
//const data = node.data as InferredBeliefNodeData;
|
const data = node.data as InferredBeliefNodeData;
|
||||||
const result: Record<string, unknown> = {
|
const leftBelief = nodes.find((node) => node.id === data.inferredBelief.left);
|
||||||
id: node.id,
|
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
|
return result
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ import { TextField } from '../../../../components/TextField';
|
|||||||
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
|
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
|
||||||
import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../HandleRules.ts";
|
import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../HandleRules.ts";
|
||||||
import useFlowStore from '../VisProgStores';
|
import useFlowStore from '../VisProgStores';
|
||||||
import { BasicBeliefReduce } from './BasicBeliefNode.tsx';
|
import {BeliefReduce} from "./BeliefReduce.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default data dot a phase node
|
* The default data dot a phase node
|
||||||
@@ -81,7 +81,7 @@ export default function NormNode(props: NodeProps<NormNode>) {
|
|||||||
allowOnlyConnectionsFromHandle([{nodeType:"phase",handleId:"data"}])
|
allowOnlyConnectionsFromHandle([{nodeType:"phase",handleId:"data"}])
|
||||||
]}/>
|
]}/>
|
||||||
<SingleConnectionHandle type="target" position={Position.Bottom} id="beliefs" rules={[
|
<SingleConnectionHandle type="target" position={Position.Bottom} id="beliefs" rules={[
|
||||||
allowOnlyConnectionsFromType(["basic_belief, inferred_belief"])
|
allowOnlyConnectionsFromType(["basic_belief", "inferred_belief"])
|
||||||
]}/>
|
]}/>
|
||||||
</div>
|
</div>
|
||||||
</>;
|
</>;
|
||||||
@@ -105,11 +105,10 @@ export function NormReduce(node: Node, nodes: Node[]) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (data.condition) {
|
if (data.condition) {
|
||||||
const reducer = BasicBeliefReduce; // TODO: also add inferred.
|
|
||||||
const conditionNode = nodes.find((node) => node.id === data.condition);
|
const conditionNode = nodes.find((node) => node.id === data.condition);
|
||||||
// In case something went wrong, and our condition doesn't actually exist;
|
// In case something went wrong, and our condition doesn't actually exist;
|
||||||
if (conditionNode == undefined) return result;
|
if (conditionNode == undefined) return result;
|
||||||
result["condition"] = reducer(conditionNode, nodes)
|
result["condition"] = BeliefReduce(conditionNode, nodes)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../H
|
|||||||
import useFlowStore from '../VisProgStores';
|
import useFlowStore from '../VisProgStores';
|
||||||
import { PlanReduce, type Plan } from '../components/Plan';
|
import { PlanReduce, type Plan } from '../components/Plan';
|
||||||
import PlanEditorDialog from '../components/PlanEditor';
|
import PlanEditorDialog from '../components/PlanEditor';
|
||||||
import { BasicBeliefReduce } from './BasicBeliefNode.tsx';
|
import {BeliefReduce} from "./BeliefReduce.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default data structure for a Trigger node
|
* 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[]) {
|
export function TriggerReduce(node: Node, nodes: Node[]) {
|
||||||
const data = node.data as TriggerNodeData;
|
const data = node.data as TriggerNodeData;
|
||||||
const conditionNode = data.condition ? nodes.find((n)=>n.id===data.condition) : undefined
|
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 {
|
return {
|
||||||
id: node.id,
|
id: node.id,
|
||||||
condition: conditionData, // Make sure we have a condition before reducing, or default to ""
|
condition: conditionData, // Make sure we have a condition before reducing, or default to ""
|
||||||
|
|||||||
Reference in New Issue
Block a user