Example #1
0
        public ModifierDelegate GetCustomModifierMethod(string methodName)
        {
            ModifierDelegate md = null;

            switch (methodName)
            {
            case "encodevalue":
                md = new ModifierDelegate(EncodeValue);
                break;

            case "unitTomilliunit":
                md = new ModifierDelegate(UnitToMilliUnit);
                break;

            case "formatmerlinlatestdateasxsddate":
                md = new ModifierDelegate(FormatMerlinLatestDateAsXsdDate);
                break;

            case "formatmerlinearliestdateasxsddate":
                md = new ModifierDelegate(FormatMerlinEarliestDateAsXsdDate);
                break;

            case "settotitlecasingoflabel":
                md = new ModifierDelegate(SetToTitleCasingOfLabel);
                break;
            }
            return(md);
        }
Example #2
0
        public static ModifierDelegate GetModifierMethod(string methodName)
        {
            ModifierDelegate modifierDelegate = null;

            switch (methodName.Trim().ToLower())
            {
            case "md5":
                modifierDelegate = new ModifierDelegate(Md5);
                break;

            case "strtolower":
                modifierDelegate = new ModifierDelegate(StrToLower);
                break;

            case "extractcurrency":
                modifierDelegate = new ModifierDelegate(ExtractCurrency);
                break;

            case "extractdenomination":
                modifierDelegate = new ModifierDelegate(ExtractDenomination);
                break;

            default:
                return(GetCustomModifier(methodName));
            }
            return(modifierDelegate);
        }
Example #3
0
 public ModifierInfo(Dictionary <string, FPInfo> fpInfos,
                     Dictionary <string, object> fpInfosObj,
                     Dictionary <string, dynamic> args,
                     ModifierDelegate modifierDelegate)
 {
     this.fpInfos          = fpInfos;
     this.fpInfosObj       = fpInfosObj;
     this.args             = args;
     this.modifierDelegate = modifierDelegate;
 }
 public ModifierInfo(Dictionary<string,FPInfo> fpInfos,
                     Dictionary<string, object> fpInfosObj,
                     Dictionary<string, dynamic> args,
                     ModifierDelegate modifierDelegate)
 {
     this.fpInfos = fpInfos;
     this.fpInfosObj = fpInfosObj;
     this.args = args;
     this.modifierDelegate = modifierDelegate;
 }
Example #5
0
 /// <summary>
 /// Add a Modifier script to an Entity, based on a code block (delegate) and a Function
 /// </summary>
 /// <param name="e">Entity to add modifier script to</param>
 /// <param name="scriptCode">Code block (delegate) that is the script</param>
 /// <param name="func">Function whose value will be passed in ScriptContext.FunctionValue to script</param>
 /// <returns></returns>
 public static ModifierScript AddModifier(Entity e, ModifierDelegate scriptCode, IFunction func)
 {
     if (!e.HasComponent<ScriptComp>())
     {
         e.AddComponent(new ScriptComp());
         e.Refresh();
     }
     var sc = e.GetComponent<ScriptComp>();
     var script = new ModifierScript(scriptCode, func);
     sc.Add(script);
     return script;
 }
Example #6
0
        public static ModifierDelegate GetModifierMethod(string methodName)
        {
            ModifierDelegate modifierDelegate = null;

            switch (methodName.Trim().ToLower())
            {
                case "md5":
                    modifierDelegate = new ModifierDelegate(Md5);
                    break;
                case "strtolower":
                    modifierDelegate = new ModifierDelegate(StrToLower);
                    break;
                case "extractcurrency":
                    modifierDelegate = new ModifierDelegate(ExtractCurrency);
                    break;
                case "extractdenomination":
                    modifierDelegate = new ModifierDelegate(ExtractDenomination);
                    break;
                default:
                    return GetCustomModifier(methodName);
            }
            return modifierDelegate;
        }
Example #7
0
 public ModifierDelegate GetCustomModifierMethod(string methodName)
 {
     ModifierDelegate md = null;
     switch (methodName)
     {
         case "encodevalue":
             md = new ModifierDelegate(EncodeValue);
             break;
         case "unitTomilliunit":
             md= new ModifierDelegate(UnitToMilliUnit);
             break;
         case "formatmerlinlatestdateasxsddate":
             md = new ModifierDelegate(FormatMerlinLatestDateAsXsdDate);
             break;
         case "formatmerlinearliestdateasxsddate":
             md = new ModifierDelegate(FormatMerlinEarliestDateAsXsdDate);
             break;
         case "settotitlecasingoflabel":
             md = new ModifierDelegate(SetToTitleCasingOfLabel);
             break;
     }
     return md;
 }
