Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/ExpressionCreatorTable.cs @ 14323

Last change on this file since 14323 was 14323, checked in by pkimmesw, 8 years ago

#2665 Added Unit Test Project, CodeGenerator and refactored project structure

File size: 5.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Algorithms.PushGP.Expressions;
4
5using LinqExpression = System.Linq.Expressions.Expression;
6
7namespace HeuristicLab.Algorithms.PushGP
8{
9    public static class ExpressionCreatorTable
10    {
11        private static Dictionary<OpCode, Func<Expression>> symbolTable = new Dictionary<OpCode, Func<Expression>>
12        {
13            #region Exec
14            { OpCode.ExecPop, GetExpressionCreator(typeof(ExecPopExpression)) },
15            { OpCode.ExecDup, GetExpressionCreator(typeof(ExecDuplicateExpression)) },
16            { OpCode.ExecDefine, GetExpressionCreator(typeof(ExecDefineExpression)) },
17            { OpCode.ExecDoXRange, GetExpressionCreator(typeof(ExecDoRangeExpression)) },
18            { OpCode.ExecY, GetExpressionCreator(typeof(ExecYExpression)) },
19            { OpCode.ExecIf, GetExpressionCreator(typeof(ExecIfExpression)) },
20            #endregion
21
22            #region Code
23            { OpCode.CodeDup, GetExpressionCreator(typeof(CodeDuplicateExpression)) },
24            { OpCode.CodeDefine, GetExpressionCreator(typeof(CodeDefineExpression)) },
25            { OpCode.CodeQuote, GetExpressionCreator(typeof(CodeQuoteExpression)) },
26            { OpCode.CodeDo, GetExpressionCreator(typeof(CodeDoExpression)) },
27            { OpCode.CodeIf, GetExpressionCreator(typeof(CodeIfExpression)) },
28            #endregion Code
29
30            #region Name
31            { OpCode.NamePop, GetExpressionCreator(typeof(NamePopExpression)) },
32            { OpCode.NameDup, GetExpressionCreator(typeof(NameDuplicateExpression)) },
33            #endregion Name
34
35            #region Boolean
36            { OpCode.BooleanPop, GetExpressionCreator(typeof(BooleanPopExpression)) },
37            { OpCode.BooleanDup, GetExpressionCreator(typeof(BooleanDuplicateExpression)) },
38            { OpCode.BooleanDefine, GetExpressionCreator(typeof(BooleanDefineExpression)) },
39            { OpCode.BooleanOr, GetExpressionCreator(typeof(BooleanOrExpression)) },
40            { OpCode.BooleanAnd, GetExpressionCreator(typeof(BooleanAndExpression)) },
41            #endregion Boolean
42
43            #region Integer
44            { OpCode.IntegerPop, GetExpressionCreator(typeof(IntegerPopExpression)) },
45            { OpCode.IntegerDup, GetExpressionCreator(typeof(IntegerDuplicateExpression)) },
46            { OpCode.IntegerDefine, GetExpressionCreator(typeof(IntegerDefineExpression)) },
47            { OpCode.IntegerMultiply, GetExpressionCreator(typeof(IntegerMultiplyExpression)) },
48            { OpCode.IntegerDivide, GetExpressionCreator(typeof(IntegerDivideExpression)) },
49            { OpCode.IntegerAdd, GetExpressionCreator(typeof(IntegerAddExpression)) },
50            { OpCode.IntegerSubtract, GetExpressionCreator(typeof(IntegerSubtractExpression)) },
51            { OpCode.IntegerMin, GetExpressionCreator(typeof(IntegerMinExpression)) },
52            { OpCode.IntegerMax, GetExpressionCreator(typeof(IntegerMaxExpression)) },
53            { OpCode.IntegerSmallerThan, GetExpressionCreator(typeof(IntegerSmallerThanExpression)) },
54            { OpCode.IntegerGreaterThan, GetExpressionCreator(typeof(IntegerGreaterThanExpression)) },
55            { OpCode.IntegerEquals, GetExpressionCreator(typeof(IntegerEqualsExpression)) },
56            #endregion Integer
57
58            #region Float
59            { OpCode.FloatPop, GetExpressionCreator(typeof(FloatPopExpression)) },
60            { OpCode.FloatDup, GetExpressionCreator(typeof(FloatDuplicateExpression)) },
61            { OpCode.FloatDefine, GetExpressionCreator(typeof(FloatDefineExpression)) },
62            { OpCode.FloatAdd, GetExpressionCreator(typeof(FloatAddExpression)) },
63            { OpCode.FloatMultiply, GetExpressionCreator(typeof(FloatMultiplyExpression)) },
64            { OpCode.FloatDivide, GetExpressionCreator(typeof(FloatDivideExpression)) },
65            { OpCode.FloatSubtract, GetExpressionCreator(typeof(FloatSubtractExpression)) },
66            { OpCode.FloatMin, GetExpressionCreator(typeof(FloatMinExpression)) },
67            { OpCode.FloatMax, GetExpressionCreator(typeof(FloatMaxExpression)) },
68            { OpCode.FloatSmallerThan, GetExpressionCreator(typeof(FloatSmallerThanExpression)) },
69            { OpCode.FloatGreaterThan, GetExpressionCreator(typeof(FloatGreaterThanExpression)) },
70            { OpCode.FloatEquals, GetExpressionCreator(typeof(FloatEqualsExpression)) },
71            #endregion Float
72        };
73
74        private static Func<Expression> GetExpressionCreator(Type type)
75        {
76            return LinqExpression.Lambda<Func<Expression>>(
77                       LinqExpression.New(type.GetConstructor(Type.EmptyTypes))
78                   ).Compile();
79        }
80
81        public static Func<Expression> GetCreator(OpCode opCode)
82        {
83            Func<Expression> creator;
84            if (symbolTable.TryGetValue(opCode, out creator)) return creator;
85            else throw new NotSupportedException("OpCode: " + opCode);
86        }
87
88        public static bool TryGetCreator(OpCode opCode, out Func<Expression> creator)
89        {
90            return symbolTable.TryGetValue(opCode, out creator);
91        }
92    }
93}
Note: See TracBrowser for help on using the repository browser.