G6流程引擎技术文档
目录
简介
G6流程引擎是一个基于AntV G6构建的工艺流程图设计系统,专为MES(制造执行系统)工艺建模而设计。该引擎提供了完整的图形化流程设计能力,支持复杂的节点类型、自定义行为、交互菜单和工具栏功能。
系统采用Vue 3 Composition API架构,结合TypeScript提供强类型支持,实现了高度模块化的组件设计。通过事件驱动的状态管理模式,确保了系统的可扩展性和维护性。
项目结构
G6流程引擎采用清晰的分层架构,主要组织结构如下:
图表来源
章节来源
核心架构
系统架构概览
G6流程引擎采用了现代化的前端架构模式,结合Vue 3的新特性实现了高效的数据流管理和组件通信。
图表来源
模块依赖关系
系统各模块之间存在清晰的依赖关系,通过依赖注入和事件总线实现松耦合设计:
图表来源
章节来源
图实例管理
Core类的核心功能
Core类是整个G6流程引擎的核心,负责图实例的创建、配置和初始化。它提供了统一的接口来管理不同类型的节点和边线。
// 核心节点创建方法
createNode({ label, type }: NodeItem, { x, y, properties }: PointType) {
const id = uuidv4()
return new Create({
id,
type,
label,
x,
y,
properties,
Name: label,
name: label,
})
}
// 默认图配置
setDefaultProps() {
return {
fitCenter: true,
animate: true,
minZoom: 0.2,
maxZoom: 1.4,
defaultNode: {
type: NODES.ACTIVITIES,
labelCfg: {
style: {
fill: '#333',
fontSize: nodeFontSize,
textAlign: 'center',
textBaseline: 'middle',
fontWeight: 'bold',
},
},
},
defaultEdge: {
type: 'polyline',
labelCfg: {
style: {
fill: '#333',
stroke: '#aaa',
fontSize: fontSize - 2,
},
},
style: {
radius: 20,
offset: 45,
endArrow: true,
lineWidth: 3,
stroke: '#aaa',
},
},
}
}
图实例生命周期管理
图表来源
章节来源
节点与边线系统
基础节点组件化封装
G6流程引擎实现了完整的节点组件化体系,包括开始节点、结束节点和普通节点三种基础类型。
开始节点(StartNode)
const StartNode: {
type: string
options: Record<string, any>
} = {
type: NODES.ACTIVITY,
options: {
drawShape(cfg: Record<string, any>, group: any) {
const rect = group.addShape('rect', {
attrs: {
x: -75 - x,
y: -25 - y,
width,
height,
radius: 10,
stroke: '#5B8FF9',
fill: '#C6E5FF',
lineWidth: 3,
},
name: 'rect-shape',
})
return rect
},
},
}
普通节点(OrdinaryNode)
普通节点支持动态图标加载和状态样式配置:
const OrdinaryNode: {
type: string
options: Record<string, any>
} = {
type: NODES.ACTIVITIES,
options: {
drawShape(cfg: Record<string, any>, group: any) {
const type = cfg?.properties?.type
const isRoot = cfg?.isRoot
const rect = group.addShape('rect', {
zIndex: 1,
attrs: {
x: -width / 2,
y: -height / 2,
width,
height,
radius: 1,
stroke: '#5B8FF9',
fill: '#C6E5FF',
lineWidth: 2,
},
name: 'rect-shape',
})
group.addShape('image', {
zIndex: 100,
draggable: false,
attrs: {
x: -width / 2 + 1,
y: -height / 2 + 1,
radius: 1,
width: 20,
height: 20,
img: `/resources/assets/images/${isRoot ? NODES.ACTIVITY : type}.png`,
},
name: 'node-icon',
})
return rect
},
},
}
节点状态样式系统
图表来源
边线配置与样式
系统支持多种类型的边线配置,包括默认边线样式和状态 样式:
defaultEdge: {
type: 'polyline',
labelCfg: {
style: {
fill: '#333',
stroke: '#aaa',
fontSize: fontSize - 2,
},
},
style: {
radius: 20,
offset: 45,
endArrow: true,
lineWidth: 3,
stroke: '#aaa',
},
},
edgeStateStyles: {
active: {
stroke: '#5a84ff',
lineWidth: 6,
'text-shape': {
fontSize: fontSize,
},
},
hover: {
stroke: '#5a84ff',
lineWidth: 6,
'text-shape': {
fontSize: fontSize,
},
},
},
章节来源
事件总线与状态管理
GraphEvent类的设计原理
GraphEvent类实现了完整的事件总线系统,负责管理所有用户交互事件和状态变化。
图表来源
事件监听器注册机制
GraphEvent类在初始化时会注册各种事件监听器:
init(graph: Graph, layout: Record<string, any>) {
this.graph = graph
this.layout = layout
// 画布事件
this.graph.on('canvas:contextmenu', this.onCanvasContextMenu)
// 触摸事件
this.graph.on('canvas:touchmove', this.onNodeTouchmove)
this.graph.on('node:touchstart', this.onNodeTouchStart)
this.graph.on('edge:touchstart', this.onNodeTouchStart)
this.graph.on('touchend', this.onNodeTouchEnd)
// 鼠标事件
this.graph.on('node:contextmenu', this.nodeContextMenu)
this.graph.on('edge:contextmenu', this.edgeContextMenu)
this.graph.on('node:mouseenter', (...arg) => this.nodeMouseOver(...arg, true))
this.graph.on('node:mouseleave', (...arg) => this.nodeMouseOver(...arg, false))
this.graph.on('edge:mouseenter', (...arg) => this.edgeMouseOver(...arg, true))
this.graph.on('edge:mouseleave', (...arg) => this.edgeMouseOver(...arg, false))
this.graph.on('edge:click', (...arg) => this.edgeClick(...arg))
// 特殊事件
this.graph.on('aftercreateedge', this.onAfterCreateEdge)
}
状态存储系统
图表来源
章节来源
交互组件设计
自定义菜单系统
G6流程引擎实现了完整的右键菜单系统,支持节点和边线的不同上下文菜单。
// 菜单项配置
const contextMenu = computed<any[]>(() => {
if (graphEvent.isShowPassNode.value && graphEvent.type.value === 'canvas') {
return [{
type: 'view',
label: _t('粘贴'),
fn: onPassNode,
divided: true,
disabled: false,
icon: 'files/pass',
}]
}
const menu: any[] = [{
type: 'view',
label: _t('查看属性'),
fn: onViewDialog,
divided: true,
disabled: false,
icon: 'viewProps',
}]
if (isEdit.value) {
if (graphEvent.type.value === 'node') {
menu.push({
label: _t('复制'),
fn: onCopyNode,
divided: true,
icon: 'copy',
type: 'copy',
})
}
menu.push({
label: _t('删除'),
fn: onDelete,
type: 'del',
divided: true,
disabled: false,
icon: 'delete-menu',
})
}
return menu
})
工具栏交互设计
工具栏提供了标准化的操作界面,支持撤销、重做、缩放、布局等功能:
图表来源
扩展接口设计
系统提供了丰富的扩展接口,允许开发者添加自定义行为和布局变换:
// 行为映射接口
export const behaviorMap: {
[key: string]: string
} = {
dragCanvas: 'drag-canvas',
dragNode: 'drag-node',
zoomCanvas: 'zoom-canvas',
clickSelect: 'click-select',
createEdge: 'create-edge',
}
// 自定义行为示例
const customBehavior = {
type: 'custom-behavior',
trigger: 'click',
callback: (graph, e) => {
// 自定义点击处理逻辑
const node = graph.find('node', { id: e.item.get('id') })
if (node) {
// 执行自定义操作
node.update({
style: { fill: '#ff0000' }
})
}
}
}
章节来源
数据转换与序列化
JSON Schema序列化规范
G6流程引擎实现了完整的XML到JSON的数据转换机制,支持复杂的流程图结构序列化。
图表来源
数据转换核心算法
// XML转JSON转换
export const getJsonByXml = (xml: string, name?: string) => {
const options = {
ignoreAttributes: false,
attributeNamePrefix: '@_',
}
const parser = new XMLParser(options)
try {
const json = parser.parse(xml)
xmlData.value = json
return json
} catch (error) {
const newXml = Core.createBaseXml(name)
const json = parser.parse(newXml)
xmlData.value = json
return json
}
}
// 流程图数据转XML
export const getFlowDataToXml = ({ nodes, edges }: FlowType) => {
const Activity: any[] = []
const Transition: any[] = []
const nodeMap: Record<string, any> = {}
nodes.forEach((item) => {
const obj: Record<string, any> = {}
Object.keys(item.properties || {}).forEach((key) => {
if (isFirstLetterUpperCase(key)) {
obj[key] = item.properties[key]
}
})
const oldId = item?.properties?.oldId
const flow = flowMap.get(oldId)
obj.Name = flow?.properties?.Name || item.properties.Name || ''
nodeMap[obj.Name] = {
x: item.x,
y: item.y,
}
Activity.push(obj)
})
edges.forEach((item) => {
const obj: Record<string, any> = {}
Object.keys(item?.properties || {}).forEach((key) => {
if (isFirstLetterUpperCase(key)) {
obj[key] = item.properties[key]
}
})
// 处理条件数据
const condition = item.properties?.Condition || {}
obj.Condition = handleConditionChildren(obj.Condition)
// 处理源节点和目标节点
const oldSourceId = item.properties?.oldSourceId
const oldTargetId = item.properties?.oldTargetId
const flowSource = flowMap.get(oldSourceId)
const flowTarget = flowMap.get(oldTargetId)
obj.Source = flowSource?.properties?.Name || ''
obj.Sink = flowTarget?.properties?.Name || ''
Transition.push(obj)
})
return getXmlByJson(Activity, Transition, nodeMap)
}
高并发场景下的数据持久化策略
图表来源
章节来源
性能优化策略
图数据缓存机制
系统实现了多层缓存机制来提升性能:
- 节点映射缓存:使用Map结构缓存节点ID到节点对象的映射
- 边线映射缓存:缓存边线关系以便快速查找
- XML数据缓存:避免重复解析XML数据
// 缓存优化 示例
const flowMap: Map<string, any> = new Map()
const flowNodeMap: Map<string, any> = new Map()
const edgeMap = new Map()
const nodeMap = new Map()
// 使用缓存进行快速查找
const findNodeById = (id: string) => {
return flowNodeMap.get(id)
}
const findEdgeBySourceTarget = (source: string, target: string) => {
return edgeMap.get(`${source}-${target}`)
}