2
* To change this license header, choose License Headers in Project Properties.
3
* To change this template file, choose Tools | Templates
4
* and open the template in the editor.
8
* File: EvaluateableProposition.cpp
11
* Created on April 27, 2016, 2:36 PM
18
#include "EvaluateableProposition.h"
19
#include "CTLParser.h"
23
//A replacement for the standard to string function, which we cannot use because old OS X does not have c++11
24
template < typename T > std::string to_string( const T& n )
26
std::ostringstream stm ;
32
EvaluateableProposition::EvaluateableProposition(std::string a, PetriEngine::PetriNet *net) {
33
if(a.substr(0,2).compare("in") == 0 || a.substr(0,2).compare("to") == 0){
35
_loperator = SetLoperator(a);
36
assert(_loperator != NOT_CARDINALITY);
38
size_t begin = a.find('(') + 1;
39
size_t end = a.find(')') - begin;
40
std::string first_parameter_str = a.substr(begin, end);
41
a = a.substr(a.find(')') + 1);
43
begin = a.find('(') + 1;
44
end = a.find(')') - begin;
45
std::string second_parameter_str = a.substr(begin, end);
46
_firstParameter = CreateParameter(first_parameter_str, net->placeNames(), net->numberOfPlaces());
47
_secondParameter = CreateParameter(second_parameter_str, net->placeNames(), net->numberOfPlaces());
49
else if(a.substr(0,2).compare("is") == 0){
51
_loperator = NOT_CARDINALITY;
52
size_t s_pos = a.find('(') + 1;
53
size_t e_pos = a.find(')') - 1;
54
assert(s_pos < e_pos);
55
int fire_str_length = e_pos - s_pos + 1;
56
std::string fireset_str = a.substr(s_pos, fire_str_length);
57
SetFireset(fireset_str, net->transitionNames(), net->numberOfTransitions());
59
else if(a.substr(0,2).compare("tr") == 0){
62
else if(a.substr(0,2).compare("fa") == 0){
66
assert(false && "Atomic string proposed for proposition could not be parsed");
70
EvaluateableProposition::~EvaluateableProposition() {
73
PropositionType EvaluateableProposition::GetPropositionType(){
77
LoperatorType EvaluateableProposition::GetLoperator(){
78
assert(_type == CARDINALITY && "Proposition is not a cardinality proposition");
82
std::vector<int> EvaluateableProposition::GetFireset() {
83
assert(_type == FIREABILITY && "Proposition is not a fireability proposition");
87
void EvaluateableProposition::SetFireset(std::string fireset_str, std::vector<std::string> t_names, unsigned int numberof_t){
88
std::string restof_firestring = fireset_str;
89
while(restof_firestring.length() > 0){
90
size_t position = restof_firestring.find(',');
91
std::string current_t = restof_firestring.substr(0, position);
92
for(int i = 0; i < numberof_t; i++){
93
if (current_t.compare(t_names[i]) == 0){
94
_fireset.push_back(i);
98
restof_firestring = restof_firestring.substr(position);
100
restof_firestring = "";
102
for ( auto f : _fireset){
103
std::cout<<f<<" is id of "<< t_names[f]<<std::endl;
107
CardinalityParameter* EvaluateableProposition::CreateParameter(std::string parameter_str, std::vector<std::string> p_names, unsigned int numberof_p){
108
CardinalityParameter *param = new CardinalityParameter();
109
std::string::size_type sz;
111
if(sscanf(parameter_str.c_str(), "%d%c", ¶m->value, &c) == 1) {
112
//If string is identifier starting with a number, you will read two items.
113
//If it's an identifier starting with a character, you will read zero items.
114
//The only time when you read just one item if the whole string is just numbers.
115
param->isPlace = false;
118
param->isPlace = true;
119
std::vector<std::string> places_str;
120
std::size_t found = parameter_str.find(",");
122
while(found != parameter_str.npos){
123
std::string temp = parameter_str.substr(0, found);
124
places_str.push_back(temp);
125
parameter_str = parameter_str.substr(found + 2);
126
found = parameter_str.find(",");
129
places_str.push_back(parameter_str);
131
for(int i = 0; i < numberof_p; i++){
132
for(std::string place : places_str){
133
if(p_names[i].compare(place) == 0){
134
param->places_i.push_back(i);
142
LoperatorType EvaluateableProposition::SetLoperator(std::string atom_str){
143
std::string loperator_str = atom_str.substr(atom_str.find(')'));
144
loperator_str = loperator_str.substr(0, loperator_str.find('('));
145
if(loperator_str.compare(" le "))
147
else if (loperator_str.compare(" ge "))
149
else if (loperator_str.compare(" eq "))
151
else assert(false && "Could not parse the given logical operator");
154
std::string EvaluateableProposition::ToString() {
155
if (_type == FIREABILITY) {
156
std::string fire_str = "Fireset(";
157
for(auto f : _fireset){
158
fire_str = fire_str + " " + patch::to_string(f);
160
return fire_str + ")";
162
else if (_type == CARDINALITY){
163
std::string cardi_str = "(";
164
if(_firstParameter->isPlace)
165
cardi_str = cardi_str + "place(" + patch::to_string(_firstParameter->value) + ")";
167
cardi_str = cardi_str = patch::to_string(_firstParameter->value);
169
cardi_str = cardi_str + " le ";
171
if(_secondParameter->isPlace)
172
cardi_str = cardi_str + "place(" + patch::to_string(_secondParameter->value) + ")";
174
cardi_str = cardi_str = patch::to_string(_secondParameter->value);
175
return cardi_str + ")";
178
assert(false && "Proposition had no type");
181
CardinalityParameter* EvaluateableProposition::GetFirstParameter() {
182
assert(_type == CARDINALITY);
183
return _firstParameter;
186
CardinalityParameter* EvaluateableProposition::GetSecondParameter() {
187
assert(_type == CARDINALITY);
188
return _secondParameter;