private static void LoopTypeResolve(JSONNode args, Dictionary <string, object> context)
    {
        JSONArray methods    = args["method"].AsArray;
        int       currentRun = 0;
        int       runLimit   = 1000;

        if (args["run_limit"] != null)
        {
            runLimit = args["run_limit"].AsInt;
        }
        JSONNode   breakMethod            = args["break_method"];
        ParamsFunc func                   = GetFunction(breakMethod);
        Dictionary <string, object> _args = GeneratingArguments(breakMethod["args"], context);

        while (true)
        {
            currentRun++;
            if (currentRun > runLimit)
            {
                break;
            }
            RunCommands(methods, context);
            if (func == null)
            {
                continue;
            }
            else if ((bool)func.Invoke(_args))
            {
                break;
            }
        }
    }
    public static object RunCommand(JSONNode command, Dictionary <string, object> context)
    {
        string commandType = command["type"].Value;

        switch (commandType)
        {
        case functionType:
            ParamsFunc func = GetFunction(command);
            return(func?.Invoke(GeneratingArguments(command["args"], context)));

        case valueType:
            switch (command["value_type"])
            {
            case "string":
                return(command[valueType].Value);

            case "float":
                return(command[valueType].AsFloat);

            case "int":
                return(command[valueType].AsInt);

            case "bool":
                return(command[valueType].AsBool);

            case "array":
                return(command[valueType].AsArray);
            }
            break;
        }
        return(null);
    }
    public static void RunCommands(JSONArray commands, Dictionary <string, object> context)
    {
        for (int i = 0; i < commands.Count; ++i)
        {
            JSONNode behavior     = commands[i];
            string   behaviorType = behavior["type"].Value;
            switch (behaviorType)
            {
            case functionType:
                ParamsFunc func = GetFunction(behavior);
                func?.Invoke(GeneratingArguments(behavior["args"], context));
                break;

            case conditionType:
                ConditionTypeResolve(behavior, context);
                break;

            case delayType:
                DelayTypeResolve(behavior, context);
                break;

            case loopType:
                LoopTypeResolve(behavior, context);
                break;
            }
        }
    }
    static void Main(string[] args)
    {
        ParamsFunc <string, string, string> func =
            (format, values) => string.Format(format, string.Join("-", values));

        string result = func("Look here: {0}", "a", "b", "c");

        Console.WriteLine(result);
    }
    private static Dictionary <string, object> GeneratingArguments(JSONNode args, Dictionary <string, object> context)
    {
        Dictionary <string, object> results = new Dictionary <string, object>();

        foreach (KeyValuePair <string, object> input in context)
        {
            results[input.Key] = input.Value;
        }

        foreach (KeyValuePair <string, JSONNode> input in args.AsObject)
        {
            string   key = input.Key;
            JSONNode arg = input.Value;
            switch (arg["type"])
            {
            case valueType:
                switch (arg["value_type"])
                {
                case "string":
                    results[key] = arg[valueType].Value;
                    break;

                case "float":
                    results[key] = arg[valueType].AsFloat;
                    break;

                case "int":
                    results[key] = arg[valueType].AsInt;
                    break;

                case "bool":
                    results[key] = arg[valueType].AsBool;
                    break;

                case "array":
                    results[key] = arg[valueType].AsArray;
                    break;
                }
                break;

            case functionType:
                ParamsFunc func = GetFunction(arg);
                Dictionary <string, object> _args = GeneratingArguments(arg["args"], context);
                results[key] = func?.Invoke(_args);
                break;
            }
        }
        return(results);
    }
    static void ConditionTypeResolve(JSONNode command, Dictionary <string, object> context)
    {
        ParamsFunc func = GetFunction(command);
        Dictionary <string, object> _args = GeneratingArguments(command["args"], context);
        bool      isTrue   = (bool)func.Invoke(_args);
        JSONArray commands = null;

        if (isTrue)
        {
            commands = command["when_true"]?.AsArray;
        }
        else
        {
            commands = command["when_false"]?.AsArray;
        }
        if (commands != null)
        {
            RunCommands(commands, context);
        }
    }
        public async void EditPost(Func <List <TEntity> > SetupList, ParamsFunc TestEditEntity)
        {
            var lEntities        = SetupList();
            var oRepository      = GenericMethods.SetupRepository <TKey, TEntity, IRepository <TKey, TEntity> >(lEntities);
            var oUnitOfWork      = GenericMethods.SetupUnitOfWork <TKey, TEntity, IRepository <TKey, TEntity> >(oRepository);
            var oEntity          = oRepository.Object.Entities.FirstOrDefault();
            var oEntityLastOldId = oEntity.Id;

            // Arrange
            TController controller = (TController)Activator.CreateInstance(typeof(TController), oUnitOfWork.Object);

            TestEditEntity(oEntity);

            // Act
            ViewResult result = await controller.Edit(oEntityLastOldId) as ViewResult;


            // Assert
            Assert.AreEqual(oEntityLastOldId, oEntity.Id);
        }
    static void DelayTypeResolve(JSONNode args, Dictionary <string, object> context)
    {
        float     delayTime         = 0;
        JSONNode  delayTimeData     = args["delay_time"];
        JSONNode  delayLoopData     = args["loop"];
        JSONNode  delayLoopTimeData = args["loop_time"];
        JSONArray methods           = args["method"].AsArray;
        string    tag      = string.Empty;
        bool      loop     = false;
        int       loopTime = 0;

        switch (delayTimeData["type"].Value)
        {
        case functionType:
            ParamsFunc func = GetFunction(delayTimeData);
            Dictionary <string, object> _args = GeneratingArguments(delayTimeData["args"], context);
            delayTime = (float)func?.Invoke(_args);
            break;

        case valueType:
            delayTime = delayTimeData[valueType].AsFloat;
            break;
        }

        if (delayLoopData != null)
        {
            switch (delayLoopData["type"].Value)
            {
            case functionType:
                ParamsFunc func = GetFunction(delayLoopData);
                Dictionary <string, object> _args = GeneratingArguments(delayLoopData["args"], context);
                loop = (bool)func?.Invoke(_args);
                break;

            case valueType:
                loop = delayLoopData[valueType].AsBool;
                break;
            }
        }

        if (delayLoopTimeData != null)
        {
            switch (delayLoopTimeData["type"].Value)
            {
            case functionType:
                ParamsFunc func = GetFunction(delayLoopTimeData);
                Dictionary <string, object> _args = GeneratingArguments(delayLoopTimeData["args"], context);
                loopTime = (int)func?.Invoke(_args);
                break;

            case valueType:
                loopTime = delayLoopTimeData[valueType].AsInt;
                break;
            }
        }

        if (loopTime == 0)
        {
            GameManager.Instance.timer.After(delayTime, () =>
            {
                RunCommands(methods, context);
            }, loop, tag);
        }
        else
        {
            GameManager.Instance.timer.After(delayTime, () =>
            {
                RunCommands(methods, context);
            }, loopTime, tag);
        }
    }
