Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GspnJsonExporter.cpp
Go to the documentation of this file.
1#include "GspnJsonExporter.h"
2
3#include <algorithm>
4#include <string>
5
9
10namespace storm {
11namespace gspn {
12
13// Prevent some magic constants
14static constexpr const uint64_t scaleFactor = 50;
15
16void GspnJsonExporter::toStream(storm::gspn::GSPN const& gspn, std::ostream& os) {
17 os << storm::dumpJson(translate(gspn)) << '\n';
18}
19
21 Json jsonGspn;
22
23 // Layouts
24 std::map<uint64_t, LayoutInfo> placeLayout = gspn.getPlaceLayoutInfos();
25 std::map<uint64_t, LayoutInfo> transitionLayout = gspn.getTransitionLayoutInfos();
26 double tmpX = 0;
27 double tmpY = 10;
28
29 // Export places
30 for (const auto& place : gspn.getPlaces()) {
31 double x = tmpX;
32 double y = tmpY;
33 if (placeLayout.count(place.getID()) > 0) {
34 x = placeLayout.at(place.getID()).x;
35 y = placeLayout.at(place.getID()).y;
36 }
37 tmpX += 3;
38 Json jsonPlace = translatePlace(place, x, y);
39 jsonGspn.push_back(jsonPlace);
40 }
41
42 // Export immediate transitions
43 for (const auto& transition : gspn.getImmediateTransitions()) {
44 double x = tmpX;
45 double y = tmpY;
46 if (transitionLayout.count(transition.getID()) > 0) {
47 x = transitionLayout.at(transition.getID()).x;
48 y = transitionLayout.at(transition.getID()).y;
49 }
50 tmpX += 3;
51 Json jsonImmediateTransition = translateImmediateTransition(transition, x, y);
52 jsonGspn.push_back(jsonImmediateTransition);
53 }
54
55 // Export timed transitions
56 for (const auto& transition : gspn.getTimedTransitions()) {
57 double x = tmpX;
58 double y = tmpY;
59 if (transitionLayout.count(transition.getID()) > 0) {
60 x = transitionLayout.at(transition.getID()).x;
61 y = transitionLayout.at(transition.getID()).y;
62 }
63 tmpX += 3;
64 Json jsonTimedTransition = translateTimedTransition(transition, x, y);
65 jsonGspn.push_back(jsonTimedTransition);
66 }
67
68 // Export arcs
69 std::vector<storm::gspn::Place> places = gspn.getPlaces();
70 // Export arcs for immediate transitions
71 for (const auto& transition : gspn.getImmediateTransitions()) {
72 // Export input arcs
73 for (auto const& entry : transition.getInputPlaces()) {
74 storm::gspn::Place place = places.at(entry.first);
75 Json jsonInputArc = translateArc(transition, place, entry.second, true, ArcType::INPUT);
76 jsonGspn.push_back(jsonInputArc);
77 }
78
79 // Export inhibitor arcs
80 for (auto const& entry : transition.getInhibitionPlaces()) {
81 storm::gspn::Place place = places.at(entry.first);
82 Json jsonInputArc = translateArc(transition, place, entry.second, true, ArcType::INHIBITOR);
83 jsonGspn.push_back(jsonInputArc);
84 }
85
86 // Export output arcs
87 for (auto const& entry : transition.getOutputPlaces()) {
88 storm::gspn::Place place = places.at(entry.first);
89 Json jsonInputArc = translateArc(transition, place, entry.second, true, ArcType::OUTPUT);
90 jsonGspn.push_back(jsonInputArc);
91 }
92 }
93 // Export arcs for timed transitions
94 for (const auto& transition : gspn.getTimedTransitions()) {
95 // Export input arcs
96 for (auto const& entry : transition.getInputPlaces()) {
97 storm::gspn::Place place = places.at(entry.first);
98 Json jsonInputArc = translateArc(transition, place, entry.second, false, ArcType::INPUT);
99 jsonGspn.push_back(jsonInputArc);
100 }
101
102 // Export inhibitor arcs
103 for (auto const& entry : transition.getInhibitionPlaces()) {
104 storm::gspn::Place place = places.at(entry.first);
105 Json jsonInputArc = translateArc(transition, place, entry.second, false, ArcType::INHIBITOR);
106 jsonGspn.push_back(jsonInputArc);
107 }
108
109 // Export output arcs
110 for (auto const& entry : transition.getOutputPlaces()) {
111 storm::gspn::Place place = places.at(entry.first);
112 Json jsonInputArc = translateArc(transition, place, entry.second, false, ArcType::OUTPUT);
113 jsonGspn.push_back(jsonInputArc);
114 }
115 }
116 return jsonGspn;
117}
118
119typename GspnJsonExporter::Json GspnJsonExporter::translatePlace(storm::gspn::Place const& place, double x, double y) {
120 Json data;
121 data["id"] = toJsonString(place);
122 data["name"] = place.getName();
123 data["marking"] = place.getNumberOfInitialTokens();
124
125 Json position;
126 position["x"] = x * scaleFactor;
127 position["y"] = y * scaleFactor;
128
129 Json jsonPlace;
130 jsonPlace["data"] = data;
131 jsonPlace["position"] = position;
132 jsonPlace["group"] = "nodes";
133 jsonPlace["classes"] = "place";
134 return jsonPlace;
135}
136
137typename GspnJsonExporter::Json GspnJsonExporter::translateImmediateTransition(storm::gspn::ImmediateTransition<double> const& transition, double x, double y) {
138 Json data;
139 data["id"] = toJsonString(transition, true);
140 data["name"] = transition.getName();
141 data["priority"] = transition.getPriority();
142 data["weight"] = transition.getWeight();
143
144 Json position;
145 position["x"] = x * scaleFactor;
146 position["y"] = y * scaleFactor;
147
148 Json jsonTrans;
149 jsonTrans["data"] = data;
150 jsonTrans["position"] = position;
151 jsonTrans["group"] = "nodes";
152 jsonTrans["classes"] = "trans_im";
153 return jsonTrans;
154}
155
156typename GspnJsonExporter::Json GspnJsonExporter::translateTimedTransition(storm::gspn::TimedTransition<double> const& transition, double x, double y) {
157 Json data;
158 data["id"] = toJsonString(transition, false);
159 data["name"] = transition.getName();
160 data["rate"] = transition.getRate();
161 data["priority"] = transition.getPriority();
162 if (!transition.hasSingleServerSemantics()) {
163 if (transition.hasInfiniteServerSemantics()) {
164 data["server-semantics"] = "infinite";
165 } else if (transition.hasKServerSemantics()) {
166 data["server-semantics"] = transition.getNumberOfServers();
167 } else {
168 STORM_LOG_WARN("Unable to export transition semantics.");
169 }
170 }
171
172 Json position;
173 position["x"] = x * scaleFactor;
174 position["y"] = y * scaleFactor;
175
176 Json jsonTrans;
177 jsonTrans["data"] = data;
178 jsonTrans["position"] = position;
179 jsonTrans["group"] = "nodes";
180 jsonTrans["classes"] = "trans_time";
181 return jsonTrans;
182}
183
184typename GspnJsonExporter::Json GspnJsonExporter::translateArc(storm::gspn::Transition const& transition, storm::gspn::Place const& place,
185 uint64_t multiplicity, bool immediate, ArcType arctype) {
186 Json data;
187 data["id"] = toJsonString(transition, place, arctype);
188 data["source"] = toJsonString(place);
189 data["target"] = toJsonString(transition, immediate);
190 data["mult"] = multiplicity;
191
192 Json jsonArc;
193 jsonArc["data"] = data;
194 // jsonTrans["group"] = "nodes";
195 switch (arctype) {
196 case INPUT:
197 jsonArc["classes"] = "input";
198 break;
199 case OUTPUT:
200 jsonArc["classes"] = "output";
201 break;
202 case INHIBITOR:
203 jsonArc["classes"] = "inhibit";
204 break;
205 default:
206 STORM_LOG_ASSERT(false, "Unknown type " << arctype << " used.");
207 }
208 return jsonArc;
209}
210
211} // namespace gspn
212} // namespace storm
std::map< uint64_t, LayoutInfo > const & getTransitionLayoutInfos() const
Definition GSPN.cpp:414
std::vector< ImmediateTransition< GSPN::WeightType > > const & getImmediateTransitions() const
Returns the vector of immediate transitions in this gspn.
Definition GSPN.cpp:57
std::vector< storm::gspn::Place > const & getPlaces() const
Returns the places of this gspn.
Definition GSPN.cpp:61
std::map< uint64_t, LayoutInfo > const & getPlaceLayoutInfos() const
Definition GSPN.cpp:410
std::vector< TimedTransition< GSPN::RateType > > const & getTimedTransitions() const
Returns the vector of timed transitions in this gspn.
Definition GSPN.cpp:53
static Json translate(storm::gspn::GSPN const &gspn)
static void toStream(storm::gspn::GSPN const &gspn, std::ostream &os)
WeightType getWeight() const
Retrieves the weight of this transition.
This class provides methods to store and retrieve data for a place in a gspn.
Definition Place.h:12
std::string getName() const
Returns the name of this place.
Definition Place.cpp:14
uint64_t getNumberOfInitialTokens() const
Returns the number of initial tokens of this place.
Definition Place.cpp:26
RateType getRate() const
Retrieves the rate of this transition.
uint64_t getNumberOfServers() const
bool hasKServerSemantics() const
Retrieves the semantics of this transition.
This class represents a transition in a gspn.
Definition Transition.h:14
std::string const & getName() const
Returns the name of the transition.
uint64_t getPriority() const
Returns the priority of this transition.
#define STORM_LOG_WARN(message)
Definition logging.h:30
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
static constexpr const uint64_t scaleFactor
LabParser.cpp.
Definition cli.cpp:18
std::string dumpJson(storm::json< ValueType > const &j, bool compact)
Dumps the given json object, producing a String.