/// <summary> /// return the category for a otDataType ID /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static otDataTypeCategory GetCategory(otDataType typeId) { // extract nullable if ((typeId & otDataType.IsNullable) == otDataType.IsNullable) { typeId ^= otDataType.IsNullable; } switch (typeId) { // primitives case otDataType.Bool: case otDataType.Binary: case otDataType.Date: case otDataType.Decimal: case otDataType.Memo: case otDataType.Number: case otDataType.Null: case otDataType.Text: case otDataType.Timespan: case otDataType.Timestamp: return(otDataTypeCategory.Primitive); // complex case otDataType.DecimalUnit: case otDataType.LanguageText: case otDataType.Symbol: return(otDataTypeCategory.Composite); // data structure case otDataType.DataObject: case otDataType.List: return(otDataTypeCategory.DataStructure); // not found default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "DataType.GetCategory" }); } }
/// <summary> /// returns a default value for the OnTrack Datatypes /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static object GetDefaultValue(otDataType typeId) { switch (GetCategory(typeId)) { case otDataTypeCategory.Primitive: return(Rulez.PrimitiveType.GetDefaultValue(typeId)); case otDataTypeCategory.Composite: return(Rulez.CompositeType.GetDefaultValue(typeId)); case otDataTypeCategory.DataStructure: return(Rulez.DataStructureType.GetDefaultValue(typeId)); default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByClass, arguments: new object[] { typeId.ToString(), "DataType.GetDefaultValue" }); } }
/// <summary> /// converts a value to an representing value of the outvalue /// </summary> /// <param name="value"></param> /// <param name="outvalue"></param> /// <param name="typeId"></param> /// <returns></returns> public static object To(object value, otDataType typeId) { switch (GetCategory(typeId)) { case otDataTypeCategory.Primitive: return(Rulez.PrimitiveType.To(value, typeId)); default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByClass, arguments: new object[] { typeId.ToString(), "DataType.To" }); } }
/// <summary> /// constructor /// </summary private PrimitiveType(otDataType typeId, bool isNullable = false) : base(typeId, isNullable, defaultvalue:GetDefaultValue(typeId), name: null, engine:null) { // check on type if ((uint) typeId > PrimitiveTypeMaxRange) throw new RulezException(RulezException.Types.DataTypeNotImplementedByClass,arguments: new object[] {typeId.ToString (), "ValueType"}); // no event for Primitives }
/// <summary> /// returns a default value for the OnTrack Datatypes /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new object GetDefaultValue(otDataType typeId) { // if nullable than return the null if ((typeId & otDataType.IsNullable ) == otDataType.IsNullable) return null; // make a case switch (typeId) { case otDataType.Null: return null; case otDataType.Bool: return false; case otDataType.Date: return DateTime.Parse(ConstNullTimestampString).Date; /*case otDataType.List: /// To do implement inner Type or accept Object() List<string> aValue = new List<string>(); return aValue.ToArray();*/ case otDataType.Number: return (long)0; case otDataType.Memo: return string.Empty; case otDataType.Decimal: return (double)0; case otDataType.Text: return string.Empty; case otDataType.Timespan: return new TimeSpan(); case otDataType.Timestamp: return DateTime.Parse(DataType.ConstNullTimestampString); case otDataType.Binary: return new byte[]{0x00}; default: if ((uint)typeId < PrimitiveTypeMaxRange) throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.DataType.GetTypeFor" }); else throw new RulezException(RulezException.Types.DataTypeNotImplementedByClass, arguments: new object[] { typeId.ToString(), "PrimitiveType" }); } }
/// <summary> /// Get native datatype /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new System.Type GetNativeType(otDataType typeId) { switch (typeId) { case otDataType.List: return typeof(List<>); case otDataType.DataObject: return typeof(iDataObject); default: if (typeId != otDataType.List) throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Rulez.StructuredType.GetTypeFor" }); else throw new RulezException(RulezException.Types.DataTypeNotImplementedByClass, arguments: new object[] { typeId.ToString(), "Structured" }); } }
/// <summary> /// returns a complex type of typeId if possible to make one /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static IDataType GetStructuredType(otDataType typeId) { // strip off if ((typeId & otDataType.IsNullable) == otDataType.IsNullable) typeId ^= otDataType.IsNullable ; switch (typeId) { case otDataType.List: return ListType.GetDataType (engine: Rules.Engine); case otDataType.DataObject: // not implementable default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.StructuredType.GetStructuredType" }); } }
/// <summary> /// returns the best fit System.Type for a OnTrack Datatype /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new System.Type GetNativeType(otDataType typeId) { // strip off if ((typeId & otDataType.IsNullable) == otDataType.IsNullable) typeId ^= otDataType.IsNullable; switch (typeId) { case otDataType.Symbol: return typeof(string); case otDataType.DecimalUnit: return typeof(DecimalUnitType.DecimalUnit); case otDataType.LanguageText: return typeof(LanguageTextType.LanguageText); case otDataType.Tuple: return typeof(TupleType.Tuple); default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.DataType.GetNativeType" }); } }
/// <summary> /// returns a default value for the OnTrack Datatypes /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new object GetDefaultValue(otDataType typeId) { // if nullable than return the null if ((typeId & otDataType.IsNullable) == otDataType.IsNullable) return null; switch (typeId) { case otDataType.List: return new List<string>(); case otDataType.DataObject: // return String.Empty; default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.StructuredType.GetDefaultValue" }); } }
/// <summary> /// returns a default value for the OnTrack Datatypes /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new object GetDefaultValue(otDataType typeId) { // if nullable than return the null if ((typeId & otDataType.IsNullable) == otDataType.IsNullable) return null; switch (typeId) { case otDataType.Symbol: return String.Empty; case otDataType.DecimalUnit: return new DecimalUnitType.DecimalUnit (); case otDataType.LanguageText: return new LanguageTextType.LanguageText(); case otDataType.Tuple: return new TupleType.Tuple(); default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.DataType.GetDefaultValue" }); } }
/// <summary> /// converts a value to an representing value of the outvalue /// </summary> /// <param name="value"></param> /// <param name="outvalue"></param> /// <param name="typeId"></param> /// <returns></returns> public static new object To(object value, otDataType typeId) { switch (typeId) { case otDataType.Null: return null; case otDataType.Bool: return ToBool(value); case otDataType.Date: return ToDate(value); case otDataType.List: return ToList(value); case otDataType.Number: return ToNumber(value); case otDataType.Memo: return ToMemo(value); case otDataType.Decimal: return ToDecimal(value); case otDataType.Text: return ToText(value); case otDataType.Timespan: return ToTimespan(value); case otDataType.Timestamp: return ToTimeStamp(value); case otDataType.Binary: return ToBinary(value); default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "PrimitiveType.To" }); } }
/// <summary> /// returns a composite type of typeId if possible to make one /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new IDataType GetCompositeType(otDataType typeId) { switch (typeId) { case otDataType.Symbol: return SymbolType.GetDataType(engine: Rules.Engine); case otDataType.DecimalUnit: // return DecimalUnitType.GetDataType(unit: unit,engine: Rules.Engine); case otDataType.LanguageText: // return LanguageTextType.GetDataType (cultural: cultural, engine: Rules.Engine); case otDataType.Tuple: // not possible without a name default: throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.CompositeType.GetCompositeType" }); } }
/// <summary> /// get the Value Type Data Type object /// </summary> /// <param name="typeId"></param> /// <param name="isNullable"></param> /// <returns></returns> public static PrimitiveType GetPrimitiveType(otDataType typeId, bool isNullable = false) { typeId = isNullable ? typeId | otDataType.IsNullable : typeId; // check if in store if (!_primitives .ContainsKey ((uint)typeId)) throw new RulezException(RulezException.Types.DataTypeNotImplementedByClass, arguments: new object[] { typeId.ToString(), "ValueType" }); // return return _primitives[(uint)typeId]; }
/// <summary> /// returns the best fit System.Type for a OnTrack Datatype /// </summary> /// <param name="typeId"></param> /// <returns></returns> public static new System.Type GetNativeType(otDataType typeId) { switch (typeId) { case otDataType.@Null: return typeof(void); case otDataType.Date: return typeof(DateTime); case otDataType.Bool: return typeof(bool); case otDataType.Number: return typeof(long); case otDataType.Memo: return typeof(string); case otDataType.Text: return typeof(string); case otDataType.Timespan: return typeof(TimeSpan); case otDataType.Decimal: return typeof(double); case otDataType.Timestamp: return typeof(DateTime); case otDataType.Binary: return typeof(byte[]); default: if ((uint)typeId < PrimitiveTypeMaxRange) throw new Rulez.RulezException(Rulez.RulezException.Types.DataTypeNotImplementedByCase, arguments: new object[] { typeId.ToString(), "Core.DataType.GetTypeFor" }); else throw new RulezException(RulezException.Types.DataTypeNotImplementedByClass, arguments: new object[] { typeId.ToString(), "ValueType" }); } }