~verifypn-cpn/verifypn/colored

« back to all changes in this revision

Viewing changes to PetriEngine/PetriNetBuilder.cpp

  • Committer: Jonas Finnemann Jensen
  • Date: 2011-09-15 13:30:00 UTC
  • Revision ID: jopsen@gmail.com-20110915133000-wnywm1odf82emiuw
Import of sources from github

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>,
 
5
 * 
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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/>.
 
18
 */
 
19
#include "PetriNetBuilder.h"
 
20
#include "PetriNet.h"
 
21
 
 
22
#include "PQL/PQLParser.h"
 
23
#include "PQL/PQL.h"
 
24
#include "PQL/Contexts.h"
 
25
//#include "PQL/CompiledCondition.h"
 
26
 
 
27
#include <assert.h>
 
28
 
 
29
using namespace std;
 
30
 
 
31
namespace PetriEngine{
 
32
 
 
33
PetriNetBuilder::PetriNetBuilder(bool JIT) : AbstractPetriNetBuilder(){
 
34
        _jit = JIT;
 
35
}
 
36
 
 
37
void PetriNetBuilder::addPlace(const string &name, int tokens, double, double){
 
38
        places.push_back(name);
 
39
        initialMarking.push_back(tokens);
 
40
}
 
41
 
 
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);
 
46
}
 
47
 
 
48
void PetriNetBuilder::addTransition(const string &name,
 
49
                                                                        const string &condition,
 
50
                                                                        const string &assignment,
 
51
                                                                        double, double){
 
52
        transitions.push_back(name);
 
53
        conditions.push_back(condition);
 
54
        assignments.push_back(assignment);
 
55
}
 
56
 
 
57
void PetriNetBuilder::addInputArc(const string &place, const string &transition, int weight){
 
58
        Arc arc;
 
59
        arc.place = place;
 
60
        arc.transition = transition;
 
61
        arc.weight = weight;
 
62
        inputArcs.push_back(arc);
 
63
}
 
64
 
 
65
void PetriNetBuilder::addOutputArc(const string &transition, const string &place, int weight){
 
66
        Arc arc;
 
67
        arc.transition = transition;
 
68
        arc.place = place;
 
69
        arc.weight = weight;
 
70
        outputArcs.push_back(arc);
 
71
}
 
72
 
 
73
PetriNet* PetriNetBuilder::makePetriNet(){
 
74
        PetriNet* net = new PetriNet(places.size(), transitions.size(), variables.size());
 
75
        size_t i;
 
76
        //Create variables
 
77
        for(i = 0; i < variables.size(); i++){
 
78
                net->_variables[i] = variables[i];
 
79
                net->_ranges[i] = ranges[i];
 
80
        }
 
81
        //Create place names
 
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);
 
93
                                /*if(_jit){
 
94
                                        PQL::CompiledCondition* CC = new PQL::CompiledCondition(net->_conditions[i]);
 
95
                                        CC->analyze(context);
 
96
                                        if(CC->compile())
 
97
                                                net->_conditions[i] = CC;
 
98
                                        else{
 
99
                                                delete CC;
 
100
                                                CC = NULL;
 
101
                                                //TODO: Print to stderr
 
102
                                        }
 
103
                                }else*/
 
104
                                        net->_conditions[i]->analyze(context);
 
105
 
 
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
 
111
                                }
 
112
                        }
 
113
                }
 
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
 
124
                                }
 
125
                        }
 
126
                }
 
127
        }
 
128
        //Create input arcs
 
129
        vector<Arc>::iterator arc;
 
130
        for(arc = inputArcs.begin(); arc != inputArcs.end(); arc++){
 
131
                int place = -1, transition = -1;
 
132
                //Find place number
 
133
                for(i = 0; i < places.size(); i++){
 
134
                        if(places[i] == arc->place){
 
135
                                place = i;
 
136
                                break;
 
137
                        }
 
138
                }
 
139
                //Find transition number
 
140
                for(i = 0; i < transitions.size(); i++){
 
141
                        if(transitions[i] == arc->transition){
 
142
                                transition = i;
 
143
                                break;
 
144
                        }
 
145
                }
 
146
                //We should have found a places and transition
 
147
                assert(place >= 0 && transition >= 0);
 
148
                net->_tv(transition)[place] = arc->weight;
 
149
        }
 
150
        //Create output arcs
 
151
        for(arc = outputArcs.begin(); arc != outputArcs.end(); arc++){
 
152
                int place = -1, transition = -1;
 
153
                //Find place number
 
154
                for(i = 0; i < places.size(); i++){
 
155
                        if(places[i] == arc->place){
 
156
                                place = i;
 
157
                                break;
 
158
                        }
 
159
                }
 
160
                //Find transition number
 
161
                for(i = 0; i < transitions.size(); i++){
 
162
                        if(transitions[i] == arc->transition){
 
163
                                transition = i;
 
164
                                break;
 
165
                        }
 
166
                }
 
167
                //We should have found a places and transition
 
168
                assert(place >= 0 && transition >= 0);
 
169
                net->_tv(transition)[place + places.size()] = arc->weight;
 
170
        }
 
171
        //Return the finished net
 
172
        return net;
 
173
}
 
174
 
 
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];
 
179
        return m;
 
180
}
 
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];
 
185
        return a;
 
186
}
 
187
 
 
188
 
 
189
} // PetriEngine