示例#1
0
 protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
 {
     if (CodeRecognizer.IsInfer(imie))
     {
         return(ConvertInfer(imie));
     }
     return(base.ConvertMethodInvoke(imie));
 }
        protected override IStatement ConvertExpressionStatement(IExpressionStatement ies)
        {
            if (parent == null)
            {
                return(ies);
            }
            bool keepIfStatement = false;
            // Only keep the surrounding if statement when a factor or constraint is being added.
            IExpression expr = ies.Expression;

            if (expr is IMethodInvokeExpression)
            {
                keepIfStatement = true;
                if (CodeRecognizer.IsInfer(expr))
                {
                    keepIfStatement = false;
                }
            }
            else if (expr is IAssignExpression)
            {
                keepIfStatement = false;
                IAssignExpression       iae  = (IAssignExpression)expr;
                IMethodInvokeExpression imie = iae.Expression as IMethodInvokeExpression;
                if (imie != null)
                {
                    keepIfStatement = true;
                    if (imie.Arguments.Count > 0)
                    {
                        // Statements that copy evidence variables should not send evidence messages.
                        IVariableDeclaration ivd    = Recognizer.GetVariableDeclaration(iae.Target);
                        IVariableDeclaration ivdArg = Recognizer.GetVariableDeclaration(imie.Arguments[0]);
                        if (ivd != null && context.InputAttributes.Has <DoNotSendEvidence>(ivd) &&
                            ivdArg != null && context.InputAttributes.Has <DoNotSendEvidence>(ivdArg))
                        {
                            keepIfStatement = false;
                        }
                    }
                }
                else
                {
                    expr = iae.Target;
                }
            }
            if (expr is IVariableDeclarationExpression)
            {
                IVariableDeclarationExpression ivde = (IVariableDeclarationExpression)expr;
                IVariableDeclaration           ivd  = ivde.Variable;
                keepIfStatement = CodeRecognizer.IsStochastic(context, ivd) && !context.InputAttributes.Has <DoNotSendEvidence>(ivd);
            }
            if (!keepIfStatement)
            {
                return(ies);
            }
            IConditionStatement cs = Builder.CondStmt(parent.Condition, Builder.BlockStmt());

            cs.Then.Statements.Add(ies);
            return(cs);
        }
示例#3
0
 protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
 {
     if (CodeRecognizer.IsInfer(imie))
     {
         return(imie);
     }
     CheckForDuplicateArguments(imie);
     return(base.ConvertMethodInvoke(imie));
 }
 protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
 {
     if (CodeRecognizer.IsInfer(imie))
     {
         // ignore the variable use
         return(imie);
     }
     return(base.ConvertMethodInvoke(imie));
 }
示例#5
0
 protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
 {
     // do not count argument of Infer as a use
     if (CodeRecognizer.IsInfer(imie))
     {
         return(imie);
     }
     return(base.ConvertMethodInvoke(imie));
 }
 protected override IStatement ConvertExpressionStatement(IExpressionStatement ies)
 {
     if (ies.Expression is IAssignExpression iae)
     {
         if (iae.Expression is IMethodInvokeExpression)
         {
             factorExprs.Add(iae);
         }
     }
     else if (ies.Expression is IMethodInvokeExpression imie)
     {
         if (CodeRecognizer.IsInfer(imie))
         {
             return(ies);
         }
         factorExprs.Add(ies.Expression);
     }
     return(ies);
 }
