Esempio n. 1
0
        private GraphModel CreateGraph()
        {
            // graph sceme:
            //   node1 -------------> node2 -------------> node3
            //          connection1          connection2

            var graph        = new GraphModel(new GraphType());
            var sourceType   = new SourceNode("test");
            var transferType = new NodeWithProperties();
            var destType     = new DestinationNode();

            var node1 = sourceType.CreateNode();

            graph.AddNode(node1);

            var node2 = transferType.CreateNode();

            graph.AddNode(node2);

            var node3 = destType.CreateNode();

            graph.AddNode(node3);

            var connection1 = graph.Connect(sourceType.Out.FromNode(node1), transferType.In.FromNode(node2));
            var connection2 = graph.Connect(transferType.Out.FromNode(node2), destType.In.FromNode(node3));

            (node2.PropertyBlock as NodeWithProperties.Properties).Value = "TestValue";

            return(graph);
        }
Esempio n. 2
0
        public void RemoveNode_Should_Batch_WithAddNode()
        {
            var graph = new GraphModel(Substitute.For <IGraphType>());

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var node2 = new NodeModel(Substitute.For <INodeType>());

            graph.AddNode(node1);
            graph.AddNode(node2);

            IReadOnlyList <NodeModel> removedNodes = null;

            graph.GraphChanged += (an, rn, ac, rc) => { removedNodes = rn; };

            using (graph.BeginChangeSet())
            {
                graph.RemoveNode(node1);
                graph.RemoveNode(node2);

                graph.AddNode(node1);

                Assert.That(removedNodes, Is.Null);
            }

            Assert.That(removedNodes, Is.Not.Null);
            Assert.That(removedNodes, Is.EquivalentTo(new[] { node2 }));
        }
        public void CanConnect_Should_ReturnFalse_WhenDirectionsIncorrect()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var node1    = new NodeModel(Substitute.For <INodeType>());
            var portIn1  = PortModel.Create <int>("In", PortDirection.Input);
            var portOut1 = PortModel.Create <int>("Out", PortDirection.Output);

            node1.AddPort(portIn1);
            node1.AddPort(portOut1);

            var node2    = new NodeModel(Substitute.For <INodeType>());
            var portIn2  = PortModel.Create <int>("In", PortDirection.Input);
            var portOut2 = PortModel.Create <int>("Out", PortDirection.Output);

            node2.AddPort(portIn2);
            node2.AddPort(portOut2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            Assert.That(graph.CanConnect(portIn1, portIn2), Is.False);     // input input
            Assert.That(graph.CanConnect(portOut1, portOut2), Is.False);   // output output
            Assert.That(graph.CanConnect(portIn1, portOut2), Is.False);    // input output
        }
 public void AddTeam(string teamName)
 {
     _teams.Add(teamName);
     _model.AddNode(new Item()
     {
         Category = "Team", Text = teamName, Team = 1, Iteration = 0
     });
 }
        public void AddNode_Should_Throw_WhenNodeIsInGraph()
        {
            var graph = new GraphModel(DefaultType);
            var node  = new NodeModel(Substitute.For <INodeType>());

            graph.AddNode(node);

            Assert.That(() => graph.AddNode(node), Throws.ArgumentException);
        }
