1
/* PeTe - Petri Engine exTremE
2
* Copyright (C) 2011 Jonas Finnemann Jensen <jopsen@gmail.com>,
3
* Thomas Søndersø Nielsen <primogens@gmail.com>,
4
* Lars Kærlund Østergaard <larsko@gmail.com>,
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "PetriNetBuilder.h"
22
#include "PQL/PQLParser.h"
24
#include "PQL/Contexts.h"
25
//#include "PQL/CompiledCondition.h"
31
namespace PetriEngine{
33
PetriNetBuilder::PetriNetBuilder(bool JIT) : AbstractPetriNetBuilder(){
37
void PetriNetBuilder::addPlace(const string &name, int tokens, double, double){
38
places.push_back(name);
39
initialMarking.push_back(tokens);
42
void PetriNetBuilder::addVariable(const string &name, int initialValue, int range){
43
variables.push_back(name);
44
initialVariableValues.push_back(initialValue);
45
ranges.push_back(range);
48
void PetriNetBuilder::addTransition(const string &name,
49
const string &condition,
50
const string &assignment,
52
transitions.push_back(name);
53
conditions.push_back(condition);
54
assignments.push_back(assignment);
57
void PetriNetBuilder::addInputArc(const string &place, const string &transition, int weight){
60
arc.transition = transition;
62
inputArcs.push_back(arc);
65
void PetriNetBuilder::addOutputArc(const string &transition, const string &place, int weight){
67
arc.transition = transition;
70
outputArcs.push_back(arc);
73
PetriNet* PetriNetBuilder::makePetriNet(){
74
PetriNet* net = new PetriNet(places.size(), transitions.size(), variables.size());
77
for(i = 0; i < variables.size(); i++){
78
net->_variables[i] = variables[i];
79
net->_ranges[i] = ranges[i];
82
for(i = 0; i < places.size(); i++)
83
net->_places[i] = places[i];
84
//Create transition names
85
for(i = 0; i < transitions.size(); i++)
86
net->_transitions[i] = transitions[i];
87
//Parse conditions and assignments
88
for(i = 0; i < transitions.size(); i++){
89
if(conditions[i] != ""){
90
net->_conditions[i] = PQL::ParseQuery(conditions[i]);
91
if(net->_conditions[i]){
92
PQL::AnalysisContext context(*net);
94
PQL::CompiledCondition* CC = new PQL::CompiledCondition(net->_conditions[i]);
97
net->_conditions[i] = CC;
101
//TODO: Print to stderr
104
net->_conditions[i]->analyze(context);
106
//Delete if there we're errors
107
if(context.errors().size() > 0){
108
delete net->_conditions[i];
109
net->_conditions[i] = NULL;
110
//TODO: Print to stderr
114
if(assignments[i] != ""){
115
net->_assignments[i] = PQL::ParseAssignment(assignments[i]);
116
if(net->_assignments[i]){
117
PQL::AnalysisContext context(*net);
118
net->_assignments[i]->analyze(context);
119
//Delete if there we're errors
120
if(context.errors().size() > 0){
121
delete net->_assignments[i];
122
net->_assignments[i] = NULL;
123
//TODO: Print to stderr
129
vector<Arc>::iterator arc;
130
for(arc = inputArcs.begin(); arc != inputArcs.end(); arc++){
131
int place = -1, transition = -1;
133
for(i = 0; i < places.size(); i++){
134
if(places[i] == arc->place){
139
//Find transition number
140
for(i = 0; i < transitions.size(); i++){
141
if(transitions[i] == arc->transition){
146
//We should have found a places and transition
147
assert(place >= 0 && transition >= 0);
148
net->_tv(transition)[place] = arc->weight;
151
for(arc = outputArcs.begin(); arc != outputArcs.end(); arc++){
152
int place = -1, transition = -1;
154
for(i = 0; i < places.size(); i++){
155
if(places[i] == arc->place){
160
//Find transition number
161
for(i = 0; i < transitions.size(); i++){
162
if(transitions[i] == arc->transition){
167
//We should have found a places and transition
168
assert(place >= 0 && transition >= 0);
169
net->_tv(transition)[place + places.size()] = arc->weight;
171
//Return the finished net
175
MarkVal* PetriNetBuilder::makeInitialMarking(){
176
MarkVal* m = new MarkVal[places.size()];
177
for(size_t i = 0; i < places.size(); i++)
178
m[i] = initialMarking[i];
181
VarVal* PetriNetBuilder::makeInitialAssignment(){
182
VarVal* a = new VarVal[variables.size()];
183
for(size_t i = 0; i < variables.size(); i++)
184
a[i] = initialVariableValues[i];