using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using HeuristicLab.Grammars; using Attribute = HeuristicLab.Grammars.Attribute; namespace CodeGenerator { // code generator for problem class public class ProblemCodeGen { private const string usings = @" using System.Collections.Generic; using System.Linq; using System; using System.Text.RegularExpressions; "; private const string problemTemplate = @" namespace ?PROBLEMNAME? { public interface IGpdlProblem { int GetCardinality(string terminalSymbol, string attribute); } public interface ISolverState { void Reset(); int PeekNextAlternative(); void Follow(int idx); void Unwind(); } public sealed class ?IDENT?Problem : IGpdlProblem { private readonly Dictionary> cardinalities = new Dictionary>(); public ?IDENT?Problem() { Initialize(); ?CONSTRUCTORSOURCE? } private void Initialize() { ?INITSOURCE? } private ISolverState _state; public double Evaluate(ISolverState _state) { this._state = _state; ?FITNESSFUNCTION? } ?ADDITIONALCODE? ?INTERPRETERSOURCE? ?CONSTRAINTSSOURCE? public int GetCardinality(string terminal, string attribute) { return cardinalities[terminal][attribute]; } } }"; /// /// Generates the source code for a brute force searcher that can be compiled with a C# compiler /// /// An abstract syntax tree for a GPDL file public void Generate(GPDefNode ast) { var problemSourceCode = new SourceBuilder(); problemSourceCode.AppendLine(usings); GenerateProblemSource(ast, problemSourceCode); GenerateSolvers(ast, problemSourceCode); problemSourceCode .Replace("?PROBLEMNAME?", ast.Name) .Replace("?IDENT?", ast.Name); // write the source file to disk using (var stream = new StreamWriter(ast.Name + ".cs")) { stream.WriteLine(problemSourceCode.ToString()); } } private void GenerateProblemSource(GPDefNode ast, SourceBuilder problemSourceCode) { var grammar = CreateGrammarFromAst(ast); problemSourceCode .AppendLine(problemTemplate) .Replace("?FITNESSFUNCTION?", ast.FitnessFunctionNode.SrcCode) .Replace("?CONSTRUCTORSOURCE?", GenerateConstructorSource(ast)) .Replace("?INITSOURCE?", ast.InitCodeNode.SrcCode) .Replace("?ADDITIONALCODE?", ast.ClassCodeNode.SrcCode) .Replace("?INTERPRETERSOURCE?", GenerateInterpreterSource(grammar)) .Replace("?CONSTRAINTSSOURCE?", GenerateConstraintMethods(ast.Terminals)) ; } private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) { var grammar = CreateGrammarFromAst(ast); var randomSearchCodeGen = new RandomSearchCodeGen(); randomSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, ast.Terminals, solverSourceCode); } #region create grammar instance from AST private AttributedGrammar CreateGrammarFromAst(GPDefNode ast) { var nonTerminals = ast.NonTerminals .Select(t => new Symbol(t.Ident, GetSymbolAttributes(t.FormalParameters))) .ToArray(); var terminals = ast.Terminals .Select(t => new Symbol(t.Ident, GetSymbolAttributes(t.FormalParameters))) .ToArray(); string startSymbolName = ast.Rules.First().NtSymbol; // create startSymbol var startSymbol = nonTerminals.Single(s => s.Name == startSymbolName); var g = new AttributedGrammar(startSymbol, nonTerminals, terminals); // add all production rules foreach (var rule in ast.Rules) { var ntSymbol = nonTerminals.Single(s => s.Name == rule.NtSymbol); foreach (var alt in GetAlternatives(rule.Alternatives, nonTerminals.Concat(terminals))) { g.AddProductionRule(ntSymbol, alt); } // local initialization code if (!string.IsNullOrEmpty(rule.LocalCode)) g.AddLocalDefinitions(ntSymbol, rule.LocalCode); } return g; } private IEnumerable GetSymbolAttributes(string formalParameters) { return (from fieldDef in Util.ExtractParameters(formalParameters) select new Attribute(fieldDef.Identifier, fieldDef.Type, AttributeType.Parse(fieldDef.RefOrOut))). ToList(); } private IEnumerable GetAlternatives(AlternativesNode altNode, IEnumerable allSymbols) { foreach (var alt in altNode.Alternatives) { yield return GetSequence(alt.Sequence, allSymbols); } } private Sequence GetSequence(IEnumerable sequence, IEnumerable allSymbols) { Debug.Assert(sequence.All(s => s is CallSymbolNode || s is RuleActionNode)); var l = new List(); foreach (var node in sequence) { var callSymbolNode = node as CallSymbolNode; var actionNode = node as RuleActionNode; if (callSymbolNode != null) { Debug.Assert(allSymbols.Any(s => s.Name == callSymbolNode.Ident)); // create a new symbol with actual parameters l.Add(new Symbol(callSymbolNode.Ident, GetSymbolAttributes(callSymbolNode.ActualParameter))); } else if (actionNode != null) { l.Add(new SemanticSymbol("SEM", actionNode.SrcCode)); } } return new Sequence(l); } #endregion #region helper methods for terminal symbols // produces helper methods for the attributes of all terminal nodes private string GenerateConstraintMethods(IEnumerable symbols) { var sb = new SourceBuilder(); var terminals = symbols.OfType(); foreach (var t in terminals) { GenerateConstraintMethods(t, sb); } return sb.ToString(); } // generates helper methods for the attributes of a given terminal node private void GenerateConstraintMethods(TerminalNode t, SourceBuilder sb) { foreach (var c in t.Constraints) { var fieldType = t.FieldDefinitions.First(d => d.Identifier == c.Ident).Type; if (c.Type == ConstraintNodeType.Range) { sb.AppendFormat("public {0} GetMax{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.RangeMaxExpression).AppendLine(); sb.AppendFormat("public {0} GetMin{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.RangeMinExpression).AppendLine(); // sb.AppendFormat("public {0} Get{1}_{2}(ISolverState _state) {{ return _state.random.NextDouble() * (GetMax{1}_{2}() - GetMin{1}_{2}()) + GetMin{1}_{2}(); }}", fieldType, t.Ident, c.Ident).AppendLine(); sb.AppendFormat("public {0} Get{1}_{2}(ISolverState _state) {{ throw new NotSupportedException(\"range constraints for terminals are not supported.\"); }}", fieldType, t.Ident, c.Ident).AppendLine(); } else if (c.Type == ConstraintNodeType.Set) { sb.AppendFormat("public IEnumerable<{0}> GetAllowed{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.SetExpression).AppendLine(); sb.AppendFormat("private readonly {0}[] values_{1}_{2};", fieldType, t.Ident, c.Ident).AppendLine(); sb.AppendFormat("public {0} Get{1}_{2}(ISolverState _state) {{ return values_{1}_{2}[_state.PeekNextAlternative()]; }}", fieldType, t.Ident, c.Ident).AppendLine(); } } } private string GenerateConstructorSource(GPDefNode ast) { var sb = new SourceBuilder(); // generate code to initialize the tables for terminals foreach (var t in ast.Terminals.OfType()) { if (t.Constraints.Any()) { foreach (var constraint in t.Constraints) { sb.AppendFormat("values_{0}_{1} = GetAllowed{0}_{1}().ToArray();", t.Ident, constraint.Ident); } sb.AppendFormat("cardinalities[\"{0}\"] = new Dictionary() {{ ", t.Ident); foreach (var constraint in t.Constraints) { sb.AppendFormat("{{ \"{1}\", values_{0}_{1}.Length }}, ", t.Ident, constraint.Ident); } sb.Append("};").AppendLine(); } } return sb.ToString(); } #endregion private string GenerateInterpreterSource(AttributedGrammar grammar) { var sb = new SourceBuilder(); // generate methods for all nonterminals and terminals using the grammar instance foreach (var s in grammar.NonTerminalSymbols) { GenerateInterpreterMethod(grammar, s, sb); } foreach (var s in grammar.TerminalSymbols) { GenerateTerminalInterpreterMethod(s, sb); } return sb.ToString(); } private void GenerateInterpreterMethod(AttributedGrammar g, ISymbol s, SourceBuilder sb) { // if this is the start symbol we additionally have to create the method which can be called from the fitness function if (g.StartSymbol.Equals(s)) { if (!s.Attributes.Any()) sb.AppendFormat("private void {0}() {{", s.Name).BeginBlock(); else sb.AppendFormat("private void {0}({1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); // get formal parameters of start symbol var attr = g.StartSymbol.Attributes; // actual parameter are the same as formalparameter only without type identifier string actualParameter; if (attr.Any()) actualParameter = attr.Skip(1).Aggregate(attr.First().AttributeType + " " + attr.First().Name, (str, a) => str + ", " + a.AttributeType + " " + a.Name); else actualParameter = string.Empty; sb.AppendLine("_state.Reset();"); sb.AppendFormat("{0}(_state, {1});", g.StartSymbol.Name, actualParameter).AppendLine(); sb.AppendLine("}").EndBlock(); } if (!s.Attributes.Any()) sb.AppendFormat("private void {0}(ISolverState _state) {{", s.Name).BeginBlock(); else sb.AppendFormat("private void {0}(ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); // generate local definitions sb.AppendLine(g.GetLocalDefinitions(s)); var altsWithSemActions = g.GetAlternativesWithSemanticActions(s).ToArray(); if (altsWithSemActions.Length > 1) { GenerateSwitchStatement(altsWithSemActions, sb); } else { int i = 0; foreach (var altSymb in altsWithSemActions.Single()) { GenerateSourceForAction(i, altSymb, sb); if (!(altSymb is SemanticSymbol)) i++; } } sb.Append("}").EndBlock(); } private void GenerateSwitchStatement(IEnumerable alts, SourceBuilder sb) { sb.Append("switch(_state.PeekNextAlternative()) {").BeginBlock(); // generate a case for each alternative int i = 0; foreach (var alt in alts) { sb.AppendFormat("case {0}: {{ ", i).BeginBlock(); // this only works for alternatives with a single non-terminal symbol (ignoring semantic symbols) so far! // a way to handle this is through grammar transformation (the examplary grammars all have the correct from) Debug.Assert(alt.Count(symb => !(symb is SemanticSymbol)) == 1); foreach (var altSymb in alt) { GenerateSourceForAction(0, altSymb, sb); // index is always 0 because of the assertion above } i++; sb.AppendLine("break;").Append("}").EndBlock(); } sb.AppendLine("default: throw new System.InvalidOperationException();").Append("}").EndBlock(); } // helper for generating calls to other symbol methods private void GenerateSourceForAction(int idx, ISymbol s, SourceBuilder sb) { var action = s as SemanticSymbol; if (action != null) sb.Append(action.Code + ";"); else if (!s.Attributes.Any()) sb.AppendFormat("_state.Follow({0}); {1}(_state); _state.Unwind();", idx, s.Name); else sb.AppendFormat("_state.Follow({0}); {1}(_state, {2}); _state.Unwind();", idx, s.Name, s.GetAttributeString()); sb.AppendLine(); } private string GenerateTerminalInterpreterMethod(ISymbol s, SourceBuilder sb) { // if the terminal symbol has attributes then we must samples values for these attributes if (!s.Attributes.Any()) sb.AppendFormat("private void {0}(ISolverState _state) {{", s.Name).BeginBlock(); else sb.AppendFormat("private void {0}(ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); // each field must match a formal parameter, assign a value for each parameter int i = 0; foreach (var element in s.Attributes) { sb.AppendFormat("_state.Follow({0});", i++).AppendLine(); sb.AppendFormat("{0} = Get{1}_{0}(_state);", element.Name, s.Name).AppendLine(); sb.AppendFormat("_state.Unwind();").AppendLine(); } sb.Append("}").EndBlock(); return sb.ToString(); } } }