Beispiel #1
0
        public BasicRule(
            ParsedRule parsedInfo,
            int branchOpenSymbol,
            int branchCloseSymbol)
        {
            _targetSymbolWithParameters = parsedInfo.coreSymbol;
            ruleGroupIndex = parsedInfo.ruleGroupIndex;

            possibleOutcomes = new RuleOutcome[] {
                new RuleOutcome(1, parsedInfo.replacementSymbols)
            };

            conditionalChecker = parsedInfo.conditionalMatch;


            backwardsMatchBuilder = new SymbolSeriesPrefixBuilder(parsedInfo.backwardsMatch);

            forwardsMatchBuilder = new SymbolSeriesSuffixBuilder(parsedInfo.forwardsMatch);
            forwardsMatchBuilder.BuildGraphIndexes(branchOpenSymbol, branchCloseSymbol);


            CapturedLocalParameterCount = _targetSymbolWithParameters.parameterLength +
                                          backwardsMatchBuilder.targetSymbolSeries.Sum(x => x.parameterLength) +
                                          forwardsMatchBuilder.targetSymbolSeries.Sum(x => x.parameterLength);
        }
    public void BuildsStructExpressionFromBuilderTree()
    {
        var paramInput   = Expression.Parameter(typeof(float), "testP");
        var operatorData = OperatorBuilder.Binary(OperatorType.MULTIPLY,
                                                  OperatorBuilder.ConstantValue(1.5f),
                                                  OperatorBuilder.ParameterReference(paramInput));

        var builder = new DynamicExpressionData(operatorData, new ParameterExpression[] { paramInput });

        using var nativeOpData = new NativeArray <OperatorDefinition>(builder.OperatorSpaceNeeded, Allocator.Persistent);
        using var inputParams  = new NativeArray <float>(new float[] { 2f }, Allocator.Persistent);
        var opDataSpace = new JaggedIndexing
        {
            index  = 0,
            length = builder.OperatorSpaceNeeded
        };
        var expression = builder.WriteIntoOpDataArray(
            nativeOpData,
            opDataSpace);

        var result = expression.EvaluateExpression(
            inputParams,
            new JaggedIndexing
        {
            index  = 0,
            length = 1
        },
            nativeOpData);

        Assert.AreEqual(3f, result);
    }
Beispiel #3
0
    private void AssertFunctionResults(string expressionString, float[,] calls, object[] results, string[] paramNames = null)
    {
        var expressionCompiler = new ExpressionCompiler(paramNames == null ? new string[0] : paramNames);
        var operatorData       = expressionCompiler.CompileToExpression(expressionString);
        var builder            = new DynamicExpressionData(operatorData, expressionCompiler.parameters.Values.ToArray());

        using var nativeOpData = new NativeArray <OperatorDefinition>(builder.OperatorSpaceNeeded, Allocator.Persistent);

        var paramSize   = (ushort)calls.GetLength(1);
        var inputParams = new NativeArray <float>(paramSize, Allocator.Persistent);
        var opDataSpace = new JaggedIndexing
        {
            index  = 0,
            length = builder.OperatorSpaceNeeded
        };
        var expression = builder.WriteIntoOpDataArray(
            nativeOpData,
            opDataSpace);

        for (int call = 0; call < calls.GetLength(0); call++)
        {
            for (int param = 0; param < calls.GetLength(1); param++)
            {
                inputParams[param] = calls[call, param];
            }
            var result = expression.EvaluateExpression(
                inputParams,
                new JaggedIndexing
            {
                index  = 0,
                length = paramSize
            },
                nativeOpData);
            if (results[call] is float floatVal)
            {
                Assert.AreEqual(result, floatVal);
            }
            else if (results[call] is bool boolValue)
            {
                if (boolValue)
                {
                    Assert.IsTrue(result > 0);
                }
                else
                {
                    Assert.IsFalse(result > 0);
                }
            }
        }

        inputParams.Dispose();
    }
Beispiel #4
0
        /// <summary>
        /// Create a new basic rule with multiple random outcomes.
        /// It is garenteed and required that all of the stochastic rules will capture the
        /// same parameters
        /// </summary>
        /// <param name="parsedRules"></param>
        public BasicRule(IEnumerable <ParsedStochasticRule> parsedRules, int branchOpenSymbol, int branchCloseSymbol)
        {
            possibleOutcomes = parsedRules
                               .Select(x =>
                                       new RuleOutcome(x.probability, x.replacementSymbols)
                                       ).ToArray();
            var firstOutcome = parsedRules.First();

            _targetSymbolWithParameters = firstOutcome.coreSymbol;
            ruleGroupIndex = firstOutcome.ruleGroupIndex;

            conditionalChecker = firstOutcome.conditionalMatch;

            backwardsMatchBuilder = new SymbolSeriesPrefixBuilder(firstOutcome.backwardsMatch);

            forwardsMatchBuilder = new SymbolSeriesSuffixBuilder(firstOutcome.forwardsMatch);
            forwardsMatchBuilder.BuildGraphIndexes(branchOpenSymbol, branchCloseSymbol);

            CapturedLocalParameterCount = _targetSymbolWithParameters.parameterLength +
                                          backwardsMatchBuilder.targetSymbolSeries.Sum(x => x.parameterLength) +
                                          forwardsMatchBuilder.targetSymbolSeries.Sum(x => x.parameterLength);
        }