예제 #1
0
        public IRuleAction CompileAction(ActionElement element, List <Declaration> declarations,
                                         List <DependencyElement> dependencies, IndexMap tupleFactMap)
        {
            var activationFactMap = IndexMap.CreateMap(element.Imports, declarations);
            var factMap           = IndexMap.Compose(tupleFactMap, activationFactMap);

            var dependencyIndexMap = IndexMap.CreateMap(element.Imports, dependencies.Select(x => x.Declaration));

            if (dependencyIndexMap.HasData)
            {
                var optimizedExpression = ExpressionOptimizer
                                          .Optimize <Action <IContext, Tuple, IDependencyResolver, IResolutionContext> >(
                    element.Expression, factMap, dependencies, dependencyIndexMap);
                var @delegate   = ExpressionCompiler.Compile(optimizedExpression);
                var argumentMap = new ArgumentMap(factMap, element.Expression.Parameters.Count - 1);
                var action      = new RuleActionWithDependencies(element.Expression, @delegate, argumentMap, element.ActionTrigger);
                return(action);
            }
            else
            {
                var optimizedExpression = ExpressionOptimizer.Optimize <Action <IContext, Tuple> >(
                    element.Expression, 1, factMap, tupleInput: true, factInput: false);
                var @delegate   = ExpressionCompiler.Compile(optimizedExpression);
                var argumentMap = new ArgumentMap(factMap, element.Expression.Parameters.Count - 1);
                var action      = new RuleAction(element.Expression, @delegate, argumentMap, element.ActionTrigger);
                return(action);
            }
        }
예제 #2
0
        public ILhsFactExpression <TResult> CompileLhsFactExpression <TResult>(ExpressionElement element)
        {
            var optimizedExpression = ExpressionOptimizer.Optimize <Func <Fact, TResult> >(
                element.Expression, IndexMap.Unit, tupleInput: false, factInput: true);
            var @delegate   = optimizedExpression.Compile();
            var argumentMap = new ArgumentMap(IndexMap.Unit, 1);
            var expression  = new LhsFactExpression <TResult>(element.Expression, @delegate, argumentMap);

            return(expression);
        }
예제 #3
0
        public ILhsExpression <TResult> CompileLhsTupleFactExpression <TResult>(ExpressionElement element, List <Declaration> declarations)
        {
            var factMap             = IndexMap.CreateMap(element.Imports, declarations);
            var optimizedExpression = ExpressionOptimizer.Optimize <Func <Tuple, Fact, TResult> >(
                element.Expression, factMap, tupleInput: true, factInput: true);
            var @delegate   = ExpressionCompiler.Compile(optimizedExpression);
            var argumentMap = new ArgumentMap(factMap, element.Expression.Parameters.Count);
            var expression  = new LhsExpression <TResult>(element.Expression, @delegate, argumentMap);

            return(expression);
        }
예제 #4
0
        public static IActivationExpression <TResult> CompileActivationExpression <TResult>(ExpressionElement element,
                                                                                            List <Declaration> declarations, IndexMap tupleFactMap)
        {
            var activationFactMap   = IndexMap.CreateMap(element.Imports, declarations);
            var factMap             = IndexMap.Compose(tupleFactMap, activationFactMap);
            var optimizedExpression = ExpressionOptimizer.Optimize <Func <Tuple, TResult> >(
                element.Expression, factMap, tupleInput: true, factInput: false);
            var @delegate   = optimizedExpression.Compile();
            var argumentMap = new ArgumentMap(factMap, element.Expression.Parameters.Count);
            var expression  = new ActivationExpression <TResult>(element.Expression, @delegate, argumentMap);

            return(expression);
        }
예제 #5
0
        public static FastDelegate <TDelegate> Create <TDelegate>(LambdaExpression expression) where TDelegate : class
        {
            if (!typeof(TDelegate).IsSubclassOf(typeof(Delegate)))
            {
                throw new InvalidOperationException(
                          string.Format("Type {0} is not a delegate", typeof(TDelegate).FullName));
            }

            var optimizer = new ExpressionOptimizer <TDelegate>();
            Expression <TDelegate> optimizedExpression = optimizer.CompactParameters(expression);
            TDelegate @delegate    = optimizedExpression.Compile();
            var       fastDelegate = new FastDelegate <TDelegate>(@delegate, expression.Parameters.Count);

            return(fastDelegate);
        }