MatLabUdmTruthTable::StringVectorManager MatLabUdmTruthTable::getStringVectorManager( std::string matrix ) { static boost::regex scPattern( "(?<!&#x[0-9A-Fa-f]{2});", boost::regex_constants::perl ); static boost::regex scCodePattern( "[Bb];", boost::regex_constants::perl ); static boost::regex cmPattern( ",", boost::regex_constants::perl ); static boost::regex cmCodePattern( "[Cc];", boost::regex_constants::perl ); static boost::regex labeledStringPattern( "\\s*(\\S+)\\s*:\\s*(.*?)\\s*\\Z", boost::regex_constants::perl | boost::regex_constants::mod_s); matrix = matrix.substr( 1, matrix.size() - 2 ); // GET RID OF ENCLOSING {} StringMatrix stringMatrix; bool scMatch = true; boost::match_results< std::string::const_iterator > results; while ( scMatch && ( boost::regex_search( matrix, results, scPattern ) || !(scMatch = false) ) ) { std::string row; if ( scMatch ) { row = results.prefix(); matrix = results.suffix(); } else { row = matrix; } row = boost::regex_replace( row, scCodePattern, ";" ); stringMatrix.push_back( StringVectorSP( new StringVector ) ); StringVector &stringVector = *stringMatrix.back(); bool cmMatch = true; while( cmMatch && ( boost::regex_search( row, results, cmPattern ) || !( cmMatch = false ) ) ) { std::string cell; if ( cmMatch ) { cell = results.prefix(); row = results.suffix(); } else { cell = row; } cell = boost::regex_replace( cell, cmCodePattern, "," ); stringVector.push_back( cell ); } } StringVectorMap stringVectorMap; for( StringMatrix::iterator stmItr = stringMatrix.begin() ; stmItr != stringMatrix.end() ; ++stmItr ) { StringVector &stringVector = **stmItr; std::string &labeledString = stringVector[1]; labeledString = RegexCommon::eliminateContinuations( labeledString ); if ( boost::regex_search( labeledString, results, labeledStringPattern ) ) { std::string key( results[1] ); std::string condition( results[2] ); stringVectorMap.insert( std::make_pair( key, *stmItr ) ); labeledString = condition; } } return StringVectorManager( stringMatrix, stringVectorMap ); }
void MatLabUdmTruthTable::setupTruthTable( void ) { State udmTruthTable = getUdmTruthTable(); udmTruthTable.Name() = udmTruthTable.name() = getUniqueName().c_str(); udmTruthTable.RefId() = getRefId(); udmTruthTable.Decomposition() = "FUNC_STATE"; udmTruthTable.Order() = "-2"; udmTruthTable.EnterAction() = RegexCommon::eliminateContinuations( MatLabEngine::globalEngine().getMxStringValue( getMatLabObject() + ".LabelString" ) ); std::string conditionTable = MatLabEngine::globalEngine().getData( getMatLabObject() + ".ConditionTable" ); StringVectorManager conditionSTM = getStringVectorManager( conditionTable ); std::string actionTable = MatLabEngine::globalEngine().getData( getMatLabObject() + ".ActionTable" ); StringVectorManager actionSTM = getStringVectorManager( actionTable ); std::string functionBody; StringMatrix conditionStringMatrix = conditionSTM.getStringMatrix(); int noConditions = (int) conditionStringMatrix[0]->size() - 2; int noConditionTerms = (int) conditionStringMatrix.size() - 1; static boost::regex crnlPattern( "\\r|\\n", boost::regex_constants::perl ); for( int ix = 0 ; ix < noConditions ; ++ix ) { if ( ix != 0 ) functionBody += "else"; functionBody += "if "; std::string condition; for( int jx = 0 ; jx < noConditionTerms ; ++jx ) { StringVector &stringVector = *conditionStringMatrix[ jx ]; std::string tf = stringVector[ ix + 2 ]; std::string rawCondition = "(" + boost::regex_replace( stringVector[1], crnlPattern, " " ) + ")"; if ( tf.find( '-' ) != std::string::npos ) continue; if ( tf.find( 'F' ) != std::string::npos ) rawCondition = "!" + rawCondition; if ( !condition.empty() ) condition += " && "; condition += rawCondition; } if ( condition.empty() ) condition = "1"; functionBody += condition + "\n"; StringVector actionVector; std::string actions = ( *conditionStringMatrix.back() )[ ix + 2 ]; static boost::regex actionPattern( "[0-9]+|[A-Za-z_][A-Za-z_0-9]*" ); boost::match_results< std::string::const_iterator > results; while ( boost::regex_search( actions, results, actionPattern ) ) { actionVector.push_back( results[0] ); actions = results.suffix(); } static boost::regex newlinePattern( "\n" ); for( StringVector::iterator stvItr = actionVector.begin() ; stvItr != actionVector.end() ; ++stvItr ) { std::string actionSpec = *stvItr; if ( isdigit( actionSpec[0] ) ) { int actionNo = boost::lexical_cast< int >( actionSpec ) - 1; std::string action = (*actionSTM.getStringMatrix()[ actionNo ])[ 1 ]; action = boost::regex_replace( action, newlinePattern, "\n " ); functionBody += " " + action + "\n"; } else { std::string action = (*actionSTM.getStringVectorMap()[ actionSpec ])[ 1 ]; action = boost::regex_replace( action, newlinePattern, "\n " ); functionBody += " " + action + "\n"; } } } functionBody += "end\n"; udmTruthTable.DuringAction() = functionBody; }