/// <summary> /// Inserts a new value in this context's object pool. See <see cref="InstancePoolTryGetValue"/>. /// </summary> /// <param name="value"></param> public void RegisterValue(LabeledInstance value) { ObjectId label = value.Label; Symbol sort = label.ObjectSort; Dictionary <ObjectId, LabeledInstance> objPool; if (pool.TryGetValue(sort, out objPool)) { if (objPool.ContainsKey(label)) { throw new InvalidOperationException("Instance with label " + label.ToString() + " already exists in this context; use static Create() method to construct instead of operator new."); } else { objPool.Add(label, value); } } else { objPool = new Dictionary <ObjectId, LabeledInstance>(); objPool.Add(label, value); pool.Add(sort, objPool); } }
public bool InstancePoolTryGetValue(ObjectId label, out LabeledInstance /*?*/ value) { Dictionary <ObjectId, LabeledInstance> objPool; value = null; return(pool.TryGetValue(label.ObjectSort, out objPool) && objPool.TryGetValue(label, out value)); }
/// <summary> /// Equality of labeled instances by object id /// </summary> public override bool Equals(object /*?*/ obj) { LabeledInstance other = obj as LabeledInstance; if ((object)other == null) { return(false); } return(Object.Equals(this.objectId, other.objectId)); }
public Field(FieldInfo field) { // to do: ensure that context is fresh in LabeledInstanceBase, or implement some other type sort // mechanism. The problem is that sort is defined only within the context of a particular model program. Type t = field.GetType(); Symbol sort = LabeledInstance.TypeSort(t); this.stateVariable = new StateVariable(GetStateVariableName(field), sort); this.field = field; }
internal static ParameterGenerator[] GetInputParameterGenerators(MethodInfo methodInfo) { ParameterInfo[] parameterInfos = methodInfo.GetParameters(); int nParameters = parameterInfos.Length; ParameterGenerator /*?*/[] result = new ParameterGenerator[nParameters]; for (int i = 0; i < nParameters; i += 1) { ParameterInfo pInfo = parameterInfos[i]; Type pType = pInfo.ParameterType; if (pType.IsByRef) { pType = pType.GetElementType(); } ParameterGenerator /*?*/ parameterGenerator = null; if (ReflectionHelper.IsInputParameter(pInfo)) { object /*?*/[] /*?*/ attrs1 = pInfo.GetCustomAttributes(typeof(DomainAttribute), true); // object/*?*/[]/*?*/ attrs2 = pInfo.GetCustomAttributes(typeof(_Attribute), true); // object/*?*/[]/*?*/ attrs3 = pInfo.GetCustomAttributes(typeof(NewAttribute), true); bool hasDomainAttr = (attrs1 != null && attrs1.Length > 0); if (hasDomainAttr) { DomainAttribute attr = (DomainAttribute)attrs1[0]; string attrName = attr.Name; if (string.Equals("new", attrName)) { Symbol sort = AbstractValue.TypeSort(pType); parameterGenerator = delegate() { return(new Set <Term>(LabeledInstance.PeekNextLabelTerm(sort))); }; } else { parameterGenerator = CreateDomainParameterGenerator(methodInfo, attr, pType); } } else { parameterGenerator = Method.GetDefaultParameterGenerator(pType); } } result[i] = parameterGenerator; } return(result); }
///// <summary> ///// The operator == for labeled instances is the same as Object.Equals ///// </summary> //public static bool operator ==(LabeledInstance/*?*/ o1, LabeledInstance/*?*/ o2) //{ // return Object.Equals(o1, o2); //} ///// <summary> ///// The operator != for labeled instances is the same as !Object.Equals ///// </summary> //public static bool operator !=(LabeledInstance/*?*/ o1, LabeledInstance/*?*/ o2) //{ // return !Object.Equals(o1, o2); //} ///// <summary> ///// o1 is less than o2 if o1.CompareTo(o2) == -1 ///// </summary> //public static bool operator <(LabeledInstance/*?*/ o1, LabeledInstance/*?*/ o2) ////^ ensures result == 0 ==> Object.Equals(o1, o2); //{ // return o1 == null ? ((object)o2 != null) : (o1.CompareTo(o2) == -1); //} ///// <summary> ///// o1 is greater than o2 if o2 is less than o1 ///// </summary> //public static bool operator >(LabeledInstance/*?*/ o1, LabeledInstance/*?*/ o2) ////^ ensures result == 0 ==> Object.Equals(o1, o2); //{ // return o1 != null && (o1.CompareTo(o2) == 1); //} ///// <summary> ///// o1 is less than or equal to o2 if o1.CompareTo(o2) is less than 1 ///// </summary> //public static bool operator <=(LabeledInstance/*?*/ o1, LabeledInstance/*?*/ o2) ////^ ensures result == 0 ==> Object.Equals(o1, o2); //{ // return o1 == null || (o1.CompareTo(o2) < 1); //} ///// <summary> ///// o1 is greater than or equal to o2 if o2 is less than or equal to o1 ///// </summary> //public static bool operator >=(LabeledInstance/*?*/ o1, LabeledInstance/*?*/ o2) ////^ ensures result == 0 ==> Object.Equals(o1, o2); //{ // return o1 == null ? ((object)o2 == null) : (o1.CompareTo(o2) > -1); //} /// <summary> /// Compares this labeled instance to a given object (null ius the minimal element) /// Throws an ArgumentException if the argument is not null and not a labaled instance /// </summary> public override int CompareTo(object /*?*/ obj) //^ requires obj != null ==> obj is IComparable; //^ ensures result == 0 <==> Object.Equals(this, obj); //^ ensures result == -1 || result == 0 || result == 1; { if (obj == null) { return(1); // null is the minimal element } LabeledInstance other = obj as LabeledInstance; if ((object)other == null) { throw new ArgumentException(MessageStrings.LocalizedFormat(MessageStrings.LabeledInstanceRequired, obj.GetType().ToString())); } return(this.objectId.CompareTo(other.objectId)); }
internal static ParameterGenerator /*?*/ GetThisParameterGenerator(MethodInfo methodInfo) { if (!methodInfo.IsStatic) { object[] attrs = methodInfo.GetCustomAttributes(typeof(DomainAttribute), true); if (attrs == null || attrs.Length == 0) { // if no Domain attr at method level, look at the class level attrs = methodInfo.DeclaringType.GetCustomAttributes(typeof(DomainAttribute), true); } if (attrs != null && attrs.Length > 0) { DomainAttribute attr = (DomainAttribute)attrs[0]; string attrName = attr.Name; ParameterGenerator parameterGenerator; if (string.Equals("new", attrName)) { Symbol sort = AbstractValue.TypeSort(methodInfo.DeclaringType); parameterGenerator = delegate() { return(new Set <Term>(LabeledInstance.PeekNextLabelTerm(sort))); }; } else { parameterGenerator = CreateDomainParameterGenerator(methodInfo, attr, methodInfo.DeclaringType); } return(parameterGenerator); } } else // if attribute applied to static method { object[] attrs = methodInfo.GetCustomAttributes(typeof(DomainAttribute), true); if (attrs != null && attrs.Length > 0) { throw new ModelProgramUserException("Domain attribute cannot be applied to a static method"); } } return(null); // otherwise compiler complains }
/// <summary> /// Inserts a new value in this context's object pool. See <see cref="InstancePoolTryGetValue"/>. /// </summary> /// <param name="value"></param> public void RegisterValue(LabeledInstance value) { ObjectId label = value.Label; Symbol sort = label.ObjectSort; Dictionary<ObjectId, LabeledInstance> objPool; if (pool.TryGetValue(sort, out objPool)) { if (objPool.ContainsKey(label)) throw new InvalidOperationException("Instance with label " + label.ToString() + " already exists in this context; use static Create() method to construct instead of operator new."); else objPool.Add(label, value); } else { objPool = new Dictionary<ObjectId, LabeledInstance>(); objPool.Add(label, value); pool.Add(sort, objPool); } }
public bool InstancePoolTryGetValue(ObjectId label, out LabeledInstance/*?*/ value) { Dictionary<ObjectId, LabeledInstance> objPool; value = null; return pool.TryGetValue(label.ObjectSort, out objPool) && objPool.TryGetValue(label, out value); }
public static int CompareValues(object /*?*/ value1, object /*?*/ value2) //^ ensures result == 0 <==> Object.Equals(value1, value2); //^ ensures result == -1 || result == 0 || result == 1; { // Case 1: objects are pointer-equal if ((object)value1 == value2) { return(0); } // Case 2: one of the values is null: treat null as the minimal element bool isNull1 = (value1 == null); bool isNull2 = (value2 == null); if (isNull1 && !isNull2) { return(-1); } if (!isNull1 && isNull2) { return(1); } // Case 3: both of the values are instances of classes with user-provided labeling: use labels to compare LabeledInstance i1 = value1 as LabeledInstance; if ((object)i1 != null && value2 is LabeledInstance) { return(i1.CompareTo(value2)); } //// TODO: factor LabeledIntance and EnumeratedInstance to share a common base class //EnumeratedInstance e1 = value1 as EnumeratedInstance; //if ((object)e1 != null && value2 is EnumeratedInstance) // return e1.CompareTo(value2); // Case 5: objects are of unequal types: do type compare in dictionary order Type t1 = value1.GetType(); Type t2 = value2.GetType(); if (t1 != t2) { return(t1.ToString().CompareTo(t2.ToString())); } // Case 6: objects are of the same type and implement IComparable; use IComparable to order IComparable comparable = value1 as IComparable; if ((object)comparable != null) { return(comparable.CompareTo(value2)); } // Case 7: objects are types; use string compare of type names if (value1 is Type) { return(value1.ToString().CompareTo(value2.ToString())); } // Case 8: objects are of the same type, do not support IComparable but are Equal but not reference equal // This is not supported. There is no way to order instances of a class that overrides Equals but // does not also implement IComparable. if (Object.Equals(value1, value2)) { throw new ArgumentException("Cannot apply value comparision to value of type that overrides Object.Equals but does not implement IComparable."); } // Case 9 (default): use a table to order objects. This is introduces a memory leak if the user // does not explicitly call UninternObject, but it acts as an ordering of last resort // for types that do not override Equals and GetHashCode. It can be a source of hidden internal nondeterminism. // It is recommended that this feature not be used. int int1 = InternObject(value1); int int2 = InternObject(value2); return(int1.CompareTo(int2)); }