public static PatternCode Scan(string code)
        {
            PatternCode lPatternCode = new PatternCode();
            int         lPosition    = 0;

            PatternToken lPreviousToken = PatternToken.UNKNOWN;

            // tokenize code
            while (lPosition < code.Length)
            {
                string       lTokenString = ReadTokenString(code, ref lPosition);
                PatternToken lToken       = GetToken(lTokenString, lPreviousToken);

                // "*" could also be a variable
                if ((lToken == PatternToken.MULTIPLY) && (lPreviousToken == PatternToken.COMMA || lPreviousToken == PatternToken.OPEN_PARA))
                {
                    lToken = PatternToken.VARIABLE;
                }

                int lParameterCount = (lToken == PatternToken.FUNCTION) ? GetParameterCount(code, lPosition) : 0;

                PatternOperand patternOperand = new PatternOperand(lToken, lTokenString, lParameterCount);

                if (lToken == PatternToken.UNKNOWN)
                {
                    lPatternCode.SetErrorMessage("Unknown Token: " + lTokenString.ToString());
                    return(lPatternCode);
                }
                lPatternCode.Add(patternOperand);

                lPreviousToken = lToken;
            }

            return(lPatternCode);
        }
Ejemplo n.º 2
0
        public static PatternCode Compile(Pattern pattern)
        {
            PatternCode lPatternCode = PatternScanner.Scan(pattern.PatternSourceCode);

            if (!lPatternCode.IsError())
            {
                PatternSyntax lPatternSyntax = new PatternSyntax(lPatternCode);

                if (!lPatternSyntax.SyntaxCheck())
                {
                    lPatternCode.SetErrorMessage(lPatternSyntax);
                    return(lPatternCode);
                }
            }

            return(lPatternCode);
        }
Ejemplo n.º 3
0
        public PatternCompiled(Pattern pattern)
        {
            Pattern = pattern;

            PatternCode = PatternCompiler.Compile(Pattern);
            if (PatternCode.IsError())
            {
                SetErrorMessage(PatternCode.GetErrorMessage());
                return;
            }

            PatternInterpretor = new PatternInterpretor(PatternCode, Pattern);

            PatternAttribute = new PatternAttribute(Pattern.PatternAttributes);
            if (PatternAttribute.IsError())
            {
                SetErrorMessage(PatternAttribute.GetErrorMessage());
                return;
            }

            PatternActionAttribute = new PatternActionAttribute(Pattern.ClassificationAttributes);
            if (PatternActionAttribute.IsError())
            {
                SetErrorMessage(PatternActionAttribute.GetErrorMessage());
                return;
            }

            PatternConstraintAttribute = new PatternConstraintAttribute(Pattern.ClassificationAttributes);
            if (PatternConstraintAttribute.IsError())
            {
                SetErrorMessage(PatternConstraintAttribute.GetErrorMessage());
                return;
            }

            PatternActionCode = PatternActionCompiler.Compile(pattern, pattern.ActionCode);
            if (PatternActionCode.IsError())
            {
                SetErrorMessage(PatternActionCode.GetErrorMessage());
                return;
            }

            OptimalDFATransformation = FindMinimizedDFATransformation();
        }
Ejemplo n.º 4
0
        public PatternCompiled(Pattern pattern)
        {
            Pattern = pattern;

            PatternCode = PatternCompiler.Compile(Pattern);
            if (PatternCode.IsError())
            {
                SetErrorMessage(PatternCode.GetErrorMessage());
                return;
            }

            PatternInterpretor = new PatternInterpretor(PatternCode, Pattern);

            PatternAttribute = new PatternAttribute(Pattern.PatternAttributes);
            if (PatternAttribute.IsError())
            {
                SetErrorMessage(PatternAttribute.GetErrorMessage());
                return;
            }

            PatternActionAttribute = new PatternActionAttribute(Pattern.ClassificationAttributes);
            if (PatternActionAttribute.IsError())
            {
                SetErrorMessage(PatternActionAttribute.GetErrorMessage());
                return;
            }

            PatternConstraintAttribute = new PatternConstraintAttribute(Pattern.ClassificationAttributes);
            if (PatternConstraintAttribute.IsError())
            {
                SetErrorMessage(PatternConstraintAttribute.GetErrorMessage());
                return;
            }

            PatternActionCode = PatternActionCompiler.Compile(pattern, pattern.ActionCode);
            if (PatternActionCode.IsError())
            {
                SetErrorMessage(PatternActionCode.GetErrorMessage());
                return;
            }

            OptimalDFATransformation = FindMinimizedDFATransformation();
        }