示例#7
0
        protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
        {
            if (CodeRecognizer.IsInfer(imie))
            {
                return(ConvertInfer(imie));
            }
            foreach (IExpression arg in imie.Arguments)
            {
                if (arg is IAddressOutExpression)
                {
                    IAddressOutExpression iaoe = (IAddressOutExpression)arg;
                    targetsOfCurrentAssignment.Add(iaoe.Expression);
                }
            }
            if (Recognizer.IsStaticGenericMethod(imie, new Func <IList <PlaceHolder>, int[][], PlaceHolder[][]>(Factor.JaggedSubarray)))
            {
                IExpression          arrayExpr = imie.Arguments[0];
                IVariableDeclaration ivd       = Recognizer.GetVariableDeclaration(imie.Arguments[0]);
                if (ivd != null && (arrayExpr is IVariableReferenceExpression) && this.variablesLackingVariableFactor.Contains(ivd) &&
                    !marginalOfVariable.ContainsKey(ivd))
                {
                    VariableInformation vi    = VariableInformation.GetVariableInformation(context, ivd);
                    IList <IStatement>  stmts = Builder.StmtCollection();
                    CreateMarginalChannel(ivd, vi, stmts);

                    Containers defContainers = context.InputAttributes.Get <Containers>(ivd);
                    int        ancIndex      = defContainers.GetMatchingAncestorIndex(context);
                    Containers missing       = defContainers.GetContainersNotInContext(context, ancIndex);
                    stmts = Containers.WrapWithContainers(stmts, missing.outputs);
                    context.AddStatementsBeforeAncestorIndex(ancIndex, stmts);

                    // none of the arguments should need to be transformed
                    IExpression             indicesExpr  = imie.Arguments[1];
                    IExpression             marginalExpr = Builder.VarRefExpr(marginalOfVariable[ivd]);
                    IMethodInvokeExpression mie          = Builder.StaticGenericMethod(new Models.FuncOut <IList <PlaceHolder>, int[][], IList <PlaceHolder>, PlaceHolder[][]>(Factor.JaggedSubarrayWithMarginal),
                                                                                       new Type[] { Utilities.Util.GetElementType(arrayExpr.GetExpressionType()) },
                                                                                       arrayExpr, indicesExpr, marginalExpr);
                    return(mie);
                }
            }
            return(base.ConvertMethodInvoke(imie));
        }
示例#8
0
        protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
        {
            CheckMethodArgumentCount(imie);
            if (CodeRecognizer.IsInfer(imie))
            {
                inferCount++;
                object decl = Recognizer.GetDeclaration(imie.Arguments[0]);
                if (decl != null && !context.InputAttributes.Has <IsInferred>(decl))
                {
                    context.InputAttributes.Set(decl, new IsInferred());
                }
                // the arguments must not be substituted for their values, so we don't call ConvertExpression
                var newArgs = imie.Arguments.Select(CodeRecognizer.RemoveCast);
                IMethodInvokeExpression infer = Builder.MethodInvkExpr();
                infer.Method = imie.Method;
                infer.Arguments.AddRange(newArgs);
                context.InputAttributes.CopyObjectAttributesTo(imie, context.OutputAttributes, infer);
                return(infer);
            }
            IExpression converted = base.ConvertMethodInvoke(imie);

            if (converted is IMethodInvokeExpression mie)
            {
                foreach (IExpression arg in mie.Arguments)
                {
                    if (arg is IAddressOutExpression iaoe)
                    {
                        IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(iaoe.Expression);
                        if (ivd != null)
                        {
                            FactorManager.FactorInfo info = CodeRecognizer.GetFactorInfo(context, mie);
                            if (info != null && info.IsDeterministicFactor && !context.InputAttributes.Has <DerivedVariable>(ivd))
                            {
                                context.InputAttributes.Set(ivd, new DerivedVariable());
                            }
                        }
                    }
                }
            }
            return(converted);
        }
