Debian Codegraph @

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

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