Esempio n. 6
0
        public async Task ExecuteAsync_Should_Succeed_MultipleNodes()
        {
            // graph sceme:
            //   node1 -------------> node2 -------------> node3
            //          connection1          connection2

            var graph        = new GraphModel(GraphModelTests.CreateGraphType(new AppendConnection("+")));
            var sourceType   = new SourceNode("test");
            var transferType = new TransferNode();
            var destType     = new DestinationNode();

            var node1 = sourceType.CreateNode();

            graph.AddNode(node1);

            var node2 = transferType.CreateNode();

            graph.AddNode(node2);

            var node3 = destType.CreateNode();

            graph.AddNode(node3);

            var connection1 = graph.Connect(sourceType.Out.FromNode(node1), transferType.In.FromNode(node2));
            var connection2 = graph.Connect(transferType.Out.FromNode(node2), destType.In.FromNode(node3));

            var result = await graph.ExecuteAsync(ExecuteNodeAsync, ExecuteConnectionAsync);

            Assert.That(result, Is.Not.Null);

            var pv1 = GetPortValue <string>(sourceType.Out, result, node1);

            Assert.That(pv1.HasValue, Is.True);
            Assert.That(pv1.Value, Is.EqualTo("test"));

            var pv2 = GetPortValue <string>(transferType.Out, result, node2);

            Assert.That(pv2.HasValue, Is.True);
            Assert.That(pv2.Value, Is.EqualTo("test+"));

            var cv1 = GetConnectionValue <string>(result, connection1);

            Assert.That(cv1.HasValue, Is.True);
            Assert.That(cv1.Value, Is.EqualTo("test+"));

            var cv2 = GetConnectionValue <string>(result, connection2);

            Assert.That(cv2.HasValue, Is.True);
            Assert.That(cv2.Value, Is.EqualTo("test++"));

            Assert.That(destType.OutValue, Is.EqualTo("test++"));
        }
        public void CanConnect_Should_ReturnFalse_WhenNoConnectionType()
        {
            var graph = new GraphModel(GraphTypeWithoutConnections);

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var port1 = PortModel.Create <string>("", PortDirection.Output);

            node1.AddPort(port1);

            var node2 = new NodeModel(Substitute.For <INodeType>());
            var port2 = PortModel.Create <string>("", PortDirection.Input);

            node2.AddPort(port2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            Assert.That(graph.CanConnect(port1, port2), Is.False);
        }
        public void Connect_Should_Throw_WhenCanNotConnect()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Input);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);

            node1.AddPort(port1);
            node2.AddPort(port2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            Assert.That(() => graph.Connect(port1, port2), Throws.InvalidOperationException);
        }
        public void Connect_Should_Remove_OtherConnections()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);
            var node3    = new NodeModel(nodeType);
            var node4    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output, PortCapacity.Single);
            var port2 = PortModel.Create <string>("In", PortDirection.Input, PortCapacity.Single);
            var port3 = PortModel.Create <string>("Out", PortDirection.Output, PortCapacity.Single);
            var port4 = PortModel.Create <string>("In", PortDirection.Input, PortCapacity.Single);

            node1.AddPort(port1);
            node2.AddPort(port2);
            node3.AddPort(port3);
            node4.AddPort(port4);

            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);
            graph.AddNode(node4);

            var connection1 = graph.Connect(port1, port2);

            IReadOnlyList <ConnectionModel> removedConnections = null;

            graph.GraphChanged += (an, rn, ac, rc) => { removedConnections = rc; };

            // ports may accept only one connection, that's why connection1 will be removed
            var connection2 = graph.Connect(port3, port2);

            Assert.That(graph.Connections, Does.Not.Contains(connection1));
            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection1 }));

            // here, connection2 will be removed
            var connection3 = graph.Connect(port3, port4);

            Assert.That(graph.Connections, Does.Not.Contains(connection2));
            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection2 }));
        }
Esempio n. 10
0
        public void Disconnect_Should_Batch_WithConnect()
        {
            var graph = new GraphModel(Substitute.For <IGraphType>());

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var node2 = new NodeModel(Substitute.For <INodeType>());

            var port1 = PortModel.Create <string>("Out1", PortDirection.Output);
            var port2 = PortModel.Create <string>("Out2", PortDirection.Output);
            var port3 = PortModel.Create <string>("In1", PortDirection.Input);
            var port4 = PortModel.Create <string>("In2", PortDirection.Input);

            node1.AddPort(port1);
            node1.AddPort(port2);
            node2.AddPort(port3);
            node2.AddPort(port4);

            graph.AddNode(node1);
            graph.AddNode(node2);

            var connection1 = graph.Connect(port1, port3);
            var connection2 = graph.Connect(port2, port4);

            IReadOnlyList <ConnectionModel> removedConnections = null;

            graph.GraphChanged += (an, rn, ac, rc) => { removedConnections = rc; };

            using (graph.BeginChangeSet())
            {
                graph.Disconnect(connection1);
                graph.Disconnect(connection2);

                var connection3 = graph.Connect(port1, port3);

                Assert.That(removedConnections, Is.Null);
                Assert.That(connection3, Is.SameAs(connection1));
            }

            Assert.That(removedConnections, Is.Not.Null);
            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection2 }));
        }
Esempio n. 11
0
        public void Serialize_Should_Throw_WhenNodeTypeNameNotSupported_ByCustomResolver()
        {
            var graphType = new GraphTypeWithResolver();
            var graph     = new GraphModel(graphType);

            var nodeType = new SourceNode();
            var node     = nodeType.CreateNode();

            graph.AddNode(node);

            Assert.That(() => GraphSerializer.Serialize(graph), Throws.InvalidOperationException);
        }
