7using System.Collections.Generic;
13using System.Runtime.Remoting.Messaging;
15using System.Threading.Tasks;
16using System.Windows.Forms.VisualStyles;
28 Dictionary<int, string> m_rgstrValueItems =
new Dictionary<int, string>();
29 Dictionary<int, string> m_rgstrValueItemsByIndex =
new Dictionary<int, string>();
30 DataTable m_rgRawValueCache =
new DataTable();
31 SqlBulkCopy m_sqlBulkCopy =
null;
38 m_rgRawValueCache.Columns.Add(
"SourceID", typeof(
int));
39 m_rgRawValueCache.Columns.Add(
"ItemID", typeof(
int));
40 m_rgRawValueCache.Columns.Add(
"TimeStamp", typeof(DateTime));
41 m_rgRawValueCache.Columns.Add(
"RawData", typeof(
byte[]));
42 m_rgRawValueCache.Columns.Add(
"Active", typeof(
bool));
53 base.Open(nSrcId, nForceLoad, ci);
64 if (m_entitiesTemporal ==
null)
67 m_sqlBulkCopy =
new SqlBulkCopy(m_entitiesTemporal.Database.Connection.ConnectionString, SqlBulkCopyOptions.TableLock);
68 m_sqlBulkCopy.DestinationTableName =
"dbo.RawValues";
69 m_sqlBulkCopy.BulkCopyTimeout = 600;
70 m_sqlBulkCopy.ColumnMappings.Add(
"SourceID",
"SourceID");
71 m_sqlBulkCopy.ColumnMappings.Add(
"ItemID",
"ItemID");
72 m_sqlBulkCopy.ColumnMappings.Add(
"TimeStamp",
"TimeStamp");
73 m_sqlBulkCopy.ColumnMappings.Add(
"RawData",
"RawData");
74 m_sqlBulkCopy.ColumnMappings.Add(
"Active",
"Active");
76 m_entitiesTemporal.Configuration.AutoDetectChangesEnabled =
false;
77 m_entitiesTemporal.Configuration.ValidateOnSaveEnabled =
false;
85 if (m_sqlBulkCopy !=
null)
87 m_sqlBulkCopy.Close();
91 if (m_entitiesTemporal !=
null)
93 m_entitiesTemporal.Dispose();
94 m_entitiesTemporal =
null;
111 if (!base.DeleteSource(nSrcId))
116 strCmd =
"IF OBJECT_ID (N'dbo.ValueItems', N'U') IS NOT NULL DELETE FROM ValueItems WHERE (SourceID = " + nSrcId.ToString() +
")";
117 entities.Database.ExecuteSqlCommand(strCmd);
119 strCmd =
"IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL DELETE FROM RawValues WHERE (SourceID = " + nSrcId.ToString() +
")";
120 entities.Database.ExecuteSqlCommand(strCmd);
135 if (!base.DeleteSourceData(nSrcId))
157 Source srcTraining =
GetSource(ds.TrainingSourceID.GetValueOrDefault());
158 Source srcTesting =
GetSource(ds.TestingSourceID.GetValueOrDefault());
162 base.DeleteDataset(strDsName, bDeleteRelatedProjects, log, evtCancel);
184 entities.Database.CommandTimeout = 180;
186 strCmd =
"DELETE ValueItems WHERE (SourceID = " + nSrcIDTrain.ToString() +
") OR (SourceID = " + nSrcIDTest.ToString() +
")";
187 entities.Database.ExecuteSqlCommand(strCmd);
200 string strCmd =
"IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL DELETE FROM RawValues WHERE (SourceID = " + nSrcId.ToString() +
")";
204 entities.Database.ExecuteSqlCommand(strCmd);
216 string strCmd =
"IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL SELECT COUNT(ID) FROM RawValues WHERE (SourceID = " + nSrcId.ToString() +
") ELSE SELECT 0";
217 Stopwatch sw =
new Stopwatch();
223 entities.Database.CommandTimeout = 180;
224 int lCount = entities.Database.SqlQuery<
int>(strCmd).FirstOrDefault();
226 long lStep = lCount / lBlock;
229 while (lIdx < lCount)
231 strCmd =
"DELETE TOP (" + lBlock.ToString() +
") RawValues WHERE (SourceID = " + nSrcId.ToString() +
")";
232 entities.Database.ExecuteSqlCommand(strCmd);
236 if (sw.Elapsed.TotalMilliseconds > 1000)
241 double dfPct = (double)lIdx / (
double)lCount;
242 log.
WriteLine(
"Deleting RawValues at " + dfPct.ToString(
"P4"));
246 strCmd =
"DELETE RawValues WHERE (SourceID = " + nSrcId.ToString() +
")";
247 entities.Database.ExecuteSqlCommand(strCmd);
262 string strCmd =
"IF OBJECT_ID (N'dbo.ValueItems', N'U') IS NOT NULL DELETE FROM ValueItems WHERE (SourceID = " + nSrcId.ToString() +
")";
266 entities.Database.ExecuteSqlCommand(strCmd);
285 DataRow dr = m_rgRawValueCache.NewRow();
287 dr[
"SourceID"] = nSrcID;
288 dr[
"ItemID"] = nItemID;
291 dr[
"RawData"] = data.
ToBytes();
294 m_rgRawValueCache.Rows.Add(dr);
308 DataRow dr = m_rgRawValueCache.NewRow();
310 dr[
"SourceID"] = nSrcID;
311 dr[
"ItemID"] = nItemID;
312 dr[
"TimeStamp"] = dt;
313 dr[
"RawData"] = data.
ToBytes();
314 dr[
"Active"] = bActive;
316 m_rgRawValueCache.Rows.Add(dr);
324 if (m_rgRawValueCache.Rows.Count > 0)
328 m_sqlBulkCopy.WriteToServer(m_rgRawValueCache);
330 catch (Exception excpt)
346 m_rgRawValueCache.Clear();
359 List<RawValue> rgData = entities.RawValues.AsNoTracking().Where(p => p.SourceID == nSrcID && p.ItemID == nItemID).OrderBy(p => p.TimeStamp).ToList();
382 public int AddValueItem(
int nSrcId,
int nItemIdx,
string strName, DateTime? dtStart =
null, DateTime? dtEnd =
null,
int? nSteps =
null)
386 List<ValueItem> rgItems = entities.ValueItems.Where(p=>p.SourceID == nSrcId && p.Name == strName).ToList();
388 if (rgItems.Count > 0)
389 return rgItems[0].ID;
391 ValueItem item =
new ValueItem();
394 item.SourceID = nSrcId;
395 item.StartTime = dtStart;
396 item.EndTime = dtEnd;
398 entities.ValueItems.Add(item);
399 entities.SaveChanges();
414 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.Name == strName).ToList();
416 if (rgItems.Count > 0)
417 return rgItems[0].ID;
430 if (m_rgstrValueItems.ContainsKey(nID))
431 return m_rgstrValueItems[nID];
435 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.ID == nID).ToList();
437 if (rgItems.Count > 0)
439 m_rgstrValueItems.Add(nID, rgItems[0].Name);
440 m_rgstrValueItemsByIndex.Add(rgItems[0].Idx.GetValueOrDefault(0), rgItems[0].Name);
441 return rgItems[0].Name;
457 return entities.ValueItems.Where(p => p.SourceID == nSrcID).Select(p => p.ID).ToList();
470 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.Name == strName).ToList();
472 if (rgItems.Count > 0)
473 return rgItems[0].Idx.GetValueOrDefault(0);
486 if (m_rgstrValueItemsByIndex.ContainsKey(nIdx))
487 return m_rgstrValueItemsByIndex[nIdx];
491 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.Idx == nIdx).ToList();
493 if (rgItems.Count > 0)
495 m_rgstrValueItemsByIndex.Add(nIdx, rgItems[0].Name);
496 m_rgstrValueItems.Add(rgItems[0].ID, rgItems[0].Name);
497 return rgItems[0].Name;
513 return entities.ValueItems.Where(p => p.SourceID == nSrcID).Select(p => p.Idx.GetValueOrDefault(0)).ToList();
526 return entities.ValueItems.Where(p => p.SourceID == nSrcID).ToList();
532 #region Value Streams
547 public int AddValueStream(
int nSrcID,
string strName,
int nOrdering, STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valType, DateTime? dtStart =
null, DateTime? dtEnd =
null,
int? nSecPerStep =
null,
int nTotalSteps = 1)
551 ValueStream vs =
null;
553 List<ValueStream> rg = entities.ValueStreams.Where(p => p.SourceID == nSrcID && p.Name == strName).ToList();
555 vs =
new ValueStream();
559 vs.SourceID = nSrcID;
561 vs.Ordering = (short)nOrdering;
562 vs.ClassTypeID = (byte)classType;
563 vs.ValueTypeID = (byte)valType;
564 vs.StartTime = dtStart;
566 vs.SecondsPerStep = nSecPerStep;
567 vs.TotalSteps = nTotalSteps;
570 entities.ValueStreams.Add(vs);
572 entities.SaveChanges();
587 return entities.ValueStreams.Where(p => p.SourceID == nSrcID).ToList();
602 List<RawValueDataCollection> m_rgValues =
new List<RawValueDataCollection>();
603 bool m_bSorted =
false;
612 m_nSourceID = nSrcID;
627 foreach (RawValue val
in rg)
640 public void Add(RawValue val)
642 if (!val.TimeStamp.HasValue)
644 if (m_staticValues !=
null)
645 throw new Exception(
"There should only be one static value set for an item!");
662 if (m_rgValues.Count == 0)
665 return m_rgValues.Count;
676 if (m_rgValues.Count == 0)
677 return DateTime.MinValue;
679 return m_rgValues[0].TimeStamp.Value;
690 if (m_rgValues.Count == 0)
691 return DateTime.MinValue;
693 return m_rgValues[m_rgValues.Count - 1].TimeStamp.Value;
702 get {
return m_nSourceID; }
710 get {
return m_nItemID; }
719 List<float> rgNum =
new List<float>();
720 List<float> rgCat =
new List<float>();
724 if (data.
ValueType == STREAM_VALUE_TYPE.NUMERIC)
725 rgNum.Add(data.
Value);
727 rgCat.Add(data.
Value);
730 return new Tuple<float[], float[]>(rgNum.ToArray(), rgCat.ToArray());
741 if (nIdx + nCount >= m_rgValues.Count)
744 List<float> rgNum =
new List<float>();
745 List<float> rgCat =
new List<float>();
746 List<DateTime> rgTime =
new List<DateTime>();
750 m_rgValues = m_rgValues.OrderBy(p => p.TimeStamp).ToList();
754 for (
int i=nIdx; i<nIdx + nCount; i++)
758 if (data.
ClassType == STREAM_CLASS_TYPE.OBSERVED)
760 if (data.
ValueType == STREAM_VALUE_TYPE.NUMERIC)
763 rgCat.Add(data.
Value);
770 return new Tuple<float[], float[], DateTime[]>(rgNum.ToArray(), rgCat.ToArray(), rgTime.ToArray());
782 if (nIdx + nCount >= m_rgValues.Count)
785 List<float> rgNum =
new List<float>();
786 List<DateTime> rgTime =
new List<DateTime>();
790 m_rgValues = m_rgValues.OrderBy(p => p.TimeStamp).ToList();
794 for (
int i = nIdx; i < nIdx + nCount; i++)
799 if (val.
ClassType != STREAM_CLASS_TYPE.OBSERVED && val.
ValueType != STREAM_VALUE_TYPE.NUMERIC)
800 throw new Exception(
"The value at index " + nValIdx.ToString() +
" is not a numeric observed value!");
806 return new Tuple<float[], DateTime[]>(rgNum.ToArray(), rgTime.ToArray());
817 if (nIdx + nCount >= m_rgValues.Count)
820 List<float> rgNum =
new List<float>();
821 List<float> rgCat =
new List<float>();
822 List<DateTime> rgTime =
new List<DateTime>();
826 m_rgValues = m_rgValues.OrderBy(p => p.TimeStamp).ToList();
830 if (nIdx + nCount >= m_rgValues.Count)
833 for (
int i = nIdx; i < nIdx + nCount; i++)
837 if (data.
ClassType == STREAM_CLASS_TYPE.KNOWN)
839 if (data.
ValueType == STREAM_VALUE_TYPE.NUMERIC)
842 rgCat.Add(data.
Value);
849 return new Tuple<float[], float[], DateTime[]>(rgNum.ToArray(), rgCat.ToArray(), rgTime.ToArray());
860 List<DateTime> rgTime =
new List<DateTime>();
862 for (
int i = nIdx; i < nIdx + nCount; i++)
869 return rgTime.ToArray();
879 List<RawValueData> m_rgData =
new List<RawValueData>();
880 Dictionary<int, RawValueData> m_rgDataById =
new Dictionary<int, RawValueData>();
903 get {
return m_rgData.Count; }
913 get {
return m_rgData[nIdx]; }
923 if (m_rgDataById.ContainsKey(data.
StreamID))
926 m_rgDataById.Add(data.
StreamID, data);
939 if (m_rgData.Count != rgData.Length)
940 throw new Exception(
"The number of data items must match the number of raw value data items!");
942 for (
int i = 0; i < rgData.Length; i++)
944 m_rgData[i].Value = rgData[i];
954 public void SetData(DateTime dt,
float[] rgData)
966 using (MemoryStream ms =
new MemoryStream())
967 using (BinaryWriter bw =
new BinaryWriter(ms))
969 bw.Write(m_rgData.Count);
971 for (
int i = 0; i < m_rgData.Count; i++)
973 m_rgData[i].Save(bw);
991 using (MemoryStream ms =
new MemoryStream(rg))
992 using (BinaryReader br =
new BinaryReader(ms))
994 int nCount = br.ReadInt32();
996 for (
int i = 0; i < nCount; i++)
1011 return ((IEnumerable<RawValueData>)m_rgData).GetEnumerator();
1018 IEnumerator IEnumerable.GetEnumerator()
1020 return ((IEnumerable)m_rgData).GetEnumerator();
1031 STREAM_CLASS_TYPE m_classType = STREAM_CLASS_TYPE.STATIC;
1032 STREAM_VALUE_TYPE m_valueType = STREAM_VALUE_TYPE.NUMERIC;
1044 public RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, DateTime? dt,
float fVal,
float? fValNorm =
null)
1047 m_classType = classType;
1048 m_valueType = valueType;
1050 m_fValNorm = fValNorm;
1059 public RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType,
int nStrmId)
1061 m_nStrmID = nStrmId;
1062 m_classType = classType;
1063 m_valueType = valueType;
1073 bw.Write((
byte)m_classType);
1074 bw.Write((
byte)m_valueType);
1077 bw.Write(m_fValNorm.HasValue);
1078 if (m_fValNorm.HasValue)
1079 bw.Write(m_fValNorm.Value);
1090 STREAM_CLASS_TYPE classType = (STREAM_CLASS_TYPE)br.ReadByte();
1091 STREAM_VALUE_TYPE valueType = (STREAM_VALUE_TYPE)br.ReadByte();
1092 float fVal = br.ReadSingle();
1094 float? fValNorm =
null;
1095 if (br.ReadBoolean())
1096 fValNorm = br.ReadSingle();
1098 return new RawValueData(classType, valueType, dt, fVal, fValNorm);
1106 get {
return m_classType; }
1114 get {
return m_valueType; }
1122 get {
return m_nStrmID; }
1130 get {
return m_dt; }
1138 get {
return m_fVal; }
1139 set { m_fVal = value; }
1147 get {
return m_fValNorm; }
1148 set { m_fValNorm = value; }
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
The ConnectInfo class specifies the server, database and username/password used to connect to a datab...
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
The value stream descriptor describes a single value stream within a value item.
The DNNEntities class defines the entities used to connecto the database via Entity Frameworks.
The Database class manages the actual connection to the physical database using Entity Framworks from...
Dataset GetDataset(int nID, ConnectInfo ci=null)
Returns the Dataset entity for a dataset ID.
FORCE_LOAD
Defines the force load type.
Source m_src
Specifies the default data source.
void DeleteSourceData()
Deletes the data source data for the open data source.
Source GetSource(string strName, ConnectInfo ci=null)
Returns the Source entity given a data source name.
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static DNNEntities CreateEntities(ConnectInfo ci=null)
Returns the DNNEntities to use.
The DNNEntities class defines the entities used to connecto the database via Entity Frameworks.
The DatabaseTemporal is used to manage all temporal specific database objects.
override void Open(int nSrcId, FORCE_LOAD nForceLoad=FORCE_LOAD.NONE, ConnectInfo ci=null)
Opens a data source.
override bool DeleteSourceData(int nSrcId=0)
Delete the data source data (images, means, results and parameters) from the database.
string GetValueItemNamByIndex(int nIdx)
Returns the value item name given the value item index if found, or 0.
static bool DeleteRawValuesEx(int nSrcId, Log log, CancelEvent evtCancel)
Delete all RawValues in a data source.
RawValueSet GetValues(int nSrcID, int nItemID)
Load the static value stream categorical values for a given source and item.
void ClearRawValues()
Clear the raw values.
static void DeleteDatasetTemporalTables(int nSrcIDTrain, int nSrcIDTest, Log log, CancelEvent evtCancel)
Delete a dataset temporal tables..
override void DeleteDataset(string strDsName, bool bDeleteRelatedProjects, Log log, CancelEvent evtCancel)
Delete a dataset.
void SaveRawValues()
Save the raw values.
int AddValueStream(int nSrcID, string strName, int nOrdering, STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valType, DateTime? dtStart=null, DateTime? dtEnd=null, int? nSecPerStep=null, int nTotalSteps=1)
Add a new value stream to the source ID.
int AddValueItem(int nSrcId, int nItemIdx, string strName, DateTime? dtStart=null, DateTime? dtEnd=null, int? nSteps=null)
Add a new value item to the database.
List< ValueStream > GetAllValueStreams(int nSrcID)
Returns a list of all value streams associated with a SourceID.
void PutRawValue(int nSrcID, int nItemID, DateTime dt, RawValueDataCollection data, bool bActive)
Add raw values for a data stream.
override bool DeleteSource(int nSrcId=0)
Delete a data source from the database.
List< ValueItem > GetAllValueItems(int nSrcID)
Returns a list of all value items associated with a SourceID.
List< int > GetAllItemIndices(int nSrcID)
Returns a list of all value item Indices associated with a SourceID.
List< int > GetAllItemIDs(int nSrcID)
Returns a list of all value item IDs associated with a SourceID.
int GetValueItemIndex(string strName)
Returns the value item ID given the value item name if found, or 0.
void DeleteRawValues(int nSrcId=0)
Delete all RawValues in a data source.
string GetValueItemName(int nID)
Returns the value item ID given the value item name if found, or 0.
DatabaseTemporal()
The constructor.
override void Close()
Close the current data source.
void EnableBulk(bool bEnable)
Enable bulk inserts.
void DeleteValueItems(int nSrcId=0)
Delete all ValueItems in a data source.
int GetValueItemID(string strName)
Returns the value item ID given the value item name if found, or 0.
void PutRawValue(int nSrcID, int nItemID, RawValueDataCollection data)
Add static raw values for a data stream.
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static new DNNEntitiesTemporal CreateEntities(ConnectInfo ci=null)
Returns the DNNEntitiesTemporal to use.
The RawValueDataCollection class is used to hold a collection of RawValueData items.
IEnumerator< RawValueData > GetEnumerator()
Returns an enumerator for the raw value data items.
void SetData(float[] rgData)
Set the data values in the collection with the data values provided.
void SetData(DateTime dt, float[] rgData)
Set the data values in the collection with the data values provided.
DateTime? TimeStamp
Returns the time stamp of the raw value data items.
static RawValueDataCollection LoadFromBytes(DateTime? dt, byte[] rg)
Loads a raw value data collection from a byte array.
int Count
Returns the number of raw value items.
bool Add(RawValueData data)
Manually add a new raw value data item.
byte[] ToBytes()
Converts the raw value data items to a byte aray.
RawValueDataCollection(DateTime? dt)
The constructor.
The RawValueData class contains a single raw value item.
DateTime? TimeStamp
Returns the time of the data point or null if static data.
RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, DateTime? dt, float fVal, float? fValNorm=null)
The constructor.
int StreamID
Returns the stream ID.
float Value
Returns the raw value item data.
static RawValueData Load(DateTime? dt, BinaryReader br)
Load a raw value item from a binary reader.
STREAM_VALUE_TYPE ValueType
Returns the raw value item value type.
RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, int nStrmId)
The constructor.
float? ValueNormalized
Returns the raw value item normalized data if it exists.
void Save(BinaryWriter bw)
Save the raw value item to a binary writer.
STREAM_CLASS_TYPE ClassType
Returns the raw value item class.
The RawValueData class is used to hold the data values for an ItemID.
RawValueSet(int nSrcID, int nItemID)
The constructor.
static RawValueSet FromData(int nSrcID, int nItemID, List< RawValue > rg)
Creates a RawValueSet from a list of RawValues.
void Add(RawValue val)
Add a raw value to the set.
Tuple< float[], float[]> GetStaticValues()
Return the static values.
Tuple< float[], float[], DateTime[]> GetObservedValues(int nIdx, int nCount)
Returns the observed values.
DateTime StartTime
Returns the start time of the data values.
Tuple< float[], DateTime[]> GetObservedNumValues(int nIdx, int nCount, int nValIdx)
Returns the observed values.
Tuple< float[], float[], DateTime[]> GetKnownValues(int nIdx, int nCount)
Returns the observed values.
int ColCount
Returns the row count of observed and known items.
DateTime[] GetTimeSyncValues(int nIdx, int nCount)
Returns the time sync values.
DateTime EndTime
Returns the end time of the data values.
int SourceID
Returns the source ID.
int ItemID
Returns the item ID.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.db.image namespace contains all image database related classes.
The MyCaffe.db.temporal namespace contains all classes used to create the MyCaffeTemporalDatabase in-...
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...