refactor: moved the nobeliefCyclerule into BeliefGlobals, and fixed function and filenaming

ref: N25B-433
This commit is contained in:
JGerla
2026-01-14 13:44:59 +01:00
parent 3d6e065dd5
commit 1f0237baac
4 changed files with 53 additions and 60 deletions

View File

@@ -1,17 +1,11 @@
import {
type NodeProps,
Position,
type Node,
getConnectedEdges, getOutgoers
} from '@xyflow/react';
import {getConnectedEdges, type Node, type NodeProps, Position} from '@xyflow/react';
import {useState} from "react";
import { Toolbar } from '../components/NodeComponents.tsx';
import {type HandleRule, type RuleResult, ruleResult} from "../HandleRuleLogic.ts";
import {BeliefGlobals, noMatchingLeftRightBelief} from "./BeliefGlobals.ts";
import styles from '../../VisProg.module.css';
import {Toolbar} from '../components/NodeComponents.tsx';
import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
import {allowOnlyConnectionsFromType} from "../HandleRules.ts";
import useFlowStore from "../VisProgStores.tsx";
import styles from '../../VisProg.module.css';
import {BeliefGlobalReduce, noBeliefCycles, noMatchingLeftRightBelief} from "./BeliefGlobals.ts";
import switchStyles from './InferredBeliefNode.module.css';
@@ -87,45 +81,6 @@ export function InferredBeliefDisconnectionSource(_thisNode: Node, _targetNodeId
// no additional connection logic exists yet
}
/**
* makes it impossible to connect Inferred belief nodes
* if the connection would create a cyclical connection between inferred beliefs
*/
const noBeliefCycles : HandleRule = (connection, _): RuleResult => {
const { nodes, edges } = useFlowStore.getState();
const defaultErrorMessage = "Cyclical connection exists between inferred beliefs";
/**
* recursively checks for cyclical connections between InferredBelief nodes
*
* to check for a cycle provide the source of an attempted connection as the targetNode for the cycle check,
* the currentNodeId should be initialised with the id of the targetNode of the attempted connection.
*
* @param {string} targetNodeId - the id of the node we are looking for as the endpoint of a cyclical connection
* @param {string} currentNodeId - the id of the node we are checking for outgoing connections to the provided target node
* @returns {RuleResult}
*/
function checkForCycle(targetNodeId: string, currentNodeId: string) : RuleResult {
const outgoingBeliefs = getOutgoers({id: currentNodeId}, nodes, edges)
.filter(node => node.type === 'inferred_belief');
if (outgoingBeliefs.length === 0) return ruleResult.satisfied;
if (outgoingBeliefs.some(node => node.id === targetNodeId)) return ruleResult
.notSatisfied(defaultErrorMessage);
const next = outgoingBeliefs.map(node => checkForCycle(targetNodeId, node.id))
.find(result => !result.isSatisfied);
return next
? next
: ruleResult.satisfied;
}
return connection.source === connection.target
? ruleResult.notSatisfied(defaultErrorMessage)
: checkForCycle(connection.source, connection.target);
}
/**
@@ -210,9 +165,9 @@ export function InferredBeliefReduce(node: Node, nodes: Node[]) {
const result: Record<string, unknown> = {
id: node.id,
left: BeliefGlobals(leftBelief, nodes),
left: BeliefGlobalReduce(leftBelief, nodes),
operator: data.inferredBelief.operator ? "AND" : "OR",
right: BeliefGlobals(rightBelief, nodes),
right: BeliefGlobalReduce(rightBelief, nodes),
};
return result