Beispiel #1
0
    /// <summary>
    /// applies the applicable node grammars from <paramref name="nodeGrammars"/> in the sequence described by the string <paramref name="ruleSequence"/> onto the graph <paramref name="nodeGraph"/> example sequence : "ABC" will transform the nodegraph according to the rules labeled "A" then "B" then "C" if they exist withing the supplied grammar rules
    /// </summary>
    /// <param name="ruleSequence">sequence of rules to apply</param>
    /// <param name="nodeGrammars">all possible rules to apply</param>
    /// <param name="nodeGraph">the graph to be cloned and transformed</param>
    /// <returns>the transformed node graph</returns>
    public static NodeGraph ApplyNodeGrammars(string ruleSequence, ref List <NodeGrammar> nodeGrammars, NodeGraph nodeGraph, int seed)
    {
        if (ruleSequence == null)
        {
            return(nodeGraph);
        }

        NodeGraph output = (NodeGraph)nodeGraph.Clone();

        // TODO : make it possible for rules to have a label of more than one character
        for (int ruleIndex = 0; ruleIndex < ruleSequence.Length; ruleIndex++)
        {
            string ruleLabel = ruleSequence[ruleIndex].ToString();
            // collect all the rules with the right label
            var rules = nodeGrammars.Where((NodeGrammar gram) => gram.Name == ruleLabel);
            if (rules.Count() == 0)
            {
                // no rules match this label
                continue;
            }
            // try all the applicable rules
            foreach (var rule in rules)
            {
                if (IsGrammarApplicable(rule, ref output, out OrderedDictionary <int, int> translationTable, seed))
                {
                    // if the grammar is applicable we get a valid translation table back, this table will give  us translations for which IDs in the graph match which IDs in the left hand side of the grammar rule
                    // we found one that works, apply it and go to the next step in the sequence
                    ApplyNodeRule(rule, ref output, ref translationTable);
                    break;
                }
            }
        }
        return(output);
    }