Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
BatchAnalysis.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "BatchAnalysis.h"
14 
15 #include <boost/filesystem/path.hpp>
16 #include <boost/filesystem/operations.hpp>
17 
18 
19 using namespace kome::operation;
20 
21 
22 #include <crtdbg.h>
23 #ifdef _DEBUG
24  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
25  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
26 #endif // _DEBUG
27 
28 
29 
30 // static members
31 const char* BatchAnalysis::FILE_NAME_PROP_NAME = "File Name";
32 const char* BatchAnalysis::DIR_PARAM_NAME = "dir";
33 
34 
35 // constructor
36 BatchAnalysis::BatchAnalysis( const char* analysisName ) : m_params( NULL, false ) {
37  // initialize
38  m_name = NVL( analysisName, "" );
39  m_filesPage = NULL;
41 
42  // add file name property
44 }
45 
46 // destructor
48 }
49 
50 // clear file property
52  m_fileProperties.clear();
53 }
54 
55 // add file property
56 int BatchAnalysis::addFileProperty( const char* prop ) {
57  int idx = (int)m_fileProperties.size();
58  m_fileProperties.push_back( NVL( prop, "" ) );
59  return idx;
60 }
61 
62 // get number of file properties
64  return m_fileProperties.size();
65 }
66 
67 // get file property
68 const char* BatchAnalysis::getFileProperty( unsigned int idx ) {
69  if( idx < 0 || idx >= (int)m_fileProperties.size() ) {
70  return NULL;
71  }
72  return m_fileProperties[ idx ].c_str();
73 }
74 
75 // get files page
76 kome::plugin::SettingsPage* BatchAnalysis::getFilesPage() {
77  // create page
78  if( m_filesPage == NULL ) {
79  m_filesPage = new kome::plugin::SettingsPage( m_params );
81  }
82 
83  return m_filesPage;
84 }
85 
86 // get files
87 void BatchAnalysis::getFiles( kome::objects::SettingParameterValues* settings, std::vector< std::pair< std::string, kome::core::Properties > >& files ) {
88  onGetFiles( settings, files );
89 }
90 
91 // get parameters page
92 kome::plugin::SettingsPage* BatchAnalysis::getParametersPage() {
93  // create page
94  if( m_parametersPage == NULL ) {
95  m_parametersPage = new kome::plugin::SettingsPage( m_params );
97  }
98 
99  return m_parametersPage;
100 }
101 
102 // create file page
103 void BatchAnalysis::onCreateFilesPage( kome::plugin::ParameterSettings& params, kome::plugin::SettingsPage& page ) {
104  // value
105  kome::plugin::SettingsValue* value = params.addValue();
106  value->setType( kome::plugin::SettingsValue::TYPE_DIR );
107  value->setRequired( true );
108 
109  // parameter
110  kome::plugin::SettingsParam* param = params.addParam();
111  param->setSection( FMT( "Batch Analysis (%s)", m_name.c_str() ).c_str() );
112  param->setKey( "DIR" );
113  param->setName( DIR_PARAM_NAME );
114 
115  // form
116  kome::plugin::SettingsForm* form = page.addForm();
117  form->setTitle( "Directory" );
118  form->setParam( param );
119  form->setDesc( "Select the data folder." );
120 }
121 
122 // get files
123 void BatchAnalysis::onGetFiles( kome::objects::SettingParameterValues* settings, std::vector< std::pair< std::string, kome::core::Properties > >& files ) {
124  // directory
125  std::string dir = settings->getParameterValue( DIR_PARAM_NAME );
126  if( dir.empty() ) {
127  return;
128  }
129 
130  // get files
131  getFiles( dir.c_str(), files );
132 }
133 
134 // analyze
135 void BatchAnalysis::analyze( std::vector< std::string >& paths, kome::core::Progress& progress ) {
136  // subprogresses
137  progress.createSubProgresses( paths.size() + 2 );
138 
139  // preparation
140  int progressIdx = 0;
141  kome::core::Progress* subProgress = progress.getSubProgress( progressIdx );
142  progressIdx++;
143  progress.setStatus( "Preparation..." );
144  onPrepareAnalysis( paths, *subProgress );
145  if( progress.isStopped() ) {
146  return;
147  }
148  subProgress->fill();
149 
150  // analyze each file
151  for( unsigned int i = 0; i < paths.size() && !progress.isStopped(); i++ ) { // each file
152  // progress
153  std::string message = FMT( "Analyzing ... [%d/%d]", ( i + 1 ), paths.size() );
154  progress.setStatus( message.c_str() );
155  subProgress = progress.getSubProgress( progressIdx );
156  progressIdx++;
157 
158  // analyze
159  onAnalyze( paths[ i ].c_str(), *subProgress );
160  subProgress->fill();
161  }
162  if( progress.isStopped() ) {
163  return;
164  }
165 
166  // post analysis
167  subProgress = progress.getSubProgress( progressIdx );
168  progress.setStatus( "Finalizing..." );
169  onFinishAnalysis( *subProgress );
170 
171  if( !progress.isStopped() ) {
172  progress.fill();
173  }
174 }
175 
176 // shows analysis result
178  onShowResult();
179 }
180 
181 // create parameters page
182 void BatchAnalysis::onCreateParametersPage( kome::plugin::ParameterSettings& params, kome::plugin::SettingsPage& page ) {
183  LOG_WARN( FMT( "This method is underconstruction. Please implement onCreateParametersPage method in the subclass of BatchAnalysis." ) );
184 }
185 
186 // get paths
187 void BatchAnalysis::getFiles( const char* dir, std::vector< std::pair< std::string, kome::core::Properties > >& files ) {
188  // path
189  boost::filesystem::path p( dir, boost::filesystem::native );
190 
191  // store
192  if( boost::filesystem::is_directory( p ) ) { // directory
193  boost::filesystem::directory_iterator end;
194  for( boost::filesystem::directory_iterator it( p ); it != end; it++ ) { // each child
195  boost::filesystem::path child = (*it);
196  getFiles( boost::filesystem::absolute( child ).string().c_str(), files );
197  }
198  }
199  else { // file
200  files.resize( files.size() + 1 );
201  files.back().first = boost::filesystem::absolute( p ).string();
202  files.back().second.setValue( FILE_NAME_PROP_NAME, p.leaf().string().c_str() );
203  }
204 }
static const char * DIR_PARAM_NAME
Definition: BatchAnalysis.h:60
virtual void onShowResult()=0
shows analysis result
virtual void onFinishAnalysis(kome::core::Progress &progress)=0
on finish analysis
void clearFileProperties()
clears file propertyes
void fill()
sets end position
Definition: Progress.cpp:206
virtual ~BatchAnalysis()
destructor
virtual void onCreateFilesPage(kome::plugin::ParameterSettings &params, kome::plugin::SettingsPage &page)
creates files page
Progress * getSubProgress(const unsigned int idx)
gets subprogress
Definition: Progress.cpp:331
setting parameter values management class
virtual void onAnalyze(const char *path, kome::core::Progress &progress)=0
on analyze
int addFileProperty(const char *prop)
adds file property
bool isStopped()
judges whether it has to finish
Definition: Progress.cpp:199
kome::plugin::SettingsPage * m_parametersPage
Definition: BatchAnalysis.h:53
void getFiles(kome::objects::SettingParameterValues *settings, std::vector< std::pair< std::string, kome::core::Properties > > &files)
gets files
void showResult()
shows analysis result
void analyze(std::vector< std::string > &paths, kome::core::Progress &progress)
analyzes
kome::plugin::SettingsPage * m_filesPage
Definition: BatchAnalysis.h:50
progress display abstract class
Definition: Progress.h:31
const char * getFileProperty(const unsigned int idx)
gets the file property
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
#define NULL
Definition: CoreMacros.h:18
interfaces of BatchAnalysis class
const char * getParameterValue(const unsigned int index)
gets parameter value
virtual void onCreateParametersPage(kome::plugin::ParameterSettings &params, kome::plugin::SettingsPage &page)
creates parameters
static const char * FILE_NAME_PROP_NAME
Definition: BatchAnalysis.h:57
BatchAnalysis(const char *analysisName)
constructor
void createSubProgresses(const unsigned int num)
creates sub progresses
Definition: Progress.cpp:299
virtual void onPrepareAnalysis(std::vector< std::string > &paths, kome::core::Progress &progress)=0
on prepares analysis
virtual void onGetFiles(kome::objects::SettingParameterValues *settings, std::vector< std::pair< std::string, kome::core::Properties > > &files)
This method is called form the getFiles method.
kome::plugin::SettingsPage * getParametersPage()
gets the parameters page
std::vector< std::string > m_fileProperties
Definition: BatchAnalysis.h:44
void setStatus(const char *status, const bool bForced=false)
sets status
Definition: Progress.cpp:160
kome::plugin::ParameterSettings m_params
Definition: BatchAnalysis.h:47
unsigned int getNumberOfFileProperties()
gets the number of file properties
kome::plugin::SettingsPage * getFilesPage()
gets the files page