Beispiel #1
0
 public void AddNode(Node <T, U> node, double cost)
 {
     Nodes.Add(node);
     if (cost == 1)
     {
     }
     Cost = Norm.Calculate(Cost, cost);
 }
        public void Compile(string sourcecode)
        {
            CodeModifySource = string.Empty;

            DateTime date  = DateTime.Now;
            var      graph = Lex.Analysis(sourcecode);

            Utils.SaveTime("Lex", date);

            date = DateTime.Now;
            var paths = graph.AllPaths(Norm).OrderByDescending(p => p.Cost);

            Utils.SaveTime("GetPaths", date);

            date = DateTime.Now;
            for (int i = 0; i < paths.Count() && i < 3; i++)
            {
                var symbols       = GetSymbols(paths.ElementAt(i));
                var pertinenceSyn = Syn.Validate(symbols);
                if (pertinenceSyn > 0)
                {
                    FixSyntatic = new FixSyn(Syn.GraphsSyntactic[0].Root, Grammar);

                    NodeToTreeContainer nttc = new NodeToTreeContainer(FixSyntatic.NodeResult, NameVars, symbols);

                    CodeModifySource = string.Empty;
                    MountCodeModifySource(FixSyntatic.NodeResult);
                    CodeModifySource = CodeModifySource.TrimStart();
                    Compile(nttc);

                    PertinenceLex   = paths.ElementAt(i).Cost;
                    PertinenceSyn   = pertinenceSyn;
                    PertinenceTotal = Norm.Calculate(PertinenceLex, PertinenceSyn);
                    break;
                }
            }
            Utils.SaveTime("ValidateSyn", date);

            GC.Collect();
            if (OnCompile != null)
            {
                OnCompile(this);
            }
        }
        public virtual double Match(string source)
        {
            TableAutomaProcessing = new List <List <double> >();
            int  iSource = 0;
            char cActual = SYMBOLEMPTY;

            TableAutomaProcessing.Add(new List <double>());
            for (int i = 0; i < Automa.States.Count; i++)
            {
                TableAutomaProcessing[0].Add(Automa.States[i].PertinenceInitial);
            }

            int steps = (source.Length * 2) + 1;

            for (int step = 1; step <= steps; step++)
            {
                if (iSource == source.Length)
                {
                    iSource++;
                    cActual = SYMBOLEMPTY;
                }
                else
                {
                    if (step % 2 == 1)
                    {
                        cActual = SYMBOLEMPTY;
                    }
                    else
                    {
                        cActual = source[iSource++];
                    }
                }

                TableAutomaProcessing.Add(new List <double>());
                for (int i = 0; i < Automa.States.Count; i++)
                {
                    int    stepOld    = step - 1;
                    double pertinence = double.MinValue;
                    int    indexMinor = 0;
                    for (int j = 0; j < TableAutomaProcessing[stepOld].Count; j++)
                    {
                        double maxState      = TableAutomaProcessing[stepOld][j];
                        double pertinenceNew = 0;
                        if (maxState > 0)
                        {
                            pertinenceNew =
                                Automa.SearchPertinence(Automa.States[j], Automa.States[i], cActual, Norm, Conorm);
                            pertinenceNew = Norm.Calculate(pertinenceNew, maxState);
                        }
                        if (cActual == SYMBOLEMPTY)
                        {
                            pertinenceNew = Conorm.Calculate(pertinenceNew, TableAutomaProcessing[stepOld][i]);
                        }

                        if (pertinence < pertinenceNew)
                        {
                            pertinence = pertinenceNew;
                            indexMinor = j;
                        }
                    }
                    TableAutomaProcessing[step].Add(pertinence);
                }
            }


            double maxFinal = 0;

            for (int i = 0; i < Automa.States.Count; i++)
            {
                if (Automa.States[i].PertinenceFinal > 0)
                {
                    maxFinal = Conorm.Calculate(Norm.Calculate(TableAutomaProcessing[steps][i], Automa.States[i].PertinenceFinal), maxFinal);
                }
            }
            return(maxFinal);
        }
        public override double Validate(List <Symbol> tokens)
        {
            CacheFirst = null;
            try
            {
                GraphsSyntactic = new List <Graph <Symbol, double> >();
                tokens.Add(Symbol.TapeFinal);
                Productions = new List <RuleProduction>();
                Graph <State <Symbol>, double> graph = new Graph <State <Symbol>, double>();
                FirstState.PertinenceInitial = 1;
                var firstNode = graph.AddNode("First", FirstState);
                firstNode.Parent = null;
                List <Node <State <Symbol>, double> > leafs = new List <Node <State <Symbol>, double> >();

                int idNode = 1;
                List <Node <State <Symbol>, double> > newLeafs = new List <Node <State <Symbol>, double> >();

                CustomValueStateNodeLR1 customValueStateNodeLR1 = new CustomValueStateNodeLR1();
                customValueStateNodeLR1.CoutPaths = 1;
                //customValueStateNodeLR1.Tokens.Add(tokens[0]);
                customValueStateNodeLR1.IToken      = 1;
                customValueStateNodeLR1.ASymbol     = tokens[0];
                customValueStateNodeLR1.ASymbolReal = tokens[0];

                firstNode.SetCustomOject("CustomValueStateNodeLR1", customValueStateNodeLR1);
                leafs.Add(firstNode);

                double maxAcept = 0;
                while (true)
                {
                    for (int iLeaf = 0; iLeaf < leafs.Count; iLeaf++)
                    {
                        var sLeaf = leafs[iLeaf];
                        customValueStateNodeLR1 = sLeaf.GetCustomOject <CustomValueStateNodeLR1>("CustomValueStateNodeLR1");

                        #region GETOP
                        double pertinenceS   = sLeaf.Info.PertinenceInitial;
                        double newPertinence = 1;

                        var operations = Table[sLeaf.Info][customValueStateNodeLR1.ASymbol];
                        var emptyrule  = false;

                        if (operations.Count == 0)
                        {
                            if (Table[sLeaf.Info].ContainsKey(Symbol.EmptySymbol))
                            {
                                operations = Table[sLeaf.Info][Symbol.EmptySymbol];
                                emptyrule  = true;
                            }
                        }

                        if (operations.Count == 0)
                        {
                            operations = Table[sLeaf.Info][Symbol.TapeFinal];
                            emptyrule  = true;
                        }

                        #endregion GETOP

                        if (operations.Count > 0)
                        {
                            for (int iOp = 0; iOp < operations.Count; iOp++)
                            {
                                customValueStateNodeLR1 = sLeaf.GetCustomOject <CustomValueStateNodeLR1>("CustomValueStateNodeLR1");

                                Operation op = operations[iOp];

                                #region FOREACH
                                if (op.Type == TypeOperation.Shift)
                                {
                                    #region SHIFT
                                    newPertinence = Norm.Calculate(pertinenceS, op.Pertinence);
                                    var newLeaf = graph.AddNode(idNode++, op.State, sLeaf, newPertinence);
                                    CustomValueStateNodeLR1 newCustomValueStateNodeLR1 = new CustomValueStateNodeLR1(customValueStateNodeLR1);
                                    newCustomValueStateNodeLR1.IndexInParent = iOp;

                                    List <Symbol> sNewTokens = new List <Symbol>();
                                    if (!emptyrule)
                                    {
                                        if (newCustomValueStateNodeLR1.IToken >= tokens.Count)
                                        {
                                            newCustomValueStateNodeLR1.ASymbol = Symbol.TapeFinal;
                                        }
                                        else
                                        {
                                            newCustomValueStateNodeLR1.ASymbol = tokens[newCustomValueStateNodeLR1.IToken];
                                        }
                                        newCustomValueStateNodeLR1.IToken++;
                                    }
                                    newLeaf.Info.PertinenceInitial = newPertinence;
                                    newLeaf.SetCustomOject("CustomValueStateNodeLR1", newCustomValueStateNodeLR1);
                                    newLeafs.Add(newLeaf);
                                    #endregion SHIFT
                                }
                                else if (op.Type == TypeOperation.Reduce)
                                {
                                    #region REDEUCE 1
                                    RuleProduction rule       = op.Rule;
                                    var            prox       = sLeaf;
                                    int            popStack   = op.Rule.Destiny.Count;
                                    int            countNodes = rule.Destiny.Count;
                                    for (int i = 0; i < popStack; i++)
                                    {
                                        newPertinence = prox.Info.PertinenceInitial;
                                        var temp = prox;
                                        //graph.RemoveNode(prox);
                                        prox = prox.Parent;
                                    }
                                    #endregion REDEUCE 1
                                    #region GOTO
                                    var operationsGoto = Table[prox.Info][rule.Source];
                                    if (operationsGoto.Count > 0)
                                    {
                                        var state = operationsGoto[0].State;

                                        newPertinence = Norm.Calculate(prox.Info.PertinenceInitial, op.Rule.Pertinence);

                                        var newLeaf = graph.AddNode(idNode++, state, prox, newPertinence);
                                        CustomValueStateNodeLR1 newCustomValueStateNodeLR1 = new CustomValueStateNodeLR1(customValueStateNodeLR1);
                                        newCustomValueStateNodeLR1.IndexInParent = iOp;
                                        newCustomValueStateNodeLR1.Productions.Add(rule);

                                        newLeaf.SetCustomOject("CustomValueStateNodeLR1", newCustomValueStateNodeLR1);

                                        newCustomValueStateNodeLR1.Pertinence = newPertinence;

                                        newLeaf.Info.PertinenceInitial = newPertinence;

                                        newLeafs.Add(newLeaf);
                                    }
                                    #endregion GOTO
                                }
                                else if (op.Type == TypeOperation.Acept)
                                {
                                    #region Acept

                                    Graph <Symbol, double> graphA =
                                        CreateTreeSyntax(customValueStateNodeLR1.Productions,
                                                         tokens);

                                    GraphsSyntactic.Add(graphA);

                                    if (sLeaf.Info.PertinenceInitial > maxAcept)
                                    {
                                        Productions = customValueStateNodeLR1.Productions;
                                        maxAcept    = sLeaf.Info.PertinenceInitial;
                                    }
                                    #endregion Acept
                                }
                                else
                                {
                                    //TODO: Recuperar de errros
                                    return(0);
                                }

                                #endregion FOREACH
                            }
                        }
                    }

                    leafs = newLeafs;

                    if (leafs.Count == 0)
                    {
                        return(maxAcept);
                    }

                    newLeafs = new List <Node <State <Symbol>, double> >();
                }
            }
            catch (Exception ex)
            {
            }
            return(0);
        }