예제 #1
0
        //called when data for any output pin is requested
        public override void Evaluate(int SpreadMax)
        {
            var FieldCount = Getters.Count;

            if (FLearn[0] && FInput.SliceCount > 0 && FInput[0] != null)
            {
                var type = FInput[0].GetType();

                FLogger.Log(LogType.Debug, "Learning Type... " + type.FullName);

                FieldInfo[] fields = type.GetFields();

                FieldCount       = fields.Length;
                FName.SliceCount = FieldCount;

                Getters.Clear();
                var formular = new MessageFormular(type.FullName, "");
                for (var i = 0; i < FieldCount; i++)
                {
                    var name = fields[i].Name;
                    FName[i] = name;

                    var fieldInfo = type.GetField(name);

                    try
                    {
                        formular.Append(new FormularFieldDescriptor(fieldInfo.FieldType, name, 1), true);

                        var getter = fieldInfo.CompileGetter();
                        Getters.Add(name, getter);

                        FLogger.Log(LogType.Debug, "Success: " + fieldInfo.FieldType.Name + " " + name);
                    }
                    catch (Exception)
                    {
                        FLogger.Log(LogType.Debug, "Failed: " + fieldInfo.FieldType.Name + " " + name);
                        //FLogger.Log(ex, LogType.Debug);
                    }
                }
                Formular = formular;
            }

            SpreadMax          = FInput.SliceCount;
            FOutput.SliceCount = SpreadMax;

            for (int i = 0; i < SpreadMax; i++)
            {
                var m = new Message(Formular.Name);
                foreach (var fieldName in Formular.FieldNames)
                {
                    var getter = Getters[fieldName];

                    var bin = BinFactory.New(Formular[fieldName].Type);
                    bin.Add(getter(FInput[i]));
                    m[fieldName] = bin;
                }
                FOutput[i] = m;
            }
        }
예제 #2
0
        //called when data for any output pin is requested
        public override void Evaluate(int SpreadMax)
        {
            var FieldCount = Getters.Count;
            if (FLearn[0] && FInput.SliceCount > 0 && FInput[0] != null)
            {
                var type = FInput[0].GetType();

                FLogger.Log(LogType.Debug, "Learning Type... "+type.FullName);

                FieldInfo[] fields = type.GetFields();

                FieldCount = fields.Length;
                FName.SliceCount = FieldCount;

                Getters.Clear();
                var formular = new MessageFormular(type.FullName, "");
                for (var i = 0; i < FieldCount; i++)
                {
                    var name = fields[i].Name;
                    FName[i] = name;

                    var fieldInfo = type.GetField(name);

                    try
                    {
                        formular.Append(new FormularFieldDescriptor(fieldInfo.FieldType, name, 1), true);

                        var getter = fieldInfo.CompileGetter();
                        Getters.Add(name, getter);

                        FLogger.Log(LogType.Debug, "Success: " + fieldInfo.FieldType.Name + " " + name);
                    }
                    catch (Exception)
                    {
                        FLogger.Log(LogType.Debug, "Failed: " + fieldInfo.FieldType.Name + " " + name);
                        //FLogger.Log(ex, LogType.Debug);
                    }
                }
                Formular = formular;
            }

            SpreadMax = FInput.SliceCount;
            FOutput.SliceCount = SpreadMax;

            for (int i = 0; i < SpreadMax; i++)
            {
                var m = new Message(Formular.Name);
                foreach (var fieldName in Formular.FieldNames)
                {
                    var getter = Getters[fieldName];

                    var bin = BinFactory.New(Formular[fieldName].Type);
                    bin.Add(getter(FInput[i]));
                    m[fieldName] = bin;

                }
                FOutput[i] = m;
            }
        }