Esempio n. 12
0
        public void Execute_Should_Succeed_CatchException_DuringNodeActionExecution()
        {
            var exceptionToCatch = new Exception("some_exception");
            var graph            = new GraphModel(GraphModelTests.GraphTypeWithConnections);

            graph.AddNode(new SourceNode("test").CreateNode());

            var result = graph.Execute(context => throw exceptionToCatch);

            Assert.IsNotEmpty(result.Exceptions);
            Assert.That(result.Exceptions, Has.Member(exceptionToCatch));
        }
        public void Connect_Should_Succeed()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);

            node1.AddPort(port1);
            node2.AddPort(port2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            IReadOnlyList <ConnectionModel> list = null;

            graph.GraphChanged += (an, rn, ac, rc) => { list = ac; };

            var connection = graph.Connect(port1, port2);

            Assert.That(connection, Is.Not.Null);

            Assert.That(graph.Connections, Does.Contain(connection));

            Assert.That(port1.Connections, Has.Count.EqualTo(1));
            Assert.That(port2.Connections, Has.Count.EqualTo(1));

            Assert.That(port1.Connections, Is.EquivalentTo(port2.Connections));

            Assert.That(connection.From, Is.EqualTo(port1));
            Assert.That(connection.To, Is.EqualTo(port2));

            Assert.That(list, Is.Not.Null);
            Assert.That(list.Count, Is.EqualTo(1));
            Assert.That(list, Does.Contain(connection));
        }
Esempio n. 14
0
        public void SerializeDeserialize_Should_Succeed_WithCustomTypeResolver()
        {
            var graphType = new GraphTypeWithResolver();
            var graph     = new GraphModel(graphType);

            var nodeType = new NodeWithProperties();
            var node     = nodeType.CreateNode();

            graph.AddNode(node);

            var newGraph = SaveLoadGraph(graph);

            Assert.That(newGraph.Nodes.Count, Is.EqualTo(graph.Nodes.Count));
        }
Esempio n. 15
0
        public void Execute_Should_Succeed_CatchException_DuringConnectionActionExecution()
        {
            var exceptionToCatch = new Exception("some_exception");

            var graph        = new GraphModel(GraphModelTests.CreateGraphType(new AppendConnection("+")));
            var sourceType   = new SourceNode("test");
            var transferType = new TransferNode();

            var node1 = sourceType.CreateNode();

            graph.AddNode(node1);

            var node2 = transferType.CreateNode();

            graph.AddNode(node2);

            graph.Connect(sourceType.Out.FromNode(node1), transferType.In.FromNode(node2));

            var result = graph.Execute(ExecuteNode, _ => throw exceptionToCatch);

            Assert.IsNotEmpty(result.Exceptions);
            Assert.That(result.Exceptions, Has.Member(exceptionToCatch));
        }
        public void RemoveNode_Should_Remove_Connections()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var node2 = new NodeModel(Substitute.For <INodeType>());
            var node3 = new NodeModel(Substitute.For <INodeType>());

            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);
            var port3 = PortModel.Create <string>("Out", PortDirection.Output);
            var port4 = PortModel.Create <string>("In", PortDirection.Input);

            node1.AddPort(port1);
            node2.AddPort(port2);
            node2.AddPort(port3);
            node3.AddPort(port4);

            var connection1 = graph.Connect(port1, port2);
            var connection2 = graph.Connect(port3, port4);

            IReadOnlyList <ConnectionModel> removedConnections = null;

            graph.GraphChanged += (an, rn, ac, rc) => removedConnections = rc;

            graph.RemoveNode(node2);

            Assert.That(graph.Connections, Does.Not.Contains(connection1));
            Assert.That(graph.Connections, Does.Not.Contains(connection2));

            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection1, connection2 }));
        }
Esempio n. 17
0
        public async Task ExecuteAsync_Should_Succeed_SingleNode()
        {
            var graph    = new GraphModel(GraphModelTests.GraphTypeWithConnections);
            var nodeType = new SourceNode("test");
            var node     = nodeType.CreateNode();

            graph.AddNode(node);

            var result = await graph.ExecuteAsync(ExecuteNodeAsync);

            Assert.That(result, Is.Not.Null);

            var v1 = GetPortValue <string>(nodeType.Out, result, node);

            Assert.That(v1.HasValue, Is.True);
            Assert.That(v1.Value, Is.EqualTo("test"));
        }
