mCRL2
Loading...
Searching...
No Matches
liblts_dot.cpp
Go to the documentation of this file.
1// Author(s): Muck van Weerdenburg
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9/// \file liblts_dot.cpp
10
11#include "mcrl2/lts/lts_io.h"
12#include "mcrl2/lts/parse.h"
13#include "mcrl2/lts/detail/liblts_swap_to_from_probabilistic_lts.h"
14
15using namespace mcrl2::core;
16
17
18namespace mcrl2::lts
19{
20
21void probabilistic_lts_dot_t::save(std::ostream& os) const
22{
23 // Language definition seems to suggest that the name is optional, but tools seem to think otherwise
24 os << "digraph G {" << std::endl;
25 os << "center = TRUE;" << std::endl;
26 os << "mclimit = 10.0;" << std::endl;
27 os << "nodesep = 0.05;" << std::endl;
28 os << "node [ width=0.25, height=0.25, label=\"\" ];" << std::endl;
29 if (num_states() > 0)
30 {
31 if (initial_probabilistic_state().size()<=1)
32 {
33 const std::size_t initial_state=initial_probabilistic_state().get();
34 if (has_state_info())
35 {
36 os << state_label(initial_state).name();
37 }
38 else
39 {
40 os << "S" << initial_state;
41 }
42 }
43 else
44 {
45 throw mcrl2::runtime_error("Cannnot save a probabilistic transition system in .dot format.");
46 }
47
48 os << " [ peripheries=2 ];" << std::endl;
49 for (std::size_t i=0; i<num_states(); i++)
50 {
51 if (has_state_info())
52 {
53 const std::string& label = state_label(i).label();
54 if (!label.empty())
55 {
56 os << state_label(i).name() << " [label=\"" << label << "\"];" << std::endl;
57 }
58 }
59 else
60 {
61 os << "S" << i << std::endl;
62 }
63 }
64 }
65
66 for (const transition& t: get_transitions())
67 {
68 if (has_state_info())
69 {
70 if (probabilistic_state(t.to()).size()>1)
71 {
72 throw mcrl2::runtime_error("Cannot save probabilistic states in .dot format.");
73 }
74 os << state_label(t.from()).name() << "->" << state_label(probabilistic_state(t.to()).get()).name() << "[label=\"" <<
75 mcrl2::lts::pp(action_label(t.label())) << "\"];" << std::endl;
76 }
77 else
78 {
79 os << "S" << t.from() << " -> " << "S" << t.to() << "[label=\"" <<
80 mcrl2::lts::pp(action_label(apply_hidden_label_map(t.label()))) << "\"];" << std::endl;
81 }
82 }
83
84 os << "}" << std::endl;
85}
86
87void probabilistic_lts_dot_t::save(const std::string& filename) const
88{
89 std::ofstream os(filename.c_str());
90 if (!os.is_open())
91 {
92 throw mcrl2::runtime_error("cannot open DOT file '" + filename + "' for writing.");
93 }
94 save(os);
95 os.close();
96}
97
98void lts_dot_t::save(std::ostream& os) const
99{
101 detail::translate_to_probabilistic_lts
102 <state_label_dot,
103 action_label_string,
104 probabilistic_state<std::size_t, mcrl2::utilities::probabilistic_arbitrary_precision_fraction>,
105 detail::lts_dot_base>(*this,l);
106 l.save(os);
107}
108
109void lts_dot_t::save(const std::string& filename) const
110{
111 if (filename.empty() || filename=="-")
112 {
113 save(std::cout);
114 }
115 else
116 {
117 std::ofstream os(filename.c_str());
118 if (!os.is_open())
119 {
120 throw mcrl2::runtime_error("cannot open DOT file '" + filename + "' for writing.");
121 }
122 save(os);
123 os.close();
124 }
125}
126
127}
A class to contain labelled transition systems in graphviz format.
Definition lts_dot.h:132
void save(const std::string &filename) const
Save the labelled transition system to a file.
void save(std::ostream &os) const
Save the labelled transition system to a stream.
A class to contain labelled transition systems in graphviz format.
Definition lts_dot.h:158
void save(std::ostream &os) const
Save the labelled transition system to a stream.
void save(const std::string &filename) const
Save the labelled transition system to a file.