예제 #3
0
        private MessageFormular RetrieveAdoptedFormular(string formularName)
        {
            MessageFormular reg;

            try
            {
                reg = RetrieveRegisteredFormular(formularName);
            }
            catch (RegistryException)
            {
                reg = new MessageFormular(MessageFormular.DYNAMIC, FConfig[0]);
                // i.e. not found. might happen during first frame
            }
            reg.Require(RequireEnum.NoneBut, Formular);

            var formular = new MessageFormular(formularName, "");

            // all required fields, in order as the old one
            // overwrites any type differences, or sizes
            var oldNames = Formular.FieldNames.ToList();
            var required = reg.FieldDescriptors.Where(f => f.IsRequired).OrderBy(f => oldNames.IndexOf(f.Name));

            foreach (var field in required)
            {
                formular.Append(field, true);
            }

            // all non-required fields, in order as from registry
            var extraNames = reg.FieldNames.ToList();
            var extra      = reg.FieldDescriptors.Where(f => !f.IsRequired).OrderBy(f => extraNames.IndexOf(f.Name));

            foreach (var field in extra)
            {
                formular.Append(field, false);
            }

            return(formular);
        }
예제 #4
0
        public void Evaluate(int SpreadMax)
        {
            if (FConfiguration.IsAnyInvalid() || FName.IsAnyInvalid()) return;

            ForceUpdate |= !FUpdate.IsAnyInvalid() && FUpdate[0];
            var change = ForceUpdate || FInherits.IsChanged || FName.IsChanged || FConfiguration.IsChanged;

            if (!ForceUpdate && !change)
            {
                if (_lastException != null)
                {
                    FError.FlushItem(_lastException.ToString());
                    throw _lastException;
                }
            }

            // untoggle exception.
            if (_lastException != null)
            {
                var tmp = _lastException;
                FError.FlushItem(_lastException.ToString());
                _lastException = null; // assume innocence
                throw tmp;
            }

            if (!change && !ForceUpdate) return;

            FOutput.SliceCount = SpreadMax = FName.SliceCount;

            var id = this.PluginHost.GetNodePath(false);
            var reg = MessageFormularRegistry.Context;

            for (int i = 0; i < SpreadMax; i++)
            {
                if (string.IsNullOrWhiteSpace(FName[i]))
                {
                    _lastException = new ParseFormularException("A Formular cannot have an empty Name.");
                    return;
                }

                var config = string.Join(", ", FConfiguration[i]).Split(',');

                var fields = new List<FormularFieldDescriptor>();
                foreach (var def in config)
                {
                    FormularFieldDescriptor field;
                    try
                    {
                        field = new FormularFieldDescriptor(def.Trim(), true);
                    }
                    catch (ParseFormularException e)
                    {
                        _lastException = e;
                        return;
                    }

                    var match = (
                                    from f in fields
                                    where f.Name == field.Name
                                    select f
                                ).FirstOrDefault();

                    if (match != null)
                    {
                        _lastException = new DuplicateFieldException("Cannot add \"" + def + "\" in Formular for [" + FName[i] + "]. Field with the same name already defined.", match, field);
                        return;
                    }
                    else fields.Add(field);
                }

                var formular = new MessageFormular(FName[i], fields);

                if (!FInherits.IsAnyInvalid())
                {
                    // flatten
                    var allFields = (
                                        from form in FInherits
                                        from field in form.FieldDescriptors
                                        select field
                                     ).Distinct();

                    foreach (var field in allFields)
                    {
                        if (!formular.CanAppend(field))
                        {
                            var duplicate = formular[field.Name];
                            _lastException = new DuplicateFieldException("Cannot add new Field \"" + field.ToString() + "\" to Formular [" + formular.Name + "]. Field is already defined as \"" + duplicate.ToString() + "\".", field, duplicate);
                            return;
                        } else
                        {
                            try
                            {
                                formular.Append(field, true);
                            }
                            catch (DuplicateFieldException e)
                            {
                                _lastException = e;
                                return;
                            }
                        }
                    }
                }

                // only register, when update has been hit!
                if (ForceUpdate)
                try
                {
                        var defined = reg.Define(id, formular); // will raise Change events to inform all formularable nodes
                        if (defined)
                            FOutput[i] = formular;
                }
                catch (RegistryException e)
                {
                    _lastException = e;
                    return;
                }
                catch (ArgumentNullException e)
                {
                    _lastException = e;
                    return;
                }
            }

            // only register, when update has been hit!
            if (ForceUpdate)
            {
                foreach (var form in reg.GetFormularsFrom(id).ToList())
                {
                    if (!FName.Contains(form.Name))
                        reg.Undefine(id, form);
                }
                FOutput.Flush();
                EnumManager.UpdateEnum(MessageFormularRegistry.RegistryName, reg.AllFormularNames.First(), reg.AllFormularNames.ToArray());
            }

            ForceUpdate = false;
        }
        public void Evaluate(int SpreadMax)
        {
            if (FConfiguration.IsAnyInvalid() || FName.IsAnyInvalid())
            {
                return;
            }

            ForceUpdate |= !FUpdate.IsAnyInvalid() && FUpdate[0];
            var change = ForceUpdate || FInherits.IsChanged || FName.IsChanged || FConfiguration.IsChanged;


            if (!ForceUpdate && !change)
            {
                if (_lastException != null)
                {
                    FError.FlushItem(_lastException.ToString());
                    throw _lastException;
                }
            }

            // untoggle exception.
            if (_lastException != null)
            {
                var tmp = _lastException;
                FError.FlushItem(_lastException.ToString());
                _lastException = null; // assume innocence
                throw tmp;
            }

            if (!change && !ForceUpdate)
            {
                return;
            }

            FOutput.SliceCount = SpreadMax = FName.SliceCount;

            var id  = this.PluginHost.GetNodePath(false);
            var reg = MessageFormularRegistry.Context;

            for (int i = 0; i < SpreadMax; i++)
            {
                if (string.IsNullOrWhiteSpace(FName[i]))
                {
                    _lastException = new ParseFormularException("A Formular cannot have an empty Name.");
                    return;
                }

                var config = string.Join(", ", FConfiguration[i]).Split(',');

                var fields = new List <FormularFieldDescriptor>();
                foreach (var def in config)
                {
                    FormularFieldDescriptor field;
                    try
                    {
                        field = new FormularFieldDescriptor(def.Trim(), true);
                    }
                    catch (ParseFormularException e)
                    {
                        _lastException = e;
                        return;
                    }

                    var match = (
                        from f in fields
                        where f.Name == field.Name
                        select f
                        ).FirstOrDefault();

                    if (match != null)
                    {
                        _lastException = new DuplicateFieldException("Cannot add \"" + def + "\" in Formular for [" + FName[i] + "]. Field with the same name already defined.", match, field);
                        return;
                    }
                    else
                    {
                        fields.Add(field);
                    }
                }


                var formular = new MessageFormular(FName[i], fields);

                if (!FInherits.IsAnyInvalid())
                {
                    // flatten
                    var allFields = (
                        from form in FInherits
                        from field in form.FieldDescriptors
                        select field
                        ).Distinct();

                    foreach (var field in allFields)
                    {
                        if (!formular.CanAppend(field))
                        {
                            var duplicate = formular[field.Name];
                            _lastException = new DuplicateFieldException("Cannot add new Field \"" + field.ToString() + "\" to Formular [" + formular.Name + "]. Field is already defined as \"" + duplicate.ToString() + "\".", field, duplicate);
                            return;
                        }
                        else
                        {
                            try
                            {
                                formular.Append(field, true);
                            }
                            catch (DuplicateFieldException e)
                            {
                                _lastException = e;
                                return;
                            }
                        }
                    }
                }

                // only register, when update has been hit!
                if (ForceUpdate)
                {
                    try
                    {
                        var defined = reg.Define(id, formular); // will raise Change events to inform all formularable nodes
                        if (defined)
                        {
                            FOutput[i] = formular;
                        }
                    }
                    catch (RegistryException e)
                    {
                        _lastException = e;
                        return;
                    }
                }