Ejemplo n.º 5
0
        //[Test]
        public void Test()
        {
            GoBoard lGoBoard = new GoBoard(13);

            lGoBoard.PlayStone("A2", Color.Black, false);
            lGoBoard.PlayStone("B1", Color.Black, false);
            lGoBoard.PlayStone("B2", Color.Black, false);
            lGoBoard.PlayStone("C2", Color.Black, false);
            lGoBoard.PlayStone("D2", Color.Black, false);
            lGoBoard.PlayStone("E2", Color.Black, false);
            lGoBoard.PlayStone("F2", Color.Black, false);
            lGoBoard.PlayStone("F1", Color.Black, false);
            lGoBoard.PlayStone("D1", Color.Black, false);
            lGoBoard.PlayStone("G8", Color.Black, false);
            lGoBoard.PlayStone("H7", Color.Black, false);
            lGoBoard.PlayStone("H9", Color.Black, false);
            lGoBoard.PlayStone("J8", Color.Black, false);
            lGoBoard.PlayStone("H2", Color.Black, false);
            lGoBoard.PlayStone("J1", Color.Black, false);
            lGoBoard.PlayStone("K2", Color.Black, false);

            lGoBoard.Dump();

            string lString = "Pattern LinkPattern4\n\n??o??\n?XoX?\n..X*.\n-----\n\n:\n\n??o??\n?Xoa?\n.*X*.\n-----\n\n;libertycount(a)>1\n\n";

            Pattern lPattern = new Pattern(lString);

            lPattern.Dump();

            PatternTest[] lPatternTests =   {
            //			new PatternTest( "1+2*3", true, true, 7, true, -1 ),		// bug: right to left (instead of left to right), but okay for now
                new PatternTest( "", true, true, 1, false, -1 ),
                new PatternTest( "", true, true, true, true, -1 ),
                new PatternTest( "1", true, true, 1, true, -1 ),
                new PatternTest( "1+1", true, true, 2, true, -1 ),
                new PatternTest( "(1+1)", true, true, 2, true, -1 ),
                new PatternTest( "1>2", true, true, false, true, -1 ),
                new PatternTest( "(1>2)", true, true, false, true, -1 ),
                new PatternTest( "((1)>(1+2))", true, true, false, true, -1 ),
                new PatternTest( "1+2+3+4+5+6+7+8+9", true, true, 45, true, -1 ),
                new PatternTest( "1+2+(3+4)+5+6+7+(8+9)", true, true, 45, true, -1 ),
                new PatternTest( "(1+2+(3+4)+5+6+7+(8+9))", true, true, 45, true, -1 ),
                new PatternTest( "0&&1&&1", true, true, false, true, -1 ),
                new PatternTest( "1||1||0", true, true, true, true, -1 ),
                new PatternTest( "0&&1&&1||1", true, true, false, true, -1 ),
                new PatternTest( "libertycount(a)", true, true, 4, false, -1 ),
                new PatternTest( "libertycount(a) > 1", true, true, true, false, -1 ),
                new PatternTest( "1", true, true, 1, true, -1 ),
                new PatternTest( "1+1", true, true, 2, true, -1 ),
                new PatternTest( "((1)>(1+2))", true, true, false, true, -1 ),
                new PatternTest( " (1+2) > libertycount(a)", true, true, 0, false, -1 ),
                new PatternTest( " (1+2) > libertycount(a,b)", true, true, 0, false, -1 ),
                new PatternTest( " (1+2) > libertycount()", true, false, 0, false, -1 ),
                new PatternTest( " (1+2) != libertycount()+1", true, false, 0, false, -1 ),
                new PatternTest( " (1+2) >= libertycount()+1+2", true, false, 0, false, -1 ),
                new PatternTest( " (libertycount(a) == 1)", true, true, 0, false, -1 ),
                new PatternTest( " libertycount(a) == 1 && libertycount(b) == 1", true, true, 0, false, -1 ),
                new PatternTest( " (libertycount(a) == 1) || (libertycount(b) == 1)", true, true, 0, false, -1 ),
                new PatternTest( " ((libertycount(a) == 1) || (libertycount(b) == 1))", true, true, 0, false, -1 ),
                new PatternTest( "1+", true, false, 0, true, -1 ),
                new PatternTest( " libertycount(a) == 1)", true, false, 0, false, 100 ),
                new PatternTest( "libertycount(a) >" , true, false, 0, false, 101 ),
                new PatternTest( "(((1)>(1+2))", true, false, 0, true, -1 ),
                new PatternTest( "((1)>(1+2)))", true, false, 0, true, -1 ),
                new PatternTest( " (1+2 > libertycount(a)", true, false, 0, false, -1 ),
                new PatternTest( " 1+2) > libertycount(a,b)", true, false, 0, false, -1 ),
                new PatternTest( " libertycount(a) libertycount(b)", true, false, 0, false, -1 ),
                new PatternTest( " libertycount(a) == 1 libertycount(b)", true, false, 0, false, -1 ),
                new PatternTest( " libertycount(a) libertycount(b) == 1", true, false, 0, false, -1 ),
                new PatternTest(" libertycount(a) == 1 & libertycount(b) == 1", false, false, 0, false, -1 )
            };

            foreach (PatternTest lPatternTest in lPatternTests)
            {
                PatternCode lPatternCode = new PatternCode();

                //	PatternScanner lPatternScanner = new PatternScanner(lPatternTest.Expression, lPatternCode);

                bool lScan = !lPatternCode.IsError();

                Assert.IsTrue(lScan == lPatternTest.ScanFlag, "1:" + lPatternTest.Expression);

                if (lScan != lPatternTest.ScanFlag)
                {
                    Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                    Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                    Console.Error.WriteLine("Scan  : " + (lScan ? "YES" : "NO") + " Expected: " + (lPatternTest.ScanFlag ? "YES" : "NO"));
                }

                if (lScan)
                {
                    PatternSyntax lPatternSyntax = new PatternSyntax(lPatternCode);

                    bool lSyntax = lPatternSyntax.SyntaxCheck();

                    Assert.IsTrue(lSyntax == lPatternTest.SyntaxFlag, "2:" + lPatternTest.Expression);

                    if (lSyntax != lPatternTest.SyntaxFlag)
                    {
                        Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                        Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                        Console.Error.WriteLine("Syntax: " + (lSyntax ? "YES" : "NO") + " Expected: " + (lPatternTest.SyntaxFlag ? "YES" : "NO"));
                    }

                    if ((lSyntax) && (lPatternTest.ExecuteFlag))
                    {
                        PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern);

                        int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0);

                        Assert.IsTrue(lResult == lPatternTest.Result, "3:" + lPatternTest.Expression + " Got: " + lResult.ToString() + " Expected: " + lPatternTest.Result);
                        Assert.IsTrue(!lPatternInterpretor.IsError(), "4:Interpretor Error: " + lPatternTest.Expression);

                        if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError()))
                        {
                            Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                            Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                            Console.Error.Write("Got : " + lResult.ToString());
                            Console.Error.WriteLine(" Expected: " + lPatternTest.Result);
                        }
                    }

                    if ((lSyntax) && (!lPatternTest.ExecuteFlag))
                    {
                        PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern);

                        int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0);

                        Assert.IsTrue(lResult == lPatternTest.Result, "5:" + lPatternTest.Expression);

                        if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError()))
                        {
                            Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                            Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                            Console.Error.Write("Got : " + lResult.ToString());
                            Console.Error.WriteLine(" Expected: " + lPatternTest.Result);
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
 public PatternInterpretor(PatternCode patternOperands, Pattern pattern)
 {
     Position        = 0;
     PatternOperands = patternOperands;
     Pattern         = pattern;
 }
Ejemplo n.º 7
0
        public static PatternCode Scan(string code)
        {
            PatternCode lPatternCode = new PatternCode();
            int lPosition = 0;

            PatternToken lPreviousToken = PatternToken.UNKNOWN;

            // tokenize code
            while (lPosition < code.Length)
            {
                string lTokenString = ReadTokenString(code, ref lPosition);
                PatternToken lToken = GetToken(lTokenString, lPreviousToken);

                // "*" could also be a variable
                if ((lToken == PatternToken.MULTIPLY) && (lPreviousToken == PatternToken.COMMA || lPreviousToken == PatternToken.OPEN_PARA))
                    lToken = PatternToken.VARIABLE;

                int lParameterCount = (lToken == PatternToken.FUNCTION) ? GetParameterCount(code, lPosition) : 0;

                PatternOperand patternOperand = new PatternOperand(lToken, lTokenString, lParameterCount);

                if (lToken == PatternToken.UNKNOWN)
                {
                    lPatternCode.SetErrorMessage("Unknown Token: " + lTokenString.ToString());
                    return lPatternCode;
                }
                lPatternCode.Add(patternOperand);

                lPreviousToken = lToken;
            }

            return lPatternCode;
        }
Ejemplo n.º 8
0
        //[Test]
        public void Test()
        {
            GoBoard lGoBoard = new GoBoard(13);

            lGoBoard.PlayStone("A2", Color.Black, false);
            lGoBoard.PlayStone("B1", Color.Black, false);
            lGoBoard.PlayStone("B2", Color.Black, false);
            lGoBoard.PlayStone("C2", Color.Black, false);
            lGoBoard.PlayStone("D2", Color.Black, false);
            lGoBoard.PlayStone("E2", Color.Black, false);
            lGoBoard.PlayStone("F2", Color.Black, false);
            lGoBoard.PlayStone("F1", Color.Black, false);
            lGoBoard.PlayStone("D1", Color.Black, false);
            lGoBoard.PlayStone("G8", Color.Black, false);
            lGoBoard.PlayStone("H7", Color.Black, false);
            lGoBoard.PlayStone("H9", Color.Black, false);
            lGoBoard.PlayStone("J8", Color.Black, false);
            lGoBoard.PlayStone("H2", Color.Black, false);
            lGoBoard.PlayStone("J1", Color.Black, false);
            lGoBoard.PlayStone("K2", Color.Black, false);

            lGoBoard.Dump();

            string lString = "Pattern LinkPattern4\n\n??o??\n?XoX?\n..X*.\n-----\n\n:\n\n??o??\n?Xoa?\n.*X*.\n-----\n\n;libertycount(a)>1\n\n";

            Pattern lPattern = new Pattern(lString);

            lPattern.Dump();

            PatternTest[] lPatternTests =
            {
                //			new PatternTest( "1+2*3", true, true, 7, true, -1 ),		// bug: right to left (instead of left to right), but okay for now
                new PatternTest("",                                                    true,  true,      1, false,  -1),
                new PatternTest("",                                                    true,  true,  true,  true,   -1),
                new PatternTest("1",                                                   true,  true,      1, true,   -1),
                new PatternTest("1+1",                                                 true,  true,      2, true,   -1),
                new PatternTest("(1+1)",                                               true,  true,      2, true,   -1),
                new PatternTest("1>2",                                                 true,  true,  false, true,   -1),
                new PatternTest("(1>2)",                                               true,  true,  false, true,   -1),
                new PatternTest("((1)>(1+2))",                                         true,  true,  false, true,   -1),
                new PatternTest("1+2+3+4+5+6+7+8+9",                                   true,  true,     45, true,   -1),
                new PatternTest("1+2+(3+4)+5+6+7+(8+9)",                               true,  true,     45, true,   -1),
                new PatternTest("(1+2+(3+4)+5+6+7+(8+9))",                             true,  true,     45, true,   -1),
                new PatternTest("0&&1&&1",                                             true,  true,  false, true,   -1),
                new PatternTest("1||1||0",                                             true,  true,  true,  true,   -1),
                new PatternTest("0&&1&&1||1",                                          true,  true,  false, true,   -1),
                new PatternTest("libertycount(a)",                                     true,  true,      4, false,  -1),
                new PatternTest("libertycount(a) > 1",                                 true,  true,  true,  false,  -1),
                new PatternTest("1",                                                   true,  true,      1, true,   -1),
                new PatternTest("1+1",                                                 true,  true,      2, true,   -1),
                new PatternTest("((1)>(1+2))",                                         true,  true,  false, true,   -1),
                new PatternTest(" (1+2) > libertycount(a)",                            true,  true,      0, false,  -1),
                new PatternTest(" (1+2) > libertycount(a,b)",                          true,  true,      0, false,  -1),
                new PatternTest(" (1+2) > libertycount()",                             true,  false,     0, false,  -1),
                new PatternTest(" (1+2) != libertycount()+1",                          true,  false,     0, false,  -1),
                new PatternTest(" (1+2) >= libertycount()+1+2",                        true,  false,     0, false,  -1),
                new PatternTest(" (libertycount(a) == 1)",                             true,  true,      0, false,  -1),
                new PatternTest(" libertycount(a) == 1 && libertycount(b) == 1",       true,  true,      0, false,  -1),
                new PatternTest(" (libertycount(a) == 1) || (libertycount(b) == 1)",   true,  true,      0, false,  -1),
                new PatternTest(" ((libertycount(a) == 1) || (libertycount(b) == 1))", true,  true,      0, false,  -1),
                new PatternTest("1+",                                                  true,  false,     0, true,   -1),
                new PatternTest(" libertycount(a) == 1)",                              true,  false,     0, false, 100),
                new PatternTest("libertycount(a) >",                                   true,  false,     0, false, 101),
                new PatternTest("(((1)>(1+2))",                                        true,  false,     0, true,   -1),
                new PatternTest("((1)>(1+2)))",                                        true,  false,     0, true,   -1),
                new PatternTest(" (1+2 > libertycount(a)",                             true,  false,     0, false,  -1),
                new PatternTest(" 1+2) > libertycount(a,b)",                           true,  false,     0, false,  -1),
                new PatternTest(" libertycount(a) libertycount(b)",                    true,  false,     0, false,  -1),
                new PatternTest(" libertycount(a) == 1 libertycount(b)",               true,  false,     0, false,  -1),
                new PatternTest(" libertycount(a) libertycount(b) == 1",               true,  false,     0, false,  -1),
                new PatternTest(" libertycount(a) == 1 & libertycount(b) == 1",        false, false,     0, false, -1)
            };

            foreach (PatternTest lPatternTest in lPatternTests)
            {
                PatternCode lPatternCode = new PatternCode();

                //	PatternScanner lPatternScanner = new PatternScanner(lPatternTest.Expression, lPatternCode);

                bool lScan = !lPatternCode.IsError();

                Assert.IsTrue(lScan == lPatternTest.ScanFlag, "1:" + lPatternTest.Expression);

                if (lScan != lPatternTest.ScanFlag)
                {
                    Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                    Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                    Console.Error.WriteLine("Scan  : " + (lScan ? "YES" : "NO") + " Expected: " + (lPatternTest.ScanFlag ? "YES" : "NO"));
                }

                if (lScan)
                {
                    PatternSyntax lPatternSyntax = new PatternSyntax(lPatternCode);

                    bool lSyntax = lPatternSyntax.SyntaxCheck();

                    Assert.IsTrue(lSyntax == lPatternTest.SyntaxFlag, "2:" + lPatternTest.Expression);

                    if (lSyntax != lPatternTest.SyntaxFlag)
                    {
                        Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                        Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                        Console.Error.WriteLine("Syntax: " + (lSyntax ? "YES" : "NO") + " Expected: " + (lPatternTest.SyntaxFlag ? "YES" : "NO"));
                    }

                    if ((lSyntax) && (lPatternTest.ExecuteFlag))
                    {
                        PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern);

                        int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0);

                        Assert.IsTrue(lResult == lPatternTest.Result, "3:" + lPatternTest.Expression + " Got: " + lResult.ToString() + " Expected: " + lPatternTest.Result);
                        Assert.IsTrue(!lPatternInterpretor.IsError(), "4:Interpretor Error: " + lPatternTest.Expression);

                        if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError()))
                        {
                            Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                            Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                            Console.Error.Write("Got : " + lResult.ToString());
                            Console.Error.WriteLine(" Expected: " + lPatternTest.Result);
                        }
                    }

                    if ((lSyntax) && (!lPatternTest.ExecuteFlag))
                    {
                        PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern);

                        int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0);

                        Assert.IsTrue(lResult == lPatternTest.Result, "5:" + lPatternTest.Expression);

                        if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError()))
                        {
                            Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)");
                            Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression);
                            Console.Error.Write("Got : " + lResult.ToString());
                            Console.Error.WriteLine(" Expected: " + lPatternTest.Result);
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
 public PatternSyntax(PatternCode patternOperands)
 {
     PatternOperands = patternOperands;
 }
Ejemplo n.º 10
0
 public PatternInterpretor(PatternCode patternOperands, Pattern pattern)
 {
     Position = 0;
     PatternOperands = patternOperands;
     Pattern = pattern;
 }
Ejemplo n.º 11
0
 public PatternSyntax(PatternCode patternOperands)
 {
     PatternOperands = patternOperands;
 }