Parsing, printing... Context in CA

This commit is contained in:
Artur Meski
2018-03-29 17:01:28 +01:00
parent 38683e1041
commit 212321c6cb
9 changed files with 71 additions and 40 deletions

View File

@@ -73,7 +73,6 @@ void CtxAut::showStates(void)
{ {
cout << "# Context Automaton States:" << endl; cout << "# Context Automaton States:" << endl;
cout << " = Init state: " << getStateName(init_state_id) << endl; cout << " = Init state: " << getStateName(init_state_id) << endl;
for (const auto &s : states_ids) { for (const auto &s : states_ids) {
cout << " * " << s << endl; cout << " * " << s << endl;
} }
@@ -84,6 +83,15 @@ void CtxAut::pushContextEntity(Entity entity_id)
tmpEntities.insert(entity_id); tmpEntities.insert(entity_id);
} }
void CtxAut::saveCurrentContextSet(Process proc_id)
{
if (!parent_rctsys->hasProcess(proc_id)) {
FERROR("No such process: ID=" << proc_id);
}
tmpProcEntities[proc_id] = tmpEntities;
tmpEntities.clear();
}
void CtxAut::addTransition(std::string srcStateName, std::string dstStateName) void CtxAut::addTransition(std::string srcStateName, std::string dstStateName)
{ {
VERB_L3("Saving transition"); VERB_L3("Saving transition");
@@ -91,7 +99,7 @@ void CtxAut::addTransition(std::string srcStateName, std::string dstStateName)
CtxAutTransition new_transition; CtxAutTransition new_transition;
new_transition.src_state = getStateID(srcStateName); new_transition.src_state = getStateID(srcStateName);
new_transition.ctx = tmpEntities; new_transition.ctx = tmpProcEntities;
tmpEntities.clear(); tmpEntities.clear();
new_transition.dst_state = getStateID(dstStateName); new_transition.dst_state = getStateID(dstStateName);
transitions.push_back(new_transition); transitions.push_back(new_transition);
@@ -100,11 +108,10 @@ void CtxAut::addTransition(std::string srcStateName, std::string dstStateName)
void CtxAut::showTransitions(void) void CtxAut::showTransitions(void)
{ {
cout << "# Context Automaton Transitions:" << endl; cout << "# Context Automaton Transitions:" << endl;
for (const auto &t : transitions) { for (const auto &t : transitions) {
cout << " * [" << getStateName(t.src_state) << " -> " << getStateName( cout << " * [" << getStateName(t.src_state) << " -> " << getStateName(
t.dst_state) t.dst_state)
<< "]: {" << parent_rctsys->entitiesToStr(t.ctx) << "}" << endl; << "]: {" << parent_rctsys->procEntitiesToStr(t.ctx) << "}" << endl;
} }
} }

View File

@@ -1,9 +1,6 @@
/* /*
Copyright (c) 2018 Copyright (c) 2018
Artur Meski <meski@ipipan.waw.pl> Artur Meski <meski@ipipan.waw.pl>
Reuse of the code or its part for any purpose
without the author's permission is strictly prohibited.
*/ */
#ifndef RS_CTX_AUT_HH #ifndef RS_CTX_AUT_HH
@@ -42,6 +39,7 @@ class CtxAut
void addTransition(std::string srcStateName, std::string dstStateName); void addTransition(std::string srcStateName, std::string dstStateName);
void showTransitions(void); void showTransitions(void);
void pushContextEntity(Entity entity_id); void pushContextEntity(Entity entity_id);
void saveCurrentContextSet(Process proc_id);
void setOptions(Options *opts) void setOptions(Options *opts)
{ {
this->opts = opts; this->opts = opts;
@@ -58,6 +56,7 @@ class CtxAut
StatesByName states_names; StatesByName states_names;
State init_state_id; State init_state_id;
bool init_state_defined; bool init_state_defined;
EntitiesForProc tmpProcEntities;
Entities tmpEntities; Entities tmpEntities;
CtxAutTransitions transitions; CtxAutTransitions transitions;
}; };

View File

@@ -1,9 +1,6 @@
/* /*
Copyright (c) 2012, 2013 Copyright (c) 2012, 2013, 2018
Artur Meski <meski@ipipan.waw.pl> Artur Meski <meski@ipipan.waw.pl>
Reuse of the code or its part for any purpose
without the author's permission is strictly prohibited.
*/ */
#ifndef RS_MAIN_HH #ifndef RS_MAIN_HH

36
rs.cc
View File

@@ -1,9 +1,6 @@
/* /*
Copyright (c) 2012-2014 Copyright (c) 2012-2018
Artur Meski <meski@ipipan.waw.pl> Artur Meski <meski@ipipan.waw.pl>
Reuse of the code or its part for any purpose
without the author's permission is strictly prohibited.
*/ */
#include "rs.hh" #include "rs.hh"
@@ -94,18 +91,25 @@ bool RctSys::hasProcess(std::string processName)
} }
} }
bool RctSys::hasProcess(Process processID)
{
if (processID >= processes_ids.size()) {
return false;
}
return true;
}
Process RctSys::getProcessID(std::string processName) Process RctSys::getProcessID(std::string processName)
{ {
if (!hasProcess(processName)) { if (!hasProcess(processName)) {
FERROR("No such process: " << processName); FERROR("No such process: " << processName);
} }
return processes_names[processName]; return processes_names[processName];
} }
std::string RctSys::getProcessName(Process processID) std::string RctSys::getProcessName(Process processID)
{ {
if (processID < processes_ids.size()) { if (hasProcess(processID)) {
return processes_ids[processID]; return processes_ids[processID];
} }
else { else {
@@ -118,7 +122,6 @@ void RctSys::pushReactant(std::string entityName)
if (!hasEntity(entityName)) { if (!hasEntity(entityName)) {
addEntity(entityName); addEntity(entityName);
} }
tmpReactants.insert(getEntityID(entityName)); tmpReactants.insert(getEntityID(entityName));
} }
void RctSys::pushInhibitor(std::string entityName) void RctSys::pushInhibitor(std::string entityName)
@@ -126,7 +129,6 @@ void RctSys::pushInhibitor(std::string entityName)
if (!hasEntity(entityName)) { if (!hasEntity(entityName)) {
addEntity(entityName); addEntity(entityName);
} }
tmpInhibitors.insert(getEntityID(entityName)); tmpInhibitors.insert(getEntityID(entityName));
} }
void RctSys::pushProduct(std::string entityName) void RctSys::pushProduct(std::string entityName)
@@ -134,7 +136,6 @@ void RctSys::pushProduct(std::string entityName)
if (!hasEntity(entityName)) { if (!hasEntity(entityName)) {
addEntity(entityName); addEntity(entityName);
} }
tmpProducts.insert(getEntityID(entityName)); tmpProducts.insert(getEntityID(entityName));
} }
@@ -175,6 +176,17 @@ std::string RctSys::entitiesToStr(const Entities &entities)
return s; return s;
} }
std::string RctSys::procEntitiesToStr(const EntitiesForProc &procEntities)
{
std::string s = " ";
for (auto const &p : procEntities)
{
s += getProcessName(p.first) + "={" + entitiesToStr(p.second) + "} ";
}
return s;
}
void RctSys::showReactions(void) void RctSys::showReactions(void)
{ {
cout << "# Reactions:" << endl; cout << "# Reactions:" << endl;
@@ -325,4 +337,10 @@ void RctSys::ctxAutPushNamedContextEntity(std::string entityName)
ctx_aut->pushContextEntity(entity_id); ctx_aut->pushContextEntity(entity_id);
} }
void RctSys::ctxAutSaveCurrentContextSet(std::string processName)
{
Process processID = getProcessID(processName);
ctx_aut->saveCurrentContextSet(processID);
}
/** EOF **/ /** EOF **/

