📄 from_l2.cpp
字号:
#ifdef ENABLE_L2_DEBUG_SECTIONS
if(has_debug_info) {
cls = new dbg_L2rClause(component, clause_index,
nclause_props, clause_props, false);
} else
#endif
{
cls = new L2rClause(clause_index,
nclause_props, clause_props, false);
}
dest()->setClause(clause_index,cls);
verbose(cls->toOStream_long(_STD_ cout));
}
return true;
}
/***************************************************************************
transition sections (nominal, failure)
***************************************************************************/
bool from_l2::parse_one_transition(bool has_debug_info, bool isNominal) {
if(!readline()) return false;
#ifdef ENABLE_L2_DEBUG_SECTIONS
char dbg_name[MBA_MAX_LINE_SIZE];
if(has_debug_info) {
str_get_next_word(dbg_name, index, line);
if(!skip_word_boundary()) return false;
}
#endif
int varID = read_integer("mode variable");
if(varID<0) return false;
if(!skip_word_boundary()) return false;
int from = read_integer("from mode", true);
// L2rTransition::ANY_MODE is negative but ok
if (from < 0 && from != L2rTransition::ANY_MODE) return false;
if(!skip_word_boundary()) return false;
int to = read_integer("to mode");
if(to<0) return false;
int rank;
if(isNominal) rank=0; // kill a potential 'uninitialized' warning
else {
if(!skip_word_boundary()) return false;
rank = read_integer("probability");
if(rank<0) return false;
}
// now, error-check all those
const L2rVariable *mode = getVar(varID);
if(!mode) return false;
switch(mode->kind()) {
case vk_unknown:
dest()->getVar_friend(varID)->setKind(vk_mode);
break;
case vk_mode:
break;
default:
tostream(_STD_ cerr<<"Transition on " << mode->kind()
<< " variable " << mode << _STD_ endl);
return false;
}
if(from != L2rTransition::ANY_MODE && !mode->type()->isValidMember(from)) {
return false;
}
if(!mode->type()->isValidMember(to)) return false;
// ok; read the clauses: two scans -- first count,
// next store
unsigned first_clause = index;
unsigned x_nclauses = 0;
while(skip_word_boundary(false)) {
int clsID = read_integer("prerequisite clause");
if(clsID<0) return false;
x_nclauses++;
}
// second scan; store
index = first_clause;
const L2rClause ** x_clauses;
if(x_nclauses == 0) {
x_clauses = 0;
} else {
x_clauses = L2_alloc_array(const L2rClause* , x_nclauses);
for(unsigned i=0; i<x_nclauses; i++) {
if(!skip_word_boundary(true)) return false;
int clsID = read_integer("prerequisite clause");
if(clsID<0) return false;
const L2rClause *the_clause = getClause(clsID);
if(!the_clause) return false;
x_clauses[i] = getClause(clsID);
}
}
L2rTransition *the_x;
#ifdef ENABLE_L2_DEBUG_SECTIONS
if(has_debug_info) {
the_x = new dbg_L2rTransition(dbg_name,
mode, from, to,
x_nclauses, x_clauses, false, // don't copy
isNominal, rank);
} else
#endif
{
the_x = new L2rTransition(
mode, from, to,
x_nclauses, x_clauses, false, // don't copy
isNominal, rank);
}
dest()->addTransition(the_x);
#ifdef WITH_OSTREAM
// debug out
verbose(the_x->toOStream_long(_STD_ cout));
#endif
return true;
}
bool from_l2::parse_transitions(bool has_debug_info) {
// check prereqs
if(!check_parsed( was_enums_parsed(), true, "enums"))
return false;
if(!check_parsed( was_variables_parsed(), true, "variables"))
return false;
if(!check_parsed( was_clauses_parsed(), true, "clauses"))
return false;
// check we haven't already parsed
if(!check_parsed( was_transitions_parsed(), false, "transitions"))
return false;
didTransitions_ = true;
// first line: count of number of nominal transitions
readline();
int nominal = read_integer("number of nominal transitions");
if(nominal<0) return false;
// second line: failures
readline();
int failure = read_integer("number of failure transitions");
if(failure<0) return false;
unsigned ntransitions = nominal+failure;
verbose(_STD_ cout<< "\n* Reading " << ntransitions << " transitions ("
<< nominal << " nominal, " << failure << " failure)\n");
// read n transitions
unsigned x_index = 0;
for( ; x_index < (unsigned)nominal; x_index++)
if(!parse_one_transition(has_debug_info, true))
return false;
for( ; x_index < ntransitions; x_index++)
if(!parse_one_transition(has_debug_info, false))
return false;
return true;
}
/***************************************************************************
observed, commands, modes sections
The three are very similar.
***************************************************************************/
bool from_l2::parse_var_modifier(VarKind kind) {
// check prereqs
if(!check_parsed( was_variables_parsed(), true, "variables"))
return false;
// initialize to squash warnings
const char *kindname=0;
const char *sectionname=0;
bool wasParsed=false;
switch(kind) {
case vk_observed:
kindname = "observed";
sectionname = "observed";
wasParsed = was_observed_parsed();
didObserved_ = true;
break;
case vk_commanded:
kindname = "commanded";
sectionname = "commands";
wasParsed = was_commands_parsed();
didCommands_ = true;
break;
case vk_mode:
kindname = "mode";
sectionname = "modes";
wasParsed = was_modes_parsed();
didModes_ = true;
break;
default:
print_parse_error();
tostream(_STD_ cerr<<"Unhandled kind " << kind << _STD_ endl);
return false;
}
if(!check_parsed(wasParsed, false, sectionname)) return false;
verbose(_STD_ cout<<"\n* Setting `" << kindname << "' flag\n");
if(!readline()) {
print_parse_error();
tostream(_STD_ cerr<<"Reached EOF in "<<sectionname<<" section\n");
return false;
}
// read to the end of the line, but don't complain about it
while(skip_word_boundary(false)) {
int varID = read_integer("var ID");
if(varID<0) return false;
if(!getVar(varID)) return false;
VarKind oldkind = getVar(varID)->kind();
if(oldkind != vk_unknown && oldkind != kind) {
tostream(_STD_ cerr << "Changing kind of "<<getVar(varID)<< " from "
<< oldkind << " to " << kind << _STD_ endl);
return false;
}
// can't use getVar since it returns const
dest()->getVar_friend(varID)->setKind(kind);
verbose(_STD_ cout << " " << getVar(varID) << _STD_ endl);
}
return true;
}
bool from_l2::parse_observed(bool /*has_debug_info*/) {
return parse_var_modifier(vk_observed);
}
bool from_l2::parse_commands(bool /*has_debug_info*/) {
return parse_var_modifier(vk_commanded);
}
bool from_l2::parse_modes(bool /*has_debug_info*/) {
return parse_var_modifier(vk_mode);
}
/***************************************************************************
background section
***************************************************************************/
bool from_l2::parse_background(bool) {
// prereqs
if(!check_parsed( was_clauses_parsed(), true, "clauses"))
return false;
// don't parse twice
if(!check_parsed( was_background_parsed(), false, "background"))
return false;
// first line: a count
if(!readline()) {
tostream(_STD_ cerr << "Expected a count\n");
return false;
}
int count = read_integer("number of variables");
if(count<0) return false;
unsigned nbackground = count;
verbose(_STD_ cout <<"\n* Reading "<< nbackground
<< " background clauses\n");
for(unsigned i=0 ; i < nbackground; i++) {
if(!readline()) {
print_parse_error();
tostream(_STD_ cerr<<"Missing " << (nbackground-i) << " clauses\n");
return false;
}
int clauseID = read_integer("clause ID");
if(clauseID < 0) return false;
const L2rClause *cls = getClause(clauseID);
if(!cls) return false;
dest()->addBackground(cls);
verbose(_STD_ cout<< " " << cls << _STD_ endl);
}
return true;
}
/***************************************************************************
initial section
***************************************************************************/
bool from_l2::parse_initial(bool) {
// prereqs
if(!check_parsed( was_enums_parsed(), true, "enums"))
return false;
if(!check_parsed( was_variables_parsed(), true, "variables"))
return false;
// don't parse twice
if(!check_parsed( was_initial_parsed(), false, "initial"))
return false;
// get count
if(!readline()) {
tostream(_STD_ cerr << "Expected a count\n");
return false;
}
int count = read_integer("number of initial mode assignments");
verbose(_STD_ cout<<"\n* Reading "<< count << " initial modes\n");
for(int i=0; i<count; i++) {
if(!readline()) {
tostream(_STD_ cerr << "Expected " << (count-i) << " more lines\n");
return false;
}
int varID = read_integer("var ID");
if(varID<0) return false;
if(!skip_word_boundary()) {
tostream(_STD_ cerr << "Expected memberID\n");
return false;
}
int memberID = read_integer("member ID");
if(memberID<0) return false;
L2rVariable *var = dest()->getVar_friend(varID);
if(!var) return false;
if(!var->type()->isValidMember(memberID)) return false;
var->setInitial(memberID);
}
return true;
}
/***************************************************************************
debugging mainline
***************************************************************************/
/**
* An entry point for stand-alone testing.
* \param argc must equal 2 (one command-line argument)
* \param argv argv[1] is the L2 model file pathname
* \return 0 if successfully read; otherwise 1
*/
#ifdef PARSER_MAIN
int main(int argc, char **argv) {
from_l2 reader;
if(argc!=2) {
tostream(_STD_ cerr<< "USAGE: parser <file>\n"
<< "The 'file' argument includes the extension (.l2)\n");
return 2;
}
if(reader.read(argv[1]))
return 0;
else
return 1;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -