Esempio n. 1
0
        /// <summary>
        /// Inserts code to check whether the subpatterns were found and code for case there were some
        /// at the given position, returns position after inserted operations
        /// </summary>
        private SearchProgramOperation insertCheckForSubpatternsFound(SearchProgramOperation insertionPoint, 
            bool isIteratedNullMatch)
        {
            PatternGraph patternGraph = patternGraphWithNestingPatterns.Peek();
            string negativeIndependentNamePrefix = NegativeIndependentNamePrefix(patternGraph);

            // check whether there were no subpattern matches found
            CheckPartialMatchForSubpatternsFound checkSubpatternsFound =
                new CheckPartialMatchForSubpatternsFound(negativeIndependentNamePrefix);
            SearchProgramOperation continuationPointAfterSubpatternsFound =
                   insertionPoint.Append(checkSubpatternsFound);
            checkSubpatternsFound.CheckFailedOperations =
                new SearchProgramList(checkSubpatternsFound);
            insertionPoint = checkSubpatternsFound.CheckFailedOperations;

            // ---- check failed, some subpattern matches found, pattern and subpatterns were matched
            PatternAndSubpatternsMatchedType type = PatternAndSubpatternsMatchedType.SubpatternOrAlternative;
            if (programType == SearchProgramType.Action) {
                type = PatternAndSubpatternsMatchedType.Action;
            } else if (programType == SearchProgramType.Iterated) {
                if (isIteratedNullMatch) type = PatternAndSubpatternsMatchedType.IteratedNullMatch;
                else type = PatternAndSubpatternsMatchedType.Iterated;
            }
            Debug.Assert(!isIteratedNullMatch || programType == SearchProgramType.Iterated);
            string inlinedPatternClassName = rulePatternClassName;
            string pathPrefixInInlinedPatternClass = patternGraph.pathPrefix;
            string unprefixedNameInInlinedPatternClass = patternGraph.name;
            if(patternGraph.originalPatternGraph != null)
            {
                inlinedPatternClassName = patternGraph.originalSubpatternEmbedding.matchingPatternOfEmbeddedGraph.GetType().Name;
                pathPrefixInInlinedPatternClass = patternGraph.originalPatternGraph.pathPrefix;
                unprefixedNameInInlinedPatternClass = patternGraph.originalPatternGraph.name;
            }
            PatternAndSubpatternsMatched patternAndSubpatternsMatched = new PatternAndSubpatternsMatched(
                inlinedPatternClassName, pathPrefixInInlinedPatternClass + unprefixedNameInInlinedPatternClass, 
                parallelized && indexOfSchedule == 1, type);
            SearchProgramOperation continuationPointAfterPatternAndSubpatternsMatched =
                insertionPoint.Append(patternAndSubpatternsMatched);
            patternAndSubpatternsMatched.MatchBuildingOperations =
                new SearchProgramList(patternAndSubpatternsMatched);
            insertionPoint = patternAndSubpatternsMatched.MatchBuildingOperations;

            // ---- ---- fill the match object with the candidates 
            // ---- ---- which have passed all the checks for being a match
            if (!isIteratedNullMatch)
            {
                insertionPoint = insertInlinedMatchObjectCreation(insertionPoint, 
                    patternGraph, MatchObjectType.Normal);
                insertionPoint = insertMatchObjectBuilding(insertionPoint,
                    patternGraph, MatchObjectType.Normal, false);
                insertionPoint = insertMatchObjectBuilding(insertionPoint,
                    patternGraph, MatchObjectType.Normal, true);

                // if an independent was inlined, we have to insert the local match into a set used for duplicate checking
                if(wasIndependentInlined(patternGraph, indexOfSchedule))
                    insertionPoint = insertFillForDuplicateMatchChecking(insertionPoint);
            }

            // ---- nesting level up
            insertionPoint = continuationPointAfterPatternAndSubpatternsMatched;

            // ---- create new matches list to search on or copy found matches into own matches list
            if (programType==SearchProgramType.Subpattern 
                || programType==SearchProgramType.AlternativeCase)
            { // not needed for iterated, because if match was found, that's it
                NewMatchesListForFollowingMatches newMatchesList =
                    new NewMatchesListForFollowingMatches(false);
                insertionPoint = insertionPoint.Append(newMatchesList);
            }

            // ---- check max matches reached will be inserted here by completion pass
            // (not for iterated, but not needed in that case, too)

            // nesting level up
            insertionPoint = continuationPointAfterSubpatternsFound;

            return insertionPoint;
        }
Esempio n. 2
0
        /// <summary>
        /// Builds search program for alternative from scheduled search plans of the alternative cases
        /// </summary>
        public static SearchProgram BuildSearchProgram(
            IGraphModel model,
            LGSPMatchingPattern matchingPattern,
            Alternative alternative,
            bool parallelized,
            bool emitProfiling)
        {
            String rulePatternClassName = NamesOfEntities.RulePatternClassName(matchingPattern.name, matchingPattern.PatternGraph.Package, !(matchingPattern is LGSPRulePattern));

            // build combined list of namesOfPatternGraphsOnPathToEnclosedPatternpath
            // from the namesOfPatternGraphsOnPathToEnclosedPatternpath of the alternative cases
            // also build combined lists of matching pattern class type names and nested independents
            List <string> namesOfPatternGraphsOnPathToEnclosedPatternpath = new List <string>();
            List <string> matchingPatternClassTypeNames = new List <string>();
            List <Dictionary <PatternGraph, bool> > nestedIndependents = new List <Dictionary <PatternGraph, bool> >();

            for (int i = 0; i < alternative.alternativeCases.Length; ++i)
            {
                PatternGraph altCase = alternative.alternativeCases[i];

                foreach (String name in altCase.patternGraphsOnPathToEnclosedPatternpath)
                {
                    if (!namesOfPatternGraphsOnPathToEnclosedPatternpath.Contains(name))
                    {
                        namesOfPatternGraphsOnPathToEnclosedPatternpath.Add(name);
                    }
                }

                ExtractNestedIndependents(matchingPatternClassTypeNames, nestedIndependents, matchingPattern, altCase);
            }

            // build outermost search program operation, create the list anchor starting its program
            SearchProgram searchProgram = new SearchProgramOfAlternative(
                rulePatternClassName,
                namesOfPatternGraphsOnPathToEnclosedPatternpath,
                "myMatch",
                matchingPatternClassTypeNames, nestedIndependents,
                parallelized);

            searchProgram.OperationsList = new SearchProgramList(searchProgram);
            SearchProgramOperation insertionPoint = searchProgram.OperationsList;

            // initialize task/result-pushdown handling in subpattern matcher
            InitializeSubpatternMatching initialize =
                new InitializeSubpatternMatching(InitializeFinalizeSubpatternMatchingType.Normal);

            insertionPoint = insertionPoint.Append(initialize);

            // build alternative matching search programs, one per case
            for (int i = 0; i < alternative.alternativeCases.Length; ++i)
            {
                PatternGraph        altCase             = alternative.alternativeCases[i];
                ScheduledSearchPlan scheduledSearchPlan = altCase.schedulesIncludingNegativesAndIndependents[0];

                string inlinedPatternClassName             = rulePatternClassName;
                string pathPrefixInInlinedPatternClass     = scheduledSearchPlan.PatternGraph.pathPrefix;
                string unprefixedNameInInlinedPatternClass = scheduledSearchPlan.PatternGraph.name;
                if (alternative.originalAlternative != null)
                {
                    inlinedPatternClassName             = alternative.originalSubpatternEmbedding.matchingPatternOfEmbeddedGraph.GetType().Name;
                    pathPrefixInInlinedPatternClass     = alternative.originalAlternative.pathPrefix + alternative.originalAlternative.name + "_";
                    unprefixedNameInInlinedPatternClass = alternative.originalAlternative.alternativeCases[i].name;
                }

                SearchProgramBodyBuilder builder = new SearchProgramBodyBuilder(
                    SearchProgramType.AlternativeCase,
                    model,
                    rulePatternClassName,
                    null,
                    null,
                    null,
                    altCase,
                    emitProfiling,
                    parallelized,
                    0
                    );

                AlternativeCaseMatching alternativeCaseMatching = new AlternativeCaseMatching(
                    pathPrefixInInlinedPatternClass,
                    unprefixedNameInInlinedPatternClass,
                    inlinedPatternClassName,
                    builder.wasIndependentInlined(altCase, 0));
                alternativeCaseMatching.OperationsList = new SearchProgramList(alternativeCaseMatching);
                SearchProgramOperation continuationPointAfterAltCase = insertionPoint.Append(alternativeCaseMatching);

                // at level of the current alt case
                insertionPoint = alternativeCaseMatching.OperationsList;
                insertionPoint = insertVariableDeclarations(insertionPoint, altCase);

                // start building with first operation in scheduled search plan

                builder.BuildScheduledSearchPlanOperationIntoSearchProgram(
                    0,
                    insertionPoint);

                // back to level of alt cases
                insertionPoint = continuationPointAfterAltCase;

                // save matches found by alternative case to get clean start for matching next alternative case
                if (i < alternative.alternativeCases.Length - 1)
                {
                    NewMatchesListForFollowingMatches newMatchesList =
                        new NewMatchesListForFollowingMatches(true);
                    insertionPoint = insertionPoint.Append(newMatchesList);
                }
            }

            // finalize task/result-pushdown handling in subpattern matcher
            FinalizeSubpatternMatching finalize =
                new FinalizeSubpatternMatching(InitializeFinalizeSubpatternMatchingType.Normal);

            insertionPoint = insertionPoint.Append(finalize);

            return(searchProgram);
        }
Esempio n. 3
0
        /// <summary>
        /// Builds search program for alternative from scheduled search plans of the alternative cases
        /// </summary>
        public SearchProgram BuildSearchProgram(
            IGraphModel model,
            LGSPMatchingPattern matchingPattern,
            Alternative alternative,
            bool parallelized,
            bool emitProfiling)
        {
            programType = SearchProgramType.AlternativeCase;
            this.model = model;
            patternGraphWithNestingPatterns = new Stack<PatternGraph>();
            this.parallelized = parallelized;
            this.alternative = alternative;
            rulePatternClassName = NamesOfEntities.RulePatternClassName(matchingPattern.name, matchingPattern.PatternGraph.Package, !(matchingPattern is LGSPRulePattern));
            this.emitProfiling = emitProfiling;
            packagePrefixedActionName = null;
            firstLoopPassed = false;

            // build combined list of namesOfPatternGraphsOnPathToEnclosedPatternpath
            // from the namesOfPatternGraphsOnPathToEnclosedPatternpath of the alternative cases
            List<string> namesOfPatternGraphsOnPathToEnclosedPatternpath = new List<string>();
            for (int i = 0; i < alternative.alternativeCases.Length; ++i)
            {
                PatternGraph altCase = alternative.alternativeCases[i];
                foreach (String name in altCase.patternGraphsOnPathToEnclosedPatternpath)
                {
                    if(!namesOfPatternGraphsOnPathToEnclosedPatternpath.Contains(name))
                        namesOfPatternGraphsOnPathToEnclosedPatternpath.Add(name);
                }
            }

            // build outermost search program operation, create the list anchor starting its program
            SearchProgram searchProgram = new SearchProgramOfAlternative(
                rulePatternClassName,
                namesOfPatternGraphsOnPathToEnclosedPatternpath,
                "myMatch",
                parallelized);
            searchProgram.OperationsList = new SearchProgramList(searchProgram);
            SearchProgramOperation insertionPoint = searchProgram.OperationsList;

            // initialize task/result-pushdown handling in subpattern matcher
            InitializeSubpatternMatching initialize = 
                new InitializeSubpatternMatching(InitializeFinalizeSubpatternMatchingType.Normal);
            insertionPoint = insertionPoint.Append(initialize);

            // build alternative matching search programs, one per case
            for (int i=0; i<alternative.alternativeCases.Length; ++i)
            {
                PatternGraph altCase = alternative.alternativeCases[i];
                ScheduledSearchPlan scheduledSearchPlan = altCase.schedulesIncludingNegativesAndIndependents[0];

                string inlinedPatternClassName = rulePatternClassName;
                string pathPrefixInInlinedPatternClass = scheduledSearchPlan.PatternGraph.pathPrefix;
                string unprefixedNameInInlinedPatternClass = scheduledSearchPlan.PatternGraph.name;
                if(alternative.originalAlternative != null)
                {
                    inlinedPatternClassName = alternative.originalSubpatternEmbedding.matchingPatternOfEmbeddedGraph.GetType().Name;
                    pathPrefixInInlinedPatternClass = alternative.originalAlternative.pathPrefix + alternative.originalAlternative.name + "_";
                    unprefixedNameInInlinedPatternClass = alternative.originalAlternative.alternativeCases[i].name;
                }

                GetPartialMatchOfAlternative matchAlternative = new GetPartialMatchOfAlternative(
                    pathPrefixInInlinedPatternClass, 
                    unprefixedNameInInlinedPatternClass,
                    inlinedPatternClassName,
                    wasIndependentInlined(altCase, indexOfSchedule));
                matchAlternative.OperationsList = new SearchProgramList(matchAlternative);
                SearchProgramOperation continuationPointAfterAltCase = insertionPoint.Append(matchAlternative);
                
                // at level of the current alt case
                insertionPoint = matchAlternative.OperationsList;
                insertionPoint = insertVariableDeclarations(insertionPoint, altCase);

                patternGraphWithNestingPatterns.Push(altCase);
                isoSpaceNeverAboveMaxIsoSpace = patternGraphWithNestingPatterns.Peek().maxIsoSpace < (int)LGSPElemFlags.MAX_ISO_SPACE;
                isNegative = false;
                isNestedInNegative = false;

                // start building with first operation in scheduled search plan

                indexOfSchedule = 0;
                BuildScheduledSearchPlanOperationIntoSearchProgram(
                    0,
                    insertionPoint);

                // back to level of alt cases
                insertionPoint = continuationPointAfterAltCase;

                // save matches found by alternative case to get clean start for matching next alternative case
                if(i<alternative.alternativeCases.Length-1)
                {
                    NewMatchesListForFollowingMatches newMatchesList =
                        new NewMatchesListForFollowingMatches(true);
                    insertionPoint = insertionPoint.Append(newMatchesList);
                }

                patternGraphWithNestingPatterns.Pop();
            }

            // finalize task/result-pushdown handling in subpattern matcher
            FinalizeSubpatternMatching finalize =
                new FinalizeSubpatternMatching(InitializeFinalizeSubpatternMatchingType.Normal);
            insertionPoint = insertionPoint.Append(finalize);

            return searchProgram;
        }