Files
esengine/packages/behavior-tree/src/editor/domain/models/Connection.ts
YHH 107439d70c Feature/runtime cdn and plugin loader (#240)
* feat(ui): 完善 UI 布局系统和编辑器可视化工具

* refactor: 移除 ModuleRegistry,统一使用 PluginManager 插件系统

* fix: 修复 CodeQL 警告并提升测试覆盖率

* refactor: 分离运行时入口点,解决 runtime bundle 包含 React 的问题

* fix(ci): 添加 editor-core 和 editor-runtime 到 CI 依赖构建步骤

* docs: 完善 ServiceContainer 文档,新增 Symbol.for 模式和 @InjectProperty 说明

* fix(ci): 修复 type-check 失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): behavior-tree 构建添加 @tauri-apps 外部依赖

* fix(ci): behavior-tree 添加 @tauri-apps/plugin-fs 类型依赖

* fix(ci): platform-web 添加缺失的 behavior-tree 依赖

* fix(lint): 移除正则表达式中不必要的转义字符
2025-11-27 20:42:46 +08:00

141 lines
3.4 KiB
TypeScript

import { ValidationError } from '../errors';
/**
* 连接类型
*/
export type ConnectionType = 'node' | 'property';
/**
* 连接领域实体
* 表示两个节点之间的连接关系
*/
export class Connection {
private readonly _from: string;
private readonly _to: string;
private readonly _fromProperty?: string;
private readonly _toProperty?: string;
private readonly _connectionType: ConnectionType;
constructor(
from: string,
to: string,
connectionType: ConnectionType = 'node',
fromProperty?: string,
toProperty?: string
) {
if (from === to) {
throw ValidationError.circularReference(from);
}
if (connectionType === 'property' && (!fromProperty || !toProperty)) {
throw new ValidationError('属性连接必须指定源属性和目标属性');
}
this._from = from;
this._to = to;
this._connectionType = connectionType;
this._fromProperty = fromProperty;
this._toProperty = toProperty;
}
get from(): string {
return this._from;
}
get to(): string {
return this._to;
}
get fromProperty(): string | undefined {
return this._fromProperty;
}
get toProperty(): string | undefined {
return this._toProperty;
}
get connectionType(): ConnectionType {
return this._connectionType;
}
/**
* 检查是否为节点连接
*/
isNodeConnection(): boolean {
return this._connectionType === 'node';
}
/**
* 检查是否为属性连接
*/
isPropertyConnection(): boolean {
return this._connectionType === 'property';
}
/**
* 检查连接是否匹配指定的条件
*/
matches(from: string, to: string, fromProperty?: string, toProperty?: string): boolean {
if (this._from !== from || this._to !== to) {
return false;
}
if (this._connectionType === 'property') {
return this._fromProperty === fromProperty && this._toProperty === toProperty;
}
return true;
}
/**
* 相等性比较
*/
equals(other: Connection): boolean {
return (
this._from === other._from &&
this._to === other._to &&
this._connectionType === other._connectionType &&
this._fromProperty === other._fromProperty &&
this._toProperty === other._toProperty
);
}
/**
* 转换为普通对象
*/
toObject(): {
from: string;
to: string;
fromProperty?: string;
toProperty?: string;
connectionType: ConnectionType;
} {
return {
from: this._from,
to: this._to,
connectionType: this._connectionType,
...(this._fromProperty && { fromProperty: this._fromProperty }),
...(this._toProperty && { toProperty: this._toProperty })
};
}
/**
* 从普通对象创建连接
*/
static fromObject(obj: {
from: string;
to: string;
fromProperty?: string;
toProperty?: string;
connectionType: ConnectionType;
}): Connection {
return new Connection(
obj.from,
obj.to,
obj.connectionType,
obj.fromProperty,
obj.toProperty
);
}
}