27 #ifndef FAUDES_MTCGENERATOR_H
28 #define FAUDES_MTCGENERATOR_H
52 template <
class GlobalAttr,
class StateAttr,
class EventAttr,
class TransAttr>
318 void DelColor(
Idx stateIndex,
const std::string& rColorName);
343 void DelColor(
const std::string& rColorName);
473 bool ExistsColor(
const std::string& rColorName)
const;
585 virtual void DotWrite(
const std::string& rFileName)
const;
595 std::string
CStr(
Idx index)
const;
744 #ifdef FAUDES_COMPATIBILITY
749 #define THIS TmtcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
750 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
751 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
794 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem()");
800 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem(rOtherGen)");
809 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem(rOtherGen)");
815 TEMP THIS::TmtcGenerator(
const std::string& rFileName) :
BASE() {
816 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem(" << rFileName <<
")");
826 res->EventSymbolTablep(BASE::mpEventSymbolTable);
827 res->mStateNamesEnabled=BASE::mStateNamesEnabled;
828 res->mReindexOnWrite=BASE::mReindexOnWrite;
830 res->ColorSymbolTable(GlobalColorSymbolTablep());
848 FD_DG(
"MtcSystem(" <<
this <<
")::Assign(gen&)");
874 FD_DG(
"MtcSystem(" <<
this <<
")::Assign(vgen&)");
888 return mpColorSymbolTable;
893 mpColorSymbolTable=pSymTab;
902 TEMP Idx THIS::InsColoredState(
const std::string& rStateName,
const std::string& rColorName) {
903 FD_DG(
"MtcSystem(" <<
this <<
")::InsColoredState(rStateName, colorName)");
906 #ifdef FAUDES_CHECKED
908 index = BASE::InsState(rStateName);
911 std::stringstream errstr;
912 errstr <<
"Name \"" << rStateName <<
"\" already exists" << std::endl;
913 throw Exception(
"MtcSystem::InsColoredState(rStateName, rColorName)", errstr.str(), 104);
916 attr = BASE::StateAttributep(index);
919 std::stringstream errstr;
920 errstr <<
"Index " << index <<
" not member of set" << std::endl;
921 throw Exception(
"MtcSystem::InsColoredState(stateIndex, colorIndex)", errstr.str(), 200);
924 attr->Colors().Insert(rColorName);
927 std::stringstream errstr;
928 errstr <<
"Name already exists / invalid name: " << rColorName << std::endl;
929 throw Exception(
"MtcSystem::InsColoredState(stateIndex, rColorName)", errstr.str(), 104);
932 index = BASE::InsState(rStateName);
933 attr = Attributep(index);
934 attr->Colors().Insert(rColorName);
940 TEMP Idx THIS::InsColoredState(
const std::string& rStateName,
const Idx colorIndex) {
941 Idx index = BASE::InsState(rStateName);
942 StateAttr *attr = BASE::StateAttributep(index);
943 #ifdef FAUDES_CHECKED
945 attr->Colors().Insert(colorIndex);
948 std::stringstream errstr;
949 errstr <<
"Color index " << colorIndex <<
" not known to symbol table" << std::endl;
950 throw Exception(
"MtcSystem::InsColoredState(rStateName, colorIndex)", errstr.str(), 200);
953 attr->Colors().Insert(colorIndex);
959 TEMP Idx THIS::InsColoredState(
const std::string& rStateName,
const ColorSet& rColors) {
960 Idx index = BASE::InsState(rStateName);
961 InsColors(index, rColors);
967 Idx index = BASE::InsState();
968 InsColors(index, rColors);
973 TEMP Idx THIS::InsColor(
Idx stateIndex,
const std::string& rColorName) {
976 #ifdef FAUDES_CHECKED
978 attr = BASE::StateAttributep(stateIndex);
981 std::stringstream errstr;
982 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
983 throw Exception(
"MtcSystem::InsColor(stateIndex, colorIndex)", errstr.str(), 200);
986 index = attr->Colors().Insert(rColorName);
989 std::stringstream errstr;
990 errstr <<
"Name already exists / invalid name: " << rColorName << std::endl;
991 throw Exception(
"MtcSystem::InsColor(stateIndex, rColorName)", errstr.str(), 104);
994 attr = Attributep(stateIndex);
995 index = attr->Colors().Insert(rColorName);
1003 #ifdef FAUDES_CHECKED
1005 attr = BASE::StateAttributep(stateIndex);
1008 std::stringstream errstr;
1009 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1010 throw Exception(
"MtcSystem::InsColor(stateIndex, colorIndex)", errstr.str(), 200);
1013 attr->Colors().Insert(colorIndex);
1016 std::stringstream errstr;
1017 errstr <<
"Color index " << colorIndex <<
" not known to symbol table" << std::endl;
1018 throw Exception(
"MtcSystem::InsColor(stateIndex, colorIndex)", errstr.str(), 200);
1021 attr = Attributep(stateIndex);
1022 attr->Colors().Insert(colorIndex);
1028 #ifdef FAUDES_CHECKED
1030 std::stringstream errstr;
1031 errstr <<
"Symbol table mismatch" << std::endl;
1032 throw Exception(
"MtcSystem::InsColors(stateIndex, rColors)", errstr.str(), 88);
1036 #ifdef FAUDES_CHECKED
1038 attr = BASE::StateAttributep(stateIndex);
1041 std::stringstream errstr;
1042 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1043 throw Exception(
"MtcSystem::InsColors(stateIndex, rColors)", errstr.str(), 200);
1046 attr->Colors().InsertSet(rColors);
1049 std::stringstream errstr;
1050 errstr <<
"Symbol table mismach" << std::endl;
1051 throw Exception(
"MtcSystem::InsColors(stateIndex, rColors)", errstr.str(), 200);
1054 attr = Attributep(stateIndex);
1055 attr->Colors().InsertSet(rColors);
1060 TEMP void THIS::DelColor(
Idx stateIndex,
const std::string& rColorName) {
1063 #ifdef FAUDES_CHECKED
1065 attr = BASE::StateAttributep(stateIndex);
1068 std::stringstream errstr;
1069 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1070 throw Exception(
"MtcSystem::DelColor(stateIndex, rColorName)", errstr.str(), 200);
1072 index = ColorIndex(rColorName);
1074 attr->Colors().Erase(index);
1077 std::stringstream errstr;
1078 errstr <<
"Color name \"" << rColorName <<
"\" not found in NameSet" << std::endl;
1079 throw Exception(
"MtcSystem::DelColor(stateIndex, rColorName)", errstr.str(), 202);
1082 attr = Attributep(stateIndex);
1083 index = ColorIndex(rColorName);
1084 attr->Colors().Erase(index);
1086 CheckSymbolTable(index);
1093 #ifdef FAUDES_CHECKED
1095 attr = BASE::StateAttributep(stateIndex);
1098 std::stringstream errstr;
1099 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1100 throw Exception(
"MtcSystem::DelColor(stateIndex, colorIndex)", errstr.str(), 200);
1103 attr = BASE::StateAttributep(stateIndex);
1105 res = attr->Colors().Erase(colorIndex);
1107 std::stringstream errstr;
1108 errstr <<
"Color index " << colorIndex <<
" not found in generator" << std::endl;
1109 throw Exception(
"MtcSystem::DelColor(stateIndex, colorIndex)", errstr.str(), 205);
1111 CheckSymbolTable(colorIndex);
1115 TEMP void THIS::DelColor(
const std::string& rColorName) {
1116 StateSet::Iterator lit;
1119 Idx index = ColorIndex(rColorName);
1121 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1122 attr = BASE::StateAttributep(*lit);
1123 attr->Colors().Erase(index);
1125 CheckSymbolTable(index);
1130 StateSet::Iterator lit;
1132 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1133 attr = BASE::StateAttributep(*lit);
1134 attr->Colors().Erase(colorIndex);
1136 CheckSymbolTable(colorIndex);
1142 ColorSet::Iterator cit;
1143 ColorSet delColors(mpColorSymbolTable);
1144 #ifdef FAUDES_CHECKED
1146 attr = BASE::StateAttributep(stateIndex);
1149 std::stringstream errstr;
1150 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1151 throw Exception(
"MtcSystem::ClrColors(stateIndex)", errstr.str(), 200);
1154 attr = BASE::StateAttributep(stateIndex);
1157 attr->Colors().Clear();
1158 CheckSymbolTable(delColors);
1163 ColorSymbolTablep()->ClrEntry(colorIndex);
1168 #ifdef FAUDES_CHECKED
1170 std::stringstream errstr;
1171 errstr <<
"Symbol table mismatch" << std::endl;
1172 throw Exception(
"MtcSystem::Colors", errstr.str(), 88);
1175 StateSet::Iterator lit;
1176 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1177 const StateAttr& attr = BASE::pStates->Attribute(*lit);
1184 StateSet::Iterator lit;
1185 ColorSet colors(mpColorSymbolTable);
1186 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1187 const StateAttr& attr = BASE::pStates->Attribute(*lit);
1195 const StateAttr* attrp;
1196 #ifdef FAUDES_CHECKED
1198 attrp = &BASE::pStates->
Attribute(stateIndex);
1201 std::stringstream errstr;
1202 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1203 throw Exception(
"MtcSystem::Colors(stateIndex)", errstr.str(), 200);
1206 attrp = &BASE::pStates->Attribute(stateIndex);
1208 return attrp->Colors();
1212 TEMP void THIS::CheckSymbolTable(
Idx colorIndex) {
1214 ColorSet colors(mpColorSymbolTable);
1216 if (!colors.
Exists(colorIndex))
1217 DelColorName(colorIndex);
1223 #ifdef FAUDES_CHECKED
1225 std::stringstream errstr;
1226 errstr <<
"Symbol table mismatch" << std::endl;
1227 throw Exception(
"MtcSystem::CheckSymbolTable", errstr.str(), 88);
1231 ColorSet colors(mpColorSymbolTable);
1234 ColorSet::Iterator cit;
1235 for(cit = rColors.
Begin(); cit != rColors.
End(); cit++) {
1237 if(!colors.
Exists(*cit)) DelColorName(*cit);
1243 TEMP bool THIS::ExistsColor(
Idx colorIndex)
const {
1244 StateSet::Iterator lit;
1245 for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1246 if (ExistsColor(*lit, colorIndex))
return true;
1252 TEMP bool THIS::ExistsColor(
const std::string& rColorName)
const {
1253 Idx colorIndex = ColorIndex(rColorName);
1254 #ifdef FAUDES_CHECKED
1255 if (colorIndex == 0) {
1256 std::stringstream errstr;
1257 errstr <<
"Color name \"" << rColorName <<
"\" not found in symbol table " << std::endl;
1258 throw Exception(
"MtcSystem::ExistsColor(rColorName)", errstr.str(), 202);
1261 return ExistsColor(colorIndex);
1265 TEMP std::string THIS::ColorName(
Idx colorIndex)
const {
1266 return ColorSymbolTablep()->Symbol(colorIndex);
1270 TEMP Idx THIS::ColorIndex(
const std::string& rColorName)
const {
1271 return ColorSymbolTablep()->Index(rColorName);
1276 return ColorName(index);
1280 TEMP bool THIS::ExistsColor(
Idx stateIndex,
Idx colorIndex)
const {
1281 #ifdef FAUDES_CHECKED
1283 return BASE::pStates->Attribute(stateIndex).Colors().Exists(colorIndex);
1286 std::stringstream errstr;
1287 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1288 throw Exception(
"MtcSystem::ExistsColor(stateIndex, colorIndex)", errstr.str(), 200);
1291 return BASE::pStates->Attribute(stateIndex).Colors().Exists(colorIndex);
1296 TEMP void THIS::DotWrite(
const std::string& rFileName)
const {
1297 FD_DG(
"TmtcGenerator(" <<
this <<
")::DotWrite(" << rFileName <<
")");
1299 StateSet::Iterator it;
1300 BASE::SetMinStateIndexMap();
1303 std::ofstream stream;
1304 stream.exceptions(std::ios::badbit|std::ios::failbit);
1305 stream.open(rFileName.c_str());
1306 stream <<
"// dot output generated by libFAUDES TmtcGenerator" << std::endl;
1307 stream <<
"digraph \"" << BASE::Name() <<
"\" {" << std::endl;
1308 stream <<
" rankdir=LR" << std::endl;
1309 stream <<
" node [shape=circle];" << std::endl;
1310 stream << std::endl;
1311 stream <<
" // initial states" << std::endl;
1313 for (it = BASE::InitStatesBegin(); it != BASE::InitStatesEnd(); ++it) {
1314 std::string xname= BASE::StateName(*it);
1315 if(xname==
"") xname=
ToStringInteger(static_cast<long int>(BASE::MinStateIndex(*it)));
1316 stream <<
" dot_dummyinit_" << i <<
" [shape=none, label=\"\" ];" << std::endl;
1317 stream <<
" dot_dummyinit_" << i <<
" -> \"" << xname <<
"\";" << std::endl;
1320 stream << std::endl;
1323 stream <<
" // uncolored states" << std::endl;
1324 for (it = BASE::pStates->Begin(); it != BASE::pStates->End(); ++it) {
1326 const StateAttr& attr = BASE::StateAttribute(*it);
1328 if (!attr.IsDefault()) {
1329 coloredStates.
Insert(*it, attr);
1332 std::string xname=BASE::StateName(*it);
1334 stream <<
" \"" << xname <<
"\";" << std::endl;
1339 std::vector<std::string> ColorVector;
1340 ColorVector.push_back(
"blue");
1341 ColorVector.push_back(
"red");
1342 ColorVector.push_back(
"magenta");
1343 ColorVector.push_back(
"green");
1344 ColorVector.push_back(
"darkorange");
1345 ColorVector.push_back(
"cyan");
1346 ColorVector.push_back(
"navy");
1347 ColorVector.push_back(
"brown");
1348 ColorVector.push_back(
"green4");
1349 ColorVector.push_back(
"yellow");
1350 ColorVector.push_back(
"darkviolet");
1351 ColorVector.push_back(
"firebrick");
1352 ColorVector.push_back(
"greenyellow");
1353 ColorVector.push_back(
"peru");
1354 ColorVector.push_back(
"skyblue");
1355 ColorVector.push_back(
"darkgreen");
1356 ColorVector.push_back(
"violetred");
1357 ColorVector.push_back(
"lightsalmon");
1358 ColorVector.push_back(
"seagreen");
1359 ColorVector.push_back(
"saddlebrown");
1360 ColorVector.push_back(
"slateblue");
1361 ColorVector.push_back(
"thistle");
1362 ColorVector.push_back(
"turquoise4");
1363 ColorVector.push_back(
"gold2");
1364 ColorVector.push_back(
"sandybrown");
1365 ColorVector.push_back(
"aquamarine3");
1366 ColorVector.push_back(
"darkolivegreen");
1367 ColorVector.push_back(
"yellow4");
1369 stream << std::endl;
1370 stream <<
" // colored states" << std::endl;
1372 for (it = coloredStates.
Begin(); it != coloredStates.
End(); it++) {
1374 std::string xname=BASE::StateName(*it);
1375 if(xname==
"") xname=
ToStringInteger(static_cast<long int>(BASE::MinStateIndex(*it)));
1376 const StateAttr& attr = coloredStates.
Attribute(*it);
1377 if (attr.Colors().Size() > 1) {
1378 for(ColorSet::Iterator cit = attr.ColorsBegin(); cit != attr.ColorsEnd(); cit++) {
1379 stream <<
" subgraph cluster_" << clustNr++ <<
" {color=" << ColorVector.at(*cit-1) <<
";" << std::endl;
1382 stream <<
" \"" << xname <<
"\" " << std::endl <<
" ";
1383 for (
int i=0; i<count; i++) {
1386 stream << std::endl;
1388 else if (attr.Colors().Size() == 1) {
1389 ColorSet::Iterator cit;
1390 if ((cit = attr.ColorsBegin()) != attr.ColorsEnd())
1391 stream <<
" \"" << xname <<
"\" " <<
"[color=" << ColorVector.at(*cit-1) <<
"]" << std::endl;
1394 stream << std::endl;
1397 stream <<
" // marked states" << std::endl;
1398 for (it = BASE::MarkedStatesBegin(); it != BASE::MarkedStatesEnd(); ++it) {
1399 std::string xname= BASE::StateName(*it);
1400 if(xname==
"") xname=
ToStringInteger(static_cast<long int>(BASE::MinStateIndex(*it)));
1401 stream <<
" \"" << xname <<
"\";" << std::endl;
1406 stream << std::endl;
1407 stream <<
" // transition relation" << std::endl;
1408 for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
1409 std::string x1name= BASE::StateName(tit->X1);
1411 std::string x2name= BASE::StateName(tit->X2);
1413 stream <<
" \"" << x1name <<
"\" -> \"" << x2name <<
"\" [label=\"" << BASE::EventName(tit->Ev) <<
"\"];" << std::endl;
1415 stream <<
"};" << std::endl;
1418 catch (std::ios::failure&) {
1419 throw Exception(
"TaGenerator::DotWrite",
1420 "Exception opening/writing dotfile \""+rFileName+
"\"", 3);
1422 BASE::ClearMinStateIndexMap();
1427 typename std::map<Idx,ColorSet>
1428 THIS::StateColorMap(
void)
const {
1429 StateSet::Iterator lit;
1430 std::map<Idx,ColorSet> map;
1431 StateSet cstates = ColoredStates();
1432 for (lit = cstates.
Begin(); lit != cstates.
End(); lit++) {
1433 map.insert(std::make_pair(*lit, Colors(*lit)));
1434 FD_DF(
"make_pair(" <<
ToStringInteger(*lit) <<
", " << (THIS::Colors(*lit).ToString()) <<
")");
1441 StateSet::Iterator lit;
1443 for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1444 if(ExistsColor(*lit, colorIndex)) coloredStates.
Insert(*lit);
1446 return coloredStates;
1451 Idx colorIndex = ColorIndex(rColorName);
1452 return ColoredStates(colorIndex);
1457 StateSet::Iterator lit;
1459 for(lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1460 if(IsColored(*lit)) coloredStates.
Insert(*lit);
1462 return coloredStates;
1467 StateSet::Iterator lit;
1469 for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1470 if (!IsColored(*lit)) uncoloredStates.
Insert(*lit);
1472 return uncoloredStates;
1476 TEMP bool THIS::IsColored(
void)
const {
1477 StateSet::Iterator lit;
1478 for(lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1479 if(IsColored(*lit))
return true;
1486 #ifdef FAUDES_CHECKED
1488 return !BASE::pStates->Attribute(stateIndex).Colors().Empty();
1491 std::stringstream errstr;
1492 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1493 throw Exception(
"MtcSystem::IsColored(stateIndex)", errstr.str(), 200);
1496 return !BASE::pStates->Attribute(stateIndex).Colors().Empty();
1502 StateSet stronglyCoac, notStronglyCoac;
1504 stronglyCoac = BASE::States();
1507 ColorSet::Iterator cit = colors.
Begin();
1508 ColorSet::Iterator cit_end = colors.
End();
1510 for (; cit != cit_end; cit++) {
1512 StateSet states = ColoredStates(*cit);
1515 tmp2 = stronglyCoac * tmp1;
1516 stronglyCoac = tmp2;
1519 return stronglyCoac;
1523 StateSet stronglyCoacSet = StronglyCoaccessibleSet();
1524 StateSet notStronglyCoac = BASE::States() - stronglyCoacSet;
1525 BASE::DelStates(notStronglyCoac);
1526 return (StronglyCoaccessibleSet() == BASE::States());
1530 return (StronglyCoaccessibleSet() == BASE::States());
1535 FD_DF(
"TmtcGenerator::StronglyTrimSet: trim states: "
1536 <<
StateSet(BASE::AccessibleSet() * StronglyCoaccessibleSet()).ToString());
1537 return BASE::AccessibleSet() * StronglyCoaccessibleSet();
1545 FD_DF(
"TmtcGenerator::StronglyTrim: trim states: " << (THIS::mpStates->ToString()));
1546 if (accessiblebool && coaccessiblebool) {
1547 FD_DF(
"TmtcGenerator::StronglyTrim: generator is trim");
1550 FD_DF(
"TmtcGenerator::StronglyTrim: generator is not trim");
1557 FD_DF(
"TmtcGenerator::IsStronglyTrim: generator is strongly trim");
1561 FD_DF(
"TmtcGenerator::IsStronglyTrim: generator is not strongly trim");
1567 TEMP void THIS::NewColorSymbolTable() {
1569 StateSet::Iterator lit;
1572 StateSet coloredStates = ColoredStates();
1573 coloredStates.
Write();
1578 for (lit = coloredStates.
Begin(); lit != coloredStates.
End(); lit++) {
1580 attr = BASE::pStates->Attributep(*lit);
1581 ColorSet::Iterator cit, cit_end;
1582 cit_end = attr->ColorsEnd();
1584 for (cit = attr->ColorsBegin(); cit != cit_end; cit++) {
1585 #ifdef FAUDES_CHECKED
1588 tmp->
InsEntry(*cit, attr->Colors().SymbolicName(*cit));
1591 std::stringstream errstr;
1592 errstr <<
"Could not insert index \"" << *cit <<
"\" and symbol \"" <<
1593 attr->Colors().SymbolicName(*cit) <<
"\" to symbol table" << std::endl;
1594 throw Exception(
"MtcSystem::NewColorSymbolTable()", errstr.str(), 44);
1597 tmp->
InsEntry(*cit, attr->Colors().SymbolicName(*cit));
1601 mpColorSymbolTable = tmp;
1602 attr->ColorSymTab(tmp);
1607 TEMP void THIS::ClearStateAttributes() {
1609 BASE::ClearStateAttributes();
1616 std::set<Idx> colcount;
1617 StateSet::Iterator sit;
1618 for(sit = BASE::StatesBegin(); sit != BASE::StatesEnd(); sit++) {
1619 colcount.insert(Colors(*sit).Size());
1622 std::multiset<Idx>::iterator cit;
1623 std::stringstream countstr;
1624 for(cit = colcount.begin(); cit != colcount.end(); cit++) {
1625 countstr << *cit <<
" ";
1628 BASE::DoSWrite(rTw);