dotWriter.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
dotWriter.cc
Go to the documentation of this file.
1 /* dotWriter.cc
2 */
3 #include "osl/search/analyzer/dotWriter.h"
4 #include "osl/search/analyzer/recordSet_.h"
5 #include "osl/search/simpleHashRecord.h"
6 #include "osl/record/csa.h"
7 #include <boost/format.hpp>
8 #include <sstream>
9 #include <iostream>
10 #include <cassert>
11
12 #define BOOST_FORMAT_BUG
13
14 osl::search::analyzer::DotWriter::
15 DotWriter(std::ostream& o)
16 : written(new RecordSet()), os(o)
17 {
18 os << "digraph OSL_DotWriter {\n";
19 }
20
21 osl::search::analyzer::DotWriter::
22 ~DotWriter()
23 {
24 os << "}\n" << std::flush;
25 }
26
27 void osl::search::analyzer::DotWriter::
28 showComment(const char *line) const
29 {
30 os << "// " << line << "\n";
31 }
32
33 void osl::search::analyzer::DotWriter::
34 showNode(Player turn, const SimpleHashRecord *record,
35 int limit, NodeType type) const
36 {
37 const bool black_turn = turn == BLACK;
38 if (written->find(record) != written->end())
39 return;
40 written->insert(record);
41 assert(record);
42 std::stringstream range;
43 int lower_limit = record->lowerLimit();
44 int lower_bound = record->lowerBound();
45 int upper_limit = record->upperLimit();
46 int upper_bound = record->upperBound();
47 if (! black_turn)
48 {
49 std::swap(lower_limit, upper_limit);
50 std::swap(lower_bound, upper_bound);
51 }
52 int bound = 0;
53 if (lower_limit >= 0)
54 {
55 ++bound;
56 #ifndef BOOST_FORMAT_BUG
57 range << (boost::format("%d(%d)") % lower_bound % lower_limit);
58 #else
59 range << lower_bound << "(" << lower_limit << ")";
60 #endif
61 }
62 range << '<';
63 if (upper_limit >= 0)
64 {
65 ++bound;
66 #ifndef BOOST_FORMAT_BUG
67 range << (boost::format("%d(%d)") % upper_bound % upper_limit);
68 #else
69 range << upper_bound << "(" << upper_limit << ")";
70 #endif
71 }
72 const char *color = 0;
73 switch (type)
74 {
75 case IMPORTANT:
76 color = "blue";
77 break;
78 case ABNORMAL:
79 color = "magenta";
80 break;
81 default:
82 color = (bound == 2) ? "red" : "black";
83 }
84 std::stringstream bestMove;
85 csaShow(bestMove, record->bestMove().move());
86 #ifndef BOOST_FORMAT_BUG
87 os << (boost::format("N%x [label=\"l=%d\\n%s\\n%s\",color=%s,shape=box]\n")
88 % record % limit % range.str() % bestMove.str()
89 % color);
90 #else
91 os << "N" << record << " [label=\"l=" << limit << "\\n" << range.str()
92 << "\\n" << bestMove.str()
93 << "\",color=" << color << ",shape=box]\n";
94 #endif
95 }
96
97 // TODO: 選手権後に showNode と共通部分をまとめる
98 void osl::search::analyzer::DotWriter::
99 showNodeQuiescence(Player turn, const SimpleHashRecord *record,
100 int limit, NodeType type) const
101 {
102 bool black_turn = (turn == BLACK);
103 if (written->find(record) != written->end())
104 return;
105 written->insert(record);
106 assert(record);
107 const QuiescenceRecord *qrecord = &record->qrecord;
108 std::stringstream range;
109 int lower_limit = qrecord->lowerDepth();
110 int lower_bound = qrecord->lowerBound();
111 int upper_limit = qrecord->upperDepth();
112 int upper_bound = qrecord->upperBound();
113 if (! black_turn)
114 {
115 std::swap(lower_limit, upper_limit);
116 std::swap(lower_bound, upper_bound);
117 }
118 int bound = 0;
119 if (lower_limit >= 0)
120 {
121 ++bound;
122 #ifndef BOOST_FORMAT_BUG
123 range << (boost::format("%d(%d)") % lower_bound % lower_limit);
124 #else
125 range << lower_bound << "(" << lower_limit << ")";
126 #endif
127 }
128 range << '<';
129 if (upper_limit >= 0)
130 {
131 ++bound;
132 #ifndef BOOST_FORMAT_BUG
133 range << (boost::format("%d(%d)") % upper_bound % upper_limit);
134 #else
135 range << upper_bound << "(" << upper_limit << ")";
136 #endif
137 }
138 const char *color = 0;
139 switch (type)
140 {
141 case IMPORTANT:
142 color = "blue";
143 break;
144 case ABNORMAL:
145 color = "magenta";
146 break;
147 default:
148 color = (bound == 2) ? "burlywood" : "cyan";
149 }
150 #ifndef BOOST_FORMAT_BUG
151 os << (boost::format("N%x [label=\"l=%d\\n%s\",color=%s,shape=box]\n")
152 % record % limit % range.str()
153 % color);
154 #else
155 os << "N" << record << " [label=\"l=" << limit << "\\n" << range.str()
156 << "\",color=" << color << ",shape=box]\n";
157 #endif
158 }
159
160 void osl::search::analyzer::DotWriter::
161 showArc(const SimpleHashRecord *from, const SimpleHashRecord *to,
162 const MoveLogProb& move, bool important) const
163 {
164 if ((written->find(from) != written->end())
165 && (written->find(to) != written->end()))
166 return;
167 assert(from);
168 assert(to);
169 std::stringstream move_string;
170 csaShow(move_string, move.move());
171 const char *color = 0;
172 if (important)
173 color = "blue";
174 else
175 color = (move.logProb() <= 100) ? "red" : "black";
176 #ifndef BOOST_FORMAT_BUG
177 os << (boost::format("N%x -> N%x [label=\"%s (%d)\", color=%s, style=bold]\n")
178 % from % to % move_string.str() % move.logProb() % color);
179 #else
180 os << "N" << from << " -> N" << to << " [label=\"" << move_string.str()
181 << " (" << move.logProb() << ")\", color=" << color
182 << ", style=bold]\n";
183 #endif
184 }
185
186 /* ------------------------------------------------------------------------- */
187 // ;;; Local Variables:
188 // ;;; mode:c++
189 // ;;; c-basic-offset:2
190 // ;;; coding:utf-8
191 // ;;; End: