Ejemplo n.º 1
0
        public static Node ToModel(this NodeBaseContract node)
        {
            var model = new Node();

            model.Id = node.Id;
            // model.NodeId = node.NodeId;
            model.InNodes  = node.InNodes == null ? new List <long>() : node.InNodes.ToList();
            model.OutNodes = node.OutNodes == null ? new List <long>() : node.OutNodes.ToList();
            model.MetaData = JsonConvert.SerializeObject(node.MetaData);
            model.Location = node.Location;
            model.NodeType = node.Type.ToString();
            model.NodeId   = node.NodeId;
            return(model);
        }
Ejemplo n.º 2
0
 public static NodeBaseContract ToContract(this Node node)
 {
     try
     {
         var contract = new NodeBaseContract();
         contract.Type     = (NodeType)Enum.Parse(typeof(NodeType), node.NodeType);
         contract.Id       = node.Id;
         contract.InNodes  = node.InNodes == null ? new List <long>() : node.InNodes;
         contract.OutNodes = node.OutNodes == null ? new List <long>() : node.OutNodes;
         contract.NodeId   = node.NodeId;
         contract.MetaData = Newtonsoft.Json.JsonConvert.DeserializeObject <NodeMetaData>(node.MetaData);
         return(contract);
     }
     catch (Exception)
     {
         return(null);
     }
 }
