Exemple #1
0
        protected void Add(Pattern pattern)
        {
            int lSize = pattern.GetFullBoardSize();

            if (lHashedMoves[lSize] == null)
                lHashedMoves[lSize] = new Dictionary<long, List<int>>();
        }
        public static PatternActionCode Compile(Pattern pattern, List<string> actions)
        {
            PatternActionCode lPatternActionCode = new PatternActionCode();

            if (actions != null)
                foreach (string lAction in actions)
                    lPatternActionCode = Compile(lPatternActionCode, pattern, lAction);

            return lPatternActionCode;
        }
Exemple #3
0
        public DFAPattern(Pattern pattern, int transform)
        {
            DFA = ToDFA(pattern, transform);
            VariationCount = 0;

            foreach (char c in DFA)
            {
                int lValue = pattern.VariationCount(c);
                VariationCount += (lValue * lValue);
            }
        }
        public static PatternActionCode Compile(PatternActionCode patternActionCode, Pattern pattern, string action)
        {
            if (string.IsNullOrEmpty(action))
                return patternActionCode;

            int lOpenParan = action.IndexOf('(');
            int lCloseParan = action.IndexOf(')');

            if (lOpenParan < 0)
            {
                patternActionCode.SetErrorMessage("Action line missing an open parentheses");
                return patternActionCode;
            }

            if (lCloseParan < 0)
            {
                patternActionCode.SetErrorMessage("Action line missing a close parentheses");
                return patternActionCode;
            }

            string lName = action.Substring(0, lOpenParan).Trim();
            string lValues = action.Substring(lOpenParan + 1, lCloseParan - lOpenParan - 1).Trim();

            PatternFunctionParameters<Coordinate> lPatternFunctionParameters = new PatternFunctionParameters<Coordinate>();

            if (!string.IsNullOrEmpty(lValues))
            {
                string[] lParameters = lValues.Split(',');

                foreach (string lParameter in lParameters)
                {
                    if (lParameter.Length != 1)
                    {
                        patternActionCode.SetErrorMessage("Invalid action function parameter: " + lParameter);
                        return patternActionCode;
                    }

                    lPatternFunctionParameters.Add(pattern.GetLetterLocation(lParameter[0]));
                }
            }

            PatternActionFunctions.PatternActionFunction lPatternActionFunction = PatternActionFunctions.GetFunction(lName, lPatternFunctionParameters.Count);

            if (lPatternActionFunction == null)
            {
                patternActionCode.SetErrorMessage("Unknown action function: " + lName + " with " + lPatternFunctionParameters.Count + " parameters");
                return patternActionCode;
            }

            patternActionCode.Add(new PatternActionOperand(lPatternActionFunction, lPatternFunctionParameters, pattern.Origin));

            return patternActionCode;
        }
Exemple #5
0
        protected ZobristHash ComputeZobrist(Pattern pattern, Color color)
        {
            int lSize = pattern.GetFullBoardSize();

            ZobristHash lZobristHash = new ZobristHash();

            for (int x = 0; x < lSize; x++)
                for (int y = 0; y < lSize; y++)
                    switch (pattern.GetCell(x, y))
                    {
                        case 'X': lZobristHash.Delta(color.IsBlack ? Color.Black : Color.White, CoordinateSystem.At(x, y, lSize)); break;
                        case 'O': lZobristHash.Delta(color.IsBlack ? Color.White : Color.Black, CoordinateSystem.At(x, y, lSize)); break;
                    }

            return lZobristHash;
        }
Exemple #6
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;
        }
Exemple #7
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();
        }
Exemple #8
0
        public static PatternActionCode Compile(Pattern pattern, string actions)
        {
            PatternActionCode lPatternActionCode = new PatternActionCode();

            return(Compile(lPatternActionCode, pattern, actions));
        }
Exemple #9
0
 public PatternHit(Pattern pattern, int transformation, Coordinate origin)
 {
     Pattern        = pattern;
     Transformation = transformation;
     Origin         = origin;
 }
        public static PatternActionCode Compile(Pattern pattern, string actions)
        {
            PatternActionCode lPatternActionCode = new PatternActionCode();

            return Compile(lPatternActionCode, pattern, actions);
        }
        //[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);
                        }
                    }
                }
            }
        }
