[14834] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Linq;
|
---|
[15771] | 3 | using System;
|
---|
| 4 | using System.Collections;
|
---|
| 5 | using System.Globalization;
|
---|
| 6 | using System.Reflection;
|
---|
| 7 | using HeuristicLab.BenchmarkSuite;
|
---|
[14834] | 8 |
|
---|
[15771] | 9 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[14834] | 10 | public static class Extensions {
|
---|
| 11 | private static readonly IDictionary<StackTypes, PropertyInfo> StackProperties = typeof(PushInterpreter)
|
---|
| 12 | .GetProperties()
|
---|
| 13 | .Select(p => new { Property = p, Attribute = p.GetCustomAttribute<PushStackAttribute>() })
|
---|
| 14 | .Where(x => x.Attribute != null)
|
---|
| 15 | .ToDictionary(x => x.Attribute.StackType, x => x.Property);
|
---|
| 16 |
|
---|
| 17 | public static IEnumerable<T> GetStackEntriesByType<T>(this IPushInterpreter interpreter, StackTypes stackType) {
|
---|
| 18 | return StackProperties.ContainsKey(stackType)
|
---|
| 19 | ? ((IEnumerable)StackProperties[stackType].GetValue(interpreter)).OfType<T>()
|
---|
| 20 | : null;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[15289] | 23 | public static string StringifyResult(this IPushInterpreter interpreter, ExampleArgumentType type, int offset) {
|
---|
| 24 | var emptyString = string.Empty;
|
---|
| 25 |
|
---|
| 26 | switch (type) {
|
---|
| 27 | case ExampleArgumentType.Integer:
|
---|
| 28 | return GetEntryAsString(offset, interpreter.IntegerStack);
|
---|
| 29 |
|
---|
| 30 | case ExampleArgumentType.IntegerVector:
|
---|
| 31 | return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
|
---|
| 32 |
|
---|
| 33 | case ExampleArgumentType.Float:
|
---|
| 34 | return interpreter.FloatStack.Count > offset
|
---|
| 35 | ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture)
|
---|
| 36 | : emptyString;
|
---|
| 37 |
|
---|
| 38 | case ExampleArgumentType.FloatVector:
|
---|
| 39 | return GetVectorEntryAsString(offset, interpreter.FloatVectorStack);
|
---|
| 40 |
|
---|
| 41 | case ExampleArgumentType.Boolean:
|
---|
| 42 | return GetEntryAsString(offset, interpreter.BooleanStack);
|
---|
| 43 |
|
---|
| 44 | case ExampleArgumentType.Char:
|
---|
| 45 | return GetEntryAsString(offset, interpreter.CharStack);
|
---|
| 46 |
|
---|
| 47 | case ExampleArgumentType.Print:
|
---|
| 48 | return interpreter.PrintStack.ToString();
|
---|
| 49 |
|
---|
| 50 | case ExampleArgumentType.String:
|
---|
| 51 | return GetEntryAsString(offset, interpreter.StringStack);
|
---|
| 52 |
|
---|
| 53 | case ExampleArgumentType.StringVector:
|
---|
| 54 | return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
|
---|
| 55 |
|
---|
| 56 | default: return string.Empty;
|
---|
[15189] | 57 | }
|
---|
[15289] | 58 | }
|
---|
[15189] | 59 |
|
---|
[15289] | 60 | private static string GetEntryAsString<T>(int offset, IPushStack<T> stack) {
|
---|
| 61 | return stack.Count > offset
|
---|
| 62 | ? stack[offset].ToString()
|
---|
| 63 | : string.Empty;
|
---|
[14834] | 64 | }
|
---|
| 65 |
|
---|
[15289] | 66 | private static string GetVectorEntryAsString<T>(int offset, IPushStack<IReadOnlyList<T>> vectorStack) {
|
---|
| 67 | return vectorStack.Count > offset
|
---|
| 68 | ? "[" + string.Join(",", vectorStack[offset]) + "]"
|
---|
| 69 | : string.Empty;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[14834] | 72 | public static void PrintStacks(this IPushInterpreter interpreter) {
|
---|
[15334] | 73 | var maxStackNameLength = interpreter.Stacks.Max(x => x.Key.ToString().Length);
|
---|
| 74 | var totalLength = maxStackNameLength + 6;
|
---|
| 75 |
|
---|
[15189] | 76 | foreach (var pair in interpreter.Stacks) {
|
---|
| 77 | var stackName = pair.Key.ToString();
|
---|
| 78 | var stack = interpreter.Stacks[pair.Key];
|
---|
[14834] | 79 |
|
---|
[15334] | 80 | if (stack.IsEmpty || !stack.IsEnabled)
|
---|
| 81 | continue;
|
---|
[15017] | 82 |
|
---|
[15334] | 83 | var stackString = string.Join(" ", interpreter.StringifyStack(pair.Key));
|
---|
| 84 |
|
---|
| 85 | //var padLength = (totalLength - stackName.Length) / 2;
|
---|
| 86 | //var padLengthLeft = stackName.Length + padLength;
|
---|
| 87 | //var padLengthRight = padLengthLeft + padLength;
|
---|
| 88 | //Console.WriteLine("{0}\n{1}\n", stackName.PadLeft(padLengthLeft, '-').PadRight(padLengthRight, '-'), stackString);
|
---|
| 89 |
|
---|
| 90 | Console.WriteLine("{0}: {1}", stackName, stackString);
|
---|
[14834] | 91 | }
|
---|
| 92 |
|
---|
| 93 | if (interpreter.CustomExpressions.Count > 0) {
|
---|
| 94 | Console.WriteLine("--------- Custom Expressions ---------");
|
---|
| 95 | foreach (var ce in interpreter.CustomExpressions) {
|
---|
| 96 | Console.WriteLine("{0}: {1}", ce.Key, ce.Value);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[15273] | 100 |
|
---|
| 101 | public static void InitExample(this IPushInterpreter interpreter, Example example) {
|
---|
[15334] | 102 | if (interpreter.Configuration.TopLevelPushInputArguments) {
|
---|
| 103 | interpreter.BooleanStack.Push(example.InputBoolean);
|
---|
| 104 | interpreter.IntegerStack.Push(example.InputInteger);
|
---|
| 105 | interpreter.FloatStack.Push(example.InputFloat);
|
---|
| 106 | interpreter.CharStack.Push(example.InputChar);
|
---|
| 107 | interpreter.StringStack.Push(example.InputString);
|
---|
| 108 | interpreter.StringVectorStack.Push(example.InputStringVector);
|
---|
| 109 | interpreter.IntegerVectorStack.Push(example.InputIntegerVector);
|
---|
| 110 | interpreter.FloatVectorStack.Push(example.InputFloatVector);
|
---|
| 111 | }
|
---|
[15273] | 112 |
|
---|
[15334] | 113 | // init in expressions
|
---|
[15273] | 114 | interpreter.SetInput(
|
---|
| 115 | integers: example.InputInteger,
|
---|
| 116 | floats: example.InputFloat,
|
---|
| 117 | booleans: example.InputBoolean,
|
---|
| 118 | chars: example.InputChar,
|
---|
| 119 | strings: example.InputString,
|
---|
| 120 | integerVectors: example.InputIntegerVector,
|
---|
| 121 | floatVectors: example.InputFloatVector,
|
---|
| 122 | stringVectors: example.InputStringVector);
|
---|
| 123 | }
|
---|
[14834] | 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|