Ejemplo n.º 3
0
        public static NodeBase CreateNode(NodeBaseContract contract, IEnumerable <InstanceMapping> mappings)
        {
            NodeBase node = Activator.CreateInstance(contract.MetaData.NodeData.NodeClass) as NodeBase;

            node.Type      = contract.Type;
            node.Id        = contract.NodeId;
            node.LogicPath = contract.LogicPath;
            node.MetaDate  = contract.MetaData;
            var nodeMapping = (from o in mappings where o.NodeId == contract.NodeId select o).FirstOrDefault();

            node.NodeMapping = nodeMapping;
            if (nodeMapping != null)
            {
                foreach (var field in contract.MetaData.FieldsMetaData)
                {
                    var fieldValues = from o in nodeMapping.FieldMappings where o.FieldName == field.Name select o;
                    if (fieldValues.Any())
                    {
                        var          fieldValue = fieldValues.ElementAtOrDefault(0);
                        PropertyInfo prop       = contract.MetaData.NodeData.NodeClass.GetRuntimeProperty(field.Name);
                        if (prop != null)
                        {
                            if (prop.PropertyType.GetTypeInfo().IsEnum)
                            {
                                prop.SetValue(node, Enum.Parse(prop.PropertyType, fieldValue.Value?.ToString()));
                            }
                            else
                            {
                                if (field.ValueType != prop.PropertyType)
                                {
                                    if (field.ValueType == typeof(TimeSpan))
                                    {
                                        prop.SetValue(node, TimeSpan.Parse(fieldValue?.Value));
                                    }
                                    else
                                    {
                                        prop.SetValue(node, Convert.ChangeType(fieldValue?.Value, field.ValueType, null));
                                    }
                                }
                                else
                                {
                                    if (fieldValue != null)
                                    {
                                        if ((fieldValue.Value.StartsWith("{") && fieldValue.Value.EndsWith("}") ||
                                             fieldValue.Value.StartsWith("[") && fieldValue.Value.EndsWith("]")))
                                        {
                                            try
                                            {
                                                var value = JsonConvert.DeserializeObject(fieldValue.Value, prop.PropertyType);
                                                prop.SetValue(node, Convert.ChangeType(value, prop.PropertyType, null));
                                            }
                                            catch
                                            {
                                                prop.SetValue(node, Convert.ChangeType(fieldValue?.Value, prop.PropertyType, null));
                                            }
                                        }
                                        else
                                        {
                                            prop.SetValue(node, Convert.ChangeType(fieldValue?.Value, prop.PropertyType, null));
                                        }
                                    }
                                    else
                                    {
                                        prop.SetValue(node, fieldValue?.Value);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(node);
        }
Ejemplo n.º 4
0
        public async System.Threading.Tasks.Task PadNodeExeTestAsync()
        {
            var padContract = new Engine.Contracts.PadContract()
            {
                Id   = 1,
                Name = "test"
            };

            var constant1 = new Engine.Contracts.NodeBaseContract()
            {
                Id       = 1,
                NodeId   = 1,
                OutNodes = new List <long>()
                {
                    3
                },
                Type     = NodeType.Input,
                MetaData = new NodeMetaData()
                {
                    NodeData = new NodeMetaDataAttribute()
                    {
                        NodeClass = typeof(ConstantNode)
                    },
                    FieldsMetaData = new List <FieldMetaDataAttribute>()
                    {
                        new FieldMetaDataAttribute()
                        {
                            Name      = "Value",
                            ValueType = typeof(int)
                        },
                        new FieldMetaDataAttribute()
                        {
                            Name      = "ConstantType",
                            ValueType = typeof(ConstantType)
                        }
                    }
                }
            };

            var constant2 = new NodeBaseContract()
            {
                Id       = 2,
                NodeId   = 2,
                OutNodes = new List <long>()
                {
                    3
                },
                Type     = NodeType.Input,
                MetaData = new NodeMetaData()
                {
                    NodeData = new NodeMetaDataAttribute()
                    {
                        NodeClass = typeof(ConstantNode)
                    },
                    FieldsMetaData = new List <FieldMetaDataAttribute>()
                    {
                        new FieldMetaDataAttribute()
                        {
                            Name      = "Value",
                            ValueType = typeof(int)
                        },
                        new FieldMetaDataAttribute()
                        {
                            Name      = "ConstantType",
                            ValueType = typeof(ConstantType)
                        }
                    }
                }
            };

            var add = new NodeBaseContract()
            {
                Id      = 3,
                NodeId  = 3,
                InNodes = new List <long>()
                {
                    1, 2
                },
                Type     = NodeType.Output,
                MetaData = new NodeMetaData()
                {
                    NodeData = new NodeMetaDataAttribute()
                    {
                        NodeClass = typeof(AddNode)
                    },
                    FieldsMetaData = new List <FieldMetaDataAttribute>()
                    {
                        new FieldMetaDataAttribute()
                        {
                            Name            = "Left",
                            Direction       = FieldDirection.Input,
                            MappedNodeId    = 1,
                            MappedFieldName = "Value"
                        },
                        new FieldMetaDataAttribute()
                        {
                            Name         = "Right",
                            MappedNodeId = 2,
                            Direction    = FieldDirection.Input,
                        }
                    }
                }
            };

            padContract.Nodes = new List <NodeBaseContract>()
            {
                constant1, constant2, add
            };

            List <InstanceMapping> mappings = new List <InstanceMapping>()
            {
                new InstanceMapping()
                {
                    NodeId        = 1,
                    FieldMappings = new List <FieldMapping>()
                    {
                        new FieldMapping()
                        {
                            FieldName = "Value", Value = "1"
                        },
                        new FieldMapping()
                        {
                            FieldName = "ConstantType", Value = "Int"
                        },
                    }
                },
                new InstanceMapping()
                {
                    NodeId        = 2,
                    FieldMappings = new List <FieldMapping>()
                    {
                        new FieldMapping()
                        {
                            FieldName = "Value", Value = "1"
                        },
                        new FieldMapping()
                        {
                            FieldName = "ConstantType", Value = "Int"
                        },
                    }
                }
            };
            var instance = new Instances(mappings);


            var padnodeContract = new Engine.Contracts.PadContract()
            {
                Id   = 1,
                Name = "test"
            };

            var padNode = new NodeBaseContract()
            {
                Id       = 1,
                NodeId   = 1,
                Type     = NodeType.Input,
                OutNodes = new List <long>()
                {
                    2
                },
                MetaData = new NodeMetaData()
                {
                    NodeData = new NodeMetaDataAttribute()
                    {
                        NodeClass = typeof(PadNode)
                    },
                    FieldsMetaData = new List <FieldMetaDataAttribute>()
                    {
                        new FieldMetaDataAttribute()
                        {
                            Name      = "PadContract",
                            ValueType = typeof(Engine.Contracts.PadContract)
                        },
                        new FieldMetaDataAttribute()
                        {
                            Name      = "PadMode",
                            ValueType = typeof(ExecutionMode)
                        },
                        new FieldMetaDataAttribute()
                        {
                            Name      = "Instance",
                            ValueType = typeof(Instances)
                        }
                    }
                }
            };
            var constantOutput = new NodeBaseContract()
            {
                Id       = 2,
                NodeId   = 2,
                Type     = NodeType.Output,
                MetaData = new NodeMetaData()
                {
                    NodeData = new NodeMetaDataAttribute()
                    {
                        NodeClass = typeof(ConstantNode)
                    },
                    FieldsMetaData = new List <FieldMetaDataAttribute>()
                    {
                        new FieldMetaDataAttribute()
                        {
                            Name         = "Value",
                            MappedNodeId = 1,
                            Direction    = FieldDirection.Input,
                        },
                        new FieldMetaDataAttribute()
                        {
                            Name      = "ConstantType",
                            ValueType = typeof(ConstantType)
                        }
                    }
                }
            };

            padnodeContract.Nodes = new List <NodeBaseContract>()
            {
                padNode, constantOutput
            };

            var padInstance = new Instances(
                new List <InstanceMapping>()
            {
                new InstanceMapping()
                {
                    NodeId        = 1,
                    FieldMappings = new List <FieldMapping>()
                    {
                        new FieldMapping()
                        {
                            FieldName = "PadContract", Value = JsonConvert.SerializeObject(padContract)
                        },
                        new FieldMapping()
                        {
                            FieldName = "PadMode", Value = "Normal"
                        },
                        new FieldMapping()
                        {
                            FieldName = "Instance", Value = JsonConvert.SerializeObject(instance)
                        },
                    }
                }
            });

            var pad = PadFactory.CreateInstance(padnodeContract, ExecutionMode.Normal, padInstance);
            await pad.Init();

            await pad.Execute(pad.Context, instance);

            Assert.Equal(ExecutionStatus.Success, pad.Context.Status);
            Assert.Equal(2.0, pad.Context.Result);
        }