using System.Collections.Generic; using System.Linq; using System; using System.Collections; using System.Globalization; using System.Reflection; using HeuristicLab.BenchmarkSuite; namespace HeuristicLab.Problems.ProgramSynthesis { public static class Extensions { private static readonly IDictionary StackProperties = typeof(PushInterpreter) .GetProperties() .Select(p => new { Property = p, Attribute = p.GetCustomAttribute() }) .Where(x => x.Attribute != null) .ToDictionary(x => x.Attribute.StackType, x => x.Property); public static IEnumerable GetStackEntriesByType(this IPushInterpreter interpreter, StackTypes stackType) { return StackProperties.ContainsKey(stackType) ? ((IEnumerable)StackProperties[stackType].GetValue(interpreter)).OfType() : null; } public static string StringifyResult(this IPushInterpreter interpreter, ExampleArgumentType type, int offset) { var emptyString = string.Empty; switch (type) { case ExampleArgumentType.Integer: return GetEntryAsString(offset, interpreter.IntegerStack); case ExampleArgumentType.IntegerVector: return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack); case ExampleArgumentType.Float: return interpreter.FloatStack.Count > offset ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture) : emptyString; case ExampleArgumentType.FloatVector: return GetVectorEntryAsString(offset, interpreter.FloatVectorStack); case ExampleArgumentType.Boolean: return GetEntryAsString(offset, interpreter.BooleanStack); case ExampleArgumentType.Char: return GetEntryAsString(offset, interpreter.CharStack); case ExampleArgumentType.Print: return interpreter.PrintStack.ToString(); case ExampleArgumentType.String: return GetEntryAsString(offset, interpreter.StringStack); case ExampleArgumentType.StringVector: return GetVectorEntryAsString(offset, interpreter.StringVectorStack); default: return string.Empty; } } private static string GetEntryAsString(int offset, IPushStack stack) { return stack.Count > offset ? stack[offset].ToString() : string.Empty; } private static string GetVectorEntryAsString(int offset, IPushStack> vectorStack) { return vectorStack.Count > offset ? "[" + string.Join(",", vectorStack[offset]) + "]" : string.Empty; } public static void PrintStacks(this IPushInterpreter interpreter) { var maxStackNameLength = interpreter.Stacks.Max(x => x.Key.ToString().Length); var totalLength = maxStackNameLength + 6; foreach (var pair in interpreter.Stacks) { var stackName = pair.Key.ToString(); var stack = interpreter.Stacks[pair.Key]; if (stack.IsEmpty || !stack.IsEnabled) continue; var stackString = string.Join(" ", interpreter.StringifyStack(pair.Key)); //var padLength = (totalLength - stackName.Length) / 2; //var padLengthLeft = stackName.Length + padLength; //var padLengthRight = padLengthLeft + padLength; //Console.WriteLine("{0}\n{1}\n", stackName.PadLeft(padLengthLeft, '-').PadRight(padLengthRight, '-'), stackString); Console.WriteLine("{0}: {1}", stackName, stackString); } if (interpreter.CustomExpressions.Count > 0) { Console.WriteLine("--------- Custom Expressions ---------"); foreach (var ce in interpreter.CustomExpressions) { Console.WriteLine("{0}: {1}", ce.Key, ce.Value); } } } public static void InitExample(this IPushInterpreter interpreter, Example example) { if (interpreter.Configuration.TopLevelPushInputArguments) { interpreter.BooleanStack.Push(example.InputBoolean); interpreter.IntegerStack.Push(example.InputInteger); interpreter.FloatStack.Push(example.InputFloat); interpreter.CharStack.Push(example.InputChar); interpreter.StringStack.Push(example.InputString); interpreter.StringVectorStack.Push(example.InputStringVector); interpreter.IntegerVectorStack.Push(example.InputIntegerVector); interpreter.FloatVectorStack.Push(example.InputFloatVector); } // init in expressions interpreter.SetInput( integers: example.InputInteger, floats: example.InputFloat, booleans: example.InputBoolean, chars: example.InputChar, strings: example.InputString, integerVectors: example.InputIntegerVector, floatVectors: example.InputFloatVector, stringVectors: example.InputStringVector); } } }