~verifypn-cpn/verifypn/colored

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
/* PeTe - Petri Engine exTremE
 * Copyright (C) 2011  Jonas Finnemann Jensen <jopsen@gmail.com>,
 *                     Thomas Søndersø Nielsen <primogens@gmail.com>,
 *                     Lars Kærlund Østergaard <larsko@gmail.com>,
 * 
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "PetriNetBuilder.h"
#include "PetriNet.h"

#include "PQL/PQLParser.h"
#include "PQL/PQL.h"
#include "PQL/Contexts.h"
//#include "PQL/CompiledCondition.h"

#include <assert.h>

using namespace std;

namespace PetriEngine{

PetriNetBuilder::PetriNetBuilder(bool JIT) : AbstractPetriNetBuilder(){
	_jit = JIT;
}

void PetriNetBuilder::addPlace(const string &name, int tokens, double, double){
	places.push_back(name);
	initialMarking.push_back(tokens);
}

void PetriNetBuilder::addVariable(const string &name, int initialValue, int range){
	variables.push_back(name);
	initialVariableValues.push_back(initialValue);
	ranges.push_back(range);
}

void PetriNetBuilder::addTransition(const string &name,
									const string &condition,
									const string &assignment,
									double, double){
	transitions.push_back(name);
	conditions.push_back(condition);
	assignments.push_back(assignment);
}

void PetriNetBuilder::addInputArc(const string &place, const string &transition, int weight){
	Arc arc;
	arc.place = place;
	arc.transition = transition;
	arc.weight = weight;
	inputArcs.push_back(arc);
}

void PetriNetBuilder::addOutputArc(const string &transition, const string &place, int weight){
	Arc arc;
	arc.transition = transition;
	arc.place = place;
	arc.weight = weight;
	outputArcs.push_back(arc);
}

PetriNet* PetriNetBuilder::makePetriNet(){
	PetriNet* net = new PetriNet(places.size(), transitions.size(), variables.size());
	size_t i;
	//Create variables
	for(i = 0; i < variables.size(); i++){
		net->_variables[i] = variables[i];
		net->_ranges[i] = ranges[i];
	}
	//Create place names
	for(i = 0; i < places.size(); i++)
		net->_places[i] = places[i];
	//Create transition names
	for(i = 0; i < transitions.size(); i++)
		net->_transitions[i] = transitions[i];
	//Parse conditions and assignments
	for(i = 0; i < transitions.size(); i++){
		if(conditions[i] != ""){
			net->_conditions[i] = PQL::ParseQuery(conditions[i]);
			if(net->_conditions[i]){
				PQL::AnalysisContext context(*net);
				/*if(_jit){
					PQL::CompiledCondition* CC = new PQL::CompiledCondition(net->_conditions[i]);
					CC->analyze(context);
					if(CC->compile())
						net->_conditions[i] = CC;
					else{
						delete CC;
						CC = NULL;
						//TODO: Print to stderr
					}
				}else*/
					net->_conditions[i]->analyze(context);

				//Delete if there we're errors
				if(context.errors().size() > 0){
					delete net->_conditions[i];
					net->_conditions[i] = NULL;
					//TODO: Print to stderr
				}
			}
		}
		if(assignments[i] != ""){
			net->_assignments[i] = PQL::ParseAssignment(assignments[i]);
			if(net->_assignments[i]){
				PQL::AnalysisContext context(*net);
				net->_assignments[i]->analyze(context);
				//Delete if there we're errors
				if(context.errors().size() > 0){
					delete net->_assignments[i];
					net->_assignments[i] = NULL;
					//TODO: Print to stderr
				}
			}
		}
	}
	//Create input arcs
	vector<Arc>::iterator arc;
	for(arc = inputArcs.begin(); arc != inputArcs.end(); arc++){
		int place = -1, transition = -1;
		//Find place number
		for(i = 0; i < places.size(); i++){
			if(places[i] == arc->place){
				place = i;
				break;
			}
		}
		//Find transition number
		for(i = 0; i < transitions.size(); i++){
			if(transitions[i] == arc->transition){
				transition = i;
				break;
			}
		}
		//We should have found a places and transition
		assert(place >= 0 && transition >= 0);
		net->_tv(transition)[place] = arc->weight;
	}
	//Create output arcs
	for(arc = outputArcs.begin(); arc != outputArcs.end(); arc++){
		int place = -1, transition = -1;
		//Find place number
		for(i = 0; i < places.size(); i++){
			if(places[i] == arc->place){
				place = i;
				break;
			}
		}
		//Find transition number
		for(i = 0; i < transitions.size(); i++){
			if(transitions[i] == arc->transition){
				transition = i;
				break;
			}
		}
		//We should have found a places and transition
		assert(place >= 0 && transition >= 0);
		net->_tv(transition)[place + places.size()] = arc->weight;
	}
	//Return the finished net
	return net;
}

MarkVal* PetriNetBuilder::makeInitialMarking(){
	MarkVal* m = new MarkVal[places.size()];
	for(size_t i = 0; i < places.size(); i++)
		m[i] = initialMarking[i];
	return m;
}
VarVal* PetriNetBuilder::makeInitialAssignment(){
	VarVal* a = new VarVal[variables.size()];
	for(size_t i = 0; i < variables.size(); i++)
		a[i] = initialVariableValues[i];
	return a;
}


} // PetriEngine