Ejemplo n.º 1
0
        // Handle - e.g Boolean:True
        internal static Parameter Parse(Metamodel metaModel, EnumParameterTypeMapping enumParameterTypeMapping, string parameterText)
        {
            string[] parameterElements = parameterText.Split(Syntax.ParameterQualifier);
            if (parameterElements.Length != 3)
            {
                throw new Exception($"Invalid Name:Type:Value format Parameter Value {parameterText}");
            }

            string name = parameterElements[0];

            ScriptType type;

            if (!Enum.TryParse <ScriptType>(parameterElements[1], out type))
            {
                throw new Exception($"Unknown Parameter Type {parameterElements[1]} - {parameterText}");
            }

            string valueText = parameterElements[2];

            dynamic value = null;

            switch (type)
            {
            case ScriptType.Boolean:
                bool boolValue;
                if (!Boolean.TryParse(valueText, out boolValue))
                {
                    throw new ApplicationException($"Invalid {nameof(Boolean)} value {valueText}");
                }
                value = boolValue;
                break;

            case ScriptType.Int32:
                int intValue;
                if (!Int32.TryParse(valueText, out intValue))
                {
                    throw new ApplicationException($"Invalid {nameof(Int32)} value {valueText}");
                }
                value = intValue;
                break;

            case ScriptType.Double:
                double doubleValue;
                if (!Double.TryParse(valueText, out doubleValue))
                {
                    throw new ApplicationException($"Invalid {nameof(Double)} value {valueText}");
                }
                value = doubleValue;
                break;

            case ScriptType.String:
                if (valueText.Length < 2 || valueText[0] != Syntax.StringQuotation || valueText[^ 1] != Syntax.StringQuotation)
                {
                    throw new Exception($"Unquoted string {nameof(Parameter)} {valueText}");
                }
                // Remove leading and trailing " chars
                value = valueText[1..^ 1];
Ejemplo n.º 2
0
        public MethodMapping(Type @class, MethodInfo method, EnumParameterTypeMapping enumParameterTypes)
        {
            this.Class              = @class;
            this.Method             = method;
            this.EnumParameterTypes = enumParameterTypes;

            this.ClassMetadata  = @class.GetCustomAttributes(typeof(ScriptClassAttribute), false).First() as ScriptClassAttribute;
            this.MethodMetadata = method.GetCustomAttributes(typeof(ScriptMethodAttribute), false).First() as ScriptMethodAttribute;
        }
Ejemplo n.º 3
0
        private void LoadReflectedMethods(Type[] assemblyTypes)
        {
            assemblyTypes
            .Where(t => t.IsSubclassOf(typeof(ScriptClass)) && t.GetCustomAttributes(typeof(ScriptClassAttribute), false).Any())
            .ToList()
            .ForEach(@class =>
            {
                ScriptClassAttribute scriptClass = (ScriptClassAttribute)Attribute.GetCustomAttribute(@class, typeof(ScriptClassAttribute));

                @class
                .GetMethods()
                .Where(m => m.GetCustomAttributes(typeof(ScriptMethodAttribute), false).Any())
                .ToList()
                .ForEach(method =>
                {
                    // throws exception on non-compliant script methods
                    VerifyMethod(method);

                    ScriptMethodAttribute scriptMethod = (ScriptMethodAttribute)Attribute.GetCustomAttribute(method, typeof(ScriptMethodAttribute));
                    string methodName = $"{new MethodNameExpression(scriptClass.Name, scriptMethod.Name)}";

                    EnumParameterTypeMapping enumParameterTypes = GetEnumParameterTypes(method);

                    MethodMapping methodMapping = new MethodMapping(@class, method, enumParameterTypes);

                    // Every method should have a unique name for the script language (the registry should ensure this)
                    switch (scriptMethod.Type)
                    {
                    case ScriptEntityType.Condition:
                        if (!this.Conditions.ContainsKey(scriptMethod.Name))
                        {
                            this.Conditions[methodName] = methodMapping;
                        }
                        else
                        {
                            throw new Exception($"Duplicate Condition Name {scriptMethod.Type}/{scriptMethod.Name}");
                        }
                        break;

                    case ScriptEntityType.Action:
                        if (!this.Actions.ContainsKey(scriptMethod.Name))
                        {
                            this.Actions[methodName] = methodMapping;
                        }
                        else
                        {
                            throw new Exception($"Duplicate Action Name {scriptMethod.Type}/{scriptMethod.Name}");
                        }
                        break;

                    default:
                        throw new Exception($"Unknown {nameof(ScriptEntityType)} - {nameof(LoadReflectedMethods)}");
                    }
                });
            });
        }
Ejemplo n.º 4
0
        // e.g. CaseId:Int32:1,IncludeInactive:Boolean:True
        internal static Parameters Parse(Metamodel metaModel, EnumParameterTypeMapping enumParameterTypeMapping, string expressionText)
        {
            Parameters parameters = new Parameters();

            if (!string.IsNullOrWhiteSpace(expressionText))
            {
                Split(expressionText).ForEach(parameterText =>
                {
                    parameters.Add(Parameter.Parse(metaModel, enumParameterTypeMapping, parameterText));
                });
            }

            return(parameters);
        }
Ejemplo n.º 5
0
        private static EnumParameterTypeMapping GetEnumParameterTypes(MethodInfo method)
        {
            EnumParameterTypeMapping enumParameterTypes = new EnumParameterTypeMapping();

            List <ParameterInfo> enumParameters
                = method
                  .GetParameters()
                  .Where(parameter => parameter.ParameterType == typeof(EnumerationValue))
                  .ToList();

            foreach (var enumParameter in enumParameters)
            {
                ScriptEnumParameterAttribute enumParameterAttribute = (ScriptEnumParameterAttribute)enumParameter.GetCustomAttribute(typeof(ScriptEnumParameterAttribute));

                if (enumParameterAttribute == null)
                {
                    throw new Exception($"Missing {nameof(ScriptEnumParameterAttribute)}, {method.Name} method, {enumParameter.Name} parameter");
                }

                enumParameterTypes[enumParameter.Name] = enumParameterAttribute.EnumTypeName;
            }

            return(enumParameterTypes);
        }