1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
// Multi-set mapping expression -*- c++ -*-
#ifdef __GNUC__
# pragma implementation
#endif // __GNUC__
#include "Mapping.h"
#include "Marking.h"
#include "VariableDefinition.h"
#include "Valuation.h"
#include "PlaceMarking.h"
#include "LeafValue.h"
#include "Substitution.h"
#include "Variable.h"
#include "Printer.h"
/** @file Mapping.C
* Multi-set mapping operation
*/
/* Copyright 1999-2003 Marko Mkel (msmakela@tcs.hut.fi).
This file is part of MARIA, a reachability analyzer and model checker
for high-level Petri nets.
MARIA is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
MARIA is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
The GNU General Public License is often shipped with GNU software, and
is generally kept in a file called COPYING or LICENSE. If you do not
have a copy of the license, write to the Free Software Foundation,
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
Mapping::Mapping (class VariableDefinition& variable,
class VariableDefinition* cardinality,
class Expression& marking,
class Expression& expr) :
myVariable (&variable), myCardinality (cardinality),
myMarking (&marking), myExpr (&expr)
{
assert (myVariable && myExpr);
assert (myMarking && myMarking->getType ());
assert (!myMarking->isTemporal ());
if (myCardinality) {
assert (myCardinality->getType ().getKind () == Type::tCard);
assert (myExpr->getKind () == Expression::eMarking);
}
else
assert (myExpr->isBasic ());
setType (*myExpr->getType ());
}
Mapping::~Mapping ()
{
delete myVariable;
delete myCardinality;
myMarking->destroy ();
myExpr->destroy ();
}
class PlaceMarking*
Mapping::meval (const class Valuation& valuation) const
{
if (class PlaceMarking* p = myMarking->meval (valuation)) {
p->setPlace (NULL);
class Valuation v (valuation);
class PlaceMarking* q = new class PlaceMarking ();
#ifndef NDEBUG
q->setType (myExpr->getType ());
#endif // NDEBUG
for (PlaceMarking::iterator i = p->begin (); i != p->end (); i++) {
if (!PlaceMarking::getCount (i))
continue;
v.setValue (*myVariable, *PlaceMarking::getValue (i).copy ());
if (myCardinality) {
v.setValue (*myCardinality,
*new class LeafValue (myCardinality->getType (),
PlaceMarking::getCount (i)));
if (!static_cast<const class Marking*>(myExpr)->add (*q, 1, v)) {
error:
valuation.copyErrors (v);
error2:
delete p;
delete q;
return NULL;
}
}
else if (class Value* val = myExpr->eval (v)) {
if (!q->add (*val, PlaceMarking::getCount (i))) {
valuation.flag (errCard, *this);
goto error2;
}
}
else
goto error;
}
delete p;
return q;
}
return NULL;
}
/** Create a Mapping, substituting the iterator variables
* @param variable the old token iterator variable
* @param cardinality the old cardinality iterator variable
* @param marking the marking expression
* @param condition the mapping expression (iterator substituted)
* @return the corresponding Submarking expression
*/
static class Mapping*
newMapping (const class VariableDefinition& variable,
const class VariableDefinition* cardinality,
class Expression& marking,
class Expression& expr)
{
class VariableDefinition& v = *new class VariableDefinition (variable);
class VariableDefinition* c = cardinality ?
new class VariableDefinition (*cardinality)
: 0;
class Substitution s;
s.setExpr (variable, *(new class Variable (v))->cse ());
if (cardinality)
s.setExpr (*cardinality, *(new class Variable (*c))->cse ());
class Expression* e = expr.substitute (s);
assert (!!e);
expr.destroy ();
return static_cast<class Mapping*>
((new class Mapping (v, c, marking, *e))->cse ());
}
class Expression*
Mapping::ground (const class Valuation& valuation,
class Transition* transition,
bool declare)
{
class Expression* marking =
myMarking->ground (valuation, transition, declare);
if (!marking) return NULL;
class Expression* expr =
myExpr->ground (valuation, transition, declare);
if (!expr) { marking->destroy (); return NULL; }
assert (valuation.isOK ());
if (marking == myMarking && expr == myExpr) {
marking->destroy ();
expr->destroy ();
return copy ();
}
else
return newMapping (*myVariable, myCardinality, *marking, *expr);
}
class Expression*
Mapping::substitute (class Substitution& substitution)
{
class Expression* marking = myMarking->substitute (substitution);
class Expression* expr = myExpr->substitute (substitution);
if (!marking || !expr) {
marking->destroy ();
expr->destroy ();
return NULL;
}
if (marking == myMarking && expr == myExpr) {
marking->destroy ();
expr->destroy ();
return copy ();
}
else
return newMapping (*myVariable, myCardinality, *marking, *expr);
}
bool
Mapping::depends (const class VariableSet& vars,
bool complement) const
{
return
myMarking->depends (vars, complement) ||
myExpr->depends (vars, complement);
}
bool
Mapping::forVariables (bool (*operation)
(const class Expression&,void*),
void* data) const
{
return
myMarking->forVariables (operation, data) &&
myExpr->forVariables (operation, data);
}
#ifdef EXPR_COMPILE
# include "CExpression.h"
# include "Constant.h"
# include "PlaceContents.h"
# include "Place.h"
void
Mapping::compileMset (class CExpression& cexpr,
unsigned indent,
const char* resulttype,
const char* result,
const class VariableSet* vars) const
{
assert (myVariable && myMarking && myExpr);
/** the multi-set iterator */
char* iter;
/** the mapping expression */
char* expr;
/** the item iterator */
char* var;
/** the multiplicity iterator */
char* card;
if (myExpr->getKind () == Expression::eConstant) {
if (cexpr.getVariable (static_cast<const class Constant&>(*myExpr), expr))
myExpr->compile (cexpr, indent, expr, vars);
var = card = 0;
}
else {
var = cexpr.getIterator (*myVariable);
card = myCardinality ? cexpr.getIterator (*myCardinality) : 0;
cexpr.getVariable (*myExpr, expr);
}
class StringBuffer& out = cexpr.getOut ();
/** flag: is the marking the contents of a place with at most one token? */
bool place1 = myMarking->getKind () == Expression::ePlaceContents &&
static_cast<const class PlaceContents*>
(myMarking)->getPlace ().getMaxNumTokens () == 1;
if (place1) {
iter = static_cast<const class PlaceContents*>(myMarking)->getName (cexpr);
out.indent (indent);
out.append ("if ("), out.append (iter), out.append (") {\n");
if (myExpr->getKind () != Expression::eConstant) {
out.indent (indent + 2);
myVariable->getType ().appendName (out);
out.append (" "), out.append (var), out.append (" = *");
out.append (iter);
out.append (";\n");
if (card) {
out.indent (indent + 2);
out.append ("card_t "), out.append (card), out.append ("=1;\n");
myExpr->compileMset (cexpr, indent + 2, resulttype, result, vars);
}
else
myExpr->compile (cexpr, indent + 2, expr, vars);
}
if (!card) {
out.indent (indent + 2);
out.append (result);
out.append ("=insert");
getType ()->appendIndex (out);
out.append (" (");
if (resulttype)
out.append (resulttype);
out.append (result), out.append (", ");
out.append ("&"), out.append (expr), out.append (", 1);\n");
}
}
else {
/** the multi-set */
char* mset;
if (cexpr.getVariable (*myMarking, mset))
myMarking->compileMset (cexpr, indent, 0, mset, vars);
iter = cexpr.getLabel ();
out.indent (indent), out.append ("{\n");
out.indent (indent + 2);
myMarking->getType ()->appendMSetName (out);
out.append ("* "), out.append (iter), out.append (" = ");
out.append (mset), out.append (";\n");
delete[] mset;
out.indent (indent + 2);
out.append ("FIRST ("), out.append (iter), out.append (");\n");
out.indent (indent + 2);
out.append ("while ("), out.append (iter), out.append (") {\n");
out.indent (indent + 4);
out.append ("if ("), out.append (iter), out.append ("->count) {\n");
/** checkpoint in the while iteration loop */
bool* checkpoint;
const unsigned checkpointSize = cexpr.getCheckpoint (checkpoint);
if (myExpr->getKind () != Expression::eConstant) {
out.indent (indent + 6);
myVariable->getType ().appendName (out);
out.append (" "), out.append (var), out.append ("=");
out.append (iter), out.append ("->item;\n");
if (card) {
out.indent (indent + 6);
out.append ("card_t "), out.append (card), out.append ("=");
out.append (iter), out.append ("->count;\n");
myExpr->compileMset (cexpr, indent + 6, resulttype, result, vars);
}
else
myExpr->compile (cexpr, indent + 6, expr, vars);
}
if (!card) {
out.indent (indent + 6);
out.append (result);
out.append ("=insert");
getType ()->appendIndex (out);
out.append (" (");
if (resulttype)
out.append (resulttype);
out.append (result), out.append (", ");
out.append ("&"), out.append (expr), out.append (", ");
out.append (iter), out.append ("->count");
out.append (");\n");
}
cexpr.setCheckpoint (indent + 6, checkpoint, checkpointSize);
out.indent (indent + 4), out.append ("}\n");
out.indent (indent + 4);
out.append ("NEXT ("), out.append (iter), out.append (");\n");
out.indent (indent + 2), out.append ("}\n");
}
out.indent (indent), out.append ("}\n");
delete[] card;
delete[] var;
delete[] expr;
delete[] iter;
}
#endif // EXPR_COMPILE
void
Mapping::display (const class Printer& printer) const
{
printer.printRaw ("map");
printer.delimiter (' ');
printer.print (myVariable->getName ());
if (myCardinality) {
printer.delimiter ('#');
printer.print (myCardinality->getName ());
}
printer.delimiter ('{')++;
myMarking->display (printer);
--printer.delimiter ('}');
printer.delimiter ('(')++;
myExpr->display (printer);
--printer.delimiter (')');
}
|