3
rs.hh
View File

@@ -44,6 +44,7 @@ class RctSys
void setCurrentProcess(std::string processName); void setCurrentProcess(std::string processName);
void addProcess(std::string processName); void addProcess(std::string processName);
bool hasProcess(Process processID);
bool hasProcess(std::string processName); bool hasProcess(std::string processName);
Process getProcessID(std::string processName); Process getProcessID(std::string processName);
std::string getProcessName(Process processID); std::string getProcessName(Process processID);
@@ -59,6 +60,7 @@ class RctSys
return entities_ids[entity]; return entities_ids[entity];
} }
std::string entitiesToStr(const Entities &entities); std::string entitiesToStr(const Entities &entities);
std::string procEntitiesToStr(const EntitiesForProc &procEntities);
void showReactions(void); void showReactions(void);
void pushStateEntity(std::string entityName); void pushStateEntity(std::string entityName);
void commitInitState(void); void commitInitState(void);
@@ -90,6 +92,7 @@ class RctSys
void ctxAutSetInitState(std::string stateName); void ctxAutSetInitState(std::string stateName);
void ctxAutAddTransition(std::string srcStateName, std::string dstStateName); void ctxAutAddTransition(std::string srcStateName, std::string dstStateName);
void ctxAutPushNamedContextEntity(std::string entity_name); void ctxAutPushNamedContextEntity(std::string entity_name);
void ctxAutSaveCurrentContextSet(std::string processName);
bool initStatesDefined(void) bool initStatesDefined(void)
{ {

View File

@@ -38,16 +38,16 @@ blank [ \t]
typedef yy::rsin_parser::token token; typedef yy::rsin_parser::token token;
%} %}
"options" return token::OPTIONS; "options" return token::OPTIONS;
"use-context-automaton" return token::USE_CTX_AUT; "use-context-automaton" return token::USE_CTX_AUT;
"use-concentrations" return token::USE_CONCENTRATIONS; "use-concentrations" return token::USE_CONCENTRATIONS;
"reactions" return token::REACTIONS; "reactions" return token::REACTIONS;
"initial-contexts" return token::INITIALCONTEXTS; "initial-contexts" return token::INITIALCONTEXTS;
"context-entities" return token::CONTEXTENTITIES; "context-entities" return token::CONTEXTENTITIES;
"context-automaton" return token::CONTEXTAUTOMATON; "context-automaton" return token::CONTEXTAUTOMATON;
"transitions" return token::TRANSITIONS; "transitions" return token::TRANSITIONS;
"states" return token::STATES; "states" return token::STATES;
"init-state" return token::INITSTATE; "init-state" return token::INITSTATE;
"rsctl-property" return token::RSCTLFORM; "rsctl-property" return token::RSCTLFORM;
"{" return token::LCB; "{" return token::LCB;
"}" return token::RCB; "}" return token::RCB;
@@ -55,16 +55,16 @@ blank [ \t]
")" return token::RRB; ")" return token::RRB;
"[" return token::LSB; "[" return token::LSB;
"]" return token::RSB; "]" return token::RSB;
"=" return token::EQ; "=" return token::EQ;
"<" return token::LAB; "<" return token::LAB;
">" return token::RAB; ">" return token::RAB;
":" return token::COL; ":" return token::COL;
";" return token::SEMICOL; ";" return token::SEMICOL;
"," return token::COMMA; "," return token::COMMA;
"->" return token::RARR; "->" return token::RARR;
"AND" return token::AND; "AND" return token::AND;
"OR" return token::OR; "OR" return token::OR;
"XOR" return token::XOR; "XOR" return token::XOR;
"IMPLIES" return token::IMPLIES; "IMPLIES" return token::IMPLIES;
"~" return token::NOT; "~" return token::NOT;
"EX" return token::EX; "EX" return token::EX;

