Commit a4f88c0f authored by Aleksandr Bakhshiev's avatar Aleksandr Bakhshiev
Browse files

UEnvironment: Added data sources

--HG--
branch : DataSrcs
parent 98454eed0c21
......@@ -21,6 +21,11 @@ See file license.txt for more information
namespace RDK {
UDataSource::UDataSource(UEnvironment *env)
: Env(env), LastReadTime(0.0), LastWriteTime(0.0)
{
}
// --------------------------
// Constructors & destructors
// --------------------------
......@@ -179,67 +184,6 @@ bool UEnvironment::SetUseIndTimeStepFlag(bool value)
Model->ChangeUseIndTimeStepMode(value);
return true;
}
/*
///
bool UEnvironment::GetDebugMode(void) const
{
return Logger.GetDebugMode();
}
bool UEnvironment::SetDebugMode(bool value)
{
return Logger.SetDebugMode();
}
///
unsigned int UEnvironment::GetDebugSysEventsMask(void) const
{
return DebugSysEventsMask;
}
bool UEnvironment::SetDebugSysEventsMask(unsigned int value)
{
if(DebugSysEventsMask == value)
return true;
DebugSysEventsMask=value;
return true;
}
///
bool UEnvironment::GetDebuggerMessageFlag(void) const
{
return DebuggerMessageFlag;
}
///
bool UEnvironment::SetDebuggerMessageFlag(bool value)
{
if(DebuggerMessageFlag == value)
return true;
DebuggerMessageFlag=value;
return true;
}
/// -
/// true -
bool UEnvironment::GetEventsLogMode(void) const
{
UGenericMutexExclusiveLocker lock(LogMutex);
return EventsLogMode;
}
bool UEnvironment::SetEventsLogMode(bool value)
{
UGenericMutexExclusiveLocker lock(LogMutex);
EventsLogMode=value;
return true;
} */
// --------------------------
......@@ -641,6 +585,76 @@ bool UEnvironment::CallSourceController(void)
// --------------------------
// --------------------------
//
// --------------------------
/// ( MDMatrix)
bool UEnvironment::RegisterDataSource(const std::string &component_name, const std::string &property_name)
{
for(size_t i=0;i<DataSources.size();i++)
if(DataSources[i] && DataSources[i]->ComponentName == component_name && DataSources[i]->PropertyName == property_name)
return true;
if(!Model)
return false;
UContainer *cont=Model->GetComponentL(component_name);
if(!cont)
return false;
UEPtr<UIProperty> prop=cont->FindProperty(property_name);
if(!prop)
return false;
if(prop->GetLanguageType() == typeid(MDMatrix<double>))
{
UDataSource *data=new UDataSourceT<double>(this);
if(!data->Configure(component_name,property_name))
{
delete data;
return false;
}
DataSources.push_back(data);
return true;
}
else
if(prop->GetLanguageType() == typeid(MDMatrix<double>))
{
UDataSource *data=new UDataSourceT<int>(this);
if(!data->Configure(component_name,property_name))
{
delete data;
return false;
}
DataSources.push_back(data);
return true;
}
return false;
}
/// ( MDMatrix)
void UEnvironment::UnRegisterDataSource(const std::string &component_name, const std::string &property_name)
{
for(size_t i=0;i<DataSources.size();i++)
{
if(DataSources[i] && DataSources[i]->ComponentName == component_name && DataSources[i]->PropertyName == property_name)
{
delete DataSources[i];
DataSources.erase(DataSources.begin()+i);
return;
}
}
}
/// ( MDMatrix)
void UEnvironment::UnRegisterAllDataSources(void)
{
for(size_t i=0;i<DataSources.size();i++)
delete DataSources[i];
DataSources.clear();
}
// --------------------------
// --------------------------
//
// --------------------------
......@@ -784,6 +798,8 @@ void UEnvironment::RTCalculate(void)
while(curtime-CurrentTime<timer_interval && i<elapsed_counter)
{
Calculate();
for(size_t i=0;i<DataSources.size();i++)
DataSources[i]->UpdateData();
++i;
curtime=GetCurrentStartupTime();
......@@ -864,6 +880,8 @@ void UEnvironment::FastCalculate(double calc_interval)
while(i<elapsed_counter)
{
Calculate();
for(size_t i=0;i<DataSources.size();i++)
DataSources[i]->UpdateData();
++i;
if(MaxCalcTime>0.0 && Time.GetDoubleTime()>=MaxCalcTime)
......@@ -887,337 +905,6 @@ void UEnvironment::FastCalculate(double calc_interval)
}
// --------------------------
// --------------------------
//
// --------------------------
/*
//
void UEnvironment::ProcessException(UException &exception) const
{
UGenericMutexExclusiveLocker lock(LogMutex);
UException* processed_exception=&exception;
UException temp_ex;
if(ExceptionPreprocessor)
{
if(ExceptionPreprocessor(const_cast<UEnvironment*>(this),Model, exception,temp_ex))
processed_exception=&temp_ex;
}
if(LastErrorLevel>processed_exception->GetType())
LastErrorLevel=processed_exception->GetType();
++CurrentExceptionsLogSize;
if(CurrentExceptionsLogSize > MaxExceptionsLogSize)
{
int erase_size=CurrentExceptionsLogSize - MaxExceptionsLogSize-1;
if(int(LogList.size())>erase_size)
{
for(int i=0;i<erase_size;i++)
LogList.erase(LogList.begin());
}
else
LogList.clear();
}
UException log(*processed_exception);
log.SetMessage(sntoa(ChannelIndex)+std::string("> ")+processed_exception->what());
LogList[LogIndex++]=log;
if(EventsLogMode) // ,
{
Logger.LogMessage(log.GetMessage()); // TODO: RDK_SUCCESS
}
if(DebuggerMessageFlag)
RdkDebuggerMessage(log.GetMessage());
if(ExceptionPostprocessor)
ExceptionPostprocessor(const_cast<UEnvironment*>(this),Model, *processed_exception); // TODO:
if(ExceptionHandler)
ExceptionHandler(ChannelIndex);
}
//
const char* UEnvironment::GetLog(int &error_level) const
{
UGenericMutexSharedLocker lock(LogMutex);
TempString.clear();
std::map<unsigned, UException >::const_iterator I,J;
I=LogList.begin(); J=LogList.end();
for(;I != J;++I)
{
TempString+=I->second.GetMessage();
TempString+="/r/n";
}
error_level=LastErrorLevel;
LastErrorLevel=INT_MAX;
return TempString.c_str();
}
///
int UEnvironment::GetNumLogLines(void) const
{
UGenericMutexSharedLocker lock(LogMutex);
return int(LogList.size());
}
/// i
const char* UEnvironment::GetLogLine(int i, int &error_level, int &number, time_t &time) const
{
UGenericMutexSharedLocker lock(LogMutex);
std::map<unsigned, UException >::const_iterator I=LogList.find(i);
if(I == LogList.end())
{
TempString.clear();
return TempString.c_str();
}
TempString=I->second.GetMessage();
error_level=I->second.GetType();
number=I->second.GetNumber();
time=I->second.GetTime();
return TempString.c_str();
}
///
int UEnvironment::GetNumUnreadLogLines(void) const
{
UGenericMutexSharedLocker lock(LogMutex);
std::map<unsigned, UException >::const_iterator I=LogList.find(LastReadExceptionLogIndex);
if(I == LogList.end())
return int(LogList.size());
int size=0;
for(;I!=LogList.end();++I)
++size;
return size;
}
//
//
const char* UEnvironment::GetUnreadLog(int &error_level, int &number, time_t &time)
{
UGenericMutexSharedLocker lock(LogMutex);
TempString.clear();
error_level=INT_MAX;
number=0;
time=time_t();
if(LogList.empty())
return TempString.c_str();
if(LastReadExceptionLogIndex == 0)
{
LastReadExceptionLogIndex=LogList.begin()->first;
TempString=LogList.begin()->second.GetMessage();
error_level=LogList.begin()->second.GetType();
number=LogList.begin()->second.GetNumber();
time=LogList.begin()->second.GetTime();
}
else
{
std::map<unsigned, UException >::const_iterator I=LogList.find(LastReadExceptionLogIndex);
if(I != LogList.end())
{
++I;
if(I != LogList.end())
{
LastReadExceptionLogIndex=I->first;
TempString=I->second.GetMessage();
error_level=I->second.GetType();
number=I->second.GetNumber();
time=I->second.GetTime();
return TempString.c_str();
}
}
else
LastReadExceptionLogIndex=LogList.begin()->first;
}
error_level=RDK_EX_UNKNOWN;
return TempString.c_str();
}
// -
UEnvironment::PExceptionHandler UEnvironment::GetExceptionHandler(void) const
{
return ExceptionHandler;
}
bool UEnvironment::SetExceptionHandler(PExceptionHandler value)
{
if(ExceptionHandler == value)
return true;
ExceptionHandler=value;
return true;
}
// -
UEnvironment::PExceptionPreprocessor UEnvironment::GetExceptionPreprocessor(void) const
{
return ExceptionPreprocessor;
}
bool UEnvironment::SetExceptionPreprocessor(PExceptionPreprocessor value)
{
if(ExceptionPreprocessor == value)
return true;
ExceptionPreprocessor=value;
return true;
}
/// -
UEnvironment::PExceptionPostprocessor UEnvironment::GetExceptionPostprocessor(void) const
{
return ExceptionPostprocessor;
}
bool UEnvironment::SetExceptionPostprocessor(PExceptionPostprocessor value)
{
if(ExceptionPostprocessor == value)
return true;
ExceptionPostprocessor=value;
return true;
}
//
// 0,
int UEnvironment::GetMaxExceptionsLogSize(void) const
{
UGenericMutexSharedLocker lock(LogMutex);
return MaxExceptionsLogSize;
}
void UEnvironment::SetMaxExceptionsLogSize(int value)
{
UGenericMutexExclusiveLocker lock(LogMutex);
if(MaxExceptionsLogSize == value)
return;
MaxExceptionsLogSize=value;
if(MaxExceptionsLogSize>0 && CurrentExceptionsLogSize>MaxExceptionsLogSize)
{
//ExceptionsLog.erase(ExceptionsLog.begin(), ExceptionsLog.begin()+int(ExceptionsLog.size())-MaxExceptionsLogSize);
CurrentExceptionsLogSize=MaxExceptionsLogSize;
}
}
///
void UEnvironment::ClearLog(void)
{
UGenericMutexExclusiveLocker lock(LogMutex);
LastReadExceptionLogIndex=0;
CurrentExceptionsLogSize=0;
LastErrorLevel=INT_MAX;
LogList.clear();
LogIndex=1;
}
///
void UEnvironment::ClearReadLog(void)
{
UGenericMutexExclusiveLocker lock(LogMutex);
std::map<unsigned, UException >::iterator I=LogList.find(LastReadExceptionLogIndex);
if(I != LogList.end())
{
++I;
std::map<unsigned, UException >::iterator J=LogList.begin(),K;
while(J != I)
{
K=J; ++K;
LogList.erase(J);
J=K;
}
}
if(!LogList.empty())
LastReadExceptionLogIndex=LogList.begin()->first;
else
LastReadExceptionLogIndex=0;
CurrentExceptionsLogSize=LogList.size();
LastErrorLevel=INT_MAX;
// TempLogString.clear();
}
//
void UEnvironment::LogMessage(int msg_level, const std::string &line, int error_event_number)
{
LogMessageEx(msg_level, "", line, error_event_number);
}
void UEnvironment::LogMessage(int msg_level, const std::string &method_name, const std::string &line, int error_event_number)
{
LogMessageEx(msg_level, "", method_name, line, error_event_number);
}
void UEnvironment::LogMessageEx(int msg_level, const std::string &object_name, const std::string &line, int error_event_number)
{
switch (msg_level)
{
case RDK_EX_FATAL:
{
EStringFatal exception(line,error_event_number);
}
break;
case RDK_EX_ERROR:
{
EStringError exception(line,error_event_number);
exception.SetObjectName(object_name);
ProcessException(exception);
}
break;
case RDK_EX_WARNING:
{
EStringWarning exception(line,error_event_number);
exception.SetObjectName(object_name);
ProcessException(exception);
}
break;
case RDK_EX_INFO:
{
EStringInfo exception(line,error_event_number);
exception.SetObjectName(object_name);
ProcessException(exception);
}
break;
case RDK_EX_DEBUG:
{
if(DebugMode)
{
EStringDebug exception(line,error_event_number);
exception.SetObjectName(object_name);
ProcessException(exception);
}
}
break;
case RDK_EX_APP:
{
EStringApp exception(line,error_event_number);
exception.SetObjectName(object_name);
ProcessException(exception);
}
break;
}
}
void UEnvironment::LogMessageEx(int msg_level, const std::string &object_name, const std::string &method_name, const std::string &line, int error_event_number)
{
LogMessageEx(msg_level, object_name, method_name+std::string(" - ")+line, error_event_number);
} */
// --------------------------
// --------------------------
//
......@@ -1321,16 +1008,13 @@ bool UEnvironment::AReset(void)
ProcEndTime=StartupTime;
LastDuration=1;
LastStepStartTime=0;
// LastErrorLevel=INT_MAX;
if(Logger)
Logger->Reset();
RTModelCalcTime=0;
// if(EventsLogMode)
// {
// Logger.Clear();
// Logger.InitLog();
// }
for(size_t i=0;i<DataSources.size();i++)
DataSources[i]->ClearData();
if(!Model)
return true;
......@@ -1399,6 +1083,9 @@ bool UEnvironment::ACalculate(void)
return false;
}
for(size_t i=0;i<DataSources.size();i++)
DataSources[i]->UpdateData();
// , ,
// ,
if(ModelCalculationComponent.GetSize() == 0)
......
......@@ -35,6 +35,63 @@ std::string ShortDescription;
std::string DetailDescription;
};
///
struct UDataSource
{
///
std::string ComponentName;
///
std::string PropertyName;
///
double LastReadTime;
///
double LastWriteTime;
///
UEnvironment *Env;
UDataSource(UEnvironment *env);
///
virtual void UpdateData(void)=0;
///
virtual void ClearData(void)=0;
///
virtual bool Configure(const std::string &component_name, const std::string &property_name)=0;
///
virtual const type_info& GetDataType(void) const=0;
};
template <class T>
struct UDataSourceT: public UDataSource
{
///
std::vector<MDMatrix<T> > Data;
UDataSourceT(UEnvironment *env);
///
virtual void UpdateData(void);
///
virtual void ExtractData(std::vector<MDMatrix<T> > &buffer);
///
virtual void ClearData(void);
///
virtual bool Configure(const std::string &component_name, const std::string &property_name);
///
virtual const type_info& GetDataType(void) const;
};
///
extern RDK_LIB_TYPE bool RDK_CALL RdkCreatePredefinedStructure(RDK::UEnvironment* env, int predefined_structure);
......@@ -108,6 +165,9 @@ std::string SourceControllerProperty;
/// predefined structure
std::map<int, UEnvPredefinedStructDescription> PredefinedStructures;
///
std::vector<UDataSource*> DataSources;
protected: //
//
UEPtr<UComponent> CurrentComponent;
......@@ -167,25 +227,6 @@ bool SetMaxCalcTime(double value);
///
bool GetUseIndTimeStepFlag(void) const;
bool SetUseIndTimeStepFlag(bool value);
///
//bool GetDebugMode(void) const;
//bool SetDebugMode(bool value);
///
//unsigned int GetDebugSysEventsMask(void) const;
//bool SetDebugSysEventsMask(unsigned int value);
///
//bool GetDebuggerMessageFlag(void) const;
///
//bool SetDebuggerMessageFlag(bool value);
/// -
/// true -
//bool GetEventsLogMode(void) const;
//bool SetEventsLogMode(bool value);
// --------------------------
// --------------------------
......@@ -284,6 +325,24 @@ bool RegisterSourceController(const std::string &component_name, const std::stri