示例#9
0
        protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
        {
            IExpression converted = base.ConvertMethodInvoke(imie);

            if (converted is IMethodInvokeExpression mie)
            {
                if (Recognizer.IsStaticGenericMethod(imie, new Func <PlaceHolder, ICompilerAttribute, PlaceHolder>(Attrib.Var)))
                {
                    IVariableReferenceExpression ivre   = imie.Arguments[0] as IVariableReferenceExpression;
                    IVariableDeclaration         target = ivre.Variable.Resolve();
                    IExpression expr = imie.Arguments[1];
                    AddAttribute(target, expr);
                    return(null);
                }
                else if (Recognizer.IsStaticMethod(imie, new Action <object, object>(Attrib.InitialiseTo)))
                {
                    IVariableReferenceExpression ivre   = imie.Arguments[0] as IVariableReferenceExpression;
                    IVariableDeclaration         target = ivre.Variable.Resolve();
                    context.OutputAttributes.Set(target, new InitialiseTo(imie.Arguments[1]));
                    return(null);
                }
                else if (CodeRecognizer.IsInfer(imie))
                {
                    // the arguments must not be substituted for their values, so we don't call ConvertExpression
                    IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(imie.Arguments[0]);
                    if (ivd != null)
                    {
                        var       vi    = VariableInformation.GetVariableInformation(context, ivd);
                        QueryType query = (imie.Arguments.Count < 3) ? null : (QueryType)evaluator.Evaluate(imie.Arguments[2]);
                        vi.NeedsMarginalDividedByPrior = (query == QueryTypes.MarginalDividedByPrior);
                    }
                    return(imie);
                }
                bool anyArgumentIsLiteral = mie.Arguments.Any(arg => arg is ILiteralExpression);
                if (anyArgumentIsLiteral)
                {
                    if (Recognizer.IsStaticMethod(converted, new Func <bool, bool, bool>(Factors.Factor.And)))
                    {
                        if (mie.Arguments.Any(arg => arg is ILiteralExpression ile && ile.Value.Equals(false)))
                        {
                            return(Builder.LiteralExpr(false));
                        }
                        // any remaining literals must be true, and therefore can be ignored.
                        var reducedArguments = mie.Arguments.Where(arg => !(arg is ILiteralExpression));
                        if (reducedArguments.Count() == 1)
                        {
                            return(reducedArguments.First());
                        }
                        else
                        {
                            return(Builder.LiteralExpr(true));
                        }
                    }
                    else if (Recognizer.IsStaticMethod(converted, new Func <bool, bool, bool>(Factors.Factor.Or)))
                    {
                        if (mie.Arguments.Any(arg => arg is ILiteralExpression ile && ile.Value.Equals(true)))
                        {
                            return(Builder.LiteralExpr(true));
                        }
                        // any remaining literals must be false, and therefore can be ignored.
                        var reducedArguments = mie.Arguments.Where(arg => !(arg is ILiteralExpression));
                        if (reducedArguments.Count() == 1)
                        {
                            return(reducedArguments.First());
                        }
                        else
                        {
                            return(Builder.LiteralExpr(false));
                        }
                    }
                    else if (Recognizer.IsStaticMethod(converted, new Func <bool, bool>(Factors.Factor.Not)))
                    {
                        bool allArgumentsAreLiteral = mie.Arguments.All(arg => arg is ILiteralExpression);
                        if (allArgumentsAreLiteral)
                        {
                            return(Builder.LiteralExpr(evaluator.Evaluate(mie)));
                        }
                    }
                }
            }
            return(converted);
        }
