ice-entity-designer-dsl 0.0.16 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -304,8 +304,9 @@ Open `examples/entity-editor-dsl.html` (ER), `examples/flowchart-dsl.html`
304
304
  `examples/power-dsl.html` (power one-line diagram) after building.
305
305
  渲染结果可以滚轮缩放、空白处拖拽平移,工具栏还有「适应视图 / 复位视图」。
306
306
 
307
- 每个示例页渲染出来的实例都带 `toDsl()`:在浏览器控制台执行
308
- `window.__dslResult.toDsl()` 就能看到当前的 DSL 文档(改过的位置与语义都在里面)。
307
+ 每个示例页都有一个 **「导出当前文档」** 按钮:在画布上拖动 / 改名 / 分合之后点一下,
308
+ 左侧 JSON 立刻变成**你改过之后**的那份文档(仍是同一份 DSL,可直接再渲染回去)。
309
+ 在控制台执行 `window.__dslResult.toDsl()` 拿到的是同一个结果。
309
310
 
310
311
  ## Agent discovery
311
312
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("ice-entity-designer");function isFlowDsl(e){return!!e&&"object"==typeof e&&"flowchart"===e.kind}function isBpmnDsl(e){return!!e&&"object"==typeof e&&"bpmn"===e.kind}function isUmlDsl(e){return!!e&&"object"==typeof e&&"uml"===e.kind}function isStatechartDsl(e){return!!e&&"object"==typeof e&&"statechart"===e.kind}function isGanttDsl(e){return!!e&&"object"==typeof e&&"gantt"===e.kind}const t=["busbar","breaker","disconnector","loadSwitch","earthingSwitch","earth","currentTransformer","voltageTransformer","transformer","fuse","arrester","reactor","generator","motor","load","capacitor","arcSuppressionCoil","threeWindingTransformer","groundingTransformer","groundingResistor","cable","cableTermination","cubicle"];function isPowerDsl(e){return!!e&&"object"==typeof e&&"power"===e.kind}const r=["terminator","process","decision","io"],i=["T","R","B","L","C"],o=["pool","lane","task","event","gateway","subprocess","dataObject","annotation"],n=["start","intermediate","end"],s=["none","message","timer","error","terminate"],a=["exclusive","parallel","inclusive","event"],d=["none","user","service","script","send","receive","manual"],l=["sequence","message","association"],p=["initial","final","state","composite"],c=["class","interface","enum"],u=["inheritance","realization","association","aggregation","composition","dependency"];function validateDsl(e){return isGanttDsl(e)?validateGanttDsl(e):isPowerDsl(e)?validatePowerDsl(e):isStatechartDsl(e)?validateStatechartDsl(e):isUmlDsl(e)?validateUmlDsl(e):isBpmnDsl(e)?validateBpmnDsl(e):isFlowDsl(e)?validateFlowDsl(e):function validateErDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);if(Array.isArray(e.entities)){const r=new Set;e.entities.forEach((e,i)=>{const o=`entities[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):r.add(e.id):t.push(o+".id must be a non-empty string"),Array.isArray(e.fields)?e.fields.forEach((e,r)=>{e&&"object"==typeof e&&"string"==typeof e.name&&e.name.trim()||t.push(`${o}.fields[${r}].name must be a non-empty string`)}):t.push(o+".fields must be an array")):t.push(o+" must be an object")})}else t.push("entities must be an array");void 0===e.relations||Array.isArray(e.relations)?(e.relations||[]).forEach((e,r)=>{const i=`relations[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()||t.push(i+".source must be a non-empty string"),"string"==typeof e.target&&e.target.trim()||t.push(i+".target must be a non-empty string")):t.push(i+" must be an object")}):t.push("relations must be an array");return{valid:0===t.length,errors:t}}(e)}function validateBpmnDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const r=new Set,i=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,l)=>{const p=`nodes[${l}]`;if(!e||"object"!=typeof e)return void t.push(p+" must be an object");"string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${p}.id is duplicated: ${e.id}`):(r.add(e.id),i.set(e.id,e.kind||"task")):t.push(p+".id must be a non-empty string"),void 0!==e.kind&&-1===o.indexOf(e.kind)&&t.push(`${p}.kind must be one of ${o.join("/")}`),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${p}.${r} must be a number when present`)});[["eventKind",n],["trigger",s],["gatewayType",a],["taskType",d]].forEach(([r,i])=>{const o=e[r];void 0!==o&&-1===i.indexOf(o)&&t.push(`${p}.${r} must be one of ${i.join("/")}`)})}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;if("string"!=typeof e.parent||!e.parent.trim())return void t.push(n+".parent must be a non-empty string when present");if(!r.has(e.parent))return void t.push(n+".parent must reference an existing node id");if(e.parent===e.id)return void t.push(n+".parent must not reference itself");const s=i.get(e.parent);"pool"===e.kind?t.push(n+".parent is not allowed on a pool (池是最外层容器)"):"lane"===e.kind&&"pool"!==s?t.push(n+".parent must reference a pool"):"pool"!==s&&"lane"!==s&&t.push(n+".parent must reference a pool or a lane")})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;if(!e||"object"!=typeof e)return void t.push(o+" must be an object");"string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(o+".target must reference an existing node id");const n=void 0!==e.type?e.type:e.flowType;void 0!==n&&-1===l.indexOf(n)&&t.push(`${o}.type must be one of ${l.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(o+".label must be a string when present"),void 0!==e.condition&&"string"!=typeof e.condition&&t.push(o+".condition must be a string when present"),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(o+'.linkShape must be "visio" or "bezier" when present')}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateFlowDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const o=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,i)=>{const n=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?o.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):o.add(e.id):t.push(n+".id must be a non-empty string"),void 0!==e.kind&&-1===r.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${r.join("/")}`),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${n}.${r} must be a number when present`)})):t.push(n+" must be an object")}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const n=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&o.has(e.source)||t.push(n+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&o.has(e.target)||t.push(n+".target must reference an existing node id"),void 0!==e.label&&"string"!=typeof e.label&&t.push(n+".label must be a string when present"),void 0!==e.sourcePort&&-1===i.indexOf(e.sourcePort)&&t.push(`${n}.sourcePort must be one of ${i.join("/")}`),void 0!==e.targetPort&&-1===i.indexOf(e.targetPort)&&t.push(`${n}.targetPort must be one of ${i.join("/")}`),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(n+'.linkShape must be "visio" or "bezier" when present')):t.push(n+" must be an object")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateUmlDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const r=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,i)=>{const o=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):r.add(e.id):t.push(o+".id must be a non-empty string"),void 0!==e.kind&&-1===c.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${c.join("/")}`),["attributes","methods"].forEach(r=>{const i=e[r];void 0!==i&&(Array.isArray(i)&&!i.some(e=>"string"!=typeof e)||t.push(`${o}.${r} must be an array of strings when present`))}),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${o}.${r} must be a number when present`)})):t.push(o+" must be an object")}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;if(!e||"object"!=typeof e)return void t.push(o+" must be an object");"string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(o+".target must reference an existing node id");const n=void 0!==e.type?e.type:e.relation;void 0!==n&&-1===u.indexOf(n)&&t.push(`${o}.type must be one of ${u.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(o+".label must be a string when present")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateStatechartDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const r=new Set,i=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,o)=>{const n=`nodes[${o}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):(r.add(e.id),i.set(e.id,e.kind||"state")):t.push(n+".id must be a non-empty string"),void 0!==e.kind&&-1===p.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${p.join("/")}`),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${n}.${r} must be a number when present`)})):t.push(n+" must be an object")}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;"string"==typeof e.parent&&e.parent.trim()&&r.has(e.parent)?e.parent!==e.id?"composite"!==i.get(e.parent)&&t.push(n+".parent must reference a composite state"):t.push(n+".parent must not reference itself"):t.push(n+".parent must reference an existing node id")})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(o+".target must reference an existing node id"),["event","guard","action","label"].forEach(r=>{const i=e[r];void 0!==i&&"string"!=typeof i&&t.push(`${o}.${r} must be a string when present`)})):t.push(o+" must be an object")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateGanttDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const r=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,i)=>{const o=`nodes[${i}]`;if(e&&"object"==typeof e){if("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):r.add(e.id):t.push(o+".id must be a non-empty string"),"string"==typeof e.start&&/^\d{4}-\d{2}-\d{2}$/.test(e.start)||t.push(o+'.start must be a "YYYY-MM-DD" string'),void 0===e.days||Number(e.days)>=1||t.push(o+".days must be a number >= 1 when present"),void 0!==e.progress){const r=Number(e.progress);r>=0&&r<=1||t.push(o+".progress must be between 0 and 1 when present")}void 0===e.row||Number(e.row)>=0||t.push(o+".row must be a number >= 0 when present")}else t.push(o+" must be an object")}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(o+".source must reference an existing task id"),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(o+".target must reference an existing task id")):t.push(o+" must be an object")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validatePowerDsl(e){const r=[],i=Array.isArray(e.nodes)?e.nodes:[];Array.isArray(e.nodes)||r.push("nodes must be an array");const o=new Set;return i.forEach((e,i)=>{const n=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id?o.has(e.id)?r.push("duplicated node id: "+e.id):o.add(e.id):r.push(n+".id must be a non-empty string"),-1===t.indexOf(e.kind)&&r.push(`${n}.kind must be one of ${t.join(" / ")}`),void 0===e.voltageLevel||/^\d+(\.\d+)?kV$/.test(String(e.voltageLevel))||r.push(n+'.voltageLevel must look like "110kV"')):r.push(n+" must be an object")}),i.forEach((e,t)=>{if(!e||void 0===e.attachedTo)return;const o=i.find(t=>t&&t.id===e.attachedTo);o?"busbar"!==o.kind&&r.push(`nodes[${t}].attachedTo must point at a busbar: ${e.attachedTo} is ${o.kind}`):r.push(`nodes[${t}].attachedTo references an unknown node: ${e.attachedTo}`)}),(e.edges||[]).forEach((e,t)=>{const i=`edges[${t}]`;e&&"object"==typeof e?(o.has(e.source)||r.push(`${i}.source references an unknown node: ${e.source}`),o.has(e.target)||r.push(`${i}.target references an unknown node: ${e.target}`),e.source===e.target&&r.push(i+" must not connect a node to itself")):r.push(i+" must be an object")}),{valid:0===r.length,errors:r}}function entityName(e){return e.entityName||e.name||e.id}function relationType(e){return e.relationType||e.type||"one-to-many"}function compileDsl(e){if(isFlowDsl(e))throw new Error("compileDsl() 只处理 ER 文档;流程图文档请使用 compileFlowDsl()");if(!e||!Array.isArray(e.entities))throw new Error("ER 文档必须包含 entities 数组");return{entities:e.entities.map(e=>({id:e.id,entityName:entityName(e),fields:e.fields||[],left:e.left,top:e.top,width:e.width,height:e.height,style:e.style,headerStyle:e.headerStyle,fieldStyle:e.fieldStyle,dividerStyle:e.dividerStyle,draggable:e.draggable,interactive:e.interactive})),relations:(e.relations||[]).map((e,t)=>({id:e.id||"relation-"+t,relationType:relationType(e),sourceId:e.source,targetId:e.target,sourceField:e.sourceField||"id",targetField:e.targetField||"id",nullable:e.nullable,onDelete:e.onDelete,onUpdate:e.onUpdate,joinTableName:e.joinTableName,fromKey:e.fromKey,toKey:e.toKey,sourceCardinality:e.sourceCardinality,targetCardinality:e.targetCardinality,label:e.label,style:e.style,arrow:e.arrow,linkShape:e.linkShape,routeType:e.routeType,routeOffset:e.routeOffset,curveType:e.curveType,lineDash:e.lineDash})),layout:e.layout,options:e.options}}function layeredLayout(e,t,r={}){const i=Number(r.gapX)||90,o=Number(r.gapY)||90,n=void 0===r.originX?80:r.originX,s=void 0===r.originY?80:r.originY,a=new Map;if(!e.length)return a;const d=new Map;e.forEach(e=>d.set(e.id,e));const l=new Map,p=new Map;e.forEach(e=>{l.set(e.id,0),p.set(e.id,[])}),t.forEach(e=>{d.has(e.sourceId)&&d.has(e.targetId)&&(l.set(e.targetId,(l.get(e.targetId)||0)+1),p.get(e.sourceId).push(e.targetId))});const c=new Map,u=[];e.forEach(e=>{0===(l.get(e.id)||0)&&(c.set(e.id,0),u.push(e.id))}),u.length||(c.set(e[0].id,0),u.push(e[0].id));let h=e.length*e.length+e.length;for(;u.length&&h-- >0;){const e=u.shift(),t=c.get(e)||0;(p.get(e)||[]).forEach(e=>{const r=t+1;(void 0===c.get(e)?-1:c.get(e))<r&&(c.set(e,r),u.push(e))})}e.forEach(e=>{c.has(e.id)||c.set(e.id,0)});const f=new Map;e.forEach(e=>{const t=c.get(e.id)||0;f.has(t)||f.set(t,[]),f.get(t).push(e)});const m=[...f.keys()].sort((e,t)=>e-t);if("horizontal"===r.direction){const e=m.map(e=>Math.max(...f.get(e).map(e=>e.width)));let t=0;m.forEach((r,n)=>{const s=f.get(r);let d=-(s.reduce((e,t)=>e+t.height,0)+Math.max(s.length-1,0)*o)/2;s.forEach(r=>{a.set(r.id,{left:Math.round(t+(e[n]-r.width)/2),top:Math.round(d)}),d+=r.height+o}),t+=e[n]+i})}else{const e=m.map(e=>Math.max(...f.get(e).map(e=>e.height)));let t=0;m.forEach((r,n)=>{const s=f.get(r);let d=-(s.reduce((e,t)=>e+t.width,0)+Math.max(s.length-1,0)*i)/2;s.forEach(r=>{a.set(r.id,{left:Math.round(d),top:Math.round(t+(e[n]-r.height)/2)}),d+=r.width+i}),t+=e[n]+o})}const g=Math.min(...e.map(e=>a.get(e.id).left)),b=Math.min(...e.map(e=>a.get(e.id).top));return a.forEach(e=>{e.left+=n-g,e.top+=s-b}),a}const h={terminator:{width:e.FLOW_NODE_KINDS.terminator.width,height:e.FLOW_NODE_KINDS.terminator.height,fill:e.FLOW_NODE_KINDS.terminator.fill,stroke:e.FLOW_NODE_KINDS.terminator.stroke},process:{width:e.FLOW_NODE_KINDS.process.width,height:e.FLOW_NODE_KINDS.process.height,fill:e.FLOW_NODE_KINDS.process.fill,stroke:e.FLOW_NODE_KINDS.process.stroke},decision:{width:e.FLOW_NODE_KINDS.decision.width,height:e.FLOW_NODE_KINDS.decision.height,fill:e.FLOW_NODE_KINDS.decision.fill,stroke:e.FLOW_NODE_KINDS.decision.stroke},io:{width:e.FLOW_NODE_KINDS.io.width,height:e.FLOW_NODE_KINDS.io.height,fill:e.FLOW_NODE_KINDS.io.fill,stroke:e.FLOW_NODE_KINDS.io.stroke}};function compileFlowDsl(e){const t=e.options||{},r=(e.nodes||[]).map(e=>{const t=function nodeKind$1(e){return e.kind&&h[e.kind]?e.kind:"process"}(e),r=h[t];return{id:e.id,typeId:"FlowNode",kind:t,title:(i=e,i.title||i.name||i.id),left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:r.width,height:"number"==typeof e.height?e.height:r.height,fillColor:e.fillColor||r.fill,strokeColor:e.strokeColor||r.stroke};var i}),i=(e.edges||[]).map((e,t)=>({id:e.id||"edge-"+t,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",label:e.label||"",linkShape:e.linkShape||"visio"})),o=t.layout||"layered";return"none"!==o&&r.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))&&(r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),function autoLayout(e,t,r,i){const o=layeredLayout(e,t,{gapX:r,gapY:i});e.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)})}(r,i,Number(t.gapX)||90,Number(t.gapY)||90)),r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"flowchart",nodes:r,edges:i,layout:o,options:t}}const f={pool:"bpmnPool",lane:"bpmnLane",task:"bpmnTask",event:"bpmnEvent",gateway:"bpmnGateway",subprocess:"bpmnSubprocess",dataObject:"bpmnDataObject",annotation:"bpmnAnnotation"},m=Number(e.FLOW_NODE_KINDS.bpmnPool.bandSize)||32,g=Number(e.FLOW_NODE_KINDS.bpmnLane.bandSize)||32,b=Number(e.FLOW_NODE_KINDS.bpmnLane.height)||130,y=Number(e.FLOW_NODE_KINDS.bpmnPool.width)||900;function toWorkingNode(t){const r=t.kind||"task",i=function presetOf(t){return e.FLOW_NODE_KINDS[t]||e.FLOW_NODE_KINDS.bpmnTask}(f[r]||"bpmnTask"),o={id:t.id,typeId:"FlowNode",kind:f[r]||"bpmnTask",title:t.title||t.name||t.id,left:"number"==typeof t.left?t.left:0,top:"number"==typeof t.top?t.top:0,width:"number"==typeof t.width?t.width:i.width,height:"number"==typeof t.height?t.height:i.height,fillColor:t.fillColor,strokeColor:t.strokeColor,dslKind:r,parent:t.parent,explicitLeft:"number"==typeof t.left,explicitTop:"number"==typeof t.top,explicitWidth:"number"==typeof t.width,explicitHeight:"number"==typeof t.height};return"event"===r&&(o.eventKind=t.eventKind||i.eventKind||"start",o.trigger=t.trigger||i.trigger||"none"),"gateway"===r&&(o.gatewayType=t.gatewayType||i.gatewayType||"exclusive"),"task"!==r&&"subprocess"!==r||(o.taskType=t.taskType||i.taskType||"none"),o}function buildGroups(e,t){const r=new Map;t.forEach(e=>r.set(e.id,e));const i=1===t.length?t[0]:null,o=[];return e.forEach(e=>{(e=>{let t=o.filter(t=>t.owner===e)[0];return t||(t={owner:e,nodes:[],box:null,didLayout:!1},o.push(t)),t})((e.parent?r.get(e.parent):void 0)||i).nodes.push(e)}),o}function layoutGroups(e,t,r,i){e.forEach(e=>{if(e.nodes.some(e=>!e.explicitLeft||!e.explicitTop)){const o=layeredLayout(e.nodes,t,{gapX:r,gapY:i,originX:0,originY:0,direction:"horizontal"});e.nodes.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)}),e.didLayout=!0}e.box=function unionBox(e){if(!e.length)return null;let t=1/0,r=1/0,i=-1/0,o=-1/0;return e.forEach(e=>{t=Math.min(t,e.left),r=Math.min(r,e.top),i=Math.max(i,e.left+e.width),o=Math.max(o,e.top+e.height)}),{minX:t,minY:r,maxX:i,maxY:o}}(e.nodes)})}function compileBpmnDsl(e){const t=e.options||{},r=(e.nodes||[]).map(toWorkingNode),i=r.filter(e=>"pool"===e.dslKind),o=r.filter(e=>"lane"===e.dslKind),n=r.filter(e=>!function isContainer(e){return"pool"===e.dslKind||"lane"===e.dslKind}(e)),s=(e.edges||[]).map((e,t)=>({id:e.id||"edge-"+t,sourceId:e.source,targetId:e.target,flowType:e.type||e.flowType||"sequence",label:e.label||"",condition:e.condition||"",isDefault:!!e.isDefault,linkShape:e.linkShape||"visio"})),a=t.layout||"auto",d=function resolveLaneOwners(e,t){const r=new Map;return e.forEach(e=>{let i=e.parent;i||1!==t.length||(i=t[0].id),i&&r.set(e.id,i)}),r}(o,i),lanesOf=e=>o.filter(t=>d.get(t.id)===e);let l=[];"none"!==a&&(l=buildGroups(n,i.concat(o)),layoutGroups(l,s,Number(t.gapX)||90,Number(t.gapY)||60)),o.forEach(e=>{const t=l.filter(t=>t.owner===e)[0],r=t?t.box:null;e.explicitHeight||(e.height=Math.round(Math.max(56+(r?r.maxY-r.minY:0),b))),e.explicitWidth||(e.width=Math.round(Math.max((r?r.maxX-r.minX:0)+g+56,y)))}),i.forEach(e=>{const t=lanesOf(e.id);if(t.length){const r=t.reduce((e,t)=>Math.max(e,t.width),y);return e.explicitWidth||(e.width=r),e.explicitWidth&&t.forEach(t=>{t.explicitWidth||(t.width=e.width)}),void(e.explicitHeight||(e.height=m+t.reduce((e,t)=>e+t.height,0)))}const r=l.filter(t=>t.owner===e)[0],i=r?r.box:null;e.explicitWidth||(e.width=Math.round(Math.max(56+(i?i.maxX-i.minX:0),y))),e.explicitHeight||(e.height=Math.round(Math.max((i?i.maxY-i.minY:0)+m+56,160)))});let p=60;i.forEach(e=>{e.explicitLeft||(e.left=60),e.explicitTop||(e.top=p),p=Math.max(p,e.top+e.height+60)}),i.forEach(e=>{const t=lanesOf(e.id);if(!t.length)return;const r=e.top+m,i=Math.max(e.height-m,1),o=t.reduce((e,t)=>e+(t.explicitHeight?t.height:0),0),n=t.filter(e=>!e.explicitHeight),s=n.length?Math.max(i-o,0)/n.length:0;let a=r;t.forEach(t=>{t.explicitLeft||(t.left=e.left),t.explicitTop||(t.top=a),!t.explicitHeight&&e.explicitHeight&&(t.height=Math.round(s||b)),a=t.top+t.height})});let c=60;o.forEach(e=>{d.get(e.id)||(e.explicitLeft||(e.left=60),e.explicitTop||(e.top=c,c=e.top+e.height+60))}),l.forEach(e=>{const t=e.box;if(!t||!e.didLayout)return;const r=e.owner;let i=80,o=80;r&&(i=r.left+28,o=r.top+28,"pool"===r.dslKind?o+=m:i+=g),e.nodes.forEach(e=>{e.left=Math.round(i+(e.left-t.minX)),e.top=Math.round(o+(e.top-t.minY))})});return{kind:"bpmn",nodes:i.concat(o).concat(n).map(e=>{const t={id:e.id,typeId:"FlowNode",kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};return void 0!==e.eventKind&&(t.eventKind=e.eventKind),void 0!==e.trigger&&(t.trigger=e.trigger),void 0!==e.gatewayType&&(t.gatewayType=e.gatewayType),void 0!==e.taskType&&(t.taskType=e.taskType),void 0!==e.fillColor&&(t.fillColor=e.fillColor),void 0!==e.strokeColor&&(t.strokeColor=e.strokeColor),t}),edges:s,layout:a,options:t}}function compileUmlDsl(e){const t=e.options||{},r=(e.nodes||[]).map(e=>{return{id:e.id,kind:(t=e,t.kind||"class"),className:e.title||e.name||e.id,abstract:!!e.abstract,attributes:Array.isArray(e.attributes)?e.attributes.map(e=>String(e)):[],methods:Array.isArray(e.methods)?e.methods.map(e=>String(e)):[],left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:240,height:"number"==typeof e.height?e.height:120+24*(e.attributes?e.attributes.length:0)+24*(e.methods?e.methods.length:0)};var t}),i=(e.edges||[]).map((e,t)=>({id:e.id||"relation-"+t,sourceId:e.source,targetId:e.target,relationKind:e.type||e.relation||"association",label:e.label||""})),o=t.layout||"layered";if("none"!==o&&r.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const e=layeredLayout(r,i.filter(e=>"inheritance"===e.relationKind||"realization"===e.relationKind).map(e=>({sourceId:e.targetId,targetId:e.sourceId})),{gapX:Number(t.gapX)||80,gapY:Number(t.gapY)||110,originX:80,originY:80,direction:"vertical"});r.forEach(t=>{const r=e.get(t.id);r&&(t.left=r.left,t.top=r.top)})}return r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"uml",nodes:r,edges:i,layout:o,options:t}}const w={width:160,height:64},v={width:500,height:200};function compileStatechartDsl(e){const t=e.options||{},r=(e.nodes||[]).map(e=>{const t=e.kind||"state",r="initial"===t||"final"===t,i="composite"===t?v:r?{width:24,height:24}:w;return{id:e.id,kind:t,title:e.title||e.name||(r?"":e.id),parent:e.parent,left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:i.width,height:"number"==typeof e.height?e.height:i.height}}),i=(e.edges||[]).map((e,t)=>({id:e.id||"transition-"+t,sourceId:e.source,targetId:e.target,event:e.event||"",guard:e.guard||"",action:e.action||"",label:e.label})),o=t.layout||"auto",n=new Set;if("none"!==o){const e=new Map;r.forEach(t=>e.set(t.id,t));const o=r.filter(t=>!(t.parent&&e.has(t.parent)));if(o.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){const containerOf=t=>{let r=e.get(t);for(;r&&r.parent&&e.has(r.parent);)r=e.get(r.parent);return r?r.id:t},r=i.map(e=>({sourceId:containerOf(e.sourceId),targetId:containerOf(e.targetId)})).filter(e=>e.sourceId!==e.targetId),n=layeredLayout(o,r,{gapX:Number(t.gapX)||110,gapY:Number(t.gapY)||80,originX:80,originY:80,direction:"horizontal"});o.forEach(e=>{const t=n.get(e.id);t&&(e.left=t.left,e.top=t.top)})}let s=0;r.filter(e=>"composite"===e.kind).forEach(e=>{const t=r.filter(t=>t.parent===e.id);if(Number.isNaN(e.left)&&(e.left=80),Number.isNaN(e.top)){const t=Math.max(...o.filter(t=>t!==e&&!Number.isNaN(t.top)).map(e=>e.top+e.height),0);e.top=Math.max(t+60,80)+s,s+=e.height+60}if(!t.length)return;let i=56,a=56,d=0,l=56;const p=e.width-112;t.forEach(e=>{(Number.isNaN(e.left)||Number.isNaN(e.top))&&(i>56&&i+e.width>56+p&&(i=56,a=l+40),n.add(e.id),e.left=i,e.top=a,i+=e.width+40,d=Math.max(d,i),l=Math.max(l,a+e.height))}),e.width=Math.max(e.width,d+56),e.height=Math.max(e.height,l+56)}),r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const offsetChildren=(e,t,i)=>{r.filter(t=>t.parent===e.id).forEach(e=>{n.has(e.id)&&(e.left+=t,e.top+=i,offsetChildren(e,t,i))})};r.filter(e=>"composite"===e.kind).forEach(e=>offsetChildren(e,e.left,e.top))}return{kind:"statechart",nodes:r,edges:i,layout:o,options:t}}function compileGanttDsl(e){const t=e.options||{};return{kind:"gantt",nodes:(e.nodes||[]).map((e,t)=>({id:e.id,title:e.title||e.name||e.id,start:String(e.start||""),days:"number"==typeof e.days?e.days:1,progress:"number"==typeof e.progress?e.progress:0,row:"number"==typeof e.row?e.row:t,resource:String(e.resource||"")})),edges:(e.edges||[]).map((e,t)=>({id:e.id||"dependency-"+t,sourceId:e.source,targetId:e.target})),options:t}}function compilePowerDsl(e){const t=e.options||{},r=(e.nodes||[]).map((e,t)=>{const r=function defaultPlacement(e){return{left:120+e%6*200,top:120+160*Math.floor(e/6)}}(t);return{id:e.id,kind:e.kind,title:e.title||e.name||e.id,voltageLevel:String(e.voltageLevel||""),left:"number"==typeof e.left?e.left:r.left,top:"number"==typeof e.top?e.top:r.top,width:e.width,height:e.height,switchState:e.switchState,source:!!e.source}}),i=new Map;r.forEach(e=>i.set(e.id,e));return{kind:"power",nodes:r,edges:(e.edges||[]).map((e,t)=>({id:e.id||"conductor-"+t,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",voltageLevel:e.voltageLevel||(i.get(e.source)||{}).voltageLevel||""})),attachments:(e.nodes||[]).filter(e=>!!e.attachedTo).map(e=>({deviceId:e.id,busId:String(e.attachedTo)})),options:t}}const D={bpmnPool:"pool",bpmnLane:"lane",bpmnTask:"task",bpmnEvent:"event",bpmnGateway:"gateway",bpmnSubprocess:"subprocess",bpmnDataObject:"dataObject",bpmnAnnotation:"annotation"},O=["terminator","process","decision","io"],x=["class","interface","enum"],k=["initial","final","state","composite"];function stateOf(e){return e&&e.state||{}}function linkOf(e,t){const r=e.links;return r&&r[t]||{}}function absolutePosition(e){let t=0,r=0,i=e;for(;i&&i.state&&"number"==typeof i.state.left;)t+=i.state.left||0,r+=i.state.top||0,i=i.parentNode;return{left:Math.round(t),top:Math.round(r)}}function parentIdOf(e){const t=e&&e.parentNode,r=t&&t.state?t.state.id:void 0;return"string"==typeof r&&r.length>0?r:void 0}function numberOf(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function textOf(e){return"string"==typeof e&&e.length>0?e:void 0}function clean(e){const t={};return Object.keys(e).forEach(r=>{void 0!==e[r]&&(t[r]=e[r])}),t}function nodesOf(e){return e&&Array.isArray(e.nodes)?e.nodes:[]}function edgesOf(e){return e&&Array.isArray(e.edges)?e.edges:[]}function baseOf(e){const t=stateOf(e),r=absolutePosition(e);return{id:t.id,title:textOf(t.title),left:r.left,top:r.top}}function toDsl(e,r={}){const i=e&&e.designer?e.designer:e,o=r.kind||e&&e.kind||function detectKind(e){if(!e)return;if(Array.isArray(e.entities))return"entity";if(void 0!==e.originDate||void 0!==e.dayWidth)return"gantt";const r=nodesOf(e)[0],i=r?String(stateOf(r).kind||""):"";return i.startsWith("bpmn")?"bpmn":x.indexOf(i)>=0?"uml":k.indexOf(i)>=0?"statechart":t.indexOf(i)>=0?"power":O.indexOf(i)>=0?"flowchart":void 0}(i);if(!o)throw new Error("toDsl(): 无法推断文档类型(实例里没有可识别的图元)。请显式传入 kind:'flowchart' | 'bpmn' | 'uml' | 'statechart' | 'gantt' | 'power' | 'entity'。");switch(o){case"flowchart":return function exportFlow(e){return{schemaVersion:1,kind:"flowchart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:t.kind,width:numberOf(t.width),height:numberOf(t.height),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,sourcePort:r.position,targetPort:i.position,label:textOf(t.label)||"",linkShape:textOf(t.linkShape)})})}}(i);case"bpmn":return function exportBpmn(e){return{schemaVersion:1,kind:"bpmn",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:D[t.kind]||"task",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height),eventKind:textOf(t.eventKind),trigger:textOf(t.trigger),gatewayType:textOf(t.gatewayType),taskType:textOf(t.taskType),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,type:textOf(t.flowType)||"sequence",label:textOf(t.label),condition:textOf(t.condition),isDefault:!0===t.isDefault||void 0,linkShape:textOf(t.linkShape)})})}}(i);case"uml":return function exportUml(e){return{schemaVersion:1,kind:"uml",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"class",title:textOf(t.className)||textOf(t.title),abstract:!0===t.abstract||void 0,attributes:Array.isArray(t.attributes)?t.attributes.map(e=>String(e)):[],methods:Array.isArray(t.methods)?t.methods.map(e=>String(e)):[],width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,type:textOf(t.relationKind)||"association",label:textOf(t.label)})})}}(i);case"statechart":return function exportStatechart(e){return{schemaVersion:1,kind:"statechart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"state",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,event:textOf(t.event),guard:textOf(t.guard),action:textOf(t.action),label:textOf(t.label)})})}}(i);case"gantt":return function exportGantt(e){return{schemaVersion:1,kind:"gantt",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({id:t.id,title:textOf(t.title),start:textOf(t.start),days:numberOf(t.days),progress:numberOf(t.progress),row:numberOf(t.row),resource:textOf(t.resource)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id})})}}(i);case"power":return function exportPower(e){return{schemaVersion:1,kind:"power",nodes:nodesOf(e).map(e=>{const t=stateOf(e),r=absolutePosition(e);return clean({id:t.id,kind:t.kind,title:textOf(t.title)||textOf(t.name),voltageLevel:textOf(t.voltageLevel),left:r.left,top:r.top,width:numberOf(t.width),height:numberOf(t.height),switchState:textOf(t.switchState),source:!0===t.source||!0===t.energizedSource||void 0,attachedTo:textOf(t.attachedBusId)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,sourcePort:r.position,targetPort:i.position,voltageLevel:textOf(t.voltageLevel)})})}}(i);case"entity":case"er":default:return function exportEr(e){const t=e&&Array.isArray(e.entities)?e.entities:[],r=e&&Array.isArray(e.relations)?e.relations:[];return{schemaVersion:1,entities:t.map(e=>{const t=stateOf(e),r=absolutePosition(e);return clean({id:t.id,name:textOf(t.entityName),fields:Array.isArray(t.fields)?t.fields:[],left:r.left,top:r.top})}),relations:r.map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,type:t.relationType,sourceField:textOf(t.sourceField),targetField:textOf(t.targetField),label:textOf(t.label),onDelete:textOf(t.onDelete),onUpdate:textOf(t.onUpdate),joinTableName:textOf(t.joinTableName),nullable:"boolean"==typeof t.nullable?t.nullable:void 0,sourceCardinality:textOf(t.sourceCardinality),targetCardinality:textOf(t.targetCardinality),linkShape:textOf(t.linkShape)})})}}(i)}}function withDsl(e){return Object.assign(e,{toDsl:()=>toDsl(e)})}function renderGanttDsl(t,r){const i=compileGanttDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.GanttDesigner(n);return o.dayWidth&&s.setDayWidth(o.dayWidth),i.nodes.forEach(e=>{s.createTask({id:e.id,title:e.title,start:e.start,days:e.days,progress:e.progress,row:e.row,resource:e.resource})}),i.edges.forEach(e=>{s.createDependency(e)}),s.select(null),o.autoSchedule&&s.autoSchedule(),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"gantt",ice:n,designer:s})}function renderPowerDsl(t,r){const i=compilePowerDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.PowerDesigner(n);o.voltageColors&&s.setVoltageColors(o.voltageColors);const a=new Map;return i.nodes.forEach(e=>{const t=s.createSymbol(e.kind,{id:e.id,title:e.title,name:e.title,voltageLevel:e.voltageLevel,left:e.left,top:e.top,width:e.width,height:e.height,switchState:e.switchState});e.source&&t.setState({energizedSource:!0}),a.set(e.id,t)}),i.attachments.forEach(e=>{const t=a.get(e.deviceId),r=a.get(e.busId);t&&r&&s.attachToBus(t,r)}),i.edges.forEach(e=>{const t=a.get(e.sourceId),r=a.get(e.targetId);t&&r&&s.createLine({sourceId:t.state.id,targetId:r.state.id,sourcePort:e.sourcePort,targetPort:e.targetPort,voltageLevel:e.voltageLevel})}),s.select(null),s.applyTopology(),!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"power",ice:n,designer:s})}function renderStatechartDsl(t,r){const i=compileStatechartDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.StatechartDesigner(n);return i.nodes.forEach(e=>{s.createState({id:e.id,kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height})}),i.edges.forEach(e=>{s.createTransition(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"statechart",ice:n,designer:s})}function renderUmlDsl(t,r){const i=compileUmlDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.UmlDesigner(n);return i.nodes.forEach(e=>{s.createClass({id:e.id,kind:e.kind,className:e.className,abstract:e.abstract,attributes:e.attributes,methods:e.methods,left:e.left,top:e.top,width:e.width})}),i.edges.forEach(e=>{s.createRelation(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"uml",ice:n,designer:s})}function renderBpmnDsl(t,r){const i=compileBpmnDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.BpmnDesigner(n);return i.nodes.forEach(e=>{const t={id:e.id,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};["eventKind","trigger","gatewayType","taskType","fillColor","strokeColor"].forEach(r=>{void 0!==e[r]&&(t[r]=e[r])}),s.createNode(e.kind,t)}),i.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"bpmn",ice:n,designer:s})}function renderFlowDsl(t,r){const i=compileFlowDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.FlowDesigner(n);return i.nodes.forEach(e=>{s.createNode(e.kind,e)}),i.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"flowchart",ice:n,designer:s})}exports.DSL_SCHEMA_VERSION=1,exports.POWER_DSL_KINDS=t,exports.compileBpmnDsl=compileBpmnDsl,exports.compileDsl=compileDsl,exports.compileFlowDsl=compileFlowDsl,exports.compileGanttDsl=compileGanttDsl,exports.compilePowerDsl=compilePowerDsl,exports.compileStatechartDsl=compileStatechartDsl,exports.compileUmlDsl=compileUmlDsl,exports.isBpmnDsl=isBpmnDsl,exports.isFlowDsl=isFlowDsl,exports.isGanttDsl=isGanttDsl,exports.isPowerDsl=isPowerDsl,exports.isStatechartDsl=isStatechartDsl,exports.isUmlDsl=isUmlDsl,exports.layeredLayout=layeredLayout,exports.renderBpmnDsl=renderBpmnDsl,exports.renderDsl=function renderDsl(t,r){const i=validateDsl(r);if(!i.valid)throw new Error(i.errors.join("\n"));return isGanttDsl(r)?renderGanttDsl(t,r):isPowerDsl(r)?renderPowerDsl(t,r):isStatechartDsl(r)?renderStatechartDsl(t,r):isUmlDsl(r)?renderUmlDsl(t,r):isBpmnDsl(r)?renderBpmnDsl(t,r):isFlowDsl(r)?renderFlowDsl(t,r):function renderErDsl(t,r){const i=compileDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.EntityDesigner(n);i.entities.forEach(e=>{s.createEntity(e)}),function positionLinks(e,t,r){const i=new Map;return e.entities.forEach(e=>{i.set(e.state.id,e.getMinBoundingBox(!0))}),t.relations.map(e=>{const t=i.get(e.sourceId),o=i.get(e.targetId);let n="R",s="L";if(t&&o){const e=o.center[0]-t.center[0],r=o.center[1]-t.center[1];Math.abs(e)>Math.abs(r)?(n=e>=0?"R":"L",s=e>=0?"L":"R"):(n=r>=0?"B":"T",s=r>=0?"T":"B")}return{...e,routeType:e.routeType||r||"orthogonal",linkShape:e.linkShape||"visio",links:{start:{id:e.sourceId,position:n},end:{id:e.targetId,position:s}}}})}(s,i,o.routeType).forEach(e=>{s.createRelation(e)}),("layered"===i.layout||"horizontal"===i.layout)&&new e.ICELayeredLayout({gapX:o.gapX||120,gapY:o.gapY||50}).layoutContainer(n);o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):o.fitViewport&&function fitViewport(e,t,r){const i=t.entities||[];let o=1/0,n=1/0,s=-1/0,a=-1/0;i.forEach(e=>{const t=e.getMinBoundingBox(!0).getMinAndMaxPoint();o=Math.min(o,t.minX),n=Math.min(n,t.minY),s=Math.max(s,t.maxX),a=Math.max(a,t.maxY)});const d=s-o,l=a-n,p=e.canvasWidth||0,c=e.canvasHeight||0;if(!p||!c||d<=0||l<=0)return;const u=Number.isFinite(r)&&r>=0?r:40,h=Math.max(1,p-2*u),f=Math.max(1,c-2*u),m=Math.min(h/d,f/l,1),g=(p-d*m)/2-o*m,b=(c-l*m)/2-n*m;e.setViewport(m,g,b)}(n,s,o.fitViewportPadding);return withDsl({kind:"entity",ice:n,designer:s})}(t,r)},exports.renderFlowDsl=renderFlowDsl,exports.renderGanttDsl=renderGanttDsl,exports.renderPowerDsl=renderPowerDsl,exports.renderStatechartDsl=renderStatechartDsl,exports.renderUmlDsl=renderUmlDsl,exports.toDsl=toDsl,exports.validateBpmnDsl=validateBpmnDsl,exports.validateDsl=validateDsl,exports.validateFlowDsl=validateFlowDsl,exports.validateGanttDsl=validateGanttDsl,exports.validatePowerDsl=validatePowerDsl,exports.validateStatechartDsl=validateStatechartDsl,exports.validateUmlDsl=validateUmlDsl;
1
+ "use strict";var e=require("ice-entity-designer");function isFlowDsl(e){return!!e&&"object"==typeof e&&"flowchart"===e.kind}function isBpmnDsl(e){return!!e&&"object"==typeof e&&"bpmn"===e.kind}function isUmlDsl(e){return!!e&&"object"==typeof e&&"uml"===e.kind}function isStatechartDsl(e){return!!e&&"object"==typeof e&&"statechart"===e.kind}function isGanttDsl(e){return!!e&&"object"==typeof e&&"gantt"===e.kind}const t=["busbar","breaker","disconnector","loadSwitch","earthingSwitch","earth","currentTransformer","voltageTransformer","transformer","fuse","arrester","reactor","generator","motor","load","capacitor","arcSuppressionCoil","threeWindingTransformer","groundingTransformer","groundingResistor","cable","cableTermination","cubicle"];function isPowerDsl(e){return!!e&&"object"==typeof e&&"power"===e.kind}const r=["terminator","process","decision","io"],i=["T","R","B","L","C"],o=["pool","lane","task","event","gateway","subprocess","dataObject","annotation"],n=["start","intermediate","end"],s=["none","message","timer","error","terminate"],a=["exclusive","parallel","inclusive","event"],d=["none","user","service","script","send","receive","manual"],l=["sequence","message","association"],p=["initial","final","state","composite"],c=["class","interface","enum"],u=["inheritance","realization","association","aggregation","composition","dependency"];function validateDsl(e){return isGanttDsl(e)?validateGanttDsl(e):isPowerDsl(e)?validatePowerDsl(e):isStatechartDsl(e)?validateStatechartDsl(e):isUmlDsl(e)?validateUmlDsl(e):isBpmnDsl(e)?validateBpmnDsl(e):isFlowDsl(e)?validateFlowDsl(e):function validateErDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);if(Array.isArray(e.entities)){const r=new Set;e.entities.forEach((e,i)=>{const o=`entities[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):r.add(e.id):t.push(`${o}.id must be a non-empty string`),Array.isArray(e.fields)?e.fields.forEach((e,r)=>{e&&"object"==typeof e&&"string"==typeof e.name&&e.name.trim()||t.push(`${o}.fields[${r}].name must be a non-empty string`)}):t.push(`${o}.fields must be an array`)):t.push(`${o} must be an object`)})}else t.push("entities must be an array");void 0===e.relations||Array.isArray(e.relations)?(e.relations||[]).forEach((e,r)=>{const i=`relations[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()||t.push(`${i}.source must be a non-empty string`),"string"==typeof e.target&&e.target.trim()||t.push(`${i}.target must be a non-empty string`)):t.push(`${i} must be an object`)}):t.push("relations must be an array");return{valid:0===t.length,errors:t}}(e)}function validateBpmnDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const r=new Set,i=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,l)=>{const p=`nodes[${l}]`;if(!e||"object"!=typeof e)return void t.push(`${p} must be an object`);"string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${p}.id is duplicated: ${e.id}`):(r.add(e.id),i.set(e.id,e.kind||"task")):t.push(`${p}.id must be a non-empty string`),void 0!==e.kind&&-1===o.indexOf(e.kind)&&t.push(`${p}.kind must be one of ${o.join("/")}`),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${p}.${r} must be a number when present`)});[["eventKind",n],["trigger",s],["gatewayType",a],["taskType",d]].forEach(([r,i])=>{const o=e[r];void 0!==o&&-1===i.indexOf(o)&&t.push(`${p}.${r} must be one of ${i.join("/")}`)})}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;if("string"!=typeof e.parent||!e.parent.trim())return void t.push(`${n}.parent must be a non-empty string when present`);if(!r.has(e.parent))return void t.push(`${n}.parent must reference an existing node id`);if(e.parent===e.id)return void t.push(`${n}.parent must not reference itself`);const s=i.get(e.parent);"pool"===e.kind?t.push(`${n}.parent is not allowed on a pool (池是最外层容器)`):"lane"===e.kind&&"pool"!==s?t.push(`${n}.parent must reference a pool`):"pool"!==s&&"lane"!==s&&t.push(`${n}.parent must reference a pool or a lane`)})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;if(!e||"object"!=typeof e)return void t.push(`${o} must be an object`);"string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(`${o}.target must reference an existing node id`);const n=void 0!==e.type?e.type:e.flowType;void 0!==n&&-1===l.indexOf(n)&&t.push(`${o}.type must be one of ${l.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(`${o}.label must be a string when present`),void 0!==e.condition&&"string"!=typeof e.condition&&t.push(`${o}.condition must be a string when present`),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(`${o}.linkShape must be "visio" or "bezier" when present`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateFlowDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const o=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,i)=>{const n=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?o.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):o.add(e.id):t.push(`${n}.id must be a non-empty string`),void 0!==e.kind&&-1===r.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${r.join("/")}`),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${n}.${r} must be a number when present`)})):t.push(`${n} must be an object`)}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const n=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&o.has(e.source)||t.push(`${n}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&o.has(e.target)||t.push(`${n}.target must reference an existing node id`),void 0!==e.label&&"string"!=typeof e.label&&t.push(`${n}.label must be a string when present`),void 0!==e.sourcePort&&-1===i.indexOf(e.sourcePort)&&t.push(`${n}.sourcePort must be one of ${i.join("/")}`),void 0!==e.targetPort&&-1===i.indexOf(e.targetPort)&&t.push(`${n}.targetPort must be one of ${i.join("/")}`),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(`${n}.linkShape must be "visio" or "bezier" when present`)):t.push(`${n} must be an object`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateUmlDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const r=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,i)=>{const o=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):r.add(e.id):t.push(`${o}.id must be a non-empty string`),void 0!==e.kind&&-1===c.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${c.join("/")}`),["attributes","methods"].forEach(r=>{const i=e[r];void 0!==i&&(Array.isArray(i)&&!i.some(e=>"string"!=typeof e)||t.push(`${o}.${r} must be an array of strings when present`))}),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${o}.${r} must be a number when present`)})):t.push(`${o} must be an object`)}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;if(!e||"object"!=typeof e)return void t.push(`${o} must be an object`);"string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(`${o}.target must reference an existing node id`);const n=void 0!==e.type?e.type:e.relation;void 0!==n&&-1===u.indexOf(n)&&t.push(`${o}.type must be one of ${u.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(`${o}.label must be a string when present`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateStatechartDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const r=new Set,i=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,o)=>{const n=`nodes[${o}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):(r.add(e.id),i.set(e.id,e.kind||"state")):t.push(`${n}.id must be a non-empty string`),void 0!==e.kind&&-1===p.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${p.join("/")}`),["left","top","width","height"].forEach(r=>{const i=e[r];void 0!==i&&"number"!=typeof i&&t.push(`${n}.${r} must be a number when present`)})):t.push(`${n} must be an object`)}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;"string"==typeof e.parent&&e.parent.trim()&&r.has(e.parent)?e.parent!==e.id?"composite"!==i.get(e.parent)&&t.push(`${n}.parent must reference a composite state`):t.push(`${n}.parent must not reference itself`):t.push(`${n}.parent must reference an existing node id`)})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(`${o}.target must reference an existing node id`),["event","guard","action","label"].forEach(r=>{const i=e[r];void 0!==i&&"string"!=typeof i&&t.push(`${o}.${r} must be a string when present`)})):t.push(`${o} must be an object`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateGanttDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const r=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,i)=>{const o=`nodes[${i}]`;if(e&&"object"==typeof e){if("string"==typeof e.id&&e.id.trim()?r.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):r.add(e.id):t.push(`${o}.id must be a non-empty string`),"string"==typeof e.start&&/^\d{4}-\d{2}-\d{2}$/.test(e.start)||t.push(`${o}.start must be a "YYYY-MM-DD" string`),void 0===e.days||Number(e.days)>=1||t.push(`${o}.days must be a number >= 1 when present`),void 0!==e.progress){const r=Number(e.progress);r>=0&&r<=1||t.push(`${o}.progress must be between 0 and 1 when present`)}void 0===e.row||Number(e.row)>=0||t.push(`${o}.row must be a number >= 0 when present`)}else t.push(`${o} must be an object`)}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&r.has(e.source)||t.push(`${o}.source must reference an existing task id`),"string"==typeof e.target&&e.target.trim()&&r.has(e.target)||t.push(`${o}.target must reference an existing task id`)):t.push(`${o} must be an object`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validatePowerDsl(e){const r=[],i=Array.isArray(e.nodes)?e.nodes:[];Array.isArray(e.nodes)||r.push("nodes must be an array");const o=new Set;return i.forEach((e,i)=>{const n=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id?o.has(e.id)?r.push(`duplicated node id: ${e.id}`):o.add(e.id):r.push(`${n}.id must be a non-empty string`),-1===t.indexOf(e.kind)&&r.push(`${n}.kind must be one of ${t.join(" / ")}`),void 0===e.voltageLevel||/^\d+(\.\d+)?kV$/.test(String(e.voltageLevel))||r.push(`${n}.voltageLevel must look like "110kV"`)):r.push(`${n} must be an object`)}),i.forEach((e,t)=>{if(!e||void 0===e.attachedTo)return;const o=i.find(t=>t&&t.id===e.attachedTo);o?"busbar"!==o.kind&&r.push(`nodes[${t}].attachedTo must point at a busbar: ${e.attachedTo} is ${o.kind}`):r.push(`nodes[${t}].attachedTo references an unknown node: ${e.attachedTo}`)}),(e.edges||[]).forEach((e,t)=>{const i=`edges[${t}]`;e&&"object"==typeof e?(o.has(e.source)||r.push(`${i}.source references an unknown node: ${e.source}`),o.has(e.target)||r.push(`${i}.target references an unknown node: ${e.target}`),e.source===e.target&&r.push(`${i} must not connect a node to itself`)):r.push(`${i} must be an object`)}),{valid:0===r.length,errors:r}}function entityName(e){return e.entityName||e.name||e.id}function relationType(e){return e.relationType||e.type||"one-to-many"}function compileDsl(e){if(isFlowDsl(e))throw new Error("compileDsl() 只处理 ER 文档;流程图文档请使用 compileFlowDsl()");if(!e||!Array.isArray(e.entities))throw new Error("ER 文档必须包含 entities 数组");return{entities:e.entities.map(e=>({id:e.id,entityName:entityName(e),fields:e.fields||[],left:e.left,top:e.top,width:e.width,height:e.height,style:e.style,headerStyle:e.headerStyle,fieldStyle:e.fieldStyle,dividerStyle:e.dividerStyle,draggable:e.draggable,interactive:e.interactive})),relations:(e.relations||[]).map((e,t)=>({id:e.id||`relation-${t}`,relationType:relationType(e),sourceId:e.source,targetId:e.target,sourceField:e.sourceField||"id",targetField:e.targetField||"id",nullable:e.nullable,onDelete:e.onDelete,onUpdate:e.onUpdate,joinTableName:e.joinTableName,fromKey:e.fromKey,toKey:e.toKey,sourceCardinality:e.sourceCardinality,targetCardinality:e.targetCardinality,label:e.label,style:e.style,arrow:e.arrow,linkShape:e.linkShape,routeType:e.routeType,routeOffset:e.routeOffset,curveType:e.curveType,lineDash:e.lineDash})),layout:e.layout,options:e.options}}function layeredLayout(e,t,r={}){const i=Number(r.gapX)||90,o=Number(r.gapY)||90,n=void 0===r.originX?80:r.originX,s=void 0===r.originY?80:r.originY,a=new Map;if(!e.length)return a;const d=new Map;e.forEach(e=>d.set(e.id,e));const l=new Map,p=new Map;e.forEach(e=>{l.set(e.id,0),p.set(e.id,[])}),t.forEach(e=>{d.has(e.sourceId)&&d.has(e.targetId)&&(l.set(e.targetId,(l.get(e.targetId)||0)+1),p.get(e.sourceId).push(e.targetId))});const c=new Map,u=[];e.forEach(e=>{0===(l.get(e.id)||0)&&(c.set(e.id,0),u.push(e.id))}),u.length||(c.set(e[0].id,0),u.push(e[0].id));let h=e.length*e.length+e.length;for(;u.length&&h-- >0;){const e=u.shift(),t=c.get(e)||0;(p.get(e)||[]).forEach(e=>{const r=t+1;(void 0===c.get(e)?-1:c.get(e))<r&&(c.set(e,r),u.push(e))})}e.forEach(e=>{c.has(e.id)||c.set(e.id,0)});const f=new Map;e.forEach(e=>{const t=c.get(e.id)||0;f.has(t)||f.set(t,[]),f.get(t).push(e)});const m=[...f.keys()].sort((e,t)=>e-t);if("horizontal"===r.direction){const e=m.map(e=>Math.max(...f.get(e).map(e=>e.width)));let t=0;m.forEach((r,n)=>{const s=f.get(r);let d=-(s.reduce((e,t)=>e+t.height,0)+Math.max(s.length-1,0)*o)/2;s.forEach(r=>{a.set(r.id,{left:Math.round(t+(e[n]-r.width)/2),top:Math.round(d)}),d+=r.height+o}),t+=e[n]+i})}else{const e=m.map(e=>Math.max(...f.get(e).map(e=>e.height)));let t=0;m.forEach((r,n)=>{const s=f.get(r);let d=-(s.reduce((e,t)=>e+t.width,0)+Math.max(s.length-1,0)*i)/2;s.forEach(r=>{a.set(r.id,{left:Math.round(d),top:Math.round(t+(e[n]-r.height)/2)}),d+=r.width+i}),t+=e[n]+o})}const g=Math.min(...e.map(e=>a.get(e.id).left)),y=Math.min(...e.map(e=>a.get(e.id).top));return a.forEach(e=>{e.left+=n-g,e.top+=s-y}),a}const h={terminator:{width:e.FLOW_NODE_KINDS.terminator.width,height:e.FLOW_NODE_KINDS.terminator.height,fill:e.FLOW_NODE_KINDS.terminator.fill,stroke:e.FLOW_NODE_KINDS.terminator.stroke},process:{width:e.FLOW_NODE_KINDS.process.width,height:e.FLOW_NODE_KINDS.process.height,fill:e.FLOW_NODE_KINDS.process.fill,stroke:e.FLOW_NODE_KINDS.process.stroke},decision:{width:e.FLOW_NODE_KINDS.decision.width,height:e.FLOW_NODE_KINDS.decision.height,fill:e.FLOW_NODE_KINDS.decision.fill,stroke:e.FLOW_NODE_KINDS.decision.stroke},io:{width:e.FLOW_NODE_KINDS.io.width,height:e.FLOW_NODE_KINDS.io.height,fill:e.FLOW_NODE_KINDS.io.fill,stroke:e.FLOW_NODE_KINDS.io.stroke}};function compileFlowDsl(t){const r=t.options||{},i=(t.nodes||[]).map(t=>{const r=function nodeKind$1(e){return e.kind&&h[e.kind]?e.kind:"process"}(t),i=h[r];return{id:t.id,typeId:e.FlowNode.typeId,kind:r,title:(o=t,o.title||o.name||o.id),left:"number"==typeof t.left?t.left:Number.NaN,top:"number"==typeof t.top?t.top:Number.NaN,width:"number"==typeof t.width?t.width:i.width,height:"number"==typeof t.height?t.height:i.height,fillColor:t.fillColor||i.fill,strokeColor:t.strokeColor||i.stroke};var o}),o=(t.edges||[]).map((e,t)=>({id:e.id||`edge-${t}`,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",label:e.label||"",linkShape:e.linkShape||"visio"})),n=r.layout||"layered";return"none"!==n&&i.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))&&(i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),function autoLayout(e,t,r,i){const o=layeredLayout(e,t,{gapX:r,gapY:i});e.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)})}(i,o,Number(r.gapX)||90,Number(r.gapY)||90)),i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"flowchart",nodes:i,edges:o,layout:n,options:r}}const f={pool:"bpmnPool",lane:"bpmnLane",task:"bpmnTask",event:"bpmnEvent",gateway:"bpmnGateway",subprocess:"bpmnSubprocess",dataObject:"bpmnDataObject",annotation:"bpmnAnnotation"},m=Number(e.FLOW_NODE_KINDS.bpmnPool.bandSize)||32,g=Number(e.FLOW_NODE_KINDS.bpmnLane.bandSize)||32,y=Number(e.FLOW_NODE_KINDS.bpmnLane.height)||130,b=Number(e.FLOW_NODE_KINDS.bpmnPool.width)||900,w=28;function toWorkingNode(t){const r=t.kind||"task",i=function presetOf(t){return e.FLOW_NODE_KINDS[t]||e.FLOW_NODE_KINDS.bpmnTask}(f[r]||"bpmnTask"),o={id:t.id,typeId:e.FlowNode.typeId,kind:f[r]||"bpmnTask",title:t.title||t.name||t.id,left:"number"==typeof t.left?t.left:0,top:"number"==typeof t.top?t.top:0,width:"number"==typeof t.width?t.width:i.width,height:"number"==typeof t.height?t.height:i.height,fillColor:t.fillColor,strokeColor:t.strokeColor,dslKind:r,parent:t.parent,explicitLeft:"number"==typeof t.left,explicitTop:"number"==typeof t.top,explicitWidth:"number"==typeof t.width,explicitHeight:"number"==typeof t.height};return"event"===r&&(o.eventKind=t.eventKind||i.eventKind||"start",o.trigger=t.trigger||i.trigger||"none"),"gateway"===r&&(o.gatewayType=t.gatewayType||i.gatewayType||"exclusive"),"task"!==r&&"subprocess"!==r||(o.taskType=t.taskType||i.taskType||"none"),o}function buildGroups(e,t){const r=new Map;t.forEach(e=>r.set(e.id,e));const i=1===t.length?t[0]:null,o=[];return e.forEach(e=>{(e=>{let t=o.filter(t=>t.owner===e)[0];return t||(t={owner:e,nodes:[],box:null,didLayout:!1},o.push(t)),t})((e.parent?r.get(e.parent):void 0)||i).nodes.push(e)}),o}function layoutGroups(e,t,r,i){e.forEach(e=>{if(e.nodes.some(e=>!e.explicitLeft||!e.explicitTop)){const o=layeredLayout(e.nodes,t,{gapX:r,gapY:i,originX:0,originY:0,direction:"horizontal"});e.nodes.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)}),e.didLayout=!0}e.box=function unionBox(e){if(!e.length)return null;let t=1/0,r=1/0,i=-1/0,o=-1/0;return e.forEach(e=>{t=Math.min(t,e.left),r=Math.min(r,e.top),i=Math.max(i,e.left+e.width),o=Math.max(o,e.top+e.height)}),{minX:t,minY:r,maxX:i,maxY:o}}(e.nodes)})}function compileBpmnDsl(t){const r=t.options||{},i=(t.nodes||[]).map(toWorkingNode),o=i.filter(e=>"pool"===e.dslKind),n=i.filter(e=>"lane"===e.dslKind),s=i.filter(e=>!function isContainer(e){return"pool"===e.dslKind||"lane"===e.dslKind}(e)),a=(t.edges||[]).map((e,t)=>({id:e.id||`edge-${t}`,sourceId:e.source,targetId:e.target,flowType:e.type||e.flowType||"sequence",label:e.label||"",condition:e.condition||"",isDefault:!!e.isDefault,linkShape:e.linkShape||"visio"})),d=r.layout||"auto",l=function resolveLaneOwners(e,t){const r=new Map;return e.forEach(e=>{let i=e.parent;i||1!==t.length||(i=t[0].id),i&&r.set(e.id,i)}),r}(n,o),lanesOf=e=>n.filter(t=>l.get(t.id)===e);let p=[];"none"!==d&&(p=buildGroups(s,o.concat(n)),layoutGroups(p,a,Number(r.gapX)||90,Number(r.gapY)||60)),n.forEach(e=>{const t=p.filter(t=>t.owner===e)[0],r=t?t.box:null;e.explicitHeight||(e.height=Math.round(Math.max(56+(r?r.maxY-r.minY:0),y))),e.explicitWidth||(e.width=Math.round(Math.max((r?r.maxX-r.minX:0)+g+56,b)))}),o.forEach(e=>{const t=lanesOf(e.id);if(t.length){const r=t.reduce((e,t)=>Math.max(e,t.width),b);return e.explicitWidth||(e.width=r),e.explicitWidth&&t.forEach(t=>{t.explicitWidth||(t.width=e.width)}),void(e.explicitHeight||(e.height=m+t.reduce((e,t)=>e+t.height,0)))}const r=p.filter(t=>t.owner===e)[0],i=r?r.box:null;e.explicitWidth||(e.width=Math.round(Math.max(56+(i?i.maxX-i.minX:0),b))),e.explicitHeight||(e.height=Math.round(Math.max((i?i.maxY-i.minY:0)+m+56,160)))});let c=60;o.forEach(e=>{e.explicitLeft||(e.left=60),e.explicitTop||(e.top=c),c=Math.max(c,e.top+e.height+60)}),o.forEach(e=>{const t=lanesOf(e.id);if(!t.length)return;const r=e.top+m,i=Math.max(e.height-m,1),o=t.reduce((e,t)=>e+(t.explicitHeight?t.height:0),0),n=t.filter(e=>!e.explicitHeight),s=n.length?Math.max(i-o,0)/n.length:0;let a=r;t.forEach(t=>{t.explicitLeft||(t.left=e.left),t.explicitTop||(t.top=a),!t.explicitHeight&&e.explicitHeight&&(t.height=Math.round(s||y)),a=t.top+t.height})});let u=60;n.forEach(e=>{l.get(e.id)||(e.explicitLeft||(e.left=60),e.explicitTop||(e.top=u,u=e.top+e.height+60))}),p.forEach(e=>{const t=e.box;if(!t||!e.didLayout)return;const r=e.owner;let i=80,o=80;r&&(i=r.left+w,o=r.top+w,"pool"===r.dslKind?o+=m:i+=g),e.nodes.forEach(e=>{e.left=Math.round(i+(e.left-t.minX)),e.top=Math.round(o+(e.top-t.minY))})});return{kind:"bpmn",nodes:o.concat(n).concat(s).map(t=>{const r={id:t.id,typeId:e.FlowNode.typeId,kind:t.kind,title:t.title,left:t.left,top:t.top,width:t.width,height:t.height};return void 0!==t.eventKind&&(r.eventKind=t.eventKind),void 0!==t.trigger&&(r.trigger=t.trigger),void 0!==t.gatewayType&&(r.gatewayType=t.gatewayType),void 0!==t.taskType&&(r.taskType=t.taskType),void 0!==t.fillColor&&(r.fillColor=t.fillColor),void 0!==t.strokeColor&&(r.strokeColor=t.strokeColor),r}),edges:a,layout:d,options:r}}function compileUmlDsl(e){const t=e.options||{},r=(e.nodes||[]).map(e=>{return{id:e.id,kind:(t=e,t.kind||"class"),className:e.title||e.name||e.id,abstract:!!e.abstract,attributes:Array.isArray(e.attributes)?e.attributes.map(e=>String(e)):[],methods:Array.isArray(e.methods)?e.methods.map(e=>String(e)):[],left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:240,height:"number"==typeof e.height?e.height:120+24*(e.attributes?e.attributes.length:0)+24*(e.methods?e.methods.length:0)};var t}),i=(e.edges||[]).map((e,t)=>({id:e.id||`relation-${t}`,sourceId:e.source,targetId:e.target,relationKind:e.type||e.relation||"association",label:e.label||""})),o=t.layout||"layered";if("none"!==o&&r.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const e=layeredLayout(r,i.filter(e=>"inheritance"===e.relationKind||"realization"===e.relationKind).map(e=>({sourceId:e.targetId,targetId:e.sourceId})),{gapX:Number(t.gapX)||80,gapY:Number(t.gapY)||110,originX:80,originY:80,direction:"vertical"});r.forEach(t=>{const r=e.get(t.id);r&&(t.left=r.left,t.top=r.top)})}return r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"uml",nodes:r,edges:i,layout:o,options:t}}const v={width:160,height:64},D={width:500,height:200},O=56;function compileStatechartDsl(e){const t=e.options||{},r=(e.nodes||[]).map(e=>{const t=e.kind||"state",r="initial"===t||"final"===t,i="composite"===t?D:r?{width:24,height:24}:v;return{id:e.id,kind:t,title:e.title||e.name||(r?"":e.id),parent:e.parent,left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:i.width,height:"number"==typeof e.height?e.height:i.height}}),i=(e.edges||[]).map((e,t)=>({id:e.id||`transition-${t}`,sourceId:e.source,targetId:e.target,event:e.event||"",guard:e.guard||"",action:e.action||"",label:e.label})),o=t.layout||"auto",n=new Set;if("none"!==o){const e=new Map;r.forEach(t=>e.set(t.id,t));const o=r.filter(t=>!(t.parent&&e.has(t.parent)));if(o.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){const containerOf=t=>{let r=e.get(t);for(;r&&r.parent&&e.has(r.parent);)r=e.get(r.parent);return r?r.id:t},r=i.map(e=>({sourceId:containerOf(e.sourceId),targetId:containerOf(e.targetId)})).filter(e=>e.sourceId!==e.targetId),n=layeredLayout(o,r,{gapX:Number(t.gapX)||110,gapY:Number(t.gapY)||80,originX:80,originY:80,direction:"horizontal"});o.forEach(e=>{const t=n.get(e.id);t&&(e.left=t.left,e.top=t.top)})}let s=0;r.filter(e=>"composite"===e.kind).forEach(e=>{const t=r.filter(t=>t.parent===e.id);if(Number.isNaN(e.left)&&(e.left=80),Number.isNaN(e.top)){const t=Math.max(...o.filter(t=>t!==e&&!Number.isNaN(t.top)).map(e=>e.top+e.height),0);e.top=Math.max(t+60,80)+s,s+=e.height+60}if(!t.length)return;let i=O,a=O,d=0,l=O;const p=e.width-112;t.forEach(e=>{(Number.isNaN(e.left)||Number.isNaN(e.top))&&(i>O&&i+e.width>O+p&&(i=O,a=l+40),n.add(e.id),e.left=i,e.top=a,i+=e.width+40,d=Math.max(d,i),l=Math.max(l,a+e.height))}),e.width=Math.max(e.width,d+O),e.height=Math.max(e.height,l+O)}),r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const offsetChildren=(e,t,i)=>{r.filter(t=>t.parent===e.id).forEach(e=>{n.has(e.id)&&(e.left+=t,e.top+=i,offsetChildren(e,t,i))})};r.filter(e=>"composite"===e.kind).forEach(e=>offsetChildren(e,e.left,e.top))}return{kind:"statechart",nodes:r,edges:i,layout:o,options:t}}function compileGanttDsl(e){const t=e.options||{};return{kind:"gantt",nodes:(e.nodes||[]).map((e,t)=>({id:e.id,title:e.title||e.name||e.id,start:String(e.start||""),days:"number"==typeof e.days?e.days:1,progress:"number"==typeof e.progress?e.progress:0,row:"number"==typeof e.row?e.row:t,resource:String(e.resource||"")})),edges:(e.edges||[]).map((e,t)=>({id:e.id||`dependency-${t}`,sourceId:e.source,targetId:e.target})),options:t}}function compilePowerDsl(e){const t=e.options||{},r=(e.nodes||[]).map((e,t)=>{const r=function defaultPlacement(e){return{left:120+e%6*200,top:120+160*Math.floor(e/6)}}(t);return{id:e.id,kind:e.kind,title:e.title||e.name||e.id,voltageLevel:String(e.voltageLevel||""),left:"number"==typeof e.left?e.left:r.left,top:"number"==typeof e.top?e.top:r.top,width:e.width,height:e.height,switchState:e.switchState,source:!!e.source}}),i=new Map;r.forEach(e=>i.set(e.id,e));return{kind:"power",nodes:r,edges:(e.edges||[]).map((e,t)=>({id:e.id||`conductor-${t}`,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",voltageLevel:e.voltageLevel||(i.get(e.source)||{}).voltageLevel||""})),attachments:(e.nodes||[]).filter(e=>!!e.attachedTo).map(e=>({deviceId:e.id,busId:String(e.attachedTo)})),options:t}}const x={bpmnPool:"pool",bpmnLane:"lane",bpmnTask:"task",bpmnEvent:"event",bpmnGateway:"gateway",bpmnSubprocess:"subprocess",bpmnDataObject:"dataObject",bpmnAnnotation:"annotation"},k=["terminator","process","decision","io"],N=["class","interface","enum"],$=["initial","final","state","composite"];function stateOf(e){return e&&e.state||{}}function linkOf(e,t){const r=e.links;return r&&r[t]||{}}function absolutePosition(e){let t=0,r=0,i=e;for(;i&&i.state&&"number"==typeof i.state.left;)t+=i.state.left||0,r+=i.state.top||0,i=i.parentNode;return{left:Math.round(t),top:Math.round(r)}}function parentIdOf(e){const t=e&&e.parentNode,r=t&&t.state?t.state.id:void 0;return"string"==typeof r&&r.length>0?r:void 0}function numberOf(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function textOf(e){return"string"==typeof e&&e.length>0?e:void 0}function clean(e){const t={};return Object.keys(e).forEach(r=>{void 0!==e[r]&&(t[r]=e[r])}),t}function nodesOf(e){return e&&Array.isArray(e.nodes)?e.nodes:[]}function edgesOf(e){return e&&Array.isArray(e.edges)?e.edges:[]}function baseOf(e){const t=stateOf(e),r=absolutePosition(e);return{id:t.id,title:textOf(t.title),left:r.left,top:r.top}}function toDsl(e,r={}){const i=e&&e.designer?e.designer:e,o=r.kind||e&&e.kind||function detectKind(e){if(!e)return;if(Array.isArray(e.entities))return"entity";if(void 0!==e.originDate||void 0!==e.dayWidth)return"gantt";const r=nodesOf(e)[0],i=r?String(stateOf(r).kind||""):"";return i.startsWith("bpmn")?"bpmn":N.indexOf(i)>=0?"uml":$.indexOf(i)>=0?"statechart":t.indexOf(i)>=0?"power":k.indexOf(i)>=0?"flowchart":void 0}(i);if(!o)throw new Error("toDsl(): 无法推断文档类型(实例里没有可识别的图元)。请显式传入 kind:'flowchart' | 'bpmn' | 'uml' | 'statechart' | 'gantt' | 'power' | 'entity'。");switch(o){case"flowchart":return function exportFlow(e){return{schemaVersion:1,kind:"flowchart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:t.kind,width:numberOf(t.width),height:numberOf(t.height),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,sourcePort:r.position,targetPort:i.position,label:textOf(t.label)||"",linkShape:textOf(t.linkShape)})})}}(i);case"bpmn":return function exportBpmn(e){return{schemaVersion:1,kind:"bpmn",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:x[t.kind]||"task",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height),eventKind:textOf(t.eventKind),trigger:textOf(t.trigger),gatewayType:textOf(t.gatewayType),taskType:textOf(t.taskType),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,type:textOf(t.flowType)||"sequence",label:textOf(t.label),condition:textOf(t.condition),isDefault:!0===t.isDefault||void 0,linkShape:textOf(t.linkShape)})})}}(i);case"uml":return function exportUml(e){return{schemaVersion:1,kind:"uml",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"class",title:textOf(t.className)||textOf(t.title),abstract:!0===t.abstract||void 0,attributes:Array.isArray(t.attributes)?t.attributes.map(e=>String(e)):[],methods:Array.isArray(t.methods)?t.methods.map(e=>String(e)):[],width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,type:textOf(t.relationKind)||"association",label:textOf(t.label)})})}}(i);case"statechart":return function exportStatechart(e){return{schemaVersion:1,kind:"statechart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"state",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,event:textOf(t.event),guard:textOf(t.guard),action:textOf(t.action),label:textOf(t.label)})})}}(i);case"gantt":return function exportGantt(e){return{schemaVersion:1,kind:"gantt",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({id:t.id,title:textOf(t.title),start:textOf(t.start),days:numberOf(t.days),progress:numberOf(t.progress),row:numberOf(t.row),resource:textOf(t.resource)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id})})}}(i);case"power":return function exportPower(e){return{schemaVersion:1,kind:"power",nodes:nodesOf(e).map(e=>{const t=stateOf(e),r=absolutePosition(e);return clean({id:t.id,kind:t.kind,title:textOf(t.title)||textOf(t.name),voltageLevel:textOf(t.voltageLevel),left:r.left,top:r.top,width:numberOf(t.width),height:numberOf(t.height),switchState:textOf(t.switchState),source:!0===t.source||!0===t.energizedSource||void 0,attachedTo:textOf(t.attachedBusId)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,sourcePort:r.position,targetPort:i.position,voltageLevel:textOf(t.voltageLevel)})})}}(i);default:return function exportEr(e){const t=e&&Array.isArray(e.entities)?e.entities:[],r=e&&Array.isArray(e.relations)?e.relations:[];return{schemaVersion:1,entities:t.map(e=>{const t=stateOf(e),r=absolutePosition(e);return clean({id:t.id,name:textOf(t.entityName),fields:Array.isArray(t.fields)?t.fields:[],left:r.left,top:r.top})}),relations:r.map(e=>{const t=stateOf(e),r=linkOf(t,"start"),i=linkOf(t,"end");return clean({id:t.id,source:r.id,target:i.id,type:t.relationType,sourceField:textOf(t.sourceField),targetField:textOf(t.targetField),label:textOf(t.label),onDelete:textOf(t.onDelete),onUpdate:textOf(t.onUpdate),joinTableName:textOf(t.joinTableName),nullable:"boolean"==typeof t.nullable?t.nullable:void 0,sourceCardinality:textOf(t.sourceCardinality),targetCardinality:textOf(t.targetCardinality),linkShape:textOf(t.linkShape)})})}}(i)}}function withDsl(e){return Object.assign(e,{toDsl:()=>toDsl(e)})}function renderGanttDsl(t,r){const i=compileGanttDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.GanttDesigner(n);return o.dayWidth&&s.setDayWidth(o.dayWidth),i.nodes.forEach(e=>{s.createTask({id:e.id,title:e.title,start:e.start,days:e.days,progress:e.progress,row:e.row,resource:e.resource})}),i.edges.forEach(e=>{s.createDependency(e)}),s.select(null),o.autoSchedule&&s.autoSchedule(),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"gantt",ice:n,designer:s})}function renderPowerDsl(t,r){const i=compilePowerDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.PowerDesigner(n);o.voltageColors&&s.setVoltageColors(o.voltageColors);const a=new Map;return i.nodes.forEach(e=>{const t=s.createSymbol(e.kind,{id:e.id,title:e.title,name:e.title,voltageLevel:e.voltageLevel,left:e.left,top:e.top,width:e.width,height:e.height,switchState:e.switchState});e.source&&t.setState({energizedSource:!0}),a.set(e.id,t)}),i.attachments.forEach(e=>{const t=a.get(e.deviceId),r=a.get(e.busId);t&&r&&s.attachToBus(t,r)}),i.edges.forEach(e=>{const t=a.get(e.sourceId),r=a.get(e.targetId);t&&r&&s.createLine({sourceId:t.state.id,targetId:r.state.id,sourcePort:e.sourcePort,targetPort:e.targetPort,voltageLevel:e.voltageLevel})}),s.select(null),s.applyTopology(),!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"power",ice:n,designer:s})}function renderStatechartDsl(t,r){const i=compileStatechartDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.StatechartDesigner(n);return i.nodes.forEach(e=>{s.createState({id:e.id,kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height})}),i.edges.forEach(e=>{s.createTransition(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"statechart",ice:n,designer:s})}function renderUmlDsl(t,r){const i=compileUmlDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.UmlDesigner(n);return i.nodes.forEach(e=>{s.createClass({id:e.id,kind:e.kind,className:e.className,abstract:e.abstract,attributes:e.attributes,methods:e.methods,left:e.left,top:e.top,width:e.width})}),i.edges.forEach(e=>{s.createRelation(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"uml",ice:n,designer:s})}function renderBpmnDsl(t,r){const i=compileBpmnDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.BpmnDesigner(n);return i.nodes.forEach(e=>{const t={id:e.id,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};["eventKind","trigger","gatewayType","taskType","fillColor","strokeColor"].forEach(r=>{void 0!==e[r]&&(t[r]=e[r])}),s.createNode(e.kind,t)}),i.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"bpmn",ice:n,designer:s})}function renderFlowDsl(t,r){const i=compileFlowDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.FlowDesigner(n);return i.nodes.forEach(e=>{s.createNode(e.kind,e)}),i.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"flowchart",ice:n,designer:s})}exports.DSL_SCHEMA_VERSION=1,exports.POWER_DSL_KINDS=t,exports.compileBpmnDsl=compileBpmnDsl,exports.compileDsl=compileDsl,exports.compileFlowDsl=compileFlowDsl,exports.compileGanttDsl=compileGanttDsl,exports.compilePowerDsl=compilePowerDsl,exports.compileStatechartDsl=compileStatechartDsl,exports.compileUmlDsl=compileUmlDsl,exports.isBpmnDsl=isBpmnDsl,exports.isFlowDsl=isFlowDsl,exports.isGanttDsl=isGanttDsl,exports.isPowerDsl=isPowerDsl,exports.isStatechartDsl=isStatechartDsl,exports.isUmlDsl=isUmlDsl,exports.layeredLayout=layeredLayout,exports.renderBpmnDsl=renderBpmnDsl,exports.renderDsl=function renderDsl(t,r){const i=validateDsl(r);if(!i.valid)throw new Error(i.errors.join("\n"));return isGanttDsl(r)?renderGanttDsl(t,r):isPowerDsl(r)?renderPowerDsl(t,r):isStatechartDsl(r)?renderStatechartDsl(t,r):isUmlDsl(r)?renderUmlDsl(t,r):isBpmnDsl(r)?renderBpmnDsl(t,r):isFlowDsl(r)?renderFlowDsl(t,r):function renderErDsl(t,r){const i=compileDsl(r),o=i.options||{},n=(new e.ICE).init(t),s=new e.EntityDesigner(n);i.entities.forEach(e=>{s.createEntity(e)}),function positionLinks(e,t,r){const i=new Map;return e.entities.forEach(e=>{i.set(e.state.id,e.getMinBoundingBox(!0))}),t.relations.map(e=>{const t=i.get(e.sourceId),o=i.get(e.targetId);let n="R",s="L";if(t&&o){const e=o.center[0]-t.center[0],r=o.center[1]-t.center[1];Math.abs(e)>Math.abs(r)?(n=e>=0?"R":"L",s=e>=0?"L":"R"):(n=r>=0?"B":"T",s=r>=0?"T":"B")}return{...e,routeType:e.routeType||r||"orthogonal",linkShape:e.linkShape||"visio",links:{start:{id:e.sourceId,position:n},end:{id:e.targetId,position:s}}}})}(s,i,o.routeType).forEach(e=>{s.createRelation(e)}),("layered"===i.layout||"horizontal"===i.layout)&&new e.ICELayeredLayout({gapX:o.gapX||120,gapY:o.gapY||50}).layoutContainer(n);o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):o.fitViewport&&function fitViewport(e,t,r){const i=t.entities||[];let o=1/0,n=1/0,s=-1/0,a=-1/0;i.forEach(e=>{const t=e.getMinBoundingBox(!0).getMinAndMaxPoint();o=Math.min(o,t.minX),n=Math.min(n,t.minY),s=Math.max(s,t.maxX),a=Math.max(a,t.maxY)});const d=s-o,l=a-n,p=e.canvasWidth||0,c=e.canvasHeight||0;if(!p||!c||d<=0||l<=0)return;const u=Number.isFinite(r)&&r>=0?r:40,h=Math.max(1,p-2*u),f=Math.max(1,c-2*u),m=Math.min(h/d,f/l,1),g=(p-d*m)/2-o*m,y=(c-l*m)/2-n*m;e.setViewport(m,g,y)}(n,s,o.fitViewportPadding);return withDsl({kind:"entity",ice:n,designer:s})}(t,r)},exports.renderFlowDsl=renderFlowDsl,exports.renderGanttDsl=renderGanttDsl,exports.renderPowerDsl=renderPowerDsl,exports.renderStatechartDsl=renderStatechartDsl,exports.renderUmlDsl=renderUmlDsl,exports.toDsl=toDsl,exports.validateBpmnDsl=validateBpmnDsl,exports.validateDsl=validateDsl,exports.validateFlowDsl=validateFlowDsl,exports.validateGanttDsl=validateGanttDsl,exports.validatePowerDsl=validatePowerDsl,exports.validateStatechartDsl=validateStatechartDsl,exports.validateUmlDsl=validateUmlDsl;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{FLOW_NODE_KINDS as e,ICE as t,GanttDesigner as i,PowerDesigner as r,StatechartDesigner as o,UmlDesigner as n,BpmnDesigner as s,FlowDesigner as a,EntityDesigner as d,ICELayeredLayout as l}from"ice-entity-designer";function isFlowDsl(e){return!!e&&"object"==typeof e&&"flowchart"===e.kind}function isBpmnDsl(e){return!!e&&"object"==typeof e&&"bpmn"===e.kind}function isUmlDsl(e){return!!e&&"object"==typeof e&&"uml"===e.kind}function isStatechartDsl(e){return!!e&&"object"==typeof e&&"statechart"===e.kind}function isGanttDsl(e){return!!e&&"object"==typeof e&&"gantt"===e.kind}const p=["busbar","breaker","disconnector","loadSwitch","earthingSwitch","earth","currentTransformer","voltageTransformer","transformer","fuse","arrester","reactor","generator","motor","load","capacitor","arcSuppressionCoil","threeWindingTransformer","groundingTransformer","groundingResistor","cable","cableTermination","cubicle"];function isPowerDsl(e){return!!e&&"object"==typeof e&&"power"===e.kind}const c=1,u=["terminator","process","decision","io"],h=["T","R","B","L","C"],f=["pool","lane","task","event","gateway","subprocess","dataObject","annotation"],m=["start","intermediate","end"],g=["none","message","timer","error","terminate"],b=["exclusive","parallel","inclusive","event"],y=["none","user","service","script","send","receive","manual"],w=["sequence","message","association"],v=["initial","final","state","composite"],k=["class","interface","enum"],x=["inheritance","realization","association","aggregation","composition","dependency"];function validateDsl(e){return isGanttDsl(e)?validateGanttDsl(e):isPowerDsl(e)?validatePowerDsl(e):isStatechartDsl(e)?validateStatechartDsl(e):isUmlDsl(e)?validateUmlDsl(e):isBpmnDsl(e)?validateBpmnDsl(e):isFlowDsl(e)?validateFlowDsl(e):function validateErDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);if(Array.isArray(e.entities)){const i=new Set;e.entities.forEach((e,r)=>{const o=`entities[${r}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(o+".id must be a non-empty string"),Array.isArray(e.fields)?e.fields.forEach((e,i)=>{e&&"object"==typeof e&&"string"==typeof e.name&&e.name.trim()||t.push(`${o}.fields[${i}].name must be a non-empty string`)}):t.push(o+".fields must be an array")):t.push(o+" must be an object")})}else t.push("entities must be an array");void 0===e.relations||Array.isArray(e.relations)?(e.relations||[]).forEach((e,i)=>{const r=`relations[${i}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()||t.push(r+".source must be a non-empty string"),"string"==typeof e.target&&e.target.trim()||t.push(r+".target must be a non-empty string")):t.push(r+" must be an object")}):t.push("relations must be an array");return{valid:0===t.length,errors:t}}(e)}function validateBpmnDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const i=new Set,r=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,o)=>{const n=`nodes[${o}]`;if(!e||"object"!=typeof e)return void t.push(n+" must be an object");"string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):(i.add(e.id),r.set(e.id,e.kind||"task")):t.push(n+".id must be a non-empty string"),void 0!==e.kind&&-1===f.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${f.join("/")}`),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${n}.${i} must be a number when present`)});[["eventKind",m],["trigger",g],["gatewayType",b],["taskType",y]].forEach(([i,r])=>{const o=e[i];void 0!==o&&-1===r.indexOf(o)&&t.push(`${n}.${i} must be one of ${r.join("/")}`)})}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;if("string"!=typeof e.parent||!e.parent.trim())return void t.push(n+".parent must be a non-empty string when present");if(!i.has(e.parent))return void t.push(n+".parent must reference an existing node id");if(e.parent===e.id)return void t.push(n+".parent must not reference itself");const s=r.get(e.parent);"pool"===e.kind?t.push(n+".parent is not allowed on a pool (池是最外层容器)"):"lane"===e.kind&&"pool"!==s?t.push(n+".parent must reference a pool"):"pool"!==s&&"lane"!==s&&t.push(n+".parent must reference a pool or a lane")})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;if(!e||"object"!=typeof e)return void t.push(o+" must be an object");"string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(o+".target must reference an existing node id");const n=void 0!==e.type?e.type:e.flowType;void 0!==n&&-1===w.indexOf(n)&&t.push(`${o}.type must be one of ${w.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(o+".label must be a string when present"),void 0!==e.condition&&"string"!=typeof e.condition&&t.push(o+".condition must be a string when present"),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(o+'.linkShape must be "visio" or "bezier" when present')}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateFlowDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const i=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,r)=>{const o=`nodes[${r}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(o+".id must be a non-empty string"),void 0!==e.kind&&-1===u.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${u.join("/")}`),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${o}.${i} must be a number when present`)})):t.push(o+" must be an object")}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(o+".target must reference an existing node id"),void 0!==e.label&&"string"!=typeof e.label&&t.push(o+".label must be a string when present"),void 0!==e.sourcePort&&-1===h.indexOf(e.sourcePort)&&t.push(`${o}.sourcePort must be one of ${h.join("/")}`),void 0!==e.targetPort&&-1===h.indexOf(e.targetPort)&&t.push(`${o}.targetPort must be one of ${h.join("/")}`),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(o+'.linkShape must be "visio" or "bezier" when present')):t.push(o+" must be an object")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateUmlDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const i=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,r)=>{const o=`nodes[${r}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(o+".id must be a non-empty string"),void 0!==e.kind&&-1===k.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${k.join("/")}`),["attributes","methods"].forEach(i=>{const r=e[i];void 0!==r&&(Array.isArray(r)&&!r.some(e=>"string"!=typeof e)||t.push(`${o}.${i} must be an array of strings when present`))}),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${o}.${i} must be a number when present`)})):t.push(o+" must be an object")}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;if(!e||"object"!=typeof e)return void t.push(o+" must be an object");"string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(o+".target must reference an existing node id");const n=void 0!==e.type?e.type:e.relation;void 0!==n&&-1===x.indexOf(n)&&t.push(`${o}.type must be one of ${x.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(o+".label must be a string when present")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateStatechartDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const i=new Set,r=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,o)=>{const n=`nodes[${o}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):(i.add(e.id),r.set(e.id,e.kind||"state")):t.push(n+".id must be a non-empty string"),void 0!==e.kind&&-1===v.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${v.join("/")}`),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${n}.${i} must be a number when present`)})):t.push(n+" must be an object")}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;"string"==typeof e.parent&&e.parent.trim()&&i.has(e.parent)?e.parent!==e.id?"composite"!==r.get(e.parent)&&t.push(n+".parent must reference a composite state"):t.push(n+".parent must not reference itself"):t.push(n+".parent must reference an existing node id")})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(o+".source must reference an existing node id"),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(o+".target must reference an existing node id"),["event","guard","action","label"].forEach(i=>{const r=e[i];void 0!==r&&"string"!=typeof r&&t.push(`${o}.${i} must be a string when present`)})):t.push(o+" must be an object")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateGanttDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push("Unsupported schemaVersion: "+e.schemaVersion);const i=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,r)=>{const o=`nodes[${r}]`;if(e&&"object"==typeof e){if("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(o+".id must be a non-empty string"),"string"==typeof e.start&&/^\d{4}-\d{2}-\d{2}$/.test(e.start)||t.push(o+'.start must be a "YYYY-MM-DD" string'),void 0===e.days||Number(e.days)>=1||t.push(o+".days must be a number >= 1 when present"),void 0!==e.progress){const i=Number(e.progress);i>=0&&i<=1||t.push(o+".progress must be between 0 and 1 when present")}void 0===e.row||Number(e.row)>=0||t.push(o+".row must be a number >= 0 when present")}else t.push(o+" must be an object")}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(o+".source must reference an existing task id"),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(o+".target must reference an existing task id")):t.push(o+" must be an object")}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validatePowerDsl(e){const t=[],i=Array.isArray(e.nodes)?e.nodes:[];Array.isArray(e.nodes)||t.push("nodes must be an array");const r=new Set;return i.forEach((e,i)=>{const o=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id?r.has(e.id)?t.push("duplicated node id: "+e.id):r.add(e.id):t.push(o+".id must be a non-empty string"),-1===p.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${p.join(" / ")}`),void 0===e.voltageLevel||/^\d+(\.\d+)?kV$/.test(String(e.voltageLevel))||t.push(o+'.voltageLevel must look like "110kV"')):t.push(o+" must be an object")}),i.forEach((e,r)=>{if(!e||void 0===e.attachedTo)return;const o=i.find(t=>t&&t.id===e.attachedTo);o?"busbar"!==o.kind&&t.push(`nodes[${r}].attachedTo must point at a busbar: ${e.attachedTo} is ${o.kind}`):t.push(`nodes[${r}].attachedTo references an unknown node: ${e.attachedTo}`)}),(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;e&&"object"==typeof e?(r.has(e.source)||t.push(`${o}.source references an unknown node: ${e.source}`),r.has(e.target)||t.push(`${o}.target references an unknown node: ${e.target}`),e.source===e.target&&t.push(o+" must not connect a node to itself")):t.push(o+" must be an object")}),{valid:0===t.length,errors:t}}function entityName(e){return e.entityName||e.name||e.id}function relationType(e){return e.relationType||e.type||"one-to-many"}function compileDsl(e){if(isFlowDsl(e))throw new Error("compileDsl() 只处理 ER 文档;流程图文档请使用 compileFlowDsl()");if(!e||!Array.isArray(e.entities))throw new Error("ER 文档必须包含 entities 数组");return{entities:e.entities.map(e=>({id:e.id,entityName:entityName(e),fields:e.fields||[],left:e.left,top:e.top,width:e.width,height:e.height,style:e.style,headerStyle:e.headerStyle,fieldStyle:e.fieldStyle,dividerStyle:e.dividerStyle,draggable:e.draggable,interactive:e.interactive})),relations:(e.relations||[]).map((e,t)=>({id:e.id||"relation-"+t,relationType:relationType(e),sourceId:e.source,targetId:e.target,sourceField:e.sourceField||"id",targetField:e.targetField||"id",nullable:e.nullable,onDelete:e.onDelete,onUpdate:e.onUpdate,joinTableName:e.joinTableName,fromKey:e.fromKey,toKey:e.toKey,sourceCardinality:e.sourceCardinality,targetCardinality:e.targetCardinality,label:e.label,style:e.style,arrow:e.arrow,linkShape:e.linkShape,routeType:e.routeType,routeOffset:e.routeOffset,curveType:e.curveType,lineDash:e.lineDash})),layout:e.layout,options:e.options}}function layeredLayout(e,t,i={}){const r=Number(i.gapX)||90,o=Number(i.gapY)||90,n=void 0===i.originX?80:i.originX,s=void 0===i.originY?80:i.originY,a=new Map;if(!e.length)return a;const d=new Map;e.forEach(e=>d.set(e.id,e));const l=new Map,p=new Map;e.forEach(e=>{l.set(e.id,0),p.set(e.id,[])}),t.forEach(e=>{d.has(e.sourceId)&&d.has(e.targetId)&&(l.set(e.targetId,(l.get(e.targetId)||0)+1),p.get(e.sourceId).push(e.targetId))});const c=new Map,u=[];e.forEach(e=>{0===(l.get(e.id)||0)&&(c.set(e.id,0),u.push(e.id))}),u.length||(c.set(e[0].id,0),u.push(e[0].id));let h=e.length*e.length+e.length;for(;u.length&&h-- >0;){const e=u.shift(),t=c.get(e)||0;(p.get(e)||[]).forEach(e=>{const i=t+1;(void 0===c.get(e)?-1:c.get(e))<i&&(c.set(e,i),u.push(e))})}e.forEach(e=>{c.has(e.id)||c.set(e.id,0)});const f=new Map;e.forEach(e=>{const t=c.get(e.id)||0;f.has(t)||f.set(t,[]),f.get(t).push(e)});const m=[...f.keys()].sort((e,t)=>e-t);if("horizontal"===i.direction){const e=m.map(e=>Math.max(...f.get(e).map(e=>e.width)));let t=0;m.forEach((i,n)=>{const s=f.get(i);let d=-(s.reduce((e,t)=>e+t.height,0)+Math.max(s.length-1,0)*o)/2;s.forEach(i=>{a.set(i.id,{left:Math.round(t+(e[n]-i.width)/2),top:Math.round(d)}),d+=i.height+o}),t+=e[n]+r})}else{const e=m.map(e=>Math.max(...f.get(e).map(e=>e.height)));let t=0;m.forEach((i,n)=>{const s=f.get(i);let d=-(s.reduce((e,t)=>e+t.width,0)+Math.max(s.length-1,0)*r)/2;s.forEach(i=>{a.set(i.id,{left:Math.round(d),top:Math.round(t+(e[n]-i.height)/2)}),d+=i.width+r}),t+=e[n]+o})}const g=Math.min(...e.map(e=>a.get(e.id).left)),b=Math.min(...e.map(e=>a.get(e.id).top));return a.forEach(e=>{e.left+=n-g,e.top+=s-b}),a}const O={terminator:{width:e.terminator.width,height:e.terminator.height,fill:e.terminator.fill,stroke:e.terminator.stroke},process:{width:e.process.width,height:e.process.height,fill:e.process.fill,stroke:e.process.stroke},decision:{width:e.decision.width,height:e.decision.height,fill:e.decision.fill,stroke:e.decision.stroke},io:{width:e.io.width,height:e.io.height,fill:e.io.fill,stroke:e.io.stroke}};function compileFlowDsl(e){const t=e.options||{},i=(e.nodes||[]).map(e=>{const t=function nodeKind$1(e){return e.kind&&O[e.kind]?e.kind:"process"}(e),i=O[t];return{id:e.id,typeId:"FlowNode",kind:t,title:(r=e,r.title||r.name||r.id),left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:i.width,height:"number"==typeof e.height?e.height:i.height,fillColor:e.fillColor||i.fill,strokeColor:e.strokeColor||i.stroke};var r}),r=(e.edges||[]).map((e,t)=>({id:e.id||"edge-"+t,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",label:e.label||"",linkShape:e.linkShape||"visio"})),o=t.layout||"layered";return"none"!==o&&i.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))&&(i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),function autoLayout(e,t,i,r){const o=layeredLayout(e,t,{gapX:i,gapY:r});e.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)})}(i,r,Number(t.gapX)||90,Number(t.gapY)||90)),i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"flowchart",nodes:i,edges:r,layout:o,options:t}}const D={pool:"bpmnPool",lane:"bpmnLane",task:"bpmnTask",event:"bpmnEvent",gateway:"bpmnGateway",subprocess:"bpmnSubprocess",dataObject:"bpmnDataObject",annotation:"bpmnAnnotation"},N=Number(e.bpmnPool.bandSize)||32,E=Number(e.bpmnLane.bandSize)||32,S=Number(e.bpmnLane.height)||130,$=Number(e.bpmnPool.width)||900;function toWorkingNode(t){const i=t.kind||"task",r=function presetOf(t){return e[t]||e.bpmnTask}(D[i]||"bpmnTask"),o={id:t.id,typeId:"FlowNode",kind:D[i]||"bpmnTask",title:t.title||t.name||t.id,left:"number"==typeof t.left?t.left:0,top:"number"==typeof t.top?t.top:0,width:"number"==typeof t.width?t.width:r.width,height:"number"==typeof t.height?t.height:r.height,fillColor:t.fillColor,strokeColor:t.strokeColor,dslKind:i,parent:t.parent,explicitLeft:"number"==typeof t.left,explicitTop:"number"==typeof t.top,explicitWidth:"number"==typeof t.width,explicitHeight:"number"==typeof t.height};return"event"===i&&(o.eventKind=t.eventKind||r.eventKind||"start",o.trigger=t.trigger||r.trigger||"none"),"gateway"===i&&(o.gatewayType=t.gatewayType||r.gatewayType||"exclusive"),"task"!==i&&"subprocess"!==i||(o.taskType=t.taskType||r.taskType||"none"),o}function buildGroups(e,t){const i=new Map;t.forEach(e=>i.set(e.id,e));const r=1===t.length?t[0]:null,o=[];return e.forEach(e=>{(e=>{let t=o.filter(t=>t.owner===e)[0];return t||(t={owner:e,nodes:[],box:null,didLayout:!1},o.push(t)),t})((e.parent?i.get(e.parent):void 0)||r).nodes.push(e)}),o}function layoutGroups(e,t,i,r){e.forEach(e=>{if(e.nodes.some(e=>!e.explicitLeft||!e.explicitTop)){const o=layeredLayout(e.nodes,t,{gapX:i,gapY:r,originX:0,originY:0,direction:"horizontal"});e.nodes.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)}),e.didLayout=!0}e.box=function unionBox(e){if(!e.length)return null;let t=1/0,i=1/0,r=-1/0,o=-1/0;return e.forEach(e=>{t=Math.min(t,e.left),i=Math.min(i,e.top),r=Math.max(r,e.left+e.width),o=Math.max(o,e.top+e.height)}),{minX:t,minY:i,maxX:r,maxY:o}}(e.nodes)})}function compileBpmnDsl(e){const t=e.options||{},i=(e.nodes||[]).map(toWorkingNode),r=i.filter(e=>"pool"===e.dslKind),o=i.filter(e=>"lane"===e.dslKind),n=i.filter(e=>!function isContainer(e){return"pool"===e.dslKind||"lane"===e.dslKind}(e)),s=(e.edges||[]).map((e,t)=>({id:e.id||"edge-"+t,sourceId:e.source,targetId:e.target,flowType:e.type||e.flowType||"sequence",label:e.label||"",condition:e.condition||"",isDefault:!!e.isDefault,linkShape:e.linkShape||"visio"})),a=t.layout||"auto",d=function resolveLaneOwners(e,t){const i=new Map;return e.forEach(e=>{let r=e.parent;r||1!==t.length||(r=t[0].id),r&&i.set(e.id,r)}),i}(o,r),lanesOf=e=>o.filter(t=>d.get(t.id)===e);let l=[];"none"!==a&&(l=buildGroups(n,r.concat(o)),layoutGroups(l,s,Number(t.gapX)||90,Number(t.gapY)||60)),o.forEach(e=>{const t=l.filter(t=>t.owner===e)[0],i=t?t.box:null;e.explicitHeight||(e.height=Math.round(Math.max(56+(i?i.maxY-i.minY:0),S))),e.explicitWidth||(e.width=Math.round(Math.max((i?i.maxX-i.minX:0)+E+56,$)))}),r.forEach(e=>{const t=lanesOf(e.id);if(t.length){const i=t.reduce((e,t)=>Math.max(e,t.width),$);return e.explicitWidth||(e.width=i),e.explicitWidth&&t.forEach(t=>{t.explicitWidth||(t.width=e.width)}),void(e.explicitHeight||(e.height=N+t.reduce((e,t)=>e+t.height,0)))}const i=l.filter(t=>t.owner===e)[0],r=i?i.box:null;e.explicitWidth||(e.width=Math.round(Math.max(56+(r?r.maxX-r.minX:0),$))),e.explicitHeight||(e.height=Math.round(Math.max((r?r.maxY-r.minY:0)+N+56,160)))});let p=60;r.forEach(e=>{e.explicitLeft||(e.left=60),e.explicitTop||(e.top=p),p=Math.max(p,e.top+e.height+60)}),r.forEach(e=>{const t=lanesOf(e.id);if(!t.length)return;const i=e.top+N,r=Math.max(e.height-N,1),o=t.reduce((e,t)=>e+(t.explicitHeight?t.height:0),0),n=t.filter(e=>!e.explicitHeight),s=n.length?Math.max(r-o,0)/n.length:0;let a=i;t.forEach(t=>{t.explicitLeft||(t.left=e.left),t.explicitTop||(t.top=a),!t.explicitHeight&&e.explicitHeight&&(t.height=Math.round(s||S)),a=t.top+t.height})});let c=60;o.forEach(e=>{d.get(e.id)||(e.explicitLeft||(e.left=60),e.explicitTop||(e.top=c,c=e.top+e.height+60))}),l.forEach(e=>{const t=e.box;if(!t||!e.didLayout)return;const i=e.owner;let r=80,o=80;i&&(r=i.left+28,o=i.top+28,"pool"===i.dslKind?o+=N:r+=E),e.nodes.forEach(e=>{e.left=Math.round(r+(e.left-t.minX)),e.top=Math.round(o+(e.top-t.minY))})});return{kind:"bpmn",nodes:r.concat(o).concat(n).map(e=>{const t={id:e.id,typeId:"FlowNode",kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};return void 0!==e.eventKind&&(t.eventKind=e.eventKind),void 0!==e.trigger&&(t.trigger=e.trigger),void 0!==e.gatewayType&&(t.gatewayType=e.gatewayType),void 0!==e.taskType&&(t.taskType=e.taskType),void 0!==e.fillColor&&(t.fillColor=e.fillColor),void 0!==e.strokeColor&&(t.strokeColor=e.strokeColor),t}),edges:s,layout:a,options:t}}function compileUmlDsl(e){const t=e.options||{},i=(e.nodes||[]).map(e=>{return{id:e.id,kind:(t=e,t.kind||"class"),className:e.title||e.name||e.id,abstract:!!e.abstract,attributes:Array.isArray(e.attributes)?e.attributes.map(e=>String(e)):[],methods:Array.isArray(e.methods)?e.methods.map(e=>String(e)):[],left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:240,height:"number"==typeof e.height?e.height:120+24*(e.attributes?e.attributes.length:0)+24*(e.methods?e.methods.length:0)};var t}),r=(e.edges||[]).map((e,t)=>({id:e.id||"relation-"+t,sourceId:e.source,targetId:e.target,relationKind:e.type||e.relation||"association",label:e.label||""})),o=t.layout||"layered";if("none"!==o&&i.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const e=layeredLayout(i,r.filter(e=>"inheritance"===e.relationKind||"realization"===e.relationKind).map(e=>({sourceId:e.targetId,targetId:e.sourceId})),{gapX:Number(t.gapX)||80,gapY:Number(t.gapY)||110,originX:80,originY:80,direction:"vertical"});i.forEach(t=>{const i=e.get(t.id);i&&(t.left=i.left,t.top=i.top)})}return i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"uml",nodes:i,edges:r,layout:o,options:t}}const T={width:160,height:64},j={width:500,height:200};function compileStatechartDsl(e){const t=e.options||{},i=(e.nodes||[]).map(e=>{const t=e.kind||"state",i="initial"===t||"final"===t,r="composite"===t?j:i?{width:24,height:24}:T;return{id:e.id,kind:t,title:e.title||e.name||(i?"":e.id),parent:e.parent,left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:r.width,height:"number"==typeof e.height?e.height:r.height}}),r=(e.edges||[]).map((e,t)=>({id:e.id||"transition-"+t,sourceId:e.source,targetId:e.target,event:e.event||"",guard:e.guard||"",action:e.action||"",label:e.label})),o=t.layout||"auto",n=new Set;if("none"!==o){const e=new Map;i.forEach(t=>e.set(t.id,t));const o=i.filter(t=>!(t.parent&&e.has(t.parent)));if(o.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){const containerOf=t=>{let i=e.get(t);for(;i&&i.parent&&e.has(i.parent);)i=e.get(i.parent);return i?i.id:t},i=r.map(e=>({sourceId:containerOf(e.sourceId),targetId:containerOf(e.targetId)})).filter(e=>e.sourceId!==e.targetId),n=layeredLayout(o,i,{gapX:Number(t.gapX)||110,gapY:Number(t.gapY)||80,originX:80,originY:80,direction:"horizontal"});o.forEach(e=>{const t=n.get(e.id);t&&(e.left=t.left,e.top=t.top)})}let s=0;i.filter(e=>"composite"===e.kind).forEach(e=>{const t=i.filter(t=>t.parent===e.id);if(Number.isNaN(e.left)&&(e.left=80),Number.isNaN(e.top)){const t=Math.max(...o.filter(t=>t!==e&&!Number.isNaN(t.top)).map(e=>e.top+e.height),0);e.top=Math.max(t+60,80)+s,s+=e.height+60}if(!t.length)return;let r=56,a=56,d=0,l=56;const p=e.width-112;t.forEach(e=>{(Number.isNaN(e.left)||Number.isNaN(e.top))&&(r>56&&r+e.width>56+p&&(r=56,a=l+40),n.add(e.id),e.left=r,e.top=a,r+=e.width+40,d=Math.max(d,r),l=Math.max(l,a+e.height))}),e.width=Math.max(e.width,d+56),e.height=Math.max(e.height,l+56)}),i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const offsetChildren=(e,t,r)=>{i.filter(t=>t.parent===e.id).forEach(e=>{n.has(e.id)&&(e.left+=t,e.top+=r,offsetChildren(e,t,r))})};i.filter(e=>"composite"===e.kind).forEach(e=>offsetChildren(e,e.left,e.top))}return{kind:"statechart",nodes:i,edges:r,layout:o,options:t}}function compileGanttDsl(e){const t=e.options||{};return{kind:"gantt",nodes:(e.nodes||[]).map((e,t)=>({id:e.id,title:e.title||e.name||e.id,start:String(e.start||""),days:"number"==typeof e.days?e.days:1,progress:"number"==typeof e.progress?e.progress:0,row:"number"==typeof e.row?e.row:t,resource:String(e.resource||"")})),edges:(e.edges||[]).map((e,t)=>({id:e.id||"dependency-"+t,sourceId:e.source,targetId:e.target})),options:t}}function compilePowerDsl(e){const t=e.options||{},i=(e.nodes||[]).map((e,t)=>{const i=function defaultPlacement(e){return{left:120+e%6*200,top:120+160*Math.floor(e/6)}}(t);return{id:e.id,kind:e.kind,title:e.title||e.name||e.id,voltageLevel:String(e.voltageLevel||""),left:"number"==typeof e.left?e.left:i.left,top:"number"==typeof e.top?e.top:i.top,width:e.width,height:e.height,switchState:e.switchState,source:!!e.source}}),r=new Map;i.forEach(e=>r.set(e.id,e));return{kind:"power",nodes:i,edges:(e.edges||[]).map((e,t)=>({id:e.id||"conductor-"+t,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",voltageLevel:e.voltageLevel||(r.get(e.source)||{}).voltageLevel||""})),attachments:(e.nodes||[]).filter(e=>!!e.attachedTo).map(e=>({deviceId:e.id,busId:String(e.attachedTo)})),options:t}}const A={bpmnPool:"pool",bpmnLane:"lane",bpmnTask:"task",bpmnEvent:"event",bpmnGateway:"gateway",bpmnSubprocess:"subprocess",bpmnDataObject:"dataObject",bpmnAnnotation:"annotation"},M=["terminator","process","decision","io"],V=["class","interface","enum"],P=["initial","final","state","composite"];function stateOf(e){return e&&e.state||{}}function linkOf(e,t){const i=e.links;return i&&i[t]||{}}function absolutePosition(e){let t=0,i=0,r=e;for(;r&&r.state&&"number"==typeof r.state.left;)t+=r.state.left||0,i+=r.state.top||0,r=r.parentNode;return{left:Math.round(t),top:Math.round(i)}}function parentIdOf(e){const t=e&&e.parentNode,i=t&&t.state?t.state.id:void 0;return"string"==typeof i&&i.length>0?i:void 0}function numberOf(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function textOf(e){return"string"==typeof e&&e.length>0?e:void 0}function clean(e){const t={};return Object.keys(e).forEach(i=>{void 0!==e[i]&&(t[i]=e[i])}),t}function nodesOf(e){return e&&Array.isArray(e.nodes)?e.nodes:[]}function edgesOf(e){return e&&Array.isArray(e.edges)?e.edges:[]}function baseOf(e){const t=stateOf(e),i=absolutePosition(e);return{id:t.id,title:textOf(t.title),left:i.left,top:i.top}}function toDsl(e,t={}){const i=e&&e.designer?e.designer:e,r=t.kind||e&&e.kind||function detectKind(e){if(!e)return;if(Array.isArray(e.entities))return"entity";if(void 0!==e.originDate||void 0!==e.dayWidth)return"gantt";const t=nodesOf(e)[0],i=t?String(stateOf(t).kind||""):"";return i.startsWith("bpmn")?"bpmn":V.indexOf(i)>=0?"uml":P.indexOf(i)>=0?"statechart":p.indexOf(i)>=0?"power":M.indexOf(i)>=0?"flowchart":void 0}(i);if(!r)throw new Error("toDsl(): 无法推断文档类型(实例里没有可识别的图元)。请显式传入 kind:'flowchart' | 'bpmn' | 'uml' | 'statechart' | 'gantt' | 'power' | 'entity'。");switch(r){case"flowchart":return function exportFlow(e){return{schemaVersion:1,kind:"flowchart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:t.kind,width:numberOf(t.width),height:numberOf(t.height),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,sourcePort:i.position,targetPort:r.position,label:textOf(t.label)||"",linkShape:textOf(t.linkShape)})})}}(i);case"bpmn":return function exportBpmn(e){return{schemaVersion:1,kind:"bpmn",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:A[t.kind]||"task",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height),eventKind:textOf(t.eventKind),trigger:textOf(t.trigger),gatewayType:textOf(t.gatewayType),taskType:textOf(t.taskType),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,type:textOf(t.flowType)||"sequence",label:textOf(t.label),condition:textOf(t.condition),isDefault:!0===t.isDefault||void 0,linkShape:textOf(t.linkShape)})})}}(i);case"uml":return function exportUml(e){return{schemaVersion:1,kind:"uml",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"class",title:textOf(t.className)||textOf(t.title),abstract:!0===t.abstract||void 0,attributes:Array.isArray(t.attributes)?t.attributes.map(e=>String(e)):[],methods:Array.isArray(t.methods)?t.methods.map(e=>String(e)):[],width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,type:textOf(t.relationKind)||"association",label:textOf(t.label)})})}}(i);case"statechart":return function exportStatechart(e){return{schemaVersion:1,kind:"statechart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"state",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,event:textOf(t.event),guard:textOf(t.guard),action:textOf(t.action),label:textOf(t.label)})})}}(i);case"gantt":return function exportGantt(e){return{schemaVersion:1,kind:"gantt",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({id:t.id,title:textOf(t.title),start:textOf(t.start),days:numberOf(t.days),progress:numberOf(t.progress),row:numberOf(t.row),resource:textOf(t.resource)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id})})}}(i);case"power":return function exportPower(e){return{schemaVersion:1,kind:"power",nodes:nodesOf(e).map(e=>{const t=stateOf(e),i=absolutePosition(e);return clean({id:t.id,kind:t.kind,title:textOf(t.title)||textOf(t.name),voltageLevel:textOf(t.voltageLevel),left:i.left,top:i.top,width:numberOf(t.width),height:numberOf(t.height),switchState:textOf(t.switchState),source:!0===t.source||!0===t.energizedSource||void 0,attachedTo:textOf(t.attachedBusId)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,sourcePort:i.position,targetPort:r.position,voltageLevel:textOf(t.voltageLevel)})})}}(i);case"entity":case"er":default:return function exportEr(e){const t=e&&Array.isArray(e.entities)?e.entities:[],i=e&&Array.isArray(e.relations)?e.relations:[];return{schemaVersion:1,entities:t.map(e=>{const t=stateOf(e),i=absolutePosition(e);return clean({id:t.id,name:textOf(t.entityName),fields:Array.isArray(t.fields)?t.fields:[],left:i.left,top:i.top})}),relations:i.map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,type:t.relationType,sourceField:textOf(t.sourceField),targetField:textOf(t.targetField),label:textOf(t.label),onDelete:textOf(t.onDelete),onUpdate:textOf(t.onUpdate),joinTableName:textOf(t.joinTableName),nullable:"boolean"==typeof t.nullable?t.nullable:void 0,sourceCardinality:textOf(t.sourceCardinality),targetCardinality:textOf(t.targetCardinality),linkShape:textOf(t.linkShape)})})}}(i)}}function withDsl(e){return Object.assign(e,{toDsl:()=>toDsl(e)})}function renderDsl(e,i){const r=validateDsl(i);if(!r.valid)throw new Error(r.errors.join("\n"));return isGanttDsl(i)?renderGanttDsl(e,i):isPowerDsl(i)?renderPowerDsl(e,i):isStatechartDsl(i)?renderStatechartDsl(e,i):isUmlDsl(i)?renderUmlDsl(e,i):isBpmnDsl(i)?renderBpmnDsl(e,i):isFlowDsl(i)?renderFlowDsl(e,i):function renderErDsl(e,i){const r=compileDsl(i),o=r.options||{},n=(new t).init(e),s=new d(n);r.entities.forEach(e=>{s.createEntity(e)}),function positionLinks(e,t,i){const r=new Map;return e.entities.forEach(e=>{r.set(e.state.id,e.getMinBoundingBox(!0))}),t.relations.map(e=>{const t=r.get(e.sourceId),o=r.get(e.targetId);let n="R",s="L";if(t&&o){const e=o.center[0]-t.center[0],i=o.center[1]-t.center[1];Math.abs(e)>Math.abs(i)?(n=e>=0?"R":"L",s=e>=0?"L":"R"):(n=i>=0?"B":"T",s=i>=0?"T":"B")}return{...e,routeType:e.routeType||i||"orthogonal",linkShape:e.linkShape||"visio",links:{start:{id:e.sourceId,position:n},end:{id:e.targetId,position:s}}}})}(s,r,o.routeType).forEach(e=>{s.createRelation(e)}),("layered"===r.layout||"horizontal"===r.layout)&&new l({gapX:o.gapX||120,gapY:o.gapY||50}).layoutContainer(n);o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):o.fitViewport&&function fitViewport(e,t,i){const r=t.entities||[];let o=1/0,n=1/0,s=-1/0,a=-1/0;r.forEach(e=>{const t=e.getMinBoundingBox(!0).getMinAndMaxPoint();o=Math.min(o,t.minX),n=Math.min(n,t.minY),s=Math.max(s,t.maxX),a=Math.max(a,t.maxY)});const d=s-o,l=a-n,p=e.canvasWidth||0,c=e.canvasHeight||0;if(!p||!c||d<=0||l<=0)return;const u=Number.isFinite(i)&&i>=0?i:40,h=Math.max(1,p-2*u),f=Math.max(1,c-2*u),m=Math.min(h/d,f/l,1),g=(p-d*m)/2-o*m,b=(c-l*m)/2-n*m;e.setViewport(m,g,b)}(n,s,o.fitViewportPadding);return withDsl({kind:"entity",ice:n,designer:s})}(e,i)}function renderGanttDsl(e,r){const o=compileGanttDsl(r),n=o.options||{},s=(new t).init(e),a=new i(s);return n.dayWidth&&a.setDayWidth(n.dayWidth),o.nodes.forEach(e=>{a.createTask({id:e.id,title:e.title,start:e.start,days:e.days,progress:e.progress,row:e.row,resource:e.resource})}),o.edges.forEach(e=>{a.createDependency(e)}),a.select(null),n.autoSchedule&&a.autoSchedule(),n.viewport?s.setViewport(n.viewport.scale,n.viewport.tx,n.viewport.ty):!1!==n.fitViewport&&a.fitViewport(n.fitViewportPadding),a.resetHistory(),withDsl({kind:"gantt",ice:s,designer:a})}function renderPowerDsl(e,i){const o=compilePowerDsl(i),n=o.options||{},s=(new t).init(e),a=new r(s);n.voltageColors&&a.setVoltageColors(n.voltageColors);const d=new Map;return o.nodes.forEach(e=>{const t=a.createSymbol(e.kind,{id:e.id,title:e.title,name:e.title,voltageLevel:e.voltageLevel,left:e.left,top:e.top,width:e.width,height:e.height,switchState:e.switchState});e.source&&t.setState({energizedSource:!0}),d.set(e.id,t)}),o.attachments.forEach(e=>{const t=d.get(e.deviceId),i=d.get(e.busId);t&&i&&a.attachToBus(t,i)}),o.edges.forEach(e=>{const t=d.get(e.sourceId),i=d.get(e.targetId);t&&i&&a.createLine({sourceId:t.state.id,targetId:i.state.id,sourcePort:e.sourcePort,targetPort:e.targetPort,voltageLevel:e.voltageLevel})}),a.select(null),a.applyTopology(),!1!==n.fitViewport&&a.fitViewport(n.fitViewportPadding),a.resetHistory(),withDsl({kind:"power",ice:s,designer:a})}function renderStatechartDsl(e,i){const r=compileStatechartDsl(i),n=r.options||{},s=(new t).init(e),a=new o(s);return r.nodes.forEach(e=>{a.createState({id:e.id,kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height})}),r.edges.forEach(e=>{a.createTransition(e)}),a.select(null),n.viewport?s.setViewport(n.viewport.scale,n.viewport.tx,n.viewport.ty):!1!==n.fitViewport&&a.fitViewport(n.fitViewportPadding),a.resetHistory(),withDsl({kind:"statechart",ice:s,designer:a})}function renderUmlDsl(e,i){const r=compileUmlDsl(i),o=r.options||{},s=(new t).init(e),a=new n(s);return r.nodes.forEach(e=>{a.createClass({id:e.id,kind:e.kind,className:e.className,abstract:e.abstract,attributes:e.attributes,methods:e.methods,left:e.left,top:e.top,width:e.width})}),r.edges.forEach(e=>{a.createRelation(e)}),a.select(null),o.viewport?s.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&a.fitViewport(o.fitViewportPadding),a.resetHistory(),withDsl({kind:"uml",ice:s,designer:a})}function renderBpmnDsl(e,i){const r=compileBpmnDsl(i),o=r.options||{},n=(new t).init(e),a=new s(n);return r.nodes.forEach(e=>{const t={id:e.id,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};["eventKind","trigger","gatewayType","taskType","fillColor","strokeColor"].forEach(i=>{void 0!==e[i]&&(t[i]=e[i])}),a.createNode(e.kind,t)}),r.edges.forEach(e=>{a.createEdge(e)}),a.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&a.fitViewport(o.fitViewportPadding),a.resetHistory(),withDsl({kind:"bpmn",ice:n,designer:a})}function renderFlowDsl(e,i){const r=compileFlowDsl(i),o=r.options||{},n=(new t).init(e),s=new a(n);return r.nodes.forEach(e=>{s.createNode(e.kind,e)}),r.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"flowchart",ice:n,designer:s})}export{c as DSL_SCHEMA_VERSION,p as POWER_DSL_KINDS,compileBpmnDsl,compileDsl,compileFlowDsl,compileGanttDsl,compilePowerDsl,compileStatechartDsl,compileUmlDsl,isBpmnDsl,isFlowDsl,isGanttDsl,isPowerDsl,isStatechartDsl,isUmlDsl,layeredLayout,renderBpmnDsl,renderDsl,renderFlowDsl,renderGanttDsl,renderPowerDsl,renderStatechartDsl,renderUmlDsl,toDsl,validateBpmnDsl,validateDsl,validateFlowDsl,validateGanttDsl,validatePowerDsl,validateStatechartDsl,validateUmlDsl};
1
+ import{FLOW_NODE_KINDS as e,FlowNode as t,ICE as i,GanttDesigner as r,PowerDesigner as o,StatechartDesigner as n,UmlDesigner as s,BpmnDesigner as a,FlowDesigner as d,EntityDesigner as l,ICELayeredLayout as p}from"ice-entity-designer";function isFlowDsl(e){return!!e&&"object"==typeof e&&"flowchart"===e.kind}function isBpmnDsl(e){return!!e&&"object"==typeof e&&"bpmn"===e.kind}function isUmlDsl(e){return!!e&&"object"==typeof e&&"uml"===e.kind}function isStatechartDsl(e){return!!e&&"object"==typeof e&&"statechart"===e.kind}function isGanttDsl(e){return!!e&&"object"==typeof e&&"gantt"===e.kind}const c=["busbar","breaker","disconnector","loadSwitch","earthingSwitch","earth","currentTransformer","voltageTransformer","transformer","fuse","arrester","reactor","generator","motor","load","capacitor","arcSuppressionCoil","threeWindingTransformer","groundingTransformer","groundingResistor","cable","cableTermination","cubicle"];function isPowerDsl(e){return!!e&&"object"==typeof e&&"power"===e.kind}const u=1,h=["terminator","process","decision","io"],f=["T","R","B","L","C"],m=["pool","lane","task","event","gateway","subprocess","dataObject","annotation"],g=["start","intermediate","end"],b=["none","message","timer","error","terminate"],y=["exclusive","parallel","inclusive","event"],w=["none","user","service","script","send","receive","manual"],v=["sequence","message","association"],k=["initial","final","state","composite"],x=["class","interface","enum"],$=["inheritance","realization","association","aggregation","composition","dependency"];function validateDsl(e){return isGanttDsl(e)?validateGanttDsl(e):isPowerDsl(e)?validatePowerDsl(e):isStatechartDsl(e)?validateStatechartDsl(e):isUmlDsl(e)?validateUmlDsl(e):isBpmnDsl(e)?validateBpmnDsl(e):isFlowDsl(e)?validateFlowDsl(e):function validateErDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);if(Array.isArray(e.entities)){const i=new Set;e.entities.forEach((e,r)=>{const o=`entities[${r}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(`${o}.id must be a non-empty string`),Array.isArray(e.fields)?e.fields.forEach((e,i)=>{e&&"object"==typeof e&&"string"==typeof e.name&&e.name.trim()||t.push(`${o}.fields[${i}].name must be a non-empty string`)}):t.push(`${o}.fields must be an array`)):t.push(`${o} must be an object`)})}else t.push("entities must be an array");void 0===e.relations||Array.isArray(e.relations)?(e.relations||[]).forEach((e,i)=>{const r=`relations[${i}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()||t.push(`${r}.source must be a non-empty string`),"string"==typeof e.target&&e.target.trim()||t.push(`${r}.target must be a non-empty string`)):t.push(`${r} must be an object`)}):t.push("relations must be an array");return{valid:0===t.length,errors:t}}(e)}function validateBpmnDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const i=new Set,r=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,o)=>{const n=`nodes[${o}]`;if(!e||"object"!=typeof e)return void t.push(`${n} must be an object`);"string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):(i.add(e.id),r.set(e.id,e.kind||"task")):t.push(`${n}.id must be a non-empty string`),void 0!==e.kind&&-1===m.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${m.join("/")}`),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${n}.${i} must be a number when present`)});[["eventKind",g],["trigger",b],["gatewayType",y],["taskType",w]].forEach(([i,r])=>{const o=e[i];void 0!==o&&-1===r.indexOf(o)&&t.push(`${n}.${i} must be one of ${r.join("/")}`)})}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;if("string"!=typeof e.parent||!e.parent.trim())return void t.push(`${n}.parent must be a non-empty string when present`);if(!i.has(e.parent))return void t.push(`${n}.parent must reference an existing node id`);if(e.parent===e.id)return void t.push(`${n}.parent must not reference itself`);const s=r.get(e.parent);"pool"===e.kind?t.push(`${n}.parent is not allowed on a pool (池是最外层容器)`):"lane"===e.kind&&"pool"!==s?t.push(`${n}.parent must reference a pool`):"pool"!==s&&"lane"!==s&&t.push(`${n}.parent must reference a pool or a lane`)})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;if(!e||"object"!=typeof e)return void t.push(`${o} must be an object`);"string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(`${o}.target must reference an existing node id`);const n=void 0!==e.type?e.type:e.flowType;void 0!==n&&-1===v.indexOf(n)&&t.push(`${o}.type must be one of ${v.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(`${o}.label must be a string when present`),void 0!==e.condition&&"string"!=typeof e.condition&&t.push(`${o}.condition must be a string when present`),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(`${o}.linkShape must be "visio" or "bezier" when present`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateFlowDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const i=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,r)=>{const o=`nodes[${r}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(`${o}.id must be a non-empty string`),void 0!==e.kind&&-1===h.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${h.join("/")}`),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${o}.${i} must be a number when present`)})):t.push(`${o} must be an object`)}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(`${o}.target must reference an existing node id`),void 0!==e.label&&"string"!=typeof e.label&&t.push(`${o}.label must be a string when present`),void 0!==e.sourcePort&&-1===f.indexOf(e.sourcePort)&&t.push(`${o}.sourcePort must be one of ${f.join("/")}`),void 0!==e.targetPort&&-1===f.indexOf(e.targetPort)&&t.push(`${o}.targetPort must be one of ${f.join("/")}`),void 0!==e.linkShape&&"visio"!==e.linkShape&&"bezier"!==e.linkShape&&t.push(`${o}.linkShape must be "visio" or "bezier" when present`)):t.push(`${o} must be an object`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateUmlDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const i=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,r)=>{const o=`nodes[${r}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(`${o}.id must be a non-empty string`),void 0!==e.kind&&-1===x.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${x.join("/")}`),["attributes","methods"].forEach(i=>{const r=e[i];void 0!==r&&(Array.isArray(r)&&!r.some(e=>"string"!=typeof e)||t.push(`${o}.${i} must be an array of strings when present`))}),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${o}.${i} must be a number when present`)})):t.push(`${o} must be an object`)}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;if(!e||"object"!=typeof e)return void t.push(`${o} must be an object`);"string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(`${o}.target must reference an existing node id`);const n=void 0!==e.type?e.type:e.relation;void 0!==n&&-1===$.indexOf(n)&&t.push(`${o}.type must be one of ${$.join("/")}`),void 0!==e.label&&"string"!=typeof e.label&&t.push(`${o}.label must be a string when present`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateStatechartDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const i=new Set,r=new Map;return Array.isArray(e.nodes)?(e.nodes.forEach((e,o)=>{const n=`nodes[${o}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${n}.id is duplicated: ${e.id}`):(i.add(e.id),r.set(e.id,e.kind||"state")):t.push(`${n}.id must be a non-empty string`),void 0!==e.kind&&-1===k.indexOf(e.kind)&&t.push(`${n}.kind must be one of ${k.join("/")}`),["left","top","width","height"].forEach(i=>{const r=e[i];void 0!==r&&"number"!=typeof r&&t.push(`${n}.${i} must be a number when present`)})):t.push(`${n} must be an object`)}),e.nodes.forEach((e,o)=>{if(!e||"object"!=typeof e||void 0===e.parent)return;const n=`nodes[${o}]`;"string"==typeof e.parent&&e.parent.trim()&&i.has(e.parent)?e.parent!==e.id?"composite"!==r.get(e.parent)&&t.push(`${n}.parent must reference a composite state`):t.push(`${n}.parent must not reference itself`):t.push(`${n}.parent must reference an existing node id`)})):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(`${o}.source must reference an existing node id`),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(`${o}.target must reference an existing node id`),["event","guard","action","label"].forEach(i=>{const r=e[i];void 0!==r&&"string"!=typeof r&&t.push(`${o}.${i} must be a string when present`)})):t.push(`${o} must be an object`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validateGanttDsl(e){const t=[];if(!e||"object"!=typeof e||Array.isArray(e))return{valid:!1,errors:["DSL root must be an object"]};void 0!==e.schemaVersion&&1!==e.schemaVersion&&t.push(`Unsupported schemaVersion: ${e.schemaVersion}`);const i=new Set;return Array.isArray(e.nodes)?e.nodes.forEach((e,r)=>{const o=`nodes[${r}]`;if(e&&"object"==typeof e){if("string"==typeof e.id&&e.id.trim()?i.has(e.id)?t.push(`${o}.id is duplicated: ${e.id}`):i.add(e.id):t.push(`${o}.id must be a non-empty string`),"string"==typeof e.start&&/^\d{4}-\d{2}-\d{2}$/.test(e.start)||t.push(`${o}.start must be a "YYYY-MM-DD" string`),void 0===e.days||Number(e.days)>=1||t.push(`${o}.days must be a number >= 1 when present`),void 0!==e.progress){const i=Number(e.progress);i>=0&&i<=1||t.push(`${o}.progress must be between 0 and 1 when present`)}void 0===e.row||Number(e.row)>=0||t.push(`${o}.row must be a number >= 0 when present`)}else t.push(`${o} must be an object`)}):t.push("nodes must be an array"),void 0===e.edges||Array.isArray(e.edges)?(e.edges||[]).forEach((e,r)=>{const o=`edges[${r}]`;e&&"object"==typeof e?("string"==typeof e.source&&e.source.trim()&&i.has(e.source)||t.push(`${o}.source must reference an existing task id`),"string"==typeof e.target&&e.target.trim()&&i.has(e.target)||t.push(`${o}.target must reference an existing task id`)):t.push(`${o} must be an object`)}):t.push("edges must be an array when present"),{valid:0===t.length,errors:t}}function validatePowerDsl(e){const t=[],i=Array.isArray(e.nodes)?e.nodes:[];Array.isArray(e.nodes)||t.push("nodes must be an array");const r=new Set;return i.forEach((e,i)=>{const o=`nodes[${i}]`;e&&"object"==typeof e?("string"==typeof e.id&&e.id?r.has(e.id)?t.push(`duplicated node id: ${e.id}`):r.add(e.id):t.push(`${o}.id must be a non-empty string`),-1===c.indexOf(e.kind)&&t.push(`${o}.kind must be one of ${c.join(" / ")}`),void 0===e.voltageLevel||/^\d+(\.\d+)?kV$/.test(String(e.voltageLevel))||t.push(`${o}.voltageLevel must look like "110kV"`)):t.push(`${o} must be an object`)}),i.forEach((e,r)=>{if(!e||void 0===e.attachedTo)return;const o=i.find(t=>t&&t.id===e.attachedTo);o?"busbar"!==o.kind&&t.push(`nodes[${r}].attachedTo must point at a busbar: ${e.attachedTo} is ${o.kind}`):t.push(`nodes[${r}].attachedTo references an unknown node: ${e.attachedTo}`)}),(e.edges||[]).forEach((e,i)=>{const o=`edges[${i}]`;e&&"object"==typeof e?(r.has(e.source)||t.push(`${o}.source references an unknown node: ${e.source}`),r.has(e.target)||t.push(`${o}.target references an unknown node: ${e.target}`),e.source===e.target&&t.push(`${o} must not connect a node to itself`)):t.push(`${o} must be an object`)}),{valid:0===t.length,errors:t}}function entityName(e){return e.entityName||e.name||e.id}function relationType(e){return e.relationType||e.type||"one-to-many"}function compileDsl(e){if(isFlowDsl(e))throw new Error("compileDsl() 只处理 ER 文档;流程图文档请使用 compileFlowDsl()");if(!e||!Array.isArray(e.entities))throw new Error("ER 文档必须包含 entities 数组");return{entities:e.entities.map(e=>({id:e.id,entityName:entityName(e),fields:e.fields||[],left:e.left,top:e.top,width:e.width,height:e.height,style:e.style,headerStyle:e.headerStyle,fieldStyle:e.fieldStyle,dividerStyle:e.dividerStyle,draggable:e.draggable,interactive:e.interactive})),relations:(e.relations||[]).map((e,t)=>({id:e.id||`relation-${t}`,relationType:relationType(e),sourceId:e.source,targetId:e.target,sourceField:e.sourceField||"id",targetField:e.targetField||"id",nullable:e.nullable,onDelete:e.onDelete,onUpdate:e.onUpdate,joinTableName:e.joinTableName,fromKey:e.fromKey,toKey:e.toKey,sourceCardinality:e.sourceCardinality,targetCardinality:e.targetCardinality,label:e.label,style:e.style,arrow:e.arrow,linkShape:e.linkShape,routeType:e.routeType,routeOffset:e.routeOffset,curveType:e.curveType,lineDash:e.lineDash})),layout:e.layout,options:e.options}}function layeredLayout(e,t,i={}){const r=Number(i.gapX)||90,o=Number(i.gapY)||90,n=void 0===i.originX?80:i.originX,s=void 0===i.originY?80:i.originY,a=new Map;if(!e.length)return a;const d=new Map;e.forEach(e=>d.set(e.id,e));const l=new Map,p=new Map;e.forEach(e=>{l.set(e.id,0),p.set(e.id,[])}),t.forEach(e=>{d.has(e.sourceId)&&d.has(e.targetId)&&(l.set(e.targetId,(l.get(e.targetId)||0)+1),p.get(e.sourceId).push(e.targetId))});const c=new Map,u=[];e.forEach(e=>{0===(l.get(e.id)||0)&&(c.set(e.id,0),u.push(e.id))}),u.length||(c.set(e[0].id,0),u.push(e[0].id));let h=e.length*e.length+e.length;for(;u.length&&h-- >0;){const e=u.shift(),t=c.get(e)||0;(p.get(e)||[]).forEach(e=>{const i=t+1;(void 0===c.get(e)?-1:c.get(e))<i&&(c.set(e,i),u.push(e))})}e.forEach(e=>{c.has(e.id)||c.set(e.id,0)});const f=new Map;e.forEach(e=>{const t=c.get(e.id)||0;f.has(t)||f.set(t,[]),f.get(t).push(e)});const m=[...f.keys()].sort((e,t)=>e-t);if("horizontal"===i.direction){const e=m.map(e=>Math.max(...f.get(e).map(e=>e.width)));let t=0;m.forEach((i,n)=>{const s=f.get(i);let d=-(s.reduce((e,t)=>e+t.height,0)+Math.max(s.length-1,0)*o)/2;s.forEach(i=>{a.set(i.id,{left:Math.round(t+(e[n]-i.width)/2),top:Math.round(d)}),d+=i.height+o}),t+=e[n]+r})}else{const e=m.map(e=>Math.max(...f.get(e).map(e=>e.height)));let t=0;m.forEach((i,n)=>{const s=f.get(i);let d=-(s.reduce((e,t)=>e+t.width,0)+Math.max(s.length-1,0)*r)/2;s.forEach(i=>{a.set(i.id,{left:Math.round(d),top:Math.round(t+(e[n]-i.height)/2)}),d+=i.width+r}),t+=e[n]+o})}const g=Math.min(...e.map(e=>a.get(e.id).left)),b=Math.min(...e.map(e=>a.get(e.id).top));return a.forEach(e=>{e.left+=n-g,e.top+=s-b}),a}const O={terminator:{width:e.terminator.width,height:e.terminator.height,fill:e.terminator.fill,stroke:e.terminator.stroke},process:{width:e.process.width,height:e.process.height,fill:e.process.fill,stroke:e.process.stroke},decision:{width:e.decision.width,height:e.decision.height,fill:e.decision.fill,stroke:e.decision.stroke},io:{width:e.io.width,height:e.io.height,fill:e.io.fill,stroke:e.io.stroke}};function compileFlowDsl(e){const i=e.options||{},r=(e.nodes||[]).map(e=>{const i=function nodeKind$1(e){return e.kind&&O[e.kind]?e.kind:"process"}(e),r=O[i];return{id:e.id,typeId:t.typeId,kind:i,title:(o=e,o.title||o.name||o.id),left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:r.width,height:"number"==typeof e.height?e.height:r.height,fillColor:e.fillColor||r.fill,strokeColor:e.strokeColor||r.stroke};var o}),o=(e.edges||[]).map((e,t)=>({id:e.id||`edge-${t}`,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",label:e.label||"",linkShape:e.linkShape||"visio"})),n=i.layout||"layered";return"none"!==n&&r.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))&&(r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),function autoLayout(e,t,i,r){const o=layeredLayout(e,t,{gapX:i,gapY:r});e.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)})}(r,o,Number(i.gapX)||90,Number(i.gapY)||90)),r.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"flowchart",nodes:r,edges:o,layout:n,options:i}}const D={pool:"bpmnPool",lane:"bpmnLane",task:"bpmnTask",event:"bpmnEvent",gateway:"bpmnGateway",subprocess:"bpmnSubprocess",dataObject:"bpmnDataObject",annotation:"bpmnAnnotation"},N=Number(e.bpmnPool.bandSize)||32,E=Number(e.bpmnLane.bandSize)||32,S=Number(e.bpmnLane.height)||130,T=Number(e.bpmnPool.width)||900,j=28;function toWorkingNode(i){const r=i.kind||"task",o=function presetOf(t){return e[t]||e.bpmnTask}(D[r]||"bpmnTask"),n={id:i.id,typeId:t.typeId,kind:D[r]||"bpmnTask",title:i.title||i.name||i.id,left:"number"==typeof i.left?i.left:0,top:"number"==typeof i.top?i.top:0,width:"number"==typeof i.width?i.width:o.width,height:"number"==typeof i.height?i.height:o.height,fillColor:i.fillColor,strokeColor:i.strokeColor,dslKind:r,parent:i.parent,explicitLeft:"number"==typeof i.left,explicitTop:"number"==typeof i.top,explicitWidth:"number"==typeof i.width,explicitHeight:"number"==typeof i.height};return"event"===r&&(n.eventKind=i.eventKind||o.eventKind||"start",n.trigger=i.trigger||o.trigger||"none"),"gateway"===r&&(n.gatewayType=i.gatewayType||o.gatewayType||"exclusive"),"task"!==r&&"subprocess"!==r||(n.taskType=i.taskType||o.taskType||"none"),n}function buildGroups(e,t){const i=new Map;t.forEach(e=>i.set(e.id,e));const r=1===t.length?t[0]:null,o=[];return e.forEach(e=>{(e=>{let t=o.filter(t=>t.owner===e)[0];return t||(t={owner:e,nodes:[],box:null,didLayout:!1},o.push(t)),t})((e.parent?i.get(e.parent):void 0)||r).nodes.push(e)}),o}function layoutGroups(e,t,i,r){e.forEach(e=>{if(e.nodes.some(e=>!e.explicitLeft||!e.explicitTop)){const o=layeredLayout(e.nodes,t,{gapX:i,gapY:r,originX:0,originY:0,direction:"horizontal"});e.nodes.forEach(e=>{const t=o.get(e.id);t&&(e.left=t.left,e.top=t.top)}),e.didLayout=!0}e.box=function unionBox(e){if(!e.length)return null;let t=1/0,i=1/0,r=-1/0,o=-1/0;return e.forEach(e=>{t=Math.min(t,e.left),i=Math.min(i,e.top),r=Math.max(r,e.left+e.width),o=Math.max(o,e.top+e.height)}),{minX:t,minY:i,maxX:r,maxY:o}}(e.nodes)})}function compileBpmnDsl(e){const i=e.options||{},r=(e.nodes||[]).map(toWorkingNode),o=r.filter(e=>"pool"===e.dslKind),n=r.filter(e=>"lane"===e.dslKind),s=r.filter(e=>!function isContainer(e){return"pool"===e.dslKind||"lane"===e.dslKind}(e)),a=(e.edges||[]).map((e,t)=>({id:e.id||`edge-${t}`,sourceId:e.source,targetId:e.target,flowType:e.type||e.flowType||"sequence",label:e.label||"",condition:e.condition||"",isDefault:!!e.isDefault,linkShape:e.linkShape||"visio"})),d=i.layout||"auto",l=function resolveLaneOwners(e,t){const i=new Map;return e.forEach(e=>{let r=e.parent;r||1!==t.length||(r=t[0].id),r&&i.set(e.id,r)}),i}(n,o),lanesOf=e=>n.filter(t=>l.get(t.id)===e);let p=[];"none"!==d&&(p=buildGroups(s,o.concat(n)),layoutGroups(p,a,Number(i.gapX)||90,Number(i.gapY)||60)),n.forEach(e=>{const t=p.filter(t=>t.owner===e)[0],i=t?t.box:null;e.explicitHeight||(e.height=Math.round(Math.max(56+(i?i.maxY-i.minY:0),S))),e.explicitWidth||(e.width=Math.round(Math.max((i?i.maxX-i.minX:0)+E+56,T)))}),o.forEach(e=>{const t=lanesOf(e.id);if(t.length){const i=t.reduce((e,t)=>Math.max(e,t.width),T);return e.explicitWidth||(e.width=i),e.explicitWidth&&t.forEach(t=>{t.explicitWidth||(t.width=e.width)}),void(e.explicitHeight||(e.height=N+t.reduce((e,t)=>e+t.height,0)))}const i=p.filter(t=>t.owner===e)[0],r=i?i.box:null;e.explicitWidth||(e.width=Math.round(Math.max(56+(r?r.maxX-r.minX:0),T))),e.explicitHeight||(e.height=Math.round(Math.max((r?r.maxY-r.minY:0)+N+56,160)))});let c=60;o.forEach(e=>{e.explicitLeft||(e.left=60),e.explicitTop||(e.top=c),c=Math.max(c,e.top+e.height+60)}),o.forEach(e=>{const t=lanesOf(e.id);if(!t.length)return;const i=e.top+N,r=Math.max(e.height-N,1),o=t.reduce((e,t)=>e+(t.explicitHeight?t.height:0),0),n=t.filter(e=>!e.explicitHeight),s=n.length?Math.max(r-o,0)/n.length:0;let a=i;t.forEach(t=>{t.explicitLeft||(t.left=e.left),t.explicitTop||(t.top=a),!t.explicitHeight&&e.explicitHeight&&(t.height=Math.round(s||S)),a=t.top+t.height})});let u=60;n.forEach(e=>{l.get(e.id)||(e.explicitLeft||(e.left=60),e.explicitTop||(e.top=u,u=e.top+e.height+60))}),p.forEach(e=>{const t=e.box;if(!t||!e.didLayout)return;const i=e.owner;let r=80,o=80;i&&(r=i.left+j,o=i.top+j,"pool"===i.dslKind?o+=N:r+=E),e.nodes.forEach(e=>{e.left=Math.round(r+(e.left-t.minX)),e.top=Math.round(o+(e.top-t.minY))})});return{kind:"bpmn",nodes:o.concat(n).concat(s).map(e=>{const i={id:e.id,typeId:t.typeId,kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};return void 0!==e.eventKind&&(i.eventKind=e.eventKind),void 0!==e.trigger&&(i.trigger=e.trigger),void 0!==e.gatewayType&&(i.gatewayType=e.gatewayType),void 0!==e.taskType&&(i.taskType=e.taskType),void 0!==e.fillColor&&(i.fillColor=e.fillColor),void 0!==e.strokeColor&&(i.strokeColor=e.strokeColor),i}),edges:a,layout:d,options:i}}function compileUmlDsl(e){const t=e.options||{},i=(e.nodes||[]).map(e=>{return{id:e.id,kind:(t=e,t.kind||"class"),className:e.title||e.name||e.id,abstract:!!e.abstract,attributes:Array.isArray(e.attributes)?e.attributes.map(e=>String(e)):[],methods:Array.isArray(e.methods)?e.methods.map(e=>String(e)):[],left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:240,height:"number"==typeof e.height?e.height:120+24*(e.attributes?e.attributes.length:0)+24*(e.methods?e.methods.length:0)};var t}),r=(e.edges||[]).map((e,t)=>({id:e.id||`relation-${t}`,sourceId:e.source,targetId:e.target,relationKind:e.type||e.relation||"association",label:e.label||""})),o=t.layout||"layered";if("none"!==o&&i.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const e=layeredLayout(i,r.filter(e=>"inheritance"===e.relationKind||"realization"===e.relationKind).map(e=>({sourceId:e.targetId,targetId:e.sourceId})),{gapX:Number(t.gapX)||80,gapY:Number(t.gapY)||110,originX:80,originY:80,direction:"vertical"});i.forEach(t=>{const i=e.get(t.id);i&&(t.left=i.left,t.top=i.top)})}return i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)}),{kind:"uml",nodes:i,edges:r,layout:o,options:t}}const A={width:160,height:64},M={width:500,height:200},V=56;function compileStatechartDsl(e){const t=e.options||{},i=(e.nodes||[]).map(e=>{const t=e.kind||"state",i="initial"===t||"final"===t,r="composite"===t?M:i?{width:24,height:24}:A;return{id:e.id,kind:t,title:e.title||e.name||(i?"":e.id),parent:e.parent,left:"number"==typeof e.left?e.left:Number.NaN,top:"number"==typeof e.top?e.top:Number.NaN,width:"number"==typeof e.width?e.width:r.width,height:"number"==typeof e.height?e.height:r.height}}),r=(e.edges||[]).map((e,t)=>({id:e.id||`transition-${t}`,sourceId:e.source,targetId:e.target,event:e.event||"",guard:e.guard||"",action:e.action||"",label:e.label})),o=t.layout||"auto",n=new Set;if("none"!==o){const e=new Map;i.forEach(t=>e.set(t.id,t));const o=i.filter(t=>!(t.parent&&e.has(t.parent)));if(o.some(e=>Number.isNaN(e.left)||Number.isNaN(e.top))){const containerOf=t=>{let i=e.get(t);for(;i&&i.parent&&e.has(i.parent);)i=e.get(i.parent);return i?i.id:t},i=r.map(e=>({sourceId:containerOf(e.sourceId),targetId:containerOf(e.targetId)})).filter(e=>e.sourceId!==e.targetId),n=layeredLayout(o,i,{gapX:Number(t.gapX)||110,gapY:Number(t.gapY)||80,originX:80,originY:80,direction:"horizontal"});o.forEach(e=>{const t=n.get(e.id);t&&(e.left=t.left,e.top=t.top)})}let s=0;i.filter(e=>"composite"===e.kind).forEach(e=>{const t=i.filter(t=>t.parent===e.id);if(Number.isNaN(e.left)&&(e.left=80),Number.isNaN(e.top)){const t=Math.max(...o.filter(t=>t!==e&&!Number.isNaN(t.top)).map(e=>e.top+e.height),0);e.top=Math.max(t+60,80)+s,s+=e.height+60}if(!t.length)return;let r=V,a=V,d=0,l=V;const p=e.width-112;t.forEach(e=>{(Number.isNaN(e.left)||Number.isNaN(e.top))&&(r>V&&r+e.width>V+p&&(r=V,a=l+40),n.add(e.id),e.left=r,e.top=a,r+=e.width+40,d=Math.max(d,r),l=Math.max(l,a+e.height))}),e.width=Math.max(e.width,d+V),e.height=Math.max(e.height,l+V)}),i.forEach(e=>{Number.isNaN(e.left)&&(e.left=0),Number.isNaN(e.top)&&(e.top=0)});const offsetChildren=(e,t,r)=>{i.filter(t=>t.parent===e.id).forEach(e=>{n.has(e.id)&&(e.left+=t,e.top+=r,offsetChildren(e,t,r))})};i.filter(e=>"composite"===e.kind).forEach(e=>offsetChildren(e,e.left,e.top))}return{kind:"statechart",nodes:i,edges:r,layout:o,options:t}}function compileGanttDsl(e){const t=e.options||{};return{kind:"gantt",nodes:(e.nodes||[]).map((e,t)=>({id:e.id,title:e.title||e.name||e.id,start:String(e.start||""),days:"number"==typeof e.days?e.days:1,progress:"number"==typeof e.progress?e.progress:0,row:"number"==typeof e.row?e.row:t,resource:String(e.resource||"")})),edges:(e.edges||[]).map((e,t)=>({id:e.id||`dependency-${t}`,sourceId:e.source,targetId:e.target})),options:t}}function compilePowerDsl(e){const t=e.options||{},i=(e.nodes||[]).map((e,t)=>{const i=function defaultPlacement(e){return{left:120+e%6*200,top:120+160*Math.floor(e/6)}}(t);return{id:e.id,kind:e.kind,title:e.title||e.name||e.id,voltageLevel:String(e.voltageLevel||""),left:"number"==typeof e.left?e.left:i.left,top:"number"==typeof e.top?e.top:i.top,width:e.width,height:e.height,switchState:e.switchState,source:!!e.source}}),r=new Map;i.forEach(e=>r.set(e.id,e));return{kind:"power",nodes:i,edges:(e.edges||[]).map((e,t)=>({id:e.id||`conductor-${t}`,sourceId:e.source,targetId:e.target,sourcePort:e.sourcePort||"B",targetPort:e.targetPort||"T",voltageLevel:e.voltageLevel||(r.get(e.source)||{}).voltageLevel||""})),attachments:(e.nodes||[]).filter(e=>!!e.attachedTo).map(e=>({deviceId:e.id,busId:String(e.attachedTo)})),options:t}}const I={bpmnPool:"pool",bpmnLane:"lane",bpmnTask:"task",bpmnEvent:"event",bpmnGateway:"gateway",bpmnSubprocess:"subprocess",bpmnDataObject:"dataObject",bpmnAnnotation:"annotation"},P=["terminator","process","decision","io"],L=["class","interface","enum"],C=["initial","final","state","composite"];function stateOf(e){return e&&e.state||{}}function linkOf(e,t){const i=e.links;return i&&i[t]||{}}function absolutePosition(e){let t=0,i=0,r=e;for(;r&&r.state&&"number"==typeof r.state.left;)t+=r.state.left||0,i+=r.state.top||0,r=r.parentNode;return{left:Math.round(t),top:Math.round(i)}}function parentIdOf(e){const t=e&&e.parentNode,i=t&&t.state?t.state.id:void 0;return"string"==typeof i&&i.length>0?i:void 0}function numberOf(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function textOf(e){return"string"==typeof e&&e.length>0?e:void 0}function clean(e){const t={};return Object.keys(e).forEach(i=>{void 0!==e[i]&&(t[i]=e[i])}),t}function nodesOf(e){return e&&Array.isArray(e.nodes)?e.nodes:[]}function edgesOf(e){return e&&Array.isArray(e.edges)?e.edges:[]}function baseOf(e){const t=stateOf(e),i=absolutePosition(e);return{id:t.id,title:textOf(t.title),left:i.left,top:i.top}}function toDsl(e,t={}){const i=e&&e.designer?e.designer:e,r=t.kind||e&&e.kind||function detectKind(e){if(!e)return;if(Array.isArray(e.entities))return"entity";if(void 0!==e.originDate||void 0!==e.dayWidth)return"gantt";const t=nodesOf(e)[0],i=t?String(stateOf(t).kind||""):"";return i.startsWith("bpmn")?"bpmn":L.indexOf(i)>=0?"uml":C.indexOf(i)>=0?"statechart":c.indexOf(i)>=0?"power":P.indexOf(i)>=0?"flowchart":void 0}(i);if(!r)throw new Error("toDsl(): 无法推断文档类型(实例里没有可识别的图元)。请显式传入 kind:'flowchart' | 'bpmn' | 'uml' | 'statechart' | 'gantt' | 'power' | 'entity'。");switch(r){case"flowchart":return function exportFlow(e){return{schemaVersion:1,kind:"flowchart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:t.kind,width:numberOf(t.width),height:numberOf(t.height),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,sourcePort:i.position,targetPort:r.position,label:textOf(t.label)||"",linkShape:textOf(t.linkShape)})})}}(i);case"bpmn":return function exportBpmn(e){return{schemaVersion:1,kind:"bpmn",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:I[t.kind]||"task",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height),eventKind:textOf(t.eventKind),trigger:textOf(t.trigger),gatewayType:textOf(t.gatewayType),taskType:textOf(t.taskType),fillColor:textOf(t.fillColor),strokeColor:textOf(t.strokeColor)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,type:textOf(t.flowType)||"sequence",label:textOf(t.label),condition:textOf(t.condition),isDefault:!0===t.isDefault||void 0,linkShape:textOf(t.linkShape)})})}}(i);case"uml":return function exportUml(e){return{schemaVersion:1,kind:"uml",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"class",title:textOf(t.className)||textOf(t.title),abstract:!0===t.abstract||void 0,attributes:Array.isArray(t.attributes)?t.attributes.map(e=>String(e)):[],methods:Array.isArray(t.methods)?t.methods.map(e=>String(e)):[],width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,type:textOf(t.relationKind)||"association",label:textOf(t.label)})})}}(i);case"statechart":return function exportStatechart(e){return{schemaVersion:1,kind:"statechart",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({...baseOf(e),kind:textOf(t.kind)||"state",parent:parentIdOf(e),width:numberOf(t.width),height:numberOf(t.height)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,event:textOf(t.event),guard:textOf(t.guard),action:textOf(t.action),label:textOf(t.label)})})}}(i);case"gantt":return function exportGantt(e){return{schemaVersion:1,kind:"gantt",nodes:nodesOf(e).map(e=>{const t=stateOf(e);return clean({id:t.id,title:textOf(t.title),start:textOf(t.start),days:numberOf(t.days),progress:numberOf(t.progress),row:numberOf(t.row),resource:textOf(t.resource)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id})})}}(i);case"power":return function exportPower(e){return{schemaVersion:1,kind:"power",nodes:nodesOf(e).map(e=>{const t=stateOf(e),i=absolutePosition(e);return clean({id:t.id,kind:t.kind,title:textOf(t.title)||textOf(t.name),voltageLevel:textOf(t.voltageLevel),left:i.left,top:i.top,width:numberOf(t.width),height:numberOf(t.height),switchState:textOf(t.switchState),source:!0===t.source||!0===t.energizedSource||void 0,attachedTo:textOf(t.attachedBusId)})}),edges:edgesOf(e).map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,sourcePort:i.position,targetPort:r.position,voltageLevel:textOf(t.voltageLevel)})})}}(i);default:return function exportEr(e){const t=e&&Array.isArray(e.entities)?e.entities:[],i=e&&Array.isArray(e.relations)?e.relations:[];return{schemaVersion:1,entities:t.map(e=>{const t=stateOf(e),i=absolutePosition(e);return clean({id:t.id,name:textOf(t.entityName),fields:Array.isArray(t.fields)?t.fields:[],left:i.left,top:i.top})}),relations:i.map(e=>{const t=stateOf(e),i=linkOf(t,"start"),r=linkOf(t,"end");return clean({id:t.id,source:i.id,target:r.id,type:t.relationType,sourceField:textOf(t.sourceField),targetField:textOf(t.targetField),label:textOf(t.label),onDelete:textOf(t.onDelete),onUpdate:textOf(t.onUpdate),joinTableName:textOf(t.joinTableName),nullable:"boolean"==typeof t.nullable?t.nullable:void 0,sourceCardinality:textOf(t.sourceCardinality),targetCardinality:textOf(t.targetCardinality),linkShape:textOf(t.linkShape)})})}}(i)}}function withDsl(e){return Object.assign(e,{toDsl:()=>toDsl(e)})}function renderDsl(e,t){const r=validateDsl(t);if(!r.valid)throw new Error(r.errors.join("\n"));return isGanttDsl(t)?renderGanttDsl(e,t):isPowerDsl(t)?renderPowerDsl(e,t):isStatechartDsl(t)?renderStatechartDsl(e,t):isUmlDsl(t)?renderUmlDsl(e,t):isBpmnDsl(t)?renderBpmnDsl(e,t):isFlowDsl(t)?renderFlowDsl(e,t):function renderErDsl(e,t){const r=compileDsl(t),o=r.options||{},n=(new i).init(e),s=new l(n);r.entities.forEach(e=>{s.createEntity(e)}),function positionLinks(e,t,i){const r=new Map;return e.entities.forEach(e=>{r.set(e.state.id,e.getMinBoundingBox(!0))}),t.relations.map(e=>{const t=r.get(e.sourceId),o=r.get(e.targetId);let n="R",s="L";if(t&&o){const e=o.center[0]-t.center[0],i=o.center[1]-t.center[1];Math.abs(e)>Math.abs(i)?(n=e>=0?"R":"L",s=e>=0?"L":"R"):(n=i>=0?"B":"T",s=i>=0?"T":"B")}return{...e,routeType:e.routeType||i||"orthogonal",linkShape:e.linkShape||"visio",links:{start:{id:e.sourceId,position:n},end:{id:e.targetId,position:s}}}})}(s,r,o.routeType).forEach(e=>{s.createRelation(e)}),("layered"===r.layout||"horizontal"===r.layout)&&new p({gapX:o.gapX||120,gapY:o.gapY||50}).layoutContainer(n);o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):o.fitViewport&&function fitViewport(e,t,i){const r=t.entities||[];let o=1/0,n=1/0,s=-1/0,a=-1/0;r.forEach(e=>{const t=e.getMinBoundingBox(!0).getMinAndMaxPoint();o=Math.min(o,t.minX),n=Math.min(n,t.minY),s=Math.max(s,t.maxX),a=Math.max(a,t.maxY)});const d=s-o,l=a-n,p=e.canvasWidth||0,c=e.canvasHeight||0;if(!p||!c||d<=0||l<=0)return;const u=Number.isFinite(i)&&i>=0?i:40,h=Math.max(1,p-2*u),f=Math.max(1,c-2*u),m=Math.min(h/d,f/l,1),g=(p-d*m)/2-o*m,b=(c-l*m)/2-n*m;e.setViewport(m,g,b)}(n,s,o.fitViewportPadding);return withDsl({kind:"entity",ice:n,designer:s})}(e,t)}function renderGanttDsl(e,t){const o=compileGanttDsl(t),n=o.options||{},s=(new i).init(e),a=new r(s);return n.dayWidth&&a.setDayWidth(n.dayWidth),o.nodes.forEach(e=>{a.createTask({id:e.id,title:e.title,start:e.start,days:e.days,progress:e.progress,row:e.row,resource:e.resource})}),o.edges.forEach(e=>{a.createDependency(e)}),a.select(null),n.autoSchedule&&a.autoSchedule(),n.viewport?s.setViewport(n.viewport.scale,n.viewport.tx,n.viewport.ty):!1!==n.fitViewport&&a.fitViewport(n.fitViewportPadding),a.resetHistory(),withDsl({kind:"gantt",ice:s,designer:a})}function renderPowerDsl(e,t){const r=compilePowerDsl(t),n=r.options||{},s=(new i).init(e),a=new o(s);n.voltageColors&&a.setVoltageColors(n.voltageColors);const d=new Map;return r.nodes.forEach(e=>{const t=a.createSymbol(e.kind,{id:e.id,title:e.title,name:e.title,voltageLevel:e.voltageLevel,left:e.left,top:e.top,width:e.width,height:e.height,switchState:e.switchState});e.source&&t.setState({energizedSource:!0}),d.set(e.id,t)}),r.attachments.forEach(e=>{const t=d.get(e.deviceId),i=d.get(e.busId);t&&i&&a.attachToBus(t,i)}),r.edges.forEach(e=>{const t=d.get(e.sourceId),i=d.get(e.targetId);t&&i&&a.createLine({sourceId:t.state.id,targetId:i.state.id,sourcePort:e.sourcePort,targetPort:e.targetPort,voltageLevel:e.voltageLevel})}),a.select(null),a.applyTopology(),!1!==n.fitViewport&&a.fitViewport(n.fitViewportPadding),a.resetHistory(),withDsl({kind:"power",ice:s,designer:a})}function renderStatechartDsl(e,t){const r=compileStatechartDsl(t),o=r.options||{},s=(new i).init(e),a=new n(s);return r.nodes.forEach(e=>{a.createState({id:e.id,kind:e.kind,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height})}),r.edges.forEach(e=>{a.createTransition(e)}),a.select(null),o.viewport?s.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&a.fitViewport(o.fitViewportPadding),a.resetHistory(),withDsl({kind:"statechart",ice:s,designer:a})}function renderUmlDsl(e,t){const r=compileUmlDsl(t),o=r.options||{},n=(new i).init(e),a=new s(n);return r.nodes.forEach(e=>{a.createClass({id:e.id,kind:e.kind,className:e.className,abstract:e.abstract,attributes:e.attributes,methods:e.methods,left:e.left,top:e.top,width:e.width})}),r.edges.forEach(e=>{a.createRelation(e)}),a.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&a.fitViewport(o.fitViewportPadding),a.resetHistory(),withDsl({kind:"uml",ice:n,designer:a})}function renderBpmnDsl(e,t){const r=compileBpmnDsl(t),o=r.options||{},n=(new i).init(e),s=new a(n);return r.nodes.forEach(e=>{const t={id:e.id,title:e.title,left:e.left,top:e.top,width:e.width,height:e.height};["eventKind","trigger","gatewayType","taskType","fillColor","strokeColor"].forEach(i=>{void 0!==e[i]&&(t[i]=e[i])}),s.createNode(e.kind,t)}),r.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"bpmn",ice:n,designer:s})}function renderFlowDsl(e,t){const r=compileFlowDsl(t),o=r.options||{},n=(new i).init(e),s=new d(n);return r.nodes.forEach(e=>{s.createNode(e.kind,e)}),r.edges.forEach(e=>{s.createEdge(e)}),s.select(null),o.viewport?n.setViewport(o.viewport.scale,o.viewport.tx,o.viewport.ty):!1!==o.fitViewport&&s.fitViewport(o.fitViewportPadding),s.resetHistory(),withDsl({kind:"flowchart",ice:n,designer:s})}export{u as DSL_SCHEMA_VERSION,c as POWER_DSL_KINDS,compileBpmnDsl,compileDsl,compileFlowDsl,compileGanttDsl,compilePowerDsl,compileStatechartDsl,compileUmlDsl,isBpmnDsl,isFlowDsl,isGanttDsl,isPowerDsl,isStatechartDsl,isUmlDsl,layeredLayout,renderBpmnDsl,renderDsl,renderFlowDsl,renderGanttDsl,renderPowerDsl,renderStatechartDsl,renderUmlDsl,toDsl,validateBpmnDsl,validateDsl,validateFlowDsl,validateGanttDsl,validatePowerDsl,validateStatechartDsl,validateUmlDsl};