Browse Source

Further work on explicit MarkovAutomaton parser.

Former-commit-id: 19fbff695b
tempestpy_adaptions
dehnert 11 years ago
parent
commit
873373eb4e
  1. 243
      src/parser/MarkovAutomataSparseTransitionParser.cpp
  2. 7
      src/parser/MarkovAutomataSparseTransitionParser.h

243
src/parser/MarkovAutomataSparseTransitionParser.cpp

@ -3,62 +3,49 @@
namespace storm {
namespace parser {
MarkovAutomataSparseTransitionParser::FirstPassResult MarkovAutomataSparseTransitionParser::performFirstPass(char* buf, SupportedLineEndingsEnum lineEndings, RewardMatrixInformationStruct* rewardMatrixInformation) {
MarkovAutomataSparseTransitionParser::FirstPassResult MarkovAutomataSparseTransitionParser::firstPass(char* buf, SupportedLineEndingsEnum lineEndings, RewardMatrixInformationStruct* rewardMatrixInformation) {
bool isRewardFile = rewardMatrixInformation != nullptr;
MarkovAutomataSparseTransitionParser::FirstPassResult result;
/*
* Check file header and extract number of transitions.
*/
if (!isRewardFile) {
// skip format hint
buf = storm::parser::forwardToNextLine(buf, lineEndings);
}
// Skip the format hint.
buf = storm::parser::forwardToNextLine(buf, lineEndings);
/*
* Read all transitions.
*/
int_fast64_t source, target, choice, lastchoice = -1;
int_fast64_t source, target = -1;
int_fast64_t lastsource = -1;
uint_fast64_t nonzero = 0;
double val;
result.numberOfChoices = 0;
result.numberOfStates = 0;
bool isMarkovianChoice = false;
char actionNameBuffer[20];
char star[20];
bool encounteredEOF = false;
while (buf[0] != '\0') {
while (buf[0] != '\0' && !encounteredEOF) {
/*
* Read source state and choice.
*/
source = checked_strtol(buf, &buf);
// Check if we encountered a state index that is bigger than all previously seen ones and record it if necessary.
if (source > result.numberOfStates) {
result.numberOfStates = source;
if (source > result.highestStateIndex) {
result.highestStateIndex = source;
}
if (isRewardFile) {
// FIXME: Fill this
// If we have skipped some states, we need to reserve the space for the self-loop insertion in the second pass.
if (source > lastsource + 1) {
result.numberOfNonzeroEntries += source - lastsource - 1;
result.numberOfChoices += source - lastsource - 1;
} else {
// If we have skipped some states, we need to reserve the space for the self-loop insertion in the second pass.
if (source > lastsource + 1) {
nonzero += source - lastsource - 1;
result.numberOfChoices += source - lastsource - 1;
} else if (source != lastsource) {
// If we have switched the source state, we need to reserve one row more.
++result.numberOfChoices;
}
++result.numberOfChoices;
}
// Record that the current source was the last source.
lastsource = source;
char actionNameBuffer[20];
#ifdef WINDOWS
int length = sscanf_s(buf, "%20s\n", actionNameBuffer, 20);
#else
int length = sscanf(buf, "%20s\n", actionNameBuffer);
#endif
// If the number of arguments filled is not one, there was an error.
if (length != 1) {
LOG4CPLUS_ERROR(logger, "Parsing error.");
@ -69,6 +56,7 @@ namespace storm {
}
// Depending on the action name, the choice is either a probabilitic one or a markovian one.
bool isMarkovianChoice = false;
if (strcmp(actionNameBuffer, "!") == 0) {
isMarkovianChoice = true;
} else {
@ -96,139 +84,89 @@ namespace storm {
// Now that we have the source state and the information whether or not the current choice is probabilistic or Markovian, we need to read the list of successors and the probabilities/rates.
bool hasSuccessorState = false;
bool encounteredNewDistribution = false;
// At this point, we need to check whether there is an additional successor or we have reached the next choice for the same or a different state.
do {
// Now parse the next symbol to see whether there is another target state for the current choice
// or not.
char star[1];
#ifdef WINDOWS
length = sscanf_s(buf, "%20s\n", star, 20);
length = sscanf_s(buf, "%1s\n", star, 1);
#else
length = sscanf(buf, "%20s\n", star);
length = sscanf(buf, "%1s\n", star);
#endif
// If the number of arguments filled is not one, there was an error.
if (length == EOF) {
if (!hasSuccessorState) {
LOG4CPLUS_ERROR(logger, "Premature end-of-file. Expected at least one successor state for state " << source << " under action " << actionNameBuffer << ".");
throw storm::exceptions::WrongFormatException() << "Premature end-of-file. Expected at least one successor state for state " << source << " under action " << actionNameBuffer << ".";
// If the number of arguments filled is not one, there was an error.
if (length == EOF) {
if (!hasSuccessorState) {
LOG4CPLUS_ERROR(logger, "Premature end-of-file. Expected at least one successor state for state " << source << " under action " << actionNameBuffer << ".");
throw storm::exceptions::WrongFormatException() << "Premature end-of-file. Expected at least one successor state for state " << source << " under action " << actionNameBuffer << ".";
} else {
// If there was at least one successor for the current choice, this is legal and we need to move on.
encounteredEOF = true;
}
} else if (length != 1) {
LOG4CPLUS_ERROR(logger, "Parsing error.");
throw storm::exceptions::WrongFormatException() << "Parsing error.";
} else if (strcmp(star, "*") == 0) {
// We need to record that we found at least one successor state for the current choice.
hasSuccessorState = true;
// As we have encountered a "*", we know that there is an additional successor state for the current choice.
buf += strlen(star);
// Now we need to read the successor state and check if we already saw a higher state index.
target = checked_strtol(buf, &buf);
if (target > result.highestStateIndex) {
result.highestStateIndex = target;
}
// And the corresponding probability/rate.
double val = checked_strtod(buf, &buf);
if (val <= 0.0) {
LOG4CPLUS_ERROR(logger, "Illegal probability/rate value " << val << ".");
throw storm::exceptions::WrongFormatException() << "Illegal probability/rate value " << val << ".";
}
// As we found a new successor, we need to increase the number of nonzero entries.
++result.numberOfNonzeroEntries;
// Proceed to beginning of next line.
switch (lineEndings) {
case SupportedLineEndingsEnum::SlashN:
buf += strcspn(buf, " \t\n");
break;
case SupportedLineEndingsEnum::SlashR:
buf += strcspn(buf, " \t\r");
break;
case SupportedLineEndingsEnum::SlashRN:
buf += strcspn(buf, " \t\r\n");
break;
default:
case storm::parser::SupportedLineEndingsEnum::Unsupported:
// This line will never be reached as the parser would have thrown already.
throw;
break;
}
buf = trimWhitespaces(buf);
} else {
// If there was at least one successor for the current choice, this is legal and we need to move on.
encounteredEOF = true;
}
} else if (length != 1) {
LOG4CPLUS_ERROR(logger, "Parsing error.");
throw storm::exceptions::WrongFormatException() << "Parsing error.";
} else if (strcmp(star, "*") == 0) {
// As we have encountered a "*", we know that there is an additional successor state for the current choice.
++result.numberOfNonzeroEntries;
buf += strlen(star);
} else {
// If it was not a "*", we have to assume that we encountered the beginning of a new choice definition. In this case, we don't move the pointer
// to the buffer, because we still need to read the new source state.
}
std::cout << "Got here! " << isMarkovianChoice << " // " << actionNameBuffer << " // " << strlen(actionNameBuffer) << std::endl;
if (isRewardFile) {
// If we have switched the source state, we possibly need to insert the rows of the last
// last source state.
if (source != lastsource && lastsource != -1) {
result.numberOfChoices += lastchoice - ((*rewardMatrixInformation->nondeterministicChoiceIndices)[lastsource + 1] - (*rewardMatrixInformation->nondeterministicChoiceIndices)[lastsource] - 1);
}
// If we skipped some states, we need to reserve empty rows for all their nondeterministic
// choices.
for (int_fast64_t i = lastsource + 1; i < source; ++i) {
result.numberOfChoices += ((*rewardMatrixInformation->nondeterministicChoiceIndices)[i + 1] - (*rewardMatrixInformation->nondeterministicChoiceIndices)[i]);
}
// If we advanced to the next state, but skipped some choices, we have to reserve rows for them.
if (source != lastsource) {
result.numberOfChoices += choice + 1;
} else if (choice != lastchoice) {
result.numberOfChoices += choice - lastchoice;
// If it was not a "*", we have to assume that we encountered the beginning of a new choice definition. In this case, we don't move the pointer
// to the buffer, because we still need to read the new source state.
encounteredNewDistribution = true;
}
} else {
// If we have skipped some states, we need to reserve the space for the self-loop insertion
// in the second pass.
if (source > lastsource + 1) {
nonzero += source - lastsource - 1;
result.numberOfChoices += source - lastsource - 1;
} else if (source != lastsource || choice != lastchoice) {
// If we have switched the source state or the nondeterministic choice, we need to
// reserve one row more.
++result.numberOfChoices;
}
}
// Read target and check if we encountered a state index that is bigger than all previously
// seen.
target = checked_strtol(buf, &buf);
if (target > result.numberOfStates) {
result.numberOfStates = target;
}
// Read value and check whether it's positive.
val = checked_strtod(buf, &buf);
if (val < 0.0) {
LOG4CPLUS_ERROR(logger, "Expected a positive probability but got \"" << std::string(buf, 0, 16) << "\".");
throw storm::exceptions::InvalidArgumentException() << "Expected a positive probability but got \"" << std::string(buf, 0, 16) << "\".";
}
lastchoice = choice;
lastsource = source;
/*
* Increase number of non-zero values.
*/
nonzero++;
// The PRISM output format lists the name of the transition in the fourth column,
// but omits the fourth column if it is an internal action. In either case, however, the third column
// is followed by a space. We need to skip over that space first (instead of trimming whitespaces),
// before we can skip to the line end, because trimming the white spaces will proceed to the next line
// in case there is no action label in the fourth column.
if (buf[0] == ' ') {
++buf;
}
/*
* Proceed to beginning of next line.
*/
switch (lineEndings) {
case SupportedLineEndingsEnum::SlashN:
buf += strcspn(buf, " \t\n");
break;
case SupportedLineEndingsEnum::SlashR:
buf += strcspn(buf, " \t\r");
break;
case SupportedLineEndingsEnum::SlashRN:
buf += strcspn(buf, " \t\r\n");
break;
default:
case storm::parser::SupportedLineEndingsEnum::Unsupported:
// This Line will never be reached as the Parser would have thrown already.
throw;
break;
}
buf = trimWhitespaces(buf);
}
if (isRewardFile) {
// If not all rows were filled for the last state, we need to insert them.
result.numberOfChoices += lastchoice - ((*rewardMatrixInformation->nondeterministicChoiceIndices)[lastsource + 1] - (*rewardMatrixInformation->nondeterministicChoiceIndices)[lastsource] - 1);
// If we skipped some states, we need to reserve empty rows for all their nondeterministic
// choices.
for (uint_fast64_t i = lastsource + 1; i < rewardMatrixInformation->nondeterministicChoiceIndices->size() - 1; ++i) {
result.numberOfChoices += ((*rewardMatrixInformation->nondeterministicChoiceIndices)[i + 1] - (*rewardMatrixInformation->nondeterministicChoiceIndices)[i]);
}
} while (!encounteredEOF && !encounteredNewDistribution);
}
exit(-1);
return result;
}
MarkovAutomataSparseTransitionParser::ResultType MarkovAutomataSparseTransitionParser::secondPass(char* buf, SupportedLineEndingsEnum lineEndings, FirstPassResult const& firstPassResult, RewardMatrixInformationStruct* rewardMatrixInformation) {
return ResultType();
}
MarkovAutomataSparseTransitionParser::ResultType MarkovAutomataSparseTransitionParser::parseMarkovAutomataTransitions(std::string const& filename, RewardMatrixInformationStruct* rewardMatrixInformation) {
/*
* Enforce locale where decimal point is '.'.
@ -253,10 +191,7 @@ namespace storm {
MappedFile file(filename.c_str());
char* buf = file.data;
FirstPassResult firstPassResult = performFirstPass(buf, lineEndings, rewardMatrixInformation);
return ResultType();
return secondPass(buf, lineEndings, firstPass(buf, lineEndings, rewardMatrixInformation), rewardMatrixInformation);
}
}

7
src/parser/MarkovAutomataSparseTransitionParser.h

@ -19,19 +19,20 @@ namespace storm {
struct FirstPassResult {
FirstPassResult() : numberOfNonzeroEntries(0), numberOfStates(0), numberOfChoices(0) {
FirstPassResult() : numberOfNonzeroEntries(0), highestStateIndex(0), numberOfChoices(0) {
// Intentionally left empty.
}
uint_fast64_t numberOfNonzeroEntries;
uint_fast64_t numberOfStates;
uint_fast64_t highestStateIndex;
uint_fast64_t numberOfChoices;
};
static ResultType parseMarkovAutomataTransitions(std::string const& filename, RewardMatrixInformationStruct* rewardMatrixInformation = nullptr);
private:
static FirstPassResult performFirstPass(char* buf, SupportedLineEndingsEnum lineEndings, RewardMatrixInformationStruct* rewardMatrixInformation = nullptr);
static FirstPassResult firstPass(char* buf, SupportedLineEndingsEnum lineEndings, RewardMatrixInformationStruct* rewardMatrixInformation = nullptr);
static ResultType secondPass(char* buf, SupportedLineEndingsEnum lineEndings, FirstPassResult const& firstPassResult, RewardMatrixInformationStruct* rewardMatrixInformation = nullptr);
};
} // namespace parser

Loading…
Cancel
Save