From b670c64208a10c789bc7ab61eddb1f8528ef8daa Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期一, 18 三月 2024 15:22:41 +0800
Subject: [PATCH] #
---
zy-asrs-flow/src/components/Flow/GraphTools.jsx | 421 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 414 insertions(+), 7 deletions(-)
diff --git a/zy-asrs-flow/src/components/Flow/GraphTools.jsx b/zy-asrs-flow/src/components/Flow/GraphTools.jsx
index 11f4f30..e93b74b 100644
--- a/zy-asrs-flow/src/components/Flow/GraphTools.jsx
+++ b/zy-asrs-flow/src/components/Flow/GraphTools.jsx
@@ -1,15 +1,422 @@
-import React, { useRef, useEffect } from "react";
+import React, { useRef, useEffect, useState } from "react";
+import { Button, message, Modal } from 'antd';
+import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
+import { solarizedlight } from 'react-syntax-highlighter/dist/esm/styles/prism';
+import { exportDataToServer } from "../../services/flow/api";
+import './css/GraphTools.less'
-export const GraphTools = ({ graphRef,isReady }) => {
+export const GraphTools = ({ graphRef, isReady }) => {
- const exportData = () => {
+ const [isModalOpen, setIsModalOpen] = useState(false);
+ const [preCode, setPreCode] = useState(null);
+
+ const handleOk = () => {
+ setIsModalOpen(false);
+ };
+
+ const handleCancel = () => {
+ setIsModalOpen(false);
+ };
+
+ //棰勮浠g爜
+ const prewCode = () => {
const graph = graphRef.current;
- if (graph) {
+ if (isReady) {
const data = graph.toJSON();
- console.log(data);
- // 杩欓噷浣犲彲浠ュ皢鏁版嵁鍙戦�佸埌鏈嶅姟鍣ㄦ垨淇濆瓨鍒版湰鍦�
+
+ const edges = [];
+ const nodes = [];
+ let rootNode = null;
+ data.cells.forEach((item) => {
+ if (item.shape == "edge") {
+ edges.push(item)
+ } else {
+ nodes.push(item)
+ if (item.data.root) {
+ rootNode = item;
+ }
+ }
+ })
+
+ if (rootNode == null) {
+ message.warning('璇疯缃▼搴忓叆鍙g粍浠�');
+ return;
+ }
+
+ const codeContent = transCode(rootNode, nodes, graph)
+ console.log(codeContent);
+
+ setPreCode(codeContent);
+ setIsModalOpen(true);
}
}
- return <button onClick={exportData}>瀵煎嚭鏁版嵁</button>;
+ const exportData = () => {
+ const graph = graphRef.current;
+ if (isReady) {
+ const data = graph.toJSON();
+ console.log(data);
+ // 杩欓噷浣犲彲浠ュ皢鏁版嵁鍙戦�佸埌鏈嶅姟鍣ㄦ垨淇濆瓨鍒版湰鍦�
+
+ const edges = [];
+ const nodes = [];
+ let rootNode = null;
+ data.cells.forEach((item) => {
+ if (item.shape == "edge") {
+ edges.push(item)
+ } else {
+ nodes.push(item)
+ if (item.data.root) {
+ rootNode = item;
+ }
+ }
+ })
+
+ if (rootNode == null) {
+ message.warning('璇疯缃▼搴忓叆鍙g粍浠�');
+ return;
+ }
+
+ let result = sortNodes(rootNode, nodes, graph);
+ exportDataToServer({
+ data: result
+ }).then((res) => {
+ console.log(res);
+ })
+ }
+ }
+
+ const sortNodes = (rootNode, nodes, graph) => {
+ let values = nodeDFS(rootNode, nodes, graph);
+ const searchNode = {
+ id: 1,
+ parent: null,
+ logicBool: true
+ };
+
+ let cpValues = JSON.parse(JSON.stringify(values))
+
+ let searchIndex = 0;
+ cpValues.forEach((value) => {
+ if (value.data.isLogic) {
+ value.data.searchLogicId = searchNode.id;
+ value.data.searchLogicBool = searchNode.logicBool;
+ value.data.searchIndex = searchIndex++;
+
+ let tmpSearchNode = JSON.parse(JSON.stringify(searchNode))
+ searchNode.parent = tmpSearchNode;
+ searchNode.id = value.id;
+ searchNode.logicBool = null;
+ searchIndex = 0;
+ } else {
+ let id = searchNode.id;
+ let logicBool = searchNode.logicBool;
+
+ const connectedEdges = graph.getConnectedEdges(value);//鍙栬竟
+ connectedEdges.forEach((edge) => {
+ let tmpSearchNode = JSON.parse(JSON.stringify(searchNode));
+ while (tmpSearchNode.parent != null) {
+ if (edge.source.cell == tmpSearchNode.id) {
+ logicBool = edge.data.logicBool;//鏇存柊鏂瑰悜
+ searchNode.logicBool = edge.data.logicBool;
+ id = tmpSearchNode.id;
+ break;
+ }
+ tmpSearchNode = tmpSearchNode.parent;
+ }
+ })
+
+ value.data.searchLogicId = id;
+ value.data.searchLogicBool = logicBool;
+ value.data.searchIndex = searchIndex++;
+ }
+ })
+
+ return cpValues;
+ }
+
+ const transCode = (rootNode, nodes, graph) => {
+ let codeContent = "";
+
+ let values = nodeDFS(rootNode, nodes, graph);
+ const searchNode = {
+ id: 1,
+ parent: null,
+ logicBool: true
+ };
+
+ let cpValues = JSON.parse(JSON.stringify(values))
+
+ let searchIndex = 0;
+ cpValues.forEach((value) => {
+ if (value.data.isLogic) {
+ value.data.searchLogicId = searchNode.id;
+ value.data.searchLogicBool = searchNode.logicBool;
+ value.data.searchIndex = searchIndex++;
+
+ let tmpSearchNode = JSON.parse(JSON.stringify(searchNode))
+ searchNode.parent = tmpSearchNode;
+ searchNode.id = value.id;
+ searchNode.logicBool = null;
+ searchIndex = 0;
+ } else {
+ let id = searchNode.id;
+ let logicBool = searchNode.logicBool;
+
+ const connectedEdges = graph.getConnectedEdges(value);//鍙栬竟
+ connectedEdges.forEach((edge) => {
+ let tmpSearchNode = JSON.parse(JSON.stringify(searchNode));
+ while (tmpSearchNode.parent != null) {
+ if (edge.source.cell == tmpSearchNode.id) {
+ logicBool = edge.data.logicBool;//鏇存柊鏂瑰悜
+ searchNode.logicBool = edge.data.logicBool;
+ id = tmpSearchNode.id;
+ break;
+ }
+ tmpSearchNode = tmpSearchNode.parent;
+ }
+ })
+
+ value.data.searchLogicId = id;
+ value.data.searchLogicBool = logicBool;
+ value.data.searchIndex = searchIndex++;
+ }
+ })
+ console.log(cpValues);
+ console.log(searchNode);
+
+ let tmp = {}
+ let tmpList = []
+ let tmpIndex = 0;
+ for (let i = cpValues.length - 1; i >= 0; i--) {
+ let item = cpValues[i];
+ if (tmp[item.data.searchLogicId] == null) {
+ tmpList[tmpIndex] = [item];
+ tmp[item.data.searchLogicId] = {
+ index: tmpIndex,
+ code: "",
+ codeTrue: "",
+ codeFalse: "",
+ condition: ""
+ };
+ tmpIndex++;
+ } else {
+ tmpList[tmp[item.data.searchLogicId].index].push(item);
+ }
+ }
+
+ console.log(tmp, tmpList);
+
+ tmpList.forEach((item) => {
+ item.forEach((val) => {
+ let originCode = tmp[val.data.searchLogicId].codeTrue;
+ if (!val.data.searchLogicBool) {
+ originCode = tmp[val.data.searchLogicId].codeFalse;
+ }
+
+ let codeContent = val.data.codeContent;
+
+ if (val.data.isLogic) {
+ codeContent = val.id + "_logic_tag";
+ console.log(val.data);
+ tmp[val.id].condition = val.data.codeContent;
+ } else {
+ codeContent = `
+ //**********${val.attrs.text.text}-start**********//
+ ${codeContent}
+ //**********${val.attrs.text.text}-start**********//
+ `;
+ }
+ let newCode = `
+ ${codeContent}
+
+ ${originCode}
+ `;
+
+ console.log(newCode);
+ if (val.data.searchLogicBool) {
+ tmp[val.data.searchLogicId].codeTrue = newCode;
+ } else {
+ tmp[val.data.searchLogicId].codeFalse = newCode;
+ }
+ })
+ })
+
+ let sortTmp = [];
+ for (var key in tmp) {
+ let obj = tmp[key];
+ obj.id = key;
+ sortTmp[tmp[key].index] = obj;
+ }
+
+ console.log(sortTmp);
+
+ // 鍚堝苟True鍜孎alse
+ sortTmp.forEach((item) => {
+ let nestedIfCode = "";
+ if (item.condition == "") {
+ nestedIfCode = `
+ ${item.codeTrue}
+ ${item.codeFalse}
+ `;
+ } else {
+ nestedIfCode = `
+ if(${item.condition}){
+ // 閫昏緫TRUE
+ ${item.codeTrue}
+ }else {
+ // 閫昏緫FALSE
+ ${item.codeFalse}
+ }
+ `;
+ }
+
+ item.code = nestedIfCode;
+ })
+
+ console.log(sortTmp);
+
+ let finalTmp = {};
+ let sortTmpCopy = JSON.parse(JSON.stringify(sortTmp));
+ sortTmpCopy.forEach((item) => {
+ if (item.id != "1") {
+ let codeContent = item.code;
+ sortTmp.forEach((val) => {
+ codeContent = codeContent.replace(val.id + "_logic_tag", val.code);
+ console.log(item, val.id, codeContent);
+ })
+ finalTmp[item.id] = {
+ code: codeContent
+ }
+ }
+ })
+
+ console.log(sortTmpCopy);
+ console.log(finalTmp);
+
+ sortTmpCopy.forEach((item) => {
+ if (item.id == "1") {
+ let finalCode = item.code;
+ for (var key in finalTmp) {
+ let obj = finalTmp[key];
+ finalCode = finalCode.replace(key + "_logic_tag", obj.code);
+ }
+
+ codeContent = finalCode;
+ }
+ })
+
+ codeContent = formatJavaCode(codeContent)
+ return codeContent;
+ }
+
+ const formatJavaCode = (codeString) => {
+ let baseIndentation = " "; //鐢ㄥ洓涓┖鏍艰〃绀轰竴涓缉杩�
+ let indentationLevel = 0; //澧炲姞杩欒浠g爜鏉ュ垵濮嬪寲indentationLevel
+
+ let formattedCode = codeString
+ .replace(/^\s+/mg, '') // 绉婚櫎姣忚鍓嶉潰鐨勭┖鐧�
+ .replace(/(\{|\})/g, ' $& ') // 璁╁ぇ鎷彿鍛ㄥ洿閮芥湁绌烘牸
+ // 涓婇潰鐨�.replace(/^\s+/mg, '')鍙兘浼氬湪鎷彿鍛ㄥ洿鎻掑叆澶氫綑鐨勭┖鏍硷紝鎵�浠ヤ笅闈㈣繖琛屼唬鐮佷細绉婚櫎寮�澶存垨鏈熬鐨勭┖鏍�
+ .replace(/^\s+|\s+$/mg, '')
+ // 鐢ㄤ簡.split('\n')鍚庯紝姣忎竴琛岄兘鏄暟缁勪腑鐨勪竴涓厓绱狅紝鎵�浠ュ彲浠ラ�氳繃鍑忓皯鎴栧鍔犺寮�澶寸殑绌烘牸鏁版潵娣诲姞鎴栧垹闄ょ缉杩�
+ .split('\n').reduce((formattedCode, currentLine) => {
+ if (currentLine.includes('}')) {
+ // 濡傛灉涓�琛屼腑鍖呭惈鍙冲ぇ鎷彿锛屾垜浠鍑忓皯涓�涓缉杩�
+ indentationLevel--;
+ }
+
+ let indentation = baseIndentation.repeat(indentationLevel);
+ let indentedLine = indentation + currentLine;
+
+ if (currentLine.includes('{')) {
+ // 濡傛灉涓�琛屼腑鍖呭惈宸﹀ぇ鎷彿锛岄偅涓ぇ鎷彿鍚庨潰鐨勪唬鐮侀渶瑕侀澶栫殑涓�涓缉杩�
+ indentationLevel++;
+ }
+
+ return formattedCode + '\n' + indentedLine;
+ }, '');
+
+ return formattedCode;
+ }
+
+ const nodeDFS = (node, nodes, graph) => {
+ let values = [];
+ if (graph) {
+ const connectedEdges = graph.getConnectedEdges(node);
+ const children = [];
+
+ // console.log(node);
+ connectedEdges.forEach((edge) => {
+ nodes.forEach((item) => {
+ if (item.id === edge.target.cell && item.id != node.id) {
+ children.push(item);
+ }
+ })
+ });
+
+ // console.log(connectedEdges);
+ if (children.length != 0) {
+ // console.log(children);
+ children.forEach((node) => {
+ // console.log(node);
+ values.push(node);
+ values = values.concat(nodeDFS(node, nodes, graph))
+ })
+ }
+ }
+
+ return values;
+ }
+
+ const getChildren = (node, nodes, graph) => {
+ const connectedEdges = graph.getConnectedEdges(node);
+ const children = [];
+
+ connectedEdges.forEach((edge) => {
+ nodes.forEach((item) => {
+ if (item.id === edge.target.cell && item.id != node.id) {
+ children.push(item);
+ }
+ })
+ });
+
+ return children;
+ }
+
+ const getDescendants = (node, nodes, graph) => {
+ const descendants = [];
+ const stack = [node];
+
+ let count = 0;
+ while (stack.length > 0) {
+ const current = stack.pop();
+ descendants.push(current);
+
+ const children = getChildren(current, nodes, graph);
+ stack.push(...children);
+ }
+
+ return descendants;
+ }
+
+ return (
+ <>
+ <div className="container">
+ <Button type="primary" onClick={exportData}>
+ 瀵煎嚭鏁版嵁
+ </Button>
+
+ <Button type="primary" onClick={prewCode}>
+ 棰勮浠g爜
+ </Button>
+ </div>
+
+ <Modal title="棰勮浠g爜" open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
+ <SyntaxHighlighter language="java" style={solarizedlight}>
+ {preCode}
+ </SyntaxHighlighter>
+ </Modal>
+ </>
+ );
}
\ No newline at end of file
--
Gitblit v1.9.1