Esempio n. 18
0
        public void ExecuteGraph_Should_Succeed_WithGraph()
        {
            var graphType = Substitute.For <IExecutableGraphType <string> >();

            var graph    = new GraphModel(graphType);
            var nodeType = new SourceNode("test");
            var node     = nodeType.CreateNode();

            graph.AddNode(node);

            var args   = "test";
            var result = GraphExecutor.ExecuteGraph(graph, args);

            Assert.That(result, Is.Not.Null);

            graphType.Received().Execute(graph, args);
        }
        public void AddNode_Should_Succeed()
        {
            var graph = new GraphModel(DefaultType);
            var node  = new NodeModel(Substitute.For <INodeType>());

            IReadOnlyList <NodeModel> list = null;

            graph.GraphChanged += (an, rn, ac, rc) => { list = an; };
            graph.AddNode(node);

            Assert.That(graph.Nodes, Does.Contain(node));
            Assert.That(node.Graph, Is.EqualTo(graph));

            Assert.That(list, Is.Not.Null);
            Assert.That(list.Count, Is.EqualTo(1));
            Assert.That(list, Does.Contain(node));
        }
Esempio n. 20
0
        public void ExecuteGraphAsync_Should_Succeed_WithGraph()
        {
            var graphType = Substitute.For <IExecutableAsyncGraphType <string> >();

            graphType.ExecuteAsync(Arg.Any <GraphModel>(), Arg.Any <string>()).Returns(Task.FromResult(Substitute.For <IGraphExecutionResult>()));

            var graph    = new GraphModel(graphType);
            var nodeType = new SourceNode("test");
            var node     = nodeType.CreateNode();

            graph.AddNode(node);

            var args   = "test";
            var result = GraphExecutor.ExecuteGraphAsync(graph, args);

            Assert.That(result, Is.Not.Null);

            graphType.Received().ExecuteAsync(graph, args);
        }
Esempio n. 21
0
        public void Execute_Should_Succeed_SingleNode_Cached()
        {
            var graph    = new GraphModel(GraphModelTests.GraphTypeWithConnections);
            var nodeType = new SourceNode("test");
            var node     = nodeType.CreateNode();

            graph.AddNode(node);

            var result = graph.Execute(ExecuteNode);

            result = graph.Execute(ExecuteNode);   // this line will take value from cache

            Assert.That(result, Is.Not.Null);

            var v1 = GetPortValue <string>(nodeType.Out, result, node);

            Assert.That(v1.HasValue, Is.True);
            Assert.That(v1.Value, Is.EqualTo("test"));
        }
        public void AddNode_Should_Throw_WhenNullNode()
        {
            var graph = new GraphModel(DefaultType);

            Assert.That(() => graph.AddNode(null), Throws.ArgumentNullException);
        }
Esempio n. 23
0
 public static GraphModel ParseGraphJson(string json, string root)
 {
     ZLog.Log("ParseGraphJson..." + json.Length);
     if (string.IsNullOrEmpty(json))
     {
         return(null);
     }
     try
     {
         var        obj   = JsonMapper.ToObject(json);
         GraphModel model = new GraphModel(root)
         {
             name = (string)obj["name"],
         };
         if (((IDictionary)obj).Contains("parent"))
         {
             model.Parent = (string)obj["parent"];
         }
         var center = (string)obj["center"];
         model.SetCenter(center);
         var nodes = obj["nodes"];
         if (nodes != null && nodes.IsArray)
         {
             foreach (JsonData item in nodes)
             {
                 NodeInfo node = new NodeInfo
                 {
                     UID    = (string)item["uid"],
                     name   = (string)item["name"],
                     avatar = (string)item["avatar"],
                     type   = (string)item["type"],
                     parent = center
                 };
                 if (string.IsNullOrEmpty(node.avatar))
                 {
                     node.avatar = Path.Combine(root, "cover/" + node.name + ".png");
                 }
                 node.fullPath = Path.Combine(root, "entity/entity_" + node.UID + ".json");
                 model.AddNode(node, null);
             }
         }
         var edges = obj["edges"];
         if (edges != null && edges.IsArray)
         {
             foreach (JsonData item in edges)
             {
                 LinkInfo edge = new LinkInfo
                 {
                     UID       = (string)item["uid"],
                     name      = (string)item["name"],
                     direction = (DirectionType)(int)item["direction"]
                 };
                 var from = (string)item["from"];
                 edge.from = model.GetNode(from);
                 var to = (string)item["to"];
                 edge.to = model.GetNode(to);
                 if (((IDictionary)item).Contains("type"))
                 {
                     edge.type = (string)item["type"];
                 }
                 model.AddEdge(edge);
             }
             model.CheckVirtualNodes();
         }
         return(model);
     }
     catch (Exception e)
     {
         ZLog.Warning(json);
         ZLog.Warning(e.Message);
         return(null);
     }
 }