Debian Codegraph @

src/org/stathissideris/ascii2image/text/GridPattern.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /**
2 * ditaa - Diagrams Through Ascii Art
3 *
•4 * Copyright (C) 2004-2011 Efstathios Sideris
5 *
•6 * ditaa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
•11 * ditaa 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 Lesser General Public License for more details.
15 *
•16 * You should have received a copy of the GNU Lesser General Public
17 * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package org.stathissideris.ascii2image.text;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.regex.Pattern;
25
26 /**
27 * This is a TextGrid (usually 3x3) that contains the equivalent of a
28 * 2D reqular expression (which uses custom syntax to make things more
29 * visual, but standard syntax is also possible).
30 *
31 * The custom syntax is:
32 * . means anything
33 * b means any boundary (any of - = / \ + | :)
34 * ! means not boundary (none of - = / \ + | :)
35 * - means - or =
36 * | means | or :
37 * [ means not | nor :
38 * ~ means not - nor =
39 * ^ means a boundary but not - nor =
40 * ( means a boundary but not | nor :
41 * s means a straight boundary (one of - = + | :)
42 * S means not a straight boundary (none of - = + | :)
43 *
44 * 1 means a cell that has entry point 1
45 * 2 means a cell that has entry point 2
46 * 3 means a cell that has entry point 3 etc. up to number 8
47 *
48 * %1 means a cell that does not have entry point 1 etc.
49 *
50 * See below for an explanation of entry points
51 *
52 * +, \, / and the space are literal (as is any other character)
53 *
54 *
55 * Entry points
56 *
57 * <pre>
58 * 1 2 3
59 * *--*--*
60 * | |
61 * 8* *4
62 * | |
63 * *--*--*
64 * 7 6 5
65 * </pre>
66 *
67 * We number the entry points for each cell as in the diagram
68 * above. If a cell is occupied by a character, we define as
69 * entry points the points of the above diagram that the character
70 * can touch with the end of its lines. For example - has
71 * entry points 8 and 4, | and : have entry points 2 and 6,
72 * / has 3 and 7, \ has 1 and 5, + has 2, 6, 8 and 4 etc.
73 *
74 *
75 * @author Efstathios Sideris
76 */
77 public class GridPattern extends TextGrid {
78
79 private ArrayList<Pattern> regExps = new ArrayList<Pattern>(); //TODO optimise: store as PatternS
80 private boolean regExpsAreValid = false;
81
82 private static final boolean DEBUG = false;
83
84 private boolean usesStandardSyntax = false;
85
86 public GridPattern(){
87 super(3, 3);
88 }
89
90 public GridPattern(String row1, String row2, String row3){
91 super(Math.max(Math.max(row1.length(), row2.length()), row3.length()), 3);
92 setTo(row1, row2, row3);
93 regExpsAreValid = false;
94 }
95
96 public boolean usesStandardSyntax() {
97 return usesStandardSyntax;
98 }
99
100 public void setUsesStandardSyntax(boolean b) {
101 usesStandardSyntax = b;
102 regExpsAreValid = false;
103 }
104
105 public boolean isMatchedBy(TextGrid grid){
106 /*if(grid.getHeight() != this.getHeight()
107 || grid.getWidth() != this.getWidth()) return false;*/
108 if(!regExpsAreValid) prepareRegExps();
109
110 for(int i = 0; i < grid.getHeight(); i++) {
111 String row = grid.getRow(i).toString();
112 Pattern regexp = regExps.get(i);
113 if(!regexp.matcher(row).matches()) {
114 if(DEBUG)
115 System.out.println(row+" does not match "+regexp);
116 return false;
117 }
118 }
119 return true;
120 }
121
122 private void prepareRegExps(){
123 regExpsAreValid = true;
124 regExps.clear();
125 if (DEBUG)
126 System.out.println("Trying to match:");
127 if(!usesStandardSyntax){
•128 Iterator<StringBuilder> it = getRows().iterator();
129 while (it.hasNext()) {
130 String row = it.next().toString();
131 regExps.add(Pattern.compile(makeRegExp(row)));
132 if(DEBUG)
133 System.out.println(row+" becomes "+makeRegExp(row));
134 }
135 } else {
•136 Iterator<StringBuilder> it = getRows().iterator();
137 while (it.hasNext()) {
138 String row = it.next().toString();
139 regExps.add(Pattern.compile(row));
140 }
141 }
142 }
143
144 private String makeRegExp(String pattern){
145 StringBuilder result = new StringBuilder();
146 int tokensHandled = 0;
147 for(int i = 0; i < pattern.length() && tokensHandled < 3; i++){
148 char c = pattern.charAt(i);
149 if(c == '[') {
150 result.append("[^|:]");
151 } else if(c == '|') {
152 result.append("[|:]");
153 } else if(c == '-') {
•154 result.append("[-=]");
155 } else if(c == '!') {
156 result.append("[^-=\\/\\\\+|:]");
157 } else if(c == 'b') {
158 result.append("[-=\\/\\\\+|:]");
159 } else if(c == '^') {
160 result.append("[\\/\\\\+|:]");
161 } else if(c == '(') {
162 result.append("[-=\\/\\\\+]");
163 } else if(c == '~') {
164 result.append(".");
165 } else if(c == '+') {
166 result.append("\\+");
167 } else if(c == '\\') {
168 result.append("\\\\");
169 } else if(c == 's') {
170 result.append("[-=+|:]");
171 } else if(c == 'S') {
172 result.append("[\\/\\\\]");
173 } else if(c == '*') {
174 result.append("\\*");
175
176 //entry points
177 } else if(c == '1') {
178 result.append("[\\\\]");
179
180 } else if(c == '2') {
181 result.append("[|:+\\/\\\\]");
182
183 } else if(c == '3') {
184 result.append("[\\/]");
185
186 } else if(c == '4') {
187 result.append("[-=+\\/\\\\]");
188
189 } else if(c == '5') {
190 result.append("[\\\\]");
191
192 } else if(c == '6') {
193 result.append("[|:+\\/\\\\]");
194
195 } else if(c == '7') {
196 result.append("[\\/]");
197
198 } else if(c == '8') {
199 result.append("[-=+\\/\\\\]");
200
201 //entry point negations
202 } else if(c == '%') {
203 if(i+1 > pattern.length()){
204 throw new RuntimeException("Invalid pattern, found % at the end");
205 }
206 c = pattern.charAt(++i);
207
208 if(c == '1') {
209 result.append("[^\\\\]");
210
211 } else if(c == '2') {
212 result.append("[^|:+\\/\\\\]");
213
214 } else if(c == '3') {
215 result.append("[^\\/]");
216
217 } else if(c == '4') {
218 result.append("[^-=+\\/\\\\]");
219
220 } else if(c == '5') {
221 result.append("[^\\\\]");
222
223 } else if(c == '6') {
224 result.append("[^|:+\\/\\\\]");
225
226 } else if(c == '7') {
227 result.append("[^\\/]");
228
229 } else if(c == '8') {
230 result.append("[^-=+\\/\\\\]");
231 }
232 } else result.append(String.valueOf(c));
233 tokensHandled++;
234 }
235 return result.toString();
236 }
237
238
239 public void setTo(String row1, String row2, String row3){
240 if(getHeight() != 3) throw new RuntimeException("This method can only be called for GridPatternS with height 3");
241 regExpsAreValid = false;
242 writeStringTo(0, 0, row1);
243 writeStringTo(0, 1, row2);
244 writeStringTo(0, 2, row3);
245 //don't use setRow() here!
246 }
247
248 public static void main(String[] args) {
249 TextGrid grid = new TextGrid(3, 3);
250 // grid.setRow(0, " ");
251 // grid.setRow(1, "-\\ ");
252 // grid.setRow(2, " | ");
253 //
254 // if(GridPatternGroup.corner2Criteria.isAnyMatchedBy(grid)){
255 // System.out.println("Grid is corner 2");
256 // } else {
257 // System.out.println("Grid is not corner 2");
258 // }
259 //
260 // if(grid.isCorner2(grid.new Cell(1,1))){
261 // System.out.println("Grid is corner 2");
262 // } else {
263 // System.out.println("Grid is not corner 2");
264 // }
265 //
266 //
267 // grid.setRow(0, "-+ ");
268 // grid.setRow(1, " | ");
269 // grid.setRow(2, "-+ ");
270 //
271 // if(GridPatternGroup.cornerCriteria.isAnyMatchedBy(grid)){
272 // System.out.println("Grid is corner");
273 // } else {
274 // System.out.println("Grid is not corner");
275 // }
276 //
277 // if(grid.isCorner(grid.new Cell(1,1))){
278 // System.out.println("Grid is corner");
279 // } else {
280 // System.out.println("Grid is not corner");
281 // }
282
283 grid.setRow(0, "---");
284 grid.setRow(1, " / ");
285 grid.setRow(2, "---");
286 grid.printDebug();
287 if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){
288 System.out.println("Grid is lone diagonal");
289 } else {
290 System.out.println("Grid is not lone diagonal");
291 }
292
293 grid.setRow(0, "--/");
294 grid.setRow(1, " / ");
295 grid.setRow(2, "---");
296 grid.printDebug();
297 if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){
298 System.out.println("Grid is lone diagonal");
299 } else {
300 System.out.println("Grid is not lone diagonal");
301 }
302
303 grid.setRow(0, "-- ");
304 grid.setRow(1, " \\ ");
305 grid.setRow(2, "---");
306 grid.printDebug();
307 if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){
308 System.out.println("Grid is lone diagonal");
309 } else {
310 System.out.println("Grid is not lone diagonal");
311 }
312
313 grid.setRow(0, "-- ");
314 grid.setRow(1, " \\ ");
315 grid.setRow(2, "--\\");
316 grid.printDebug();
317 if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){
318 System.out.println("Grid is lone diagonal");
319 } else {
320 System.out.println("Grid is not lone diagonal");
321 }
322
323 grid.setRow(0, " ");
324 grid.setRow(1, "-\\/");
325 grid.setRow(2, " ||");
326 grid.printDebug();
327 if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){
328 System.out.println("Grid is lone diagonal");
329 } else {
330 System.out.println("Grid is not lone diagonal");
331 }
332 }
333 }