00001 #include "HltConfig/MyFrame.h"
00002 #include "HltSteerData/Sequence.h"
00003 #include "HltSteerData/Signature.h"
00004 #include "HltSteerData/HltCriteria.h"
00005
00006 #include "xercesc/framework/XMLFormatter.hpp"
00007 #include "xercesc/util/PlatformUtils.hpp"
00008 #include "xercesc/util/XMLString.hpp"
00009 #include "xercesc/util/XMLUniDefs.hpp"
00010 #include "xercesc/util/TranscodingException.hpp"
00011 #include "xercesc/parsers/XercesDOMParser.hpp"
00012
00013 #include <xercesc/dom/DOM.hpp>
00014
00015 namespace HltProcessor
00016 {
00017
00018
00019 MyFrame* MyFrame::s_FrameInstance = 0;
00020
00021
00022 MyFrame* MyFrame::instance() {
00023 if(s_FrameInstance == 0) {
00024 s_FrameInstance = new MyFrame;
00025 }
00026 return s_FrameInstance;
00027 }
00028 void MyFrame::destruct() {
00029 if (s_FrameInstance) delete s_FrameInstance;
00030 }
00031
00032
00033 MyFrame::MyFrame():
00034 m_FrameNumSequenceTables(0),m_FrameNumTables(0),
00035 m_FrameAbsolutMaxStep(0),
00036 m_Signature_DocNode(NULL), m_Sequence_DocNode(NULL),
00037 m_SignatureVec(0),m_SequenceVec(0),m_TableVector(0),
00038 m_idSequence(0),m_idSignature(0),m_idCriteria(0),m_idItem(0),
00039 m_first(true)
00040 {
00041 if(s_FrameInstance){
00042 std::cout << " MyFrame constructed twice!"<< std::endl;
00043 }
00044 s_FrameInstance=this;
00045 m_thisName = "HltConfig";
00046 m_msgSvcProvider = MessageSvcProvider::instance();
00047 }
00048
00049
00050 MyFrame::~MyFrame()
00051 {
00052 for (unsigned int i = 0; i < m_SignatureVec.size(); i++) delete m_SignatureVec[i];
00053 for (unsigned int j = 0; j < m_SequenceVec.size(); j++) delete m_SequenceVec[j];
00054
00055 }
00056
00057
00058 void MyFrame::init(const std::string sequenceFile, const std::string signatureFile)
00059 {
00060
00061
00062
00063
00064 try {
00065 XMLPlatformUtils::Initialize();
00066
00067 }
00068
00069 catch (const XMLException& toCatch) {
00070
00071 exit(1);
00072 }
00073
00074
00075 XercesDOMParser* signatureParser = new XercesDOMParser;
00076 signatureParser->setValidationScheme(XercesDOMParser::Val_Always);
00077 signatureParser->setDoNamespaces(true);
00078
00079
00080 XercesDOMParser* sequenceParser = new XercesDOMParser;
00081 sequenceParser->setValidationScheme(XercesDOMParser::Val_Always);
00082 sequenceParser->setDoNamespaces(true);
00083
00084
00085 try {
00086 signatureParser->parse(signatureFile.c_str());
00087
00088
00089 sequenceParser->parse(sequenceFile.c_str());
00090
00091 }
00092
00093 catch (const XMLException& toCatch) {
00094
00095 exit(1);
00096 }
00097 catch (const DOMException& toCatch) {
00098
00099 XMLPlatformUtils::Terminate();
00100 exit(1);
00101 }
00102 catch (...) {
00103
00104 XMLPlatformUtils::Terminate();
00105 exit(1);
00106 }
00107
00108
00109 m_Sequence_DocNode = sequenceParser->getDocument();
00110 m_Signature_DocNode = signatureParser->getDocument();
00111
00112
00113
00114 if(m_first){
00115 m_first=false;
00116 }
00117 else{
00118 for (unsigned int i = 0; i < m_SignatureVec.size(); i++) delete m_SignatureVec[i];
00119 for (unsigned int j = 0; j < m_SequenceVec.size(); j++) delete m_SequenceVec[j];
00120 m_SignatureVec.clear();
00121 m_SequenceVec.clear();
00122 m_TableVector.clear();
00123 m_idCriteria=0;
00124 m_idSignature=0;
00125 m_idSequence=0;
00126 m_idItem=0;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 buildSequenceList(m_Sequence_DocNode, true);
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 buildSignatureList(m_Signature_DocNode, true);
00153
00154
00155
00156 delete sequenceParser;
00157 delete signatureParser;
00158
00159 makeTableVector();
00160
00161 m_first=false;
00162 std::cout << "MyFrame init() succuss" << "criteria num=" << m_idCriteria << std::endl;
00163 }
00164
00165 void MyFrame::makeTableVector()
00166 {
00167
00168
00169 for (unsigned int i = 0; i < m_SequenceVec.size(); i++) {
00170 std::string seqId = m_SequenceVec[i]->getSeqID();
00171 bool matched = false;
00172 for (unsigned int j = 0; j < m_SignatureVec.size(); j++) {
00173 if (m_SignatureVec[j]->label() == m_SequenceVec[i]->getSeqID()) {
00174 m_TableVector.push_back(std::make_pair(m_SignatureVec[j], m_SequenceVec[i]));
00175 matched = true;
00176 break;
00177 }
00178 }
00179 if (!matched) {
00180
00181 exit(1);
00182 }
00183 }
00184 }
00185
00186 void MyFrame::final()
00187 {
00188
00189
00190
00191
00192
00193
00194
00195 XMLPlatformUtils::Terminate();
00196 }
00197
00198 void MyFrame::buildSequenceList(const DOMNode* thisNode, bool first)
00199 {
00200
00201 if ( first ) {
00202 first=false;
00203
00204
00205
00206
00207 }
00208 std::string outString, inputString, seq_idString, algoString;
00209
00210 #define XMLCF XMLString::compareString
00211 #define XMLTF XMLString::transcode
00212 DOMNode *helpNode = thisNode->getFirstChild();
00213
00214 while (helpNode != 0) {
00215
00216 if(XMLCF(XMLTF(helpNode->getNodeName()),"SEQUENCE")==0) {
00217 DOMNamedNodeMap *attributes = helpNode->getAttributes();
00218 int attributesCount = attributes->getLength();
00219 for(int i = 0; i < attributesCount; i++) {
00220 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"sequence_id")==0)
00221 seq_idString = XMLTF(attributes->item(i)->getNodeValue());
00222 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"input")==0)
00223 inputString = XMLTF(attributes->item(i)->getNodeValue());
00224 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"output")==0)
00225 outString = XMLTF(attributes->item(i)->getNodeValue());
00226 }
00227 std::cout <<"+-Found SEQUENCE: " << seq_idString << std::endl;
00228
00229 Sequence* m_helpSequence = new Sequence();
00230 m_helpSequence->setSeqID(seq_idString);
00231 m_helpSequence->setInputID(inputString);
00232 m_helpSequence->setOutputID(outString);
00233 m_SequenceVec.push_back(m_helpSequence);
00234 }
00235
00236 if (XMLCF(XMLTF(helpNode->getNodeName()),"ESALGORITHM") == 0) {
00237 DOMNamedNodeMap *attributes = helpNode->getAttributes();
00238 int attributesCount = attributes->getLength();
00239 for (int i = 0; i < attributesCount; i++) {
00240 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"algorithm")==0)
00241 algoString = XMLTF(attributes->item(i)->getNodeValue());
00242 }
00243 std::cout << "| +-Found ESALGORITHM: " << algoString << std::endl;
00244
00245 m_SequenceVec.back()->makeVectors(algoString);
00246 }
00247
00248
00249 MyFrame::buildSequenceList(helpNode, first);
00250 helpNode = helpNode->getNextSibling();
00251 }
00252 #undef XMLCF
00253 #undef XMLTF
00254
00255 }
00256
00257
00258 void MyFrame::buildSignatureList(const DOMNode* thisNode, bool first)
00259 {
00260
00261
00262 if ( first ) {
00263 first = false;
00264
00265
00266
00267
00268 }
00269 std::string helpString, helpPrescale, behaviour, itemName, itemType;
00270 int prescale=1;
00271 float forcedAccept=1, itemValue=0;
00272
00273 #define XMLCF XMLString::compareString
00274 #define XMLTF XMLString::transcode
00275 DOMNode *helpNode = thisNode->getFirstChild();
00276
00277 while (helpNode != 0) {
00278 static std::string current_sigID = "";
00279 if(XMLCF(XMLTF(helpNode->getNodeName()),"SIGNATURE")==0) {
00280 DOMNamedNodeMap *attributes = helpNode->getAttributes();
00281 unsigned int attributesCount = attributes->getLength();
00282 for(unsigned int i = 0; i < attributesCount; i++) {
00283 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"signature_id")==0)
00284 helpString = XMLTF(attributes->item(i)->getNodeValue());
00285 current_sigID = helpString;
00286 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"prescale")==0) {
00287 helpPrescale = XMLTF(attributes->item(i)->getNodeValue());
00288 prescale = atoi(helpPrescale.c_str());
00289 }
00290 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"forced_accept")==0)
00291 forcedAccept = (float)DOMToDouble(attributes->item(i)->getNodeValue());
00292 }
00293 std::cout<< "+-Found SIGNATURE: " << helpString << std::endl;
00294
00295 Signature* m_helpSignature = new Signature();
00296 m_helpSignature->setSigLabel(helpString);
00297 m_helpSignature->setPrescale(prescale);
00298 m_helpSignature->setForcedAccept(forcedAccept);
00299 m_SignatureVec.push_back(m_helpSignature);
00300 }
00301
00302 static HltCriteria* current_HltCriteria = NULL;
00303 if(XMLCF(XMLTF(helpNode->getNodeName()),"CRITERIA")==0) {
00304 HltCriteria *m_HltCriteria = new HltCriteria(m_idCriteria);
00305 DOMNamedNodeMap *attributes = helpNode->getAttributes();
00306 unsigned int attributesCount = attributes->getLength();
00307
00308 for(unsigned int i = 0; i < attributesCount; i++) {
00309 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"behaviour")==0) {
00310 behaviour = XMLTF(attributes->item(i)->getNodeValue());
00311 if (behaviour == "reject") behaviour += "_reject";
00312 }
00313 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"next")==0) {
00314 if (behaviour != "jump") {
00315 std::cout << "Unconsistent of behaviour and next" << std::endl;
00316
00317 exit(1);
00318 }
00319 std::string nextID = XMLTF(attributes->item(i)->getNodeValue());
00320
00321 for (unsigned int i = 0; i < m_SequenceVec.size(); i++) {
00322
00323 if (m_SequenceVec[i]->getSeqID() == current_sigID) {
00324 while (++i < m_SequenceVec.size()) {
00325
00326 if (m_SequenceVec[i]->getSeqID() == nextID) break;
00327 }
00328 if (i == m_SequenceVec.size()) {
00329 std::cout << "Undefined jump target: " << nextID << std::endl;
00330
00331 exit(1);
00332 }
00333 break;
00334 }
00335 }
00336 behaviour = behaviour +'_'+ nextID;
00337 }
00338 if(XMLCF(XMLTF(attributes->item(i)->getNodeName()),"mark")==0) {
00339 if ((behaviour!= "accept")&&(behaviour!= "continue")) {
00340 std::cout << "Unconsistent of behaviour and mark" << std::endl;
00341
00342 exit(1);
00343 }
00344 behaviour = behaviour +'_'+ XMLTF(attributes->item(i)->getNodeValue());
00345 }
00346 }
00347 if (behaviour.find('_') == string::npos) {
00348 std::cout << "Unvalid behaviour ==> " << behaviour << std::endl;
00349
00350 exit(1);
00351 }
00352
00353 m_HltCriteria->setBehaviour(behaviour);
00354 std::cout << "| +-Found CRITERIA: behaviour as " << m_HltCriteria->getBehaviour() << std::endl;
00355
00356 m_SignatureVec.back()->makeVectors(m_HltCriteria);
00357 m_idCriteria++;
00358 current_HltCriteria = m_HltCriteria;
00359 }
00360
00361 if(XMLCF(XMLTF(helpNode->getNodeName()),"ITEM") == 0) {
00362 DOMNamedNodeMap *itemAttr = helpNode->getAttributes();
00363 if (itemAttr->getLength() != 3) {
00364
00365 exit(1);
00366 }
00367 for (int i = 0; i < 3; i++) {
00368 if(XMLCF(XMLTF(itemAttr->item(i)->getNodeName()), "name") == 0)
00369 itemName = XMLTF(itemAttr->item(i)->getNodeValue());
00370 if(XMLCF(XMLTF(itemAttr->item(i)->getNodeName()), "value") == 0)
00371 itemValue = (float)DOMToDouble(itemAttr->item(i)->getNodeValue());
00372 if(XMLCF(XMLTF(itemAttr->item(i)->getNodeName()), "type") == 0)
00373 itemType = XMLTF(itemAttr->item(i)->getNodeValue());
00374 }
00375
00376 current_HltCriteria->addItem(itemName, itemValue, itemType,m_idItem);
00377 m_idItem++;
00378 }
00379
00380
00381 MyFrame::buildSignatureList(helpNode, first);
00382 helpNode = helpNode->getNextSibling();
00383 }
00384
00385 #undef XMLCF
00386 #undef XMLTF
00387 }
00388
00389
00390 const std::vector<std::pair<Signature*, Sequence*> >& MyFrame::getTablesVector() const
00391 {
00392 return m_TableVector;
00393 }
00394
00395 const double MyFrame::DOMToDouble(const XMLCh * thisDOMString) const
00396 {
00397 double returnValue(0);
00398 std::string helpString(XMLString::transcode(thisDOMString));
00399 std::istringstream is(helpString);
00400 is >> returnValue;
00401 return returnValue;
00402 }
00403
00404 }