Beispiel #1
0
        public void DrawAST()
        {
            DrawAST DD = new DrawAST();

            DD.root = ParseRoot;
            DD.Show();
        }
Beispiel #2
0
        public object RUN(Node Root, int NexStateIndex)
        {
            if (Root != null)
            {
                string Next = "";
                if (Root.NodeKind.ToLower() == "conjunction" || Root.NodeKind.ToLower() == "disjunction" || Root.NodeKind.ToLower() == "block" || Root.NodeKind.ToLower() == "mathexp")
                {
                    Next = Root.NodeKind;
                }
                else
                if (Root.nextstates.Count > 0 && Root.nextstates[0] != null)
                {
                    Next = ((Node)Root.nextstates[NexStateIndex]).NodeKind;
                }
                else
                {
                    if (Root.NodeKind == "String")
                    {
                        return(Root.RunValue);
                    }
                    if (Root.NodeKind == "_" || Root.NodeKind == "+" || Root.NodeKind == "." || Root.NodeKind == "-" || Root.NodeKind == "*" || Root.NodeKind == "/" ||
                        Root.NodeKind == "if" || Root.NodeKind == "then" || Root.NodeKind == "else")
                    {
                        return(Root.NodeKind);
                    }
                    else
                    {
                        return("");
                    }
                }
                Next = Next.ToLower();
                switch (Next)
                {
                case "conjunction": string DisResult = "";
                    for (int i = 0; i < Root.nextstates.Count; i++)
                    {
                        if (i == 1)
                        {
                            DisResult += " AND ";
                        }
                        else
                        {
                            DisResult += (string)RUN((Node)Root.nextstates[i], i);
                        }
                    }
                    return(DisResult);

                case "disjunction": DisResult = "";
                    for (int i = 0; i < Root.nextstates.Count; i++)
                    {
                        if (i == 1)
                        {
                            DisResult += " OR ";
                        }
                        else
                        {
                            DisResult += (string)RUN((Node)Root.nextstates[i], i);
                        }
                    }
                    return(DisResult);

                case "block": int SuccessNum = 0; string StringResult = "";
                    for (int i = 0; i < Root.nextstates.Count; i++)
                    {
                        object RE = null;
                        if (((Node)Root.nextstates[0]).NodeKind == "MathExp")
                        {
                            RE = RUN((Node)Root.nextstates[0], 0);
                        }
                        else
                        {
                            RE = RUN((Node)Root.nextstates[i], 0);
                        }
                        if (RE != null)
                        {
                            try
                            {
                                bool R = (bool)RE;
                                return(R);
                            }
                            catch (Exception ex)
                            {
                                if ((string)RE != "")
                                {
                                    StringResult += " " + (string)RE + " ";
                                }
                            }
                        }
                    }
                    return(StringResult);

                case "encode": string encodedData = "";
                    for (int i = 0; i < ((Node)Root.nextstates[1]).nextstates.Count; i++)
                    {
                        if (((Node)((Node)Root.nextstates[1]).nextstates[i]).NodeKind == "MathExp")
                        {
                            encodedData += RUN((Node)Root.nextstates[1], i);
                        }
                        else
                        {
                            encodedData += RUN((Node)((Node)Root.nextstates[1]).nextstates[i], i);
                        }
                    }
                    string Res = CD.getStringBitStream(CD.HoffmanCompress(encodedData), encodedData);
                    if (!AbstractUse)
                    {
                        ResultText += "\nEncoding Result: " + Res;
                    }
                    TExtRef.Text += "\nEncoding Result: " + Res;
                    return(Res);

                    break;

                case "htree": DrawHoffmanTree(); break;

                case "addf": string Data        = "";
                    bool            checkSource = false;
                    if (((Node)((Node)Root.nextstates[1]).nextstates[0]).NodeKind == "MathExp")
                    {
                        Data       += RUN((Node)Root.nextstates[1], 0);
                        checkSource = true;
                    }
                    else
                    {
                        Data += RUN((Node)((Node)Root.nextstates[1]).nextstates[1], 0);
                    }
                    ES_Lib.Fact F = new Fact(Data);
                    F.Index = FactCount;
                    F.ProcessFact();
                    FactCount++;
                    Facts.Add(F);
                    foreach (Rule R in Rules)
                    {
                        for (int i = 0; i < R.IFPART.Count; i++)
                        {
                            Rule.InnerStruct IS = (Rule.InnerStruct)R.IFPART[i];
                            if (IS.IFPart.Trim() == F.Data.Trim())
                            {
                                R.IFPART.Remove(IS);
                                IS.IsTrue = true;
                                R.IFPART.Add(IS);
                            }
                        }
                    }
                    if (!checkSource && !AbstractUse)
                    {
                        ResultText   += "\nAdded Fact: " + F.Data;
                        TExtRef.Text += "\nAdded Fact: " + F.Data;
                    }
                    if (Data != "" && F != null)
                    {
                        return(true);
                    }
                    break;

                case "addr": Data = "";
                    if (((Node)Root.nextstates[2]).NodeKind != "Rule" && !AbstractUse)
                    {
                        ResultText   += "\nCannot Add Rule : No Rule To be Added.";
                        TExtRef.Text += "\nCannot Add Rule : No Rule To be Added.";
                        return(null);
                    }
                    bool ifPart = false;
                    for (int i = 0; i < ((Node)Root.nextstates[2]).nextstates.Count; i++)
                    {
                        string LocalData = RUN((Node)Root.nextstates[2], NexStateIndex) + " ";
                        Data += LocalData;
                        NexStateIndex++;
                        if (LocalData.Trim() == "if" || LocalData.Trim() == "then" || LocalData.Trim() == "else")
                        {
                            try
                            {
                                Data += RUN((Node)((Node)Root.nextstates[2]).nextstates[i + 1], NexStateIndex).ToString();
                            }
                            catch (Exception ex)
                            {
                                Data += (string)RUN((Node)((Node)Root.nextstates[2]).nextstates[i + 1], NexStateIndex);
                            }
                            NexStateIndex++;
                            i++;
                        }
                    }
                    NexStateIndex = 0;
                    Rule  RD = new Rule(Data);
                    float CF = 1;
                    try
                    {
                        CF = (float)Convert.ToDouble(((Node)Root.nextstates[4]).RunValue);
                    }
                    catch (Exception ex)
                    { }
                    RD.CertaintyFactor = CF;
                    RD.Index           = RuleCount;
                    RuleCount++;
                    RD.ProcessRule(ifPart);
                    Rules.Add(RD);
                    if (!AbstractUse)
                    {
                        ResultText   += "\n" + RD.Data;
                        TExtRef.Text += "\n" + RD.Data;
                    }
                    break;

                case "rule": ifPart = false; Data = "";
                    NexStateIndex   = 0;
                    for (int i = 0; i < ((Node)Root.nextstates[0]).nextstates.Count; i++)
                    {
                        if ((i == 3 && !ifPart) || (i == 5 && ifPart))
                        {
                            NexStateIndex++;
                            continue;
                        }
                        object REsp = RUN((Node)Root.nextstates[0], NexStateIndex);
                        try
                        {
                            string LocalData = (string)REsp + " ";
                            Data += LocalData;
                            if (i == 1)
                            {
                                for (int j = 0; j < Facts.Count; j++)
                                {
                                    Fact TF = (Fact)Facts[j];
                                    if (TF.Data == LocalData.Trim())
                                    {
                                        ifPart = true;
                                        break;
                                    }
                                }
                            }
                            else
                            if (LocalData.Trim() != "then" && LocalData.Trim() != "else" && LocalData.Trim() != "if" && !AbstractUse)
                            {
                                ResultText   += "\n" + LocalData;
                                TExtRef.Text += "\n" + LocalData;
                            }
                        }
                        catch (Exception ex)
                        {
                            bool boolRES = (bool)REsp;
                            if (i == 1 && boolRES)
                            {
                                ifPart = true;
                            }
                        }
                        NexStateIndex++;
                    }
                    break;

                case "modr,": string[] Old_New = new string[2];
                    for (int j = 0; j < Old_New.Length; j++)
                    {
                        Old_New[j] = (string)RUN((Node)Root.nextstates[j], 0);
                    }
                    try
                    {
                        int RIndex = Convert.ToInt32(Old_New[0]);
                        for (int j = 0; j < Rules.Count; j++)
                        {
                            Rule NewR = (Rule)Rules[j];
                            if (NewR.Index == RIndex)
                            {
                                NewR.Data = Old_New[1];
                                if (!AbstractUse)
                                {
                                    ResultText   += "\n" + NewR.Data;
                                    TExtRef.Text += "\n" + NewR.Data;
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        for (int j = 0; j < Rules.Count; j++)
                        {
                            Rule NewR = (Rule)Rules[j];
                            if (NewR.Data == Old_New[0])
                            {
                                NewR.Data = Old_New[1];
                                if (!AbstractUse)
                                {
                                    ResultText   += "\n" + NewR.Data;
                                    TExtRef.Text += "\n" + NewR.Data;
                                }
                                break;
                            }
                        }
                    }

                    break;

                case "modf,": string[] Old_New2 = new string[2];
                    for (int j = 0; j < Old_New2.Length; j++)
                    {
                        Old_New2[j] = (string)RUN((Node)Root.nextstates[j], 0);
                    }
                    try
                    {
                        int RIndex = Convert.ToInt32(Old_New2[0]);
                        for (int j = 0; j < Facts.Count; j++)
                        {
                            Fact NewR = (Fact)Facts[j];
                            if (NewR.Index == RIndex)
                            {
                                NewR.Data = Old_New2[1];
                                if (!AbstractUse)
                                {
                                    ResultText   += "\n" + NewR.Data;
                                    TExtRef.Text += "\n" + NewR.Data;
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        for (int j = 0; j < Rules.Count; j++)
                        {
                            Fact NewR = (Fact)Facts[j];
                            if (NewR.Data == Old_New2[0])
                            {
                                NewR.Data = Old_New2[1];
                                if (!AbstractUse)
                                {
                                    ResultText   += "\n" + NewR.Data;
                                    TExtRef.Text += "\n" + NewR.Data;
                                }
                                break;
                            }
                        }
                    }
                    break;

                case "clear": Facts.Clear(); break;

                case "ast": DrawAST DD   = new DrawAST();
                    Node            Temp = new Node();
                    Temp.NodeKind  = "Main";
                    Temp.isInitial = true;
                    Temp.Value     = ".";
                    Temp.nextstates.Add(ASTDrawRoot);
                    DD.root = ASTDrawRoot;
                    DD.Show(); break;

                case "explain": if (!AbstractUse)
                    {
                        ResultText += "\n Under Construction..";
                    }
                    TExtRef.Text += "\n Under Construction.."; break;

                case "facts": string FactsData = "";
                    Fact             FCC       = null;
                    for (int j = 0; j < Facts.Count; j++)
                    {
                        FCC = (Fact)Facts[j];
                        if (FCC.DegreeOfBelieve > -1)
                        {
                            FactsData += FCC.Index + " : " + FCC.Data + " with " + FCC.DegreeOfBelieve + " degree of believe.\n";
                        }
                        else
                        {
                            FactsData += FCC.Index + " : " + FCC.Data + "\n";
                        }
                    }
                    if (!AbstractUse)
                    {
                        if (FCC != null && FCC.DegreeOfBelieve != -1)
                        {
                            ResultText   += "\n" + FactsData;
                            TExtRef.Text += "\n" + FactsData;
                        }
                        else
                        {
                            ResultText   += "\n" + FactsData;
                            TExtRef.Text += "\n" + FactsData;
                        }
                    }
                    return(FactsData);

                    break;

                case "rrules": for (int j = 0; j < Rules.Count; j++)
                    {
                        Rule FC = (Rule)Rules[j];
                        if (!AbstractUse)
                        {
                            ResultText   += "\n" + FC.Index + " : " + FC.Data;
                            TExtRef.Text += "\n" + FC.Index + " : " + FC.Data;
                        }
                    }
                    break;

                case "delf": if (Root.nextstates.Count == 0)
                    {
                        Parse.ErrorTracing.Push("Cannot Delete Fact.");
                        return(null);
                    }
                    string Data2 = (string)RUN((Node)Root.nextstates[0], 0);
                    for (int j = 0; j < Facts.Count; j++)
                    {
                        Fact FT = (Fact)Facts[j];
                        if (FT.Data == Data2)
                        {
                            Facts.Remove(FT);
                            if (!AbstractUse)
                            {
                                ResultText   += "\nDeleted at index " + FT.Index;
                                TExtRef.Text += "\nDeleted at index " + FT.Index;
                            }
                            if (Facts.Count > 0)
                            {
                                j--;
                            }
                        }
                    }
                    break;

                case "delr": string Data3 = (string)RUN((Node)Root.nextstates[0], 0);
                    for (int j = 0; j < Rules.Count; j++)
                    {
                        Rule RE = (Rule)Rules[j];
                        if (RE.Data == Data3)
                        {
                            Rules.Remove(RE);
                            if (!AbstractUse)
                            {
                                ResultText   += "\nDeleted at index " + RE.Index;
                                TExtRef.Text += "\nDeleted at index " + RE.Index;
                            }
                            if (Rules.Count > 0)
                            {
                                j--;
                            }
                        }
                    }
                    break;

                case "mathexp": string MathData = "";
                    for (int j = 1; j < Root.nextstates.Count - 1; j++)
                    {
                        MathData += RUN((Node)Root.nextstates[j], 0);
                    }
                    if (!AbstractUse)
                    {
                        ResultText   += "\n" + Parse.MathParsing(MathData);
                        TExtRef.Text += "\n" + Parse.MathParsing(MathData);
                    }
                    return(Parse.MathParsing(MathData));

                    break;

                case "save": string FilePath      = ((Node)((Node)Root.nextstates[1]).nextstates[1]).Value;
                    string          SavedData     = (string)RUN((Node)((Node)Root.nextstates[1]).nextstates[3], 0);
                    string[]        RealSavedData = SavedData.Split('\n');
                    try
                    {
                        File.WriteAllLines(FilePath, RealSavedData);
                        if (!AbstractUse)
                        {
                            ResultText += "\nData has been saved successfully.";
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!AbstractUse)
                        {
                            ResultText   += "\n" + ex.Message;
                            TExtRef.Text += "\n" + ex.Message;
                        }
                    }
                    break;              //savef<"path",data>

                case "commands": break; // list engine commands

                case "getf": break;

                case "getr": break;

                case "getfs": break;        //make facts group

                case "getrs": break;

                case "oc": break;         // Open channel

                case "send": break;       // send none encrypted data

                case "sendc": break;      //send cyphered text

                case "listen": break;     //listen to the opened channel

                case "listenc": break;    //listen to the cyphered opened channel

                case "fattrib": break;    //get fact attribute (Indexed)

                case "clears": ResultText = "";
                    TExtRef.Text          = ""; break;//Clear Screen

                case "exit": Application.Exit();
                    break;

                default: if (Root.NodeKind.ToLower() == "lstatement" || Root.NodeKind.ToLower() == "dstatement" || Root.NodeKind.ToLower() == "pstatement" || Root.NodeKind.ToLower() == "mstatement")
                    {
                        if (Root.nextstates.Count >= 2)
                        {
                            for (int i = 0; i < Root.nextstates.Count; i++)
                            {
                                Root.RunValue = Root.RunValue + RUN((Node)Root.nextstates[i], 0);
                            }
                            return(Root.RunValue);
                        }
                        else
                        {
                            return(Root.RunValue);
                        }
                    }
                    else
                    {
                        return(RUN((Node)Root.nextstates[NexStateIndex], 0));
                    } break;
                }
            }
            return("");
        }