View File

@@ -106,9 +106,11 @@ option:
*/ */
reactionsets: reactionsets:
| processname LCB reactions RCB SEMICOL | reactionsets process_reactions
; ;
process_reactions: processname LCB reactions RCB SEMICOL
processname: IDENTIFIER { processname: IDENTIFIER {
driver.getReactionSystem()->setCurrentProcess(*$1); driver.getReactionSystem()->setCurrentProcess(*$1);
free($1); free($1);
@@ -227,21 +229,25 @@ auttransitions:
| auttrans SEMICOL auttransitions | auttrans SEMICOL auttransitions
; ;
auttrans: LCB proc_contextset RCB COL IDENTIFIER RARR IDENTIFIER { auttrans: LCB proc_ctxsets RCB COL IDENTIFIER RARR IDENTIFIER {
driver.getReactionSystem()->ctxAutAddTransition(*$5, *$7); driver.getReactionSystem()->ctxAutAddTransition(*$5, *$7);
free($5); free($5);
free($7); free($7);
} }
; ;
proc_contextset: IDENTIFIER EQ LCB contextset RCB { proc_ctxsets:
driver.getReactionSystem()->setCurrentProcess(*$1); | proc_ctxsets single_proc_ctxset
;
single_proc_ctxset: IDENTIFIER EQ LCB contextset RCB {
driver.getReactionSystem()->ctxAutSaveCurrentContextSet(*$1);
free($1); free($1);
} }
; ;
contextset: contextset:
ctxentity | ctxentity
| contextset COMMA ctxentity | contextset COMMA ctxentity
; ;

View File

@@ -581,7 +581,8 @@ void SymRS::encodeCtxAutTrans(void)
<< " -> " << rs->ctx_aut->getStateName(t.dst_state)); << " -> " << rs->ctx_aut->getStateName(t.dst_state));
BDD enc_src = encCtxAutState(t.src_state); BDD enc_src = encCtxAutState(t.src_state);
BDD enc_dst = encCtxAutStateSucc(t.dst_state); BDD enc_dst = encCtxAutStateSucc(t.dst_state);
BDD enc_ctx = compContext(encActEntitiesConj(t.ctx)); assert(0); // enc_ctx
BDD enc_ctx = BDD_FALSE; //compContext(encActEntitiesConj(t.ctx));
*tr_ca += enc_src * enc_ctx * enc_dst; *tr_ca += enc_src * enc_ctx * enc_dst;
} }

View File

@@ -50,7 +50,7 @@ typedef std::vector<int> StateEntityToAction;
struct CtxAutTransition { struct CtxAutTransition {
State src_state; State src_state;
Entities ctx; EntitiesForProc ctx;
State dst_state; State dst_state;
}; };