Example #8
0
 public SineModifier(ModifierDelegate code, T objectToModify) :
     base(code, objectToModify)
 {
 }
Example #9
0
 //Constructor with modifier delegate
 public Creature()
 {
     ModifierDelegate modifier = new ModifierDelegate(Modify);           
 }
Example #10
0
        private string[] GetValues(string attr, XmlNode idNode, XmlNode data, ModifierDelegate mod_func = null, int min=0, int max=0)
        {
            //GetValues2(attr, idNode, data, mod_func, min, max);

            List<string> toReturn = new List<string>();

            var parent = idNode.ParentNode;
            if(min > 0 && idNode.Attributes[attr] == null)
                throw new Exception("Missing attribute " + attr + "=\"...\" in: " + idNode.OuterXml);

            var value = idNode.Attributes[attr].Value;
            var prefix = idNode.Attributes["prefix"] != null ?  idNode.Attributes["prefix"].Value : "";

            //is there an xpath expression?
            var xpaths1 = Regex.Matches(value, "{(.*?)}");

            var internalFuncCalls = Regex.Matches(value, "\\^(.*?)~");
            List<Match> xpaths = new List<Match>();

            //We'll add the internalFuncCalls first so their are computed first (really we want to create a parse tree but will mean a v2)
            xpaths.AddRange(internalFuncCalls.Cast<Match>());
            xpaths.AddRange(xpaths1.Cast<Match>());

            if (/*value.Contains("{") &&*/ xpaths.Count > 0)
            {
                //  if there is more than one xpath to look up in this value,
                //  object-{obj_id}-{part_id} then we want only one match for
                //  each xpath term
                if (xpaths.Count > 1 || value.Substring(0, 1) != "{" || value.Substring(value.Length - 1, 1) != "}")
                {
                    bool matchedSomething = false;
                    foreach (Match xPath in xpaths)
                    {
                        /*If we are here then we have the situation of multiple xpath queriers: xxx-{xpath1}xxxx{xpath2}
                          If so, before we continue, we need to rule out any sub calls to internal functions such as:
                        {/xpath[{counter_CounterVariable}]}
                     */
                        string xPathValue = xPath.Value;
                        //MatchCollection xpaths2 = Regex.Matches(RemoveClosingBrackets(xPathValue), "\\^(.*?)\\~");
                        //foreach (Match m in xpaths2)
                        //{
                        //    string result2;
                        //    if (TryInternalFunction(RemoveClosingBrackets(m.Value), out result2))
                        //    {
                        //        xPathValue = xPathValue.Replace(m.Value, result2);
                        //    }
                        //}
                        xPathValue = MatchNestedInternalFunctionCalls(xPathValue);

                        string result;
                        if (TryInternalFunction(RemoveClosingBrackets(xPathValue), out result))
                        {
                            value = value.Replace(xPathValue, result);
                        }
                        else
                        {
                            XPathNavigator nav = data.CreateNavigator();
                            XPathExpression expr = nav.Compile(RemoveClosingBrackets(xPathValue));
                            expr.SetContext(_namespaceManager);

                            //XPathNodeIterator iterator = nav.Select(expr);
                            //var iterator2 = nav.Evaluate(expr);
                            switch (expr.ReturnType)
                            {
                                case XPathResultType.NodeSet:
                                    XPathNodeIterator iterator = (XPathNodeIterator) nav.Select(expr);
                                    if (iterator.Count == 1 || (xpaths.Count == 1 && iterator.Count > 0))
                                    {
                                        iterator.MoveNext();

                                        if (iterator.Current.Value.Trim().Length > 0)
                                            matchedSomething = true;
                                        value = value.Replace(xPathValue, iterator.Current.Value);
                                    }
                                    else if (xpaths.Count != 1 && iterator.Count == 0)
                                    {
                                        throw new Exception("No match for " + xPathValue + " in " + attr + "=\"" +
                                                            value + "\"");
                                    }
                                    else if (xpaths.Count > 1)
                                    {
                                        throw new Exception("Multiple matches for " + xPathValue + " in " + attr +
                                                            "=\"" + value + "\"");
                                    }
                                    break;
                                case XPathResultType.String:
                                    string st = (string) nav.Evaluate(expr);
                                    if (!string.IsNullOrEmpty(st) && st.Trim().Length > 0)
                                    {
                                        matchedSomething = true;
                                        value = value.Replace(xPathValue, st);
                                    }
                                    break;
                                case XPathResultType.Number:
                                    //Assume it's a double
                                    double dNumber = (double) nav.Evaluate(expr);
                                    int iNumber;
                                    matchedSomething = true;
                                    //if it can be an int, then make it so...
                                    if (int.TryParse(dNumber.ToString(), out iNumber))
                                    {
                                        value = value.Replace(xPathValue, iNumber.ToString());
                                    }
                                    else
                                    {
                                        value = value.Replace(xPathValue, dNumber.ToString());
                                    }
                                    break;
                                default:
                                    throw new Exception("No match for " + xPathValue + " in " + attr + "=\"" + value +
                                                        "\"");
                            }
                        }

                    }
                    if (mod_func != null)
                        value = mod_func(value);

                    if (matchedSomething && !string.IsNullOrEmpty(value.Trim()))
                        toReturn.Add(prefix + value);

                }
                else {

                    /*If we are here then we don't have the situation of multiple xpath queriers: xxx-{xpath1}xxxx{xpath2}
                    Therefore we have just a normal bracketed xpath.  If so, before we continue, we need to rule out any sub calls to internal functions such as:
                        {/xpath[{counter_CounterVariable}]}
                     */
                    //MatchCollection xpaths2 = Regex.Matches(value, "\\^(.*?)~");
                    //foreach (Match m in xpaths2)
                    //{
                    //    string result2;
                    //    if (TryInternalFunction(RemoveClosingBrackets(m.Value), out result2))
                    //    {
                    //        value = value.Replace(m.Value, result2);
                    //    }
                    //}
                    value = MatchNestedInternalFunctionCalls(value);

                    //  quite happy to enumerate for all values which match the xpath
                    var xpath = RemoveClosingBrackets(value);
                    string result;
                    if (TryInternalFunction(xpath, out result))
                    {
                        value = value.Replace(value, result);
                        if (mod_func != null)
                            value = mod_func(value);

                        if (!string.IsNullOrEmpty(value.Trim()))
                            toReturn.Add(prefix + value);
                    }
                    else
                    {
                        var matches = data.SelectNodes(xpath, _namespaceManager);
                        foreach (XmlNode node in matches)
                        {
                            //  ignore empty strings
                            if (!string.IsNullOrEmpty(node.InnerText))
                            {
                                //  apply modification function if required
                                var v = value.Replace("{" + xpath + "}", node.InnerText);
                                if (mod_func != null)
                                    v = mod_func(v);
                                if (!string.IsNullOrEmpty(v.Trim()))
                                    toReturn.Add(prefix + v);
                            }
                        }
                    }
                }
            }
            else{
                //  no xpath shenanigans, just return the content of the attribute
                if (mod_func != null)
                    value = mod_func(value);
                if (!string.IsNullOrEmpty(value.Trim()))
                    toReturn.Add(prefix + value);
            }

            //  check results between $min and $max
            if (toReturn.Count < min)
                throw new Exception("Unable to match " + attr + "=\"" + value + "\" \n\n   Caused by config node ");
            if (toReturn.Count > max && max > 0)
                throw new Exception("Too many attributes " + attr + "=\"\"");

            return toReturn.ToArray();
        }
Example #11
0
 /// <summary>
 /// Add a Modifier script to an Entity, based on a code block (delegate) and an empty (=unity) Function
 /// </summary>
 /// <param name="e">Entity to add modifier script to</param>
 /// <param name="scriptCode">Code block (delegate) that is the script</param>
 /// <returns></returns>
 public static ModifierScript AddModifier(Entity e, ModifierDelegate scriptCode)
 {
     return AddModifier(e, scriptCode, null);
 }
 /// <summary>
 /// Create new ModifierScript
 /// </summary>
 /// <param name="code"></param>
 /// <param name="function">if null is passed, the unity function f(x)=x is applied as Function</param>
 public ModifierScript(ModifierDelegate code, IFunction function)
 {
     this.ScriptCode = code;
     this.Function = function;
 }
Example #13
0
        //private IComponent compToModify;

        /// <summary>
        /// Create a new Modifier that can modify an object of specified type T
        /// </summary>
        /// <param name="code">Code (method or delegate block) to execute, must have 'void method(T obj, double value)' signature</param>
        public Modifier(ModifierDelegate code, T objectToModify)
        {
            //this.Type = ModifierType.MODIFIER_MODIFIER;
            this.ModifierCode   = code;
            this.objectToModify = objectToModify;
        }