//辅助Agent添加节点(粘贴添加)
        public static void AddNode(AgentDesigner agent, NodeDesigner node)
        {
            node.ID = agent.GenNodeID();

            if (node.StartNode)
            {
                node.StartNode = agent.ExistStartNode() ? false : true;
            }

            agent.AddNode(node);

            if (node.Transitions.Count > 0)
            {
                for (int i = 0; i < node.Transitions.Count; i++)
                {
                    Transition transition = node.Transitions[i];
                    transition.FromNode   = node;
                    transition.FromNodeID = node.ID;
                    NodeDesigner childNode = transition.ToNode;
                    AddNode(agent, childNode);
                    transition.ToNodeID = childNode.ID;
                }
                node.Sort();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 添加Agent
        /// </summary>
        private void AddAgent()
        {
            if (WorkSpaceData == null)
            {
                ShowMessage("当前没有工作空间,请新建或者打开工作空间!");
                return;
            }

            if (TreeData == null)
            {
                return;
            }

            string group = string.Empty;

            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is GroupItem)
                {
                    GroupItem groupItem = treeView1.SelectedNode.Tag as GroupItem;
                    group = groupItem.Group.GroupName;
                }
                else if (treeView1.SelectedNode.Tag is AgentItem)
                {
                    AgentItem agentItem = treeView1.SelectedNode.Tag as AgentItem;
                    if (agentItem.GroupItem != null)
                    {
                        group = agentItem.GroupItem.Group.GroupName;
                    }
                }
            }

            AgentDesigner agent = new AgentDesigner();

            agent.GroupName = group;
            string agentID = "NewAgent_" + DateTime.Now.Ticks;

            do
            {
                agentID = "NewAgent_" + DateTime.Now.Ticks;
            } while (TreeData.ExistAgent(agentID));

            agent.AgentID = agentID;

            //创建开始节点
            NodeDesigner startNode = null;
            NodeClass    nodeClass = NodeClasses.FindNode("Sequence");

            if (nodeClass != null)
            {
                Rect rect = new Rect(EditorUtility.Center.x, EditorUtility.Center.y, EditorUtility.NodeWidth,
                                     EditorUtility.NodeHeight);
                startNode           = new NodeDesigner(nodeClass.Label, nodeClass.ClassType, rect);
                startNode.ID        = agent.GenNodeID();
                startNode.StartNode = true;
                startNode.NodeType  = nodeClass.NodeType;
                startNode.Describe  = nodeClass.Describe;

                //创建字段
                for (int i = 0; i < nodeClass.Fields.Count; i++)
                {
                    NodeField     nodeField = nodeClass.Fields[i];
                    FieldDesigner field     = EditorUtility.CreateFieldByNodeField(nodeField);
                    if (field == null)
                    {
                        continue;
                    }
                    startNode.Fields.Add(field);
                }

                agent.AddNode(startNode);
            }

            //创建空操作节点
            NodeClass noopClass = NodeClasses.FindNode("Noop");

            if (startNode != null && noopClass != null)
            {
                Rect rect = new Rect(EditorUtility.Center.x + 250, EditorUtility.Center.y, EditorUtility.NodeWidth,
                                     EditorUtility.NodeHeight);
                NodeDesigner noopNode = new NodeDesigner(noopClass.Label, noopClass.ClassType, rect);
                noopNode.ID       = agent.GenNodeID();
                noopNode.NodeType = noopClass.NodeType;
                noopNode.Describe = noopClass.Describe;
                agent.AddNode(noopNode);

                startNode.AddChildNode(noopNode);
            }

            TreeViewManager.AddAgent(agent);
        }