private void SearchMethodInvoke(MethodInvoke method) { if (searched.Contains(method)) { return; } searched.Add(method); if (method.returnValue != null) { IModelExpression target = method.returnValue; if (method.method.DeclaringType == target.GetType() && method.method.Name == new Func <bool>(Variable <bool> .RemovedBySetTo).Method.Name) { throw new InvalidOperationException("Variable '" + target + "' was consumed by variable.SetTo(). It can no longer be used or inferred. Perhaps you meant Variable.ConstrainEqual instead of SetTo."); } } if (method.returnValue != null) { toSearch.Push(method.returnValue); } foreach (IModelExpression arg in method.args) { toSearch.Push(arg); } SearchContainers(method.Containers); }
protected void FinishExpressionUntyped(IModelExpression expr, IAlgorithm alg) { if (expr is MethodInvoke) { return; } MethodInfo mb = new Action <IModelExpression <object>, IAlgorithm>(this.FinishExpression <object>).Method.GetGenericMethodDefinition(); Type domainType = null; // Look through the interfaces for this model expression (is there a better way of doing this?). // We expect to find IModelExpression<> - we can then get the type parameter from this interface Type[] faces = expr.GetType().GetInterfaces(); foreach (Type face in faces) { if (face.IsGenericType && face.GetGenericTypeDefinition() == typeof(IModelExpression <>)) { domainType = face.GetGenericArguments()[0]; break; } } if (domainType == null) { throw new ArgumentException("Expression: " + expr + " does not implement IModelExpression<>"); } MethodInfo mi2 = mb.MakeGenericMethod(domainType); Util.Invoke(mi2, this, expr, alg); }
protected void SearchExpressionUntyped(IModelExpression expr) { if (expr == null) { throw new NullReferenceException("Model expression was null."); } // Console.WriteLine("Searching expression: "+var+" "+builtVars.ContainsKey(var)); if (searched.Contains(expr)) { return; } if (expr is MethodInvoke methodInvoke) { SearchMethodInvoke(methodInvoke); return; } if (expr is Range range) { SearchRange(range); return; } MethodInfo mb = new Action <IModelExpression <object> >(this.SearchExpression <object>).Method.GetGenericMethodDefinition(); Type domainType = null; // Look through the interfaces for this model expression (is there a better way of doing this?). // We expect to find IModelExpression<> - we can then get the type parameter from this interface Type[] faces = expr.GetType().GetInterfaces(); foreach (Type face in faces) { if (face.IsGenericType && face.GetGenericTypeDefinition() == typeof(IModelExpression <>)) { domainType = face.GetGenericArguments()[0]; break; } } if (domainType == null) { throw new ArgumentException("Expression: " + expr + " does not implement IModelExpression<>"); } MethodInfo mi2 = mb.MakeGenericMethod(domainType); Util.Invoke(mi2, this, expr); }
protected void BuildExpressionUntyped(IModelExpression var) { if (var == null) { throw new NullReferenceException("Model expression was null."); } // Console.WriteLine("Building expression: "+var+" "+builtVars.ContainsKey(var)); if (var is MethodInvoke methodInvoke) { BuildMethodInvoke(methodInvoke); return; } MethodInfo mb = new Action <IModelExpression <object> >(this.BuildExpression <object>).Method.GetGenericMethodDefinition(); Type domainType = null; // Look through the interfaces for this model expression (is there a better way of doing this?). // We expect to find IModelExpression<> - we can then get the type parameter from this interface Type[] faces = var.GetType().GetInterfaces(); foreach (Type face in faces) { if (face.IsGenericType && face.GetGenericTypeDefinition() == typeof(IModelExpression <>)) { domainType = face.GetGenericArguments()[0]; break; } } if (domainType == null) { throw new ArgumentException("Expression: " + var + " does not implement IModelExpression<>"); } // Construct the BuildExpression method for this type. MethodInfo mi2 = mb.MakeGenericMethod(domainType); // Invoke the typed BuildExpression method. This will recurse into BuildExpressionUntyped // as necessary Util.Invoke(mi2, this, var); }
/// <summary> /// Build a variable expression /// </summary> /// <typeparam name="T">Domain type of the variable expression</typeparam> /// <param name="expr">The variable expression</param> private void BuildExpression <T>(IModelExpression <T> expr) { if (expr is Variable <T> var) { BuildVariable <T>(var); } else { throw new InferCompilerException("Unhandled model expression type: " + expr.GetType()); } }
private void FinishExpression <T>(IModelExpression <T> expr, IAlgorithm alg) { if (expr is Variable <T> ) { FinishVariable <T>((Variable <T>)expr, alg); } else { throw new InferCompilerException("Unhandled model expression type: " + expr.GetType()); } }
/// <summary> /// Search a variable expression /// </summary> /// <typeparam name="T">Domain type of the variable expression</typeparam> /// <param name="var">The variable expression</param> /// public void SearchExpression <T>(IModelExpression <T> var) { if (var is Variable <T> varT) { SearchVariable <T>(varT); } else { throw new InferCompilerException("Unhandled model expression type: " + var.GetType()); } }