Exemple #12
0
        public bool Load(MemFile memFile, bool continueOnError)
        {
            ClearErrorMessages();

            StringBuilder lBuffer = new StringBuilder();

            bool pStarted = false;
            int lLineNbr = 0;

            while (!memFile.EOF)
            {
                string lLine = memFile.ReadLine('\n');

                if (lLine.Trim().ToUpper() == "###MATRIX")
                    return IsError();

                if (lLine.Length > 0)
                {
                    if (memFile.EOF)
                        lBuffer.Append(lLine);

                    if ((lLine[0] == 'P') || memFile.EOF)
                    {

                        if (pStarted)
                        {
                            // EOF of a Pattern
                            Pattern lPattern = new Pattern(lBuffer.ToString(), lLineNbr);

                            if (lPattern.IsOk())
                                Add(lPattern);
                            else
                            {
                                SetErrorMessage("ERROR: Unable to compile pattern - Line # " + lLineNbr.ToString() + ":", lPattern);

                                Console.Error.WriteLine("ERROR: Unable to compile pattern.");
                                Console.Error.WriteLine(lPattern.GetErrorMessage());
                                //								return true;
                                if (!continueOnError)
                                    return false;
                            }

                            lBuffer = new StringBuilder();

                            pStarted = false;
                        }
                        else
                        {
                            pStarted = true;
                            lLineNbr = memFile.LineNbr;
                        }
                    }

                    if (lLine[0] == 'P')
                        pStarted = true;

                    if (pStarted)
                        lBuffer.Append(lLine);

                }

            }

            return true;
        }
Exemple #13
0
 public void Add(Pattern pattern)
 {
     Patterns.Add(pattern);
 }
        public void Merge(Pattern pattern)
        {
            MinTerritorialValue = Compare.Max(MinTerritorialValue, pattern.PatternCompiled.PatternAttribute.MinTerritorialValue);
            MaxTerritorialValue = Compare.Min(MaxTerritorialValue, pattern.PatternCompiled.PatternAttribute.MaxTerritorialValue);
            MinValue = Compare.Max(MinValue, pattern.PatternCompiled.PatternAttribute.MinValue);
            MaxValue = Compare.Min(MaxValue, pattern.PatternCompiled.PatternAttribute.MaxValue);

            AdjShapeFactor = Compare.Max(AdjShapeFactor, pattern.PatternCompiled.PatternAttribute.AdjShapeFactor);
            AdjFollowupFactor = Compare.Max(AdjFollowupFactor, pattern.PatternCompiled.PatternAttribute.AdjFollowupFactor);
        }
Exemple #15
0
 public PatternKey(Pattern pattern, int transformation)
 {
     Pattern = pattern;
     Transformation =  transformation;
 }
Exemple #16
0
        public void Add(Pattern pattern)
        {
            if (!pattern.IsOk())
                return;

            List<string> lPatternSignatures = new List<string>(8);

            for (int lTransform = 0; lTransform < 8; lTransform++)
            {
                string lSignature = DFAPattern.ToDFA(pattern, lTransform) + "\t" + DFAPattern.ToVariableDFA(pattern, lTransform);

                if (!lPatternSignatures.Contains(lSignature))
                {
                    DFAMatrixes.Push(new DFAMatrix(new PatternKey(pattern, lTransform)));
                    lPatternSignatures.Add(lSignature);
                }
            }
        }
Exemple #17
0
 public PatternInterpretor(PatternCode patternOperands, Pattern pattern)
 {
     Position = 0;
     PatternOperands = patternOperands;
     Pattern = pattern;
 }
Exemple #18
0
        public static string ToVariableDFA(Pattern pattern, int transform)
        {
            StringBuilder lDFA = new StringBuilder(200);
            Coordinate lSpiral = new Coordinate(0, 0);

            int lArea = pattern.Width * pattern.Height;

            while (lArea > 0)
            {
                Coordinate p = pattern.Origin + lSpiral.Transform(transform);
                lSpiral.SpiralNext();

                lDFA.Append(pattern.GetVariable(p));

                if (pattern.IsInPattern(p))
                    lArea--;
            }

            return lDFA.ToString().TrimEnd();
        }