using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter { using System; using System.Collections; using System.Globalization; using System.Reflection; using Attributes; using HeuristicLab.BenchmarkSuite; using Stack; 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) { 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).Reverse()); Console.WriteLine("--------- {0} ---------\n{1}\n", 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) { 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); 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); } } }