Example #9
0
#pragma warning disable IDE0060 // 移除未使用的參數
        private static void ParamsFunc_Method <R>(ParamsFunc <R> paramsFunc)
        {
        }
Example #10
0
            public Factory(string Path, IAddChild Container)
            {
                this.ToImage =
                    k => new Image
                    {
                        Source = (Path + "/" + k + ".png").ToSource(),
                    }.AttachTo(Container);

                this.ToWaterImages =
                    a =>
                        a.ToArray(
                            k => new Image
                            {
                                Source = (Path + "/" + k + ".png").ToSource(),
                                Opacity = DefaultWaterOpacity,
                                Visibility = Visibility.Hidden
                            }.AttachTo(Container)
                        );

                this.ToHiddenImages =
                    a =>
                        a.ToArray(
                            k => new Image
                            {
                                Source = (Path + "/" + k + ".png").ToSource(),
                                Visibility = Visibility.Hidden
                            }.AttachTo(Container)
                        );
            }
Example #11
0
        public static void Invoke(Document doc)
        {
            var query = from k in doc.responseData.feed.entries
                        let t = new TitleParser(k.title)
                                select new { k, t };

            var ruleset = new StringBuilder();

            ruleset.AppendLine(@"
set(hyper_res).
set(factor).
set(print_kept).
formula_list(sos).
			"            );

            #region VariableEqualsToAny
            ParamsFunc <string, string, string> VariableEqualsToAny =
                (variable, values) =>
            {
                var w = new StringBuilder();

                w.Append("(");
                values.ForEach(
                    (k, i) =>
                {
                    if (i > 0)
                    {
                        w.AppendLine(" | ");
                    }
                    w.AppendLine("$EQ(" + variable + ", " + k.GetHashCode() + @") %" + k);
                }
                    );

                w.Append(")");

                return(w.ToString());
            };

            ParamsFunc <string, int, string> VariableEqualsToAnyInteger =
                (variable, values) =>
            {
                var w = new StringBuilder();

                w.Append("(");
                values.ForEach(
                    (k, i) =>
                {
                    if (i > 0)
                    {
                        w.AppendLine(" | ");
                    }
                    w.AppendLine("$EQ(" + variable + ", " + k + @")");
                }
                    );

                w.Append(")");

                return(w.ToString());
            };
            #endregion

            #region Question 1
            ruleset.AppendLine(@"
% Question 1
all x all z all u (
    facts(x,category,u) & " + VariableEqualsToAny("u", "Animation") + @" -> ForChildren(x)
).
");

            ruleset.AppendLine(@"
all x all z all u (
    facts(x,category,u) & " + VariableEqualsToAny("u", "Animation", "Comedy", "Family", "Fantasy") + @" -> ForFamily(x)
).
");

            ruleset.AppendLine(@"
all x all z all u (
    facts(x,category,u) & " + VariableEqualsToAny("u", "Adventure", "Drama", "Fantasy", "Mystery", "Thriller", "Action", "Crime") + @" -> ForAdults(x)
).
");
            #endregion

            #region Question 2

            ruleset.AppendLine(@"
% Question 2
all x all z all u (
    facts(x,year,u) & " + VariableEqualsToAnyInteger("u", DateTime.Now.Year, DateTime.Now.Year + 1) + @" -> ForGeeks(x)
).
");

            ruleset.AppendLine(@"
all x all z all u (
    facts(x,year,u) & " + VariableEqualsToAnyInteger("u", DateTime.Now.Year, DateTime.Now.Year + 1, DateTime.Now.Year - 1, DateTime.Now.Year - 2) + @" -> ForRecent(x)
).
");

            #endregion

            #region Question 3
            ruleset.AppendLine(@"
% Question 3
all x all z all u (
    facts(x,raiting,u) & $GT(u, 65)
    -> GoodRaiting(x)
).

all x all z all u (
    facts(x,raiting,u) & $LT(u, 70)
    -> BadRaiting(x)
).
");
            #endregion

            #region Question 4

            ruleset.AppendLine(@"
% Question 4
all x all z all u (
    facts(x,category,u) & " + VariableEqualsToAny("u", "TV shows") + @" -> IsTVShow(x)
).

all x all z all u (
    facts(x,category,u) & " + VariableEqualsToAny("u", "Movies") + @" -> IsMovie(x)
).
");
            #endregion

            #region facts
            foreach (var entry in query)
            {
                entry.k.content.ParseMovieItem(
                    m =>
                {
                    ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', raiting, " + Convert.ToInt32(m.Raiting * 100) + ").");
                }
                    );

                var p = new BasicFileNameParser(entry.t.Title);

                ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', year, " + entry.t.Year + ").");

                //if (p.Season != null)
                //    ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', season, " + int.Parse(p.Season) + ").");
                //if (p.Episode != null)
                //    ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', episode, " + int.Parse(p.Episode) + ").");

                foreach (var category in entry.k.categories)
                {
                    ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', category, " + category.GetHashCode() + "). %" + category);
                }
            }
            #endregion



            var Filters = new List <string>();

            Func <string, Action> f =
                filter => () => Filters.Add(filter);

            ConsoleQuestions.Ask(
                new Options
            {
                { "There are some kids over here", f("ForChildren(x)") },
                { "My family is in the room, keep it decent", f("ForFamily(x)") },
                { "There are only some dudes in the room", f("ForAdults(x)") },
                { "Neither", () => {} },
            },
                new Options
            {
                { "I am looking for new stuff", f("ForGeeks(x)") },
                { "I haven't watched that much tv recently!", f("ForRecent(x)") },
                { "I do not like recent movies at all!", f("-ForRecent(x)") },
                { "Neither", () => {} },
            },
                new Options
            {
                { "I am looking for having a good time", f("GoodRaiting(x)") },
                { "I want to suggest someething really bad to a friend", f("BadRaiting(x)") },
                { "I cannot decide between options above", () => {} },
            },
                new Options
            {
                { "I cannot watch tv very long", f("IsTVShow(x)") },
                { "30 minutes is not enough for me", f("IsMovie(x)") },
                { "I cannot decide between options above", () => {} },
            }
                );

            #region IsSelected
            ruleset.AppendLine(@"
% The anwser
all x  (
"
                               );

            Filters.ForEach(
                (k, i) =>
            {
                if (i > 0)
                {
                    ruleset.AppendLine(" & ");
                }

                ruleset.AppendLine(k);
            }
                );

            ruleset.AppendLine(@"
	-> IsSelected(x)
)."
                               );
            #endregion



            var otter = new OtterApplication(ruleset.ToString());

            Console.WriteLine();
            Console.WriteLine("Movies for you");
            otter.ToConsole(k => k == "IsSelected");

            File.WriteAllText(@"Data\IDX5711.in", ruleset.ToString());
            File.WriteAllText(@"Data\IDX5711.out", otter.Output);
        }