internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable <int> argumentIndexes, IEnumerable <CutPoint> argumentCutPoints) { foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b })) { var argSymbol = new Argument(pair.Index); grammar.AddSymbol(argSymbol); grammar.SetSubtreeCount(argSymbol, 0, 0); foreach (var symb in grammar.Symbols) { if (symb is ProgramRootSymbol || symb is StartSymbol) { continue; } if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol)) { grammar.AddAllowedChildSymbol(symb, argSymbol); } else { for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) { if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i)) { grammar.AddAllowedChildSymbol(symb, argSymbol, i); } } } } } }
internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) { foreach (var symb in grammar.Symbols) { if (symb is ProgramRootSymbol) { continue; } if (newSymbol is Argument && symb is StartSymbol) { continue; } if (grammar.IsAllowedChildSymbol(symb, symbol)) { grammar.AddAllowedChildSymbol(symb, newSymbol); } else { for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) { if (grammar.IsAllowedChildSymbol(symb, symbol, i)) { grammar.AddAllowedChildSymbol(symb, newSymbol, i); } } } } }
internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) { foreach (var symb in grammar.Symbols) { if (symb is ProgramRootSymbol) continue; if (newSymbol is Argument && symb is StartSymbol) continue; if (grammar.IsAllowedChildSymbol(symb, symbol)) grammar.AddAllowedChildSymbol(symb, newSymbol); else { for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) { if (grammar.IsAllowedChildSymbol(symb, symbol, i)) grammar.AddAllowedChildSymbol(symb, newSymbol, i); } } } }
public bool IsMatchingPointType(ISymbolicExpressionTreeNode newChild) { var parent = this.Parent; if (newChild == null) { // make sure that one subtree can be removed and that only the last subtree is removed return(grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount && this.ChildIndex == parent.SubtreeCount - 1); } else { // check syntax constraints of direct parent - child relation if (!grammar.ContainsSymbol(newChild.Symbol) || !grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, this.ChildIndex)) { return(false); } bool result = true; // check point type for the whole branch newChild.ForEachNodePostfix((n) => { result = result && grammar.ContainsSymbol(n.Symbol) && n.SubtreeCount >= grammar.GetMinimumSubtreeCount(n.Symbol) && n.SubtreeCount <= grammar.GetMaximumSubtreeCount(n.Symbol); }); return(result); } }
internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) { foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b })) { var argSymbol = new Argument(pair.Index); grammar.AddSymbol(argSymbol); grammar.SetSubtreeCount(argSymbol, 0, 0); foreach (var symb in grammar.Symbols) { if (symb is ProgramRootSymbol || symb is StartSymbol) continue; if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol)) grammar.AddAllowedChildSymbol(symb, argSymbol); else { for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) { if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i)) grammar.AddAllowedChildSymbol(symb, argSymbol, i); } } } } }
internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable<CutPoint> argumentCutPoints) { if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) return; var invokeSym = new InvokeFunction(functionName); grammar.AddSymbol(invokeSym); grammar.SetSubtreeCount(invokeSym, nArgs, nArgs); //allow invoke symbol everywhere, where the child of the startCutPoint was allowed SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym); if (nArgs > 0) { //set allowed child symbols of invoke symbol foreach (ISymbol child in grammar.Symbols) { if (child.Name == invokeSym.Name) continue; int i = 0; foreach (CutPoint argumentCutPoint in argumentCutPoints) { if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex)) grammar.AddAllowedChildSymbol(invokeSym, child, i); i++; } } } }
internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable <CutPoint> argumentCutPoints) { if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) { return; } var invokeSym = new InvokeFunction(functionName); grammar.AddSymbol(invokeSym); grammar.SetSubtreeCount(invokeSym, nArgs, nArgs); //allow invoke symbol everywhere, where the child of the startCutPoint was allowed SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym); if (nArgs > 0) { //set allowed child symbols of invoke symbol foreach (ISymbol child in grammar.Symbols) { if (child.Name == invokeSym.Name) { continue; } int i = 0; foreach (CutPoint argumentCutPoint in argumentCutPoints) { if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex)) { grammar.AddAllowedChildSymbol(invokeSym, child, i); } i++; } } } }