/// <summary> /// Close blocks in order to recover from exceptions /// </summary> internal static void CloseAllBlocks() { List <IStatementBlock> blocks = new List <IStatementBlock>(StatementBlock.GetOpenBlocks()); blocks.Reverse(); foreach (StatementBlock block in blocks) { block.CloseBlock(); } }
/// <summary> /// Builds the model necessary to infer marginals for the supplied variables and algorithm. /// </summary> /// <param name="engine">The inference algorithm being used</param> /// <param name="inferOnlySpecifiedVars">If true, inference will be restricted to only the variables given.</param> /// <param name="vars">Variables to infer.</param> /// <returns></returns> /// <remarks> /// Algorithm: starting from the variables to infer, we search through the graph to build up a "searched set". /// Each Variable and MethodInvoke in this set has an associated timestamp. /// We sort by timestamp, and then generate code. /// </remarks> public ITypeDeclaration Build(InferenceEngine engine, bool inferOnlySpecifiedVars, IEnumerable <IVariable> vars) { List <IStatementBlock> openBlocks = StatementBlock.GetOpenBlocks(); if (openBlocks.Count > 0) { throw new InvalidOperationException("The block " + openBlocks[0] + " has not been closed."); } Reset(); this.inferOnlySpecifiedVars = inferOnlySpecifiedVars; variablesToInfer.AddRange(vars); foreach (IVariable var in vars) { toSearch.Push(var); } while (toSearch.Count > 0) { IModelExpression expr = toSearch.Pop(); SearchExpressionUntyped(expr); } // lock in the set of model expressions. ModelExpressions = new List <IModelExpression>(searched); List <int> timestamps = new List <int>(); List <IModelExpression> exprs = new List <IModelExpression>(); foreach (IModelExpression expr in ModelExpressions) { if (expr is Variable var) { exprs.Add(var); timestamps.Add(var.timestamp); } else if (expr is MethodInvoke mi) { exprs.Add(mi); timestamps.Add(mi.timestamp); } } Collection.Sort(timestamps, exprs); foreach (IModelExpression expr in exprs) { BuildExpressionUntyped(expr); } foreach (IModelExpression expr in exprs) { FinishExpressionUntyped(expr, engine.Algorithm); } return(modelType); }
IVariableArray IVariableArray.ReplaceRanges(Dictionary <Range, Range> rangeReplacements, Dictionary <IModelExpression, IModelExpression> expressionReplacements, bool deepCopy) { // must do this replacement first, since it will influence how we replace the itemPrototype Range newRange = Range.Replace(rangeReplacements, expressionReplacements); TItem itemPrototype = (TItem)((IVariableJaggedArray)this).ItemPrototype; if (itemPrototype is IVariableArray) { IVariable result = ((IVariableArray)itemPrototype).ReplaceRanges(rangeReplacements, expressionReplacements, deepCopy); itemPrototype = (TItem)result; } else { // make a clone in the current containers itemPrototype = (TItem)itemPrototype.Clone(); itemPrototype.containers = StatementBlock.GetOpenBlocks(); } return(new VariableArray <TItem, TArray>(itemPrototype, newRange)); }
internal MethodInvoke(MethodInfo method, params IModelExpression[] args) : this(StatementBlock.GetOpenBlocks(), method, args) { }