Пример #1
0
        public override StepPatternEvaluationResult Evaluate(Point?lastStepEndPoint, List <Line> lines, StepContext context)
        {
            if (_repeatCount > 0)
            {
                throw new NotImplementedException("Repeat count is not implemented yet");
            }

            var preEvaluationList = new List <Line> (lines);

            var           func           = _pattern.Compile();
            var           isValid        = false;
            PatternResult lastResult     = null;
            var           lastValidCount = preEvaluationList.Count;

            bool control = true;

            while (control)
            {
                if (preEvaluationList.Count == 0)
                {
                    control = false;
                    continue;
                }

                var pattern = func.Invoke(new StepPattern());
                var result  = pattern.Evaluate(preEvaluationList, context);
                if (result.IsValid)
                {
                    isValid        = true;
                    lastValidCount = preEvaluationList.Count;
                }
                else
                {
                    control = false;
                }

                lastResult = result;
            }

            if (isValid)
            {
                lines.RemoveRange(0, lines.Count - lastValidCount);
            }

            return(new StepPatternEvaluationResult(isValid, lastResult.LastPointInPattern));
        }
Пример #2
0
        public override PatternResult Evaluate(List <Line> lines, StepContext context)
        {
            PatternResult result = null;

            if (_allowInverse)
            {
                StoreContext(context, lines);
            }

            if (_allowAnyOrder)
            {
                _allowAnyOrderStepsLeft = lines.Count - 1;
                StoreContext(context, lines);
            }

            foreach (var item in _children)
            {
                result = item.Evaluate(lines, context);
                if (!result.IsValid)
                {
                    if (_allowInverse)
                    {
                        // REMARK: This is just concept code and subject to change

                        // restore the context
                        RestoreOriginalContext(context, lines);

                        // Crude way to flip the lines
                        InverseLines(lines);

                        // do the inverse
                        _allowInverse = false;
                        return(Evaluate(lines, context));
                    }

                    if (_allowAnyOrder)
                    {
                        // REMARK: This is just concept code and subject to change.
                        // This implementation has the following issues:
                        //
                        // 1)   It looks ugly
                        // 2)   It will reorder lines that are possibly outside the scope of this
                        //      pattern. That can be calculated (not easily for Repeat-patterns though)
                        //      at best. Perhaps through a Pattern.NumberOfLinesNeeded() property?

                        while (_allowAnyOrderStepsLeft > 0)
                        {
                            // Restore the context
                            RestoreOriginalContext(context, lines);

                            // ReorderLines
                            ReorderLines(lines);
                            _originalLines = lines.ToList();

                            // Evaluate
                            result = item.Evaluate(lines, context);
                            if (result.IsValid)
                            {
                                return(result);
                            }

                            _allowAnyOrderStepsLeft--;
                        }
                    }

                    return(result);
                }
            }

            return(result);
        }