Example #1
0
        public ApplicationViewModel()
        {
            ParseSQL = new RelaySimpleCommand(() =>
            {
                try
                {
                    if (Input_SQL == null)
                    {
                        Input_Valid_SQL = false; return;
                    }
                    String sql = Input_SQL.Replace("\n", " ");
                    while (sql.Last() == ' ')
                    {
                        sql = sql.Substring(0, sql.Length - 1);
                    }
                    Input_Valid_SQL = true;
                    if (sql == "")
                    {
                        Input_Valid_SQL = false; return;
                    }

                    List <State>[] stateSets = sqlParser.Parse(sql);

                    if (!sqlParser.IsValid(stateSets))
                    {
                        Input_Valid_SQL = false; return;
                    }

                    stateSets = sqlParser.FilterAndReverse(stateSets);
                    TreeNode <String> tree = sqlParser.parse_tree(sql, stateSets);

                    this.Input_RA           = SqlToRa.TranslateQuery(tree);
                    this.Parsed_SQL         = sql;
                    this.Parsed_RA_From_SQL = this.Input_RA;
                    this.Input_Type         = "ra";
                    return;
                }
                catch (HeuristicException e)
                {
                    Console.WriteLine(e.ToString());
                    Error = e.Message.Replace(Environment.NewLine, "<br/>");
                    return;
                }
            });

            ParseRA = new RelaySimpleCommand(() =>
            {
                try
                {
                    if (Input_RA == null)
                    {
                        Input_Valid_RA = false; return;
                    }
                    String ra = Input_RA.Replace("\n", " ");
                    while (ra.Last() == ' ')
                    {
                        ra = ra.Substring(0, ra.Length - 1);
                    }
                    Input_Valid_RA = true;
                    if (Input_RA == "")
                    {
                        Input_Valid_RA = false; return;
                    }

                    List <State>[] stateSets = raParser.Parse(Input_RA);

                    if (!raParser.IsValid(stateSets))
                    {
                        Input_Valid_RA = false; return;
                    }

                    if (this.Input_RA == this.Parsed_RA_From_SQL)
                    {
                        this.SQL = this.Parsed_SQL;
                    }
                    else
                    {
                        this.SQL = "";
                    }

                    this.RA = Input_RA;

                    stateSets = raParser.FilterAndReverse(stateSets);
                    TreeNode <String> tree = raParser.parse_tree(Input_RA, stateSets);

                    Squish(tree);

                    this.ops = RAToOps.Translate(tree, Relations.ToDictionary(relation => relation.name));
                    ops      = new TreeNode <Operation>(new Query())
                    {
                        ops
                    };

                    new Heuristic0(ops).Complete();

                    HeuristicsArray = new Heuristic[] {
                        new Heuristic1(ops),
                        new Heuristic2(ops),
                        new Heuristic3(ops),
                        new Heuristic4(ops),
                        new Heuristic5(ops)
                    };

                    UpdateCurrentHeuristic();

                    this.OpsJSON     = ops.Child().ToJSON().Replace("\"", "&quot;").Replace("'", "\"");
                    this.CurrentView = "output";
                    return;
                }
                catch (HeuristicException e)
                {
                    Console.WriteLine(e.ToString());
                    Error = e.Message.Replace(Environment.NewLine, "<br/>");
                    return;
                }
            });

            Step = new RelaySimpleCommand(() =>
            {
                try
                {
                    UpdateCurrentHeuristic();
                    if (CurrentHeuristic != null)
                    {
                        CurrentHeuristic.Step();
                    }
                    this.OpsJSON = ops.Child().ToJSON().Replace("\"", "&quot;").Replace("'", "\"");
                    UpdateCurrentHeuristic();
                }
                catch (HeuristicException e)
                {
                    Console.WriteLine(e.ToString());
                    Error = e.Message.Replace(Environment.NewLine, "<br/>");
                    return;
                }
            });

            Complete = new RelaySimpleCommand(() =>
            {
                try
                {
                    UpdateCurrentHeuristic();
                    if (CurrentHeuristic != null)
                    {
                        CurrentHeuristic.Complete();
                    }
                    this.OpsJSON = ops.Child().ToJSON().Replace("\"", "&quot;").Replace("'", "\"");
                    UpdateCurrentHeuristic();
                }
                catch (HeuristicException e)
                {
                    Console.WriteLine(e.ToString());
                    Error = e.Message.Replace(Environment.NewLine, "<br/>");
                    return;
                }
            });

            Reset = new RelaySimpleCommand(() =>
            {
                try
                {
                    List <State>[] stateSets = raParser.Parse(RA);
                    stateSets = raParser.FilterAndReverse(stateSets);
                    TreeNode <String> tree = raParser.parse_tree(RA, stateSets);

                    Squish(tree);

                    this.ops = RAToOps.Translate(tree, Relations.ToDictionary(relation => relation.name));
                    ops      = new TreeNode <Operation>(new Query())
                    {
                        ops
                    };

                    new Heuristic0(ops).Complete();

                    HeuristicsArray = new Heuristic[] {
                        new Heuristic1(ops),
                        new Heuristic2(ops),
                        new Heuristic3(ops),
                        new Heuristic4(ops),
                        new Heuristic5(ops)
                    };

                    UpdateCurrentHeuristic();

                    this.OpsJSON = ops.Child().ToJSON().Replace("\"", "&quot;").Replace("'", "\"");
                    return;
                }
                catch (HeuristicException e)
                {
                    Console.WriteLine(e.ToString());
                    Error = e.Message.Replace(Environment.NewLine, "<br/>");
                    return;
                }
            });

            this.DeleteRelation = new RelaySimpleCommand <Relation>(relation =>
            {
                List <Relation> list = this.Relations.ToList();
                list.Remove(relation);
                this.Relations = list.ToArray();
            });

            this.NewRelation = new RelaySimpleCommand <String>(relation =>
            {
                List <Relation> list = this.Relations.ToList();
                list.Add(new Relation(relation, new List <Field>()
                {
                    new Field("", new List <String>()
                    {
                    }), new Field("", new List <String>()
                    {
                    }), new Field("", new List <String>()
                    {
                    }), new Field("", new List <String>()
                    {
                    })
                }));
                this.Relations = list.ToArray();
            });
        }