示例#10
0
        protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
        {
            if (Recognizer.IsStaticGenericMethod(imie, new Func <PlaceHolder, ICompilerAttribute, PlaceHolder>(Attrib.Var)))
            {
                IVariableReferenceExpression ivre   = imie.Arguments[0] as IVariableReferenceExpression;
                IVariableDeclaration         target = ivre.Variable.Resolve();
                IExpression expr = CodeRecognizer.RemoveCast(imie.Arguments[1]);
                AddAttribute(target, expr);
                return(null);
            }
            else if (Recognizer.IsStaticMethod(imie, new Action <object, object>(Attrib.InitialiseTo)))
            {
                IVariableReferenceExpression ivre   = CodeRecognizer.RemoveCast(imie.Arguments[0]) as IVariableReferenceExpression;
                IVariableDeclaration         target = ivre.Variable.Resolve();
                context.OutputAttributes.Set(target, new InitialiseTo(imie.Arguments[1]));
                return(null);
            }
            else if (CodeRecognizer.IsInfer(imie))
            {
                inferCount++;
                object decl = Recognizer.GetDeclaration(imie.Arguments[0]);
                if (decl != null && !context.InputAttributes.Has <IsInferred>(decl))
                {
                    context.InputAttributes.Set(decl, new IsInferred());
                }
                // the arguments must not be substituted for their values, so we don't call ConvertExpression
                List <IExpression> newArgs = new List <IExpression>();
                foreach (var arg in imie.Arguments)
                {
                    newArgs.Add(CodeRecognizer.RemoveCast(arg));
                }
                IMethodInvokeExpression mie = Builder.MethodInvkExpr();
                mie.Method = imie.Method;
                mie.Arguments.AddRange(newArgs);
                context.InputAttributes.CopyObjectAttributesTo(imie, context.OutputAttributes, mie);
                return(mie);
            }
            IExpression converted = base.ConvertMethodInvoke(imie);

            if (converted is IMethodInvokeExpression)
            {
                var  mie   = (IMethodInvokeExpression)converted;
                bool isAnd = Recognizer.IsStaticMethod(converted, new Func <bool, bool, bool>(Factors.Factor.And));
                bool isOr  = Recognizer.IsStaticMethod(converted, new Func <bool, bool, bool>(Factors.Factor.Or));
                bool anyArgumentIsLiteral = mie.Arguments.Any(arg => arg is ILiteralExpression);
                if (anyArgumentIsLiteral)
                {
                    if (isAnd)
                    {
                        if (mie.Arguments.Any(arg => arg is ILiteralExpression && ((ILiteralExpression)arg).Value.Equals(false)))
                        {
                            return(Builder.LiteralExpr(false));
                        }
                        // any remaining literals must be true, and therefore can be ignored.
                        var reducedArguments = mie.Arguments.Where(arg => !(arg is ILiteralExpression));
                        if (reducedArguments.Count() == 1)
                        {
                            return(reducedArguments.First());
                        }
                        else
                        {
                            return(Builder.LiteralExpr(true));
                        }
                    }
                    else if (isOr)
                    {
                        if (mie.Arguments.Any(arg => arg is ILiteralExpression && ((ILiteralExpression)arg).Value.Equals(true)))
                        {
                            return(Builder.LiteralExpr(true));
                        }
                        // any remaining literals must be false, and therefore can be ignored.
                        var reducedArguments = mie.Arguments.Where(arg => !(arg is ILiteralExpression));
                        if (reducedArguments.Count() == 1)
                        {
                            return(reducedArguments.First());
                        }
                        else
                        {
                            return(Builder.LiteralExpr(false));
                        }
                    }
                    else if (Recognizer.IsStaticMethod(converted, new Func <bool, bool>(Factors.Factor.Not)))
                    {
                        bool allArgumentsAreLiteral = mie.Arguments.All(arg => arg is ILiteralExpression);
                        if (allArgumentsAreLiteral)
                        {
                            return(Builder.LiteralExpr(evaluator.Evaluate(mie)));
                        }
                    }
                }
                foreach (IExpression arg in mie.Arguments)
                {
                    if (arg is IAddressOutExpression)
                    {
                        IAddressOutExpression iaoe = (IAddressOutExpression)arg;
                        IVariableDeclaration  ivd  = Recognizer.GetVariableDeclaration(iaoe.Expression);
                        if (ivd != null)
                        {
                            FactorManager.FactorInfo info = CodeRecognizer.GetFactorInfo(context, mie);
                            if (info != null && info.IsDeterministicFactor && !context.InputAttributes.Has <DerivedVariable>(ivd))
                            {
                                context.InputAttributes.Set(ivd, new DerivedVariable());
                            }
                        }
                    }
                }
            }
            return(converted);
        }