Exemplo 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);
        }
        public void Connect_Should_Throw_WhenNullPorts()
        {
            var graph = new GraphModel(GraphTypeWithConnections);
            var port  = PortModel.Create <string>("", PortDirection.Input);

            Assert.That(() => graph.Connect(null, null), Throws.ArgumentNullException);
            Assert.That(() => graph.Connect(port, null), Throws.ArgumentNullException);
            Assert.That(() => graph.Connect(null, port), Throws.ArgumentNullException);
        }
Exemplo n.º 3
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 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 }));
        }
Exemplo n.º 5
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 }));
        }
        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 }));
        }
        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 CanConnect_Should_ReturnFalse_WhenPortsAlreadyConnected()
        {
            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);

            graph.Connect(port1, port2);

            Assert.That(graph.CanConnect(port1, port2), Is.False);
        }
        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));
        }
Exemplo n.º 10
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));
        }