Skip to content

Commit 804e968

Browse files
committed
do not print directly to print writer but use a bufferedwriter and an intermediate string builder
1 parent 3dd0cf6 commit 804e968

File tree

9 files changed

+64
-39
lines changed

9 files changed

+64
-39
lines changed

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/GeneralAutomatonPrinter.java

+29-12
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@
2626
*/
2727
package de.uni_freiburg.informatik.ultimate.automata;
2828

29+
import java.io.BufferedWriter;
30+
import java.io.IOException;
2931
import java.io.PrintWriter;
3032
import java.util.Collection;
3133
import java.util.Map;
3234

35+
import de.uni_freiburg.informatik.ultimate.util.CoreUtil;
36+
3337
/**
3438
* Abstract superclass for automaton printers.
35-
*
39+
*
3640
* @author Christian Schilling ([email protected])
3741
*/
3842
@SuppressWarnings("squid:S1694")
@@ -41,33 +45,50 @@ public abstract class GeneralAutomatonPrinter {
4145
public static final String NEW_LINE = System.lineSeparator();
4246

4347
private final PrintWriter mPrintWriter;
48+
private StringBuilder mStringBuilder;
4449

4550
/**
4651
* @param printWriter
4752
* Print writer to write to.
4853
*/
4954
protected GeneralAutomatonPrinter(final PrintWriter printWriter) {
5055
mPrintWriter = printWriter;
56+
mStringBuilder = new StringBuilder();
57+
}
58+
59+
protected void finish() {
60+
final BufferedWriter bw = new BufferedWriter(mPrintWriter);
61+
try {
62+
bw.write(mStringBuilder.toString());
63+
bw.flush();
64+
mStringBuilder = new StringBuilder();
65+
} catch (final IOException e) {
66+
throw new RuntimeException(e);
67+
}
5168
}
5269

5370
protected final void println(final String string) {
54-
mPrintWriter.println(string);
71+
mStringBuilder.append(string).append(CoreUtil.getPlatformLineSeparator());
5572
}
5673

5774
protected final void println(final char character) {
58-
mPrintWriter.println(character);
75+
mStringBuilder.append(character).append(CoreUtil.getPlatformLineSeparator());
5976
}
6077

6178
protected final void print(final String string) {
62-
mPrintWriter.print(string);
79+
mStringBuilder.append(string);
80+
}
81+
82+
protected final void printElement(final String elem) {
83+
mStringBuilder.append(elem).append(' ');
6384
}
6485

6586
protected final void print(final char character) {
66-
mPrintWriter.print(character);
87+
mStringBuilder.append(character);
6788
}
6889

6990
protected final void print(final StringBuilder builder) {
70-
mPrintWriter.print(builder);
91+
mStringBuilder.append(builder);
7192
}
7293

7394
protected final void printAutomatonPrefix() {
@@ -100,10 +121,6 @@ protected final void printlnCollectionPrefix(final String string) {
100121
println(getCollectionPrefix(string));
101122
}
102123

103-
protected final void printElement(final String elem) {
104-
mPrintWriter.print(elem + ' ');
105-
}
106-
107124
private static String replaceBackslashes(final Object input) {
108125
return input.toString().replaceAll("\"", "\\\"");
109126
}
@@ -131,11 +148,11 @@ protected final void printOneTransitionSuffix() {
131148
protected final void printTransitionsSuffix() {
132149
print("\t}");
133150
}
134-
151+
135152
protected final void printTransitionListSeparator() {
136153
println(",");
137154
}
138-
155+
139156
protected final void printLastTransitionLineSeparator() {
140157
println("");
141158
}

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/alternating/visualization/AlternatingAutomatonWriter.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/**
3636
* Constructor takes a AlternatingAutomaton and writes it to a testfile.
37-
*
37+
*
3838
* @author Alexander Nutz ([email protected])
3939
* @author Christian Schilling ([email protected])
4040
* @param <LETTER>
@@ -69,6 +69,7 @@ public AlternatingAutomatonWriter(final PrintWriter writer, final String name,
6969
// printFinalStates(mAa.getFinalStates());
7070
// printInternalTransitions(mAa.getTransitionsMap());
7171
printAutomatonSuffix();
72+
finish();
7273
}
7374

7475
/*
@@ -78,25 +79,25 @@ public AlternatingAutomatonWriter(final PrintWriter writer, final String name,
7879
* private void printExistentialStates(Set<STATE> set) { mprintWriter.print(TAB + "existentialStates = { "); for
7980
* (STATE state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
8081
*
81-
* private void printUniversalStates(Set<STATE> set) { mprintWriter.print(TAB + "universalStates = { "); for
82-
* (STATE state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
82+
* private void printUniversalStates(Set<STATE> set) { mprintWriter.print(TAB + "universalStates = { "); for (STATE
83+
* state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
8384
*
8485
* private void printInitialStates(Set<STATE> set) { mprintWriter.print(TAB + "initialStates = { "); for (STATE
8586
* state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
8687
*
87-
* private void printFinalStates(Set<STATE> set) { mprintWriter.print(TAB + "finalStates = { "); for (STATE
88-
* state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
88+
* private void printFinalStates(Set<STATE> set) { mprintWriter.print(TAB + "finalStates = { "); for (STATE state :
89+
* set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
8990
*
9091
* private void printInternalTransitions(Map<STATE, Map<LETTER, Set<STATE>>> map) { mprintWriter.println(TAB +
9192
* "internalTransitions = {"); for (Entry<STATE, Map<LETTER, Set<STATE>>> entry : map.entrySet()) { STATE pre =
92-
* entry.getKey(); Map<LETTER, Set<STATE>> transitionsMap = entry.getValue(); if (transitionsMap != null) {//
93-
* state has no outgoing transitions, so nothing has to be printed for (Entry<LETTER, Set<STATE>> entry1 :
93+
* entry.getKey(); Map<LETTER, Set<STATE>> transitionsMap = entry.getValue(); if (transitionsMap != null) {// state
94+
* has no outgoing transitions, so nothing has to be printed for (Entry<LETTER, Set<STATE>> entry1 :
9495
* transitionsMap.entrySet()) { LETTER letter = entry1.getKey(); Set<STATE> succStates = entry1.getValue(); for
9596
* (STATE succ : succStates) { printInternalTransition(pre, letter, succ); } } }
9697
*
9798
* } mprintWriter.println("\t},"); }
9899
*
99-
* private void printInternalTransition(STATE pre, LETTER letter, STATE succ) { mprintWriter.println("\t\t (" +
100-
* pre + ' ' + letter + ' ' + succ + ')' ); }
100+
* private void printInternalTransition(STATE pre, LETTER letter, STATE succ) { mprintWriter.println("\t\t (" + pre
101+
* + ' ' + letter + ' ' + succ + ')' ); }
101102
*/
102103
}

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/nestedword/visualization/BaFormatWriter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
/**
3838
* <tt>BA</tt> format writer.
39-
*
39+
*
4040
* @author Matthias Heizmann ([email protected])
4141
* @author Christian Schilling ([email protected])
4242
* @param <LETTER>
@@ -57,6 +57,7 @@ public final class BaFormatWriter<LETTER, STATE> extends CommonExternalFormatWri
5757
public BaFormatWriter(final PrintWriter writer, final INestedWordAutomaton<LETTER, STATE> nwa) {
5858
super(writer, nwa);
5959
doPrint();
60+
finish();
6061
}
6162

6263
private void doPrint() {

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/nestedword/visualization/CommonExternalFormatWriter.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
/**
4242
* Common methods for writers of external formats.
43-
*
43+
*
4444
* @author Matthias Heizmann ([email protected])
4545
* @author Christian Schilling ([email protected])
4646
* @param <LETTER>
@@ -64,14 +64,15 @@ public CommonExternalFormatWriter(final PrintWriter writer, final INestedWordAut
6464
mAlphabetMapping = getAlphabetMapping(nwa.getVpAlphabet().getInternalAlphabet());
6565
mStateMapping = getStateMapping(nwa.getStates());
6666
mNwa = nwa;
67+
finish();
6768
}
6869

6970
private Map<LETTER, String> getAlphabetMapping(final Collection<LETTER> alphabet) {
7071
int counter = 0;
7172
final Map<LETTER, String> alphabetMapping = new LinkedHashMap<>();
72-
73-
ArrayList<LETTER> alphabetList=new ArrayList<LETTER>(alphabet);
74-
73+
74+
final ArrayList<LETTER> alphabetList = new ArrayList<>(alphabet);
75+
7576
Collections.sort(alphabetList, new sortLetter<LETTER>());
7677
for (final LETTER letter : alphabetList) {
7778
alphabetMapping.put(letter, Integer.toString(counter));

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/nestedword/visualization/GoalFormatWriter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/**
3636
* <tt>GOAL</tt> format writer.
37-
*
37+
*
3838
* @author Matthias Heizmann ([email protected])
3939
* @author Christian Schilling ([email protected])
4040
* @param <LETTER>
@@ -62,6 +62,7 @@ public GoalFormatWriter(final PrintWriter writer, final INestedWordAutomaton<LET
6262
mLetterConverter = new MapBasedConverter<>(mAlphabetMapping);
6363
mStateConverter = new MapBasedConverter<>(mStateMapping);
6464
doPrint();
65+
finish();
6566
}
6667

6768
private void doPrint() {

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/nestedword/visualization/HanoiFormatWriter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/**
3636
* {@code Hanoi} format writer.
37-
*
37+
*
3838
* @author Matthias Heizmann ([email protected])
3939
* @author Christian Schilling ([email protected])
4040
* @param <LETTER>
@@ -60,6 +60,7 @@ public HanoiFormatWriter(final PrintWriter writer, final INestedWordAutomaton<LE
6060
super(writer, nwa);
6161
mLetterConverter = USE_LABELS ? new ToStringConverter<>() : new MapBasedConverter<>(mAlphabetMapping);
6262
doPrint();
63+
finish();
6364
}
6465

6566
private void doPrint() {

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/nestedword/visualization/NwaWriter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
/**
4242
* Prints an {@link INestedWordAutomaton}.
43-
*
43+
*
4444
* @author Matthias Heizmann ([email protected])
4545
* @author Christian Schilling ([email protected])
4646
* @param <LETTER>
@@ -123,6 +123,7 @@ public NwaWriter(final PrintWriter writer, final String name, final INestedWordA
123123
printAutomatonSuffix();
124124
}
125125
}
126+
finish();
126127
}
127128

128129
/**
@@ -254,7 +255,7 @@ private void printReturnTransitions(final Collection<STATE> allStates) {
254255
}
255256
printTransitionsSuffix();
256257
}
257-
258+
258259
private void printEpsilonTransitions(final Collection<STATE> allStates) {
259260
printlnCollectionPrefix("epsilonTransitions");
260261
for (final STATE state : allStates) {

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/petrinet/visualization/NetWriter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
/**
4040
* Prints a {@link PetriNetJulian}.
41-
*
41+
*
4242
* @author Matthias Heizmann ([email protected])
4343
* @author Christian Schilling ([email protected])
4444
* @param <LETTER>
@@ -73,6 +73,7 @@ public NetWriter(final PrintWriter writer, final String name, final PetriNetJuli
7373
printInitialMarking(net.getInitialMarking());
7474
printAcceptingPlaces(net.getAcceptingPlaces());
7575
printAutomatonSuffix();
76+
finish();
7677
}
7778

7879
protected abstract Map<LETTER, String> getAlphabetMapping(final Collection<LETTER> alphabet);

trunk/source/Library-Automata/src/de/uni_freiburg/informatik/ultimate/automata/tree/visualization/TreeAutomatonWriter.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected TreeAutomatonWriter(final PrintWriter writer, final String name,
8787
printFinalStates(mTreeAutomaton.getFinalStates(), stateMapping);
8888
printTransitionTable(mTreeAutomaton.getRules(), alphabetMapping, stateMapping);
8989
printAutomatonSuffix();
90+
finish();
9091
}
9192

9293
private void printTransitionTable(final Iterable<TreeAutomatonRule<LETTER, STATE>> rules,
@@ -195,25 +196,25 @@ protected Map<STATE, String> getStateMapping(final Collection<STATE> states) {
195196
* private void printExistentialStates(Set<STATE> set) { mprintWriter.print(TAB + "existentialStates = { "); for
196197
* (STATE state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
197198
*
198-
* private void printUniversalStates(Set<STATE> set) { mprintWriter.print(TAB + "universalStates = { "); for
199-
* (STATE state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
199+
* private void printUniversalStates(Set<STATE> set) { mprintWriter.print(TAB + "universalStates = { "); for (STATE
200+
* state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
200201
*
201202
* private void printInitialStates(Set<STATE> set) { mprintWriter.print(TAB + "initialStates = { "); for (STATE
202203
* state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
203204
*
204-
* private void printFinalStates(Set<STATE> set) { mprintWriter.print(TAB + "finalStates = { "); for (STATE
205-
* state : set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
205+
* private void printFinalStates(Set<STATE> set) { mprintWriter.print(TAB + "finalStates = { "); for (STATE state :
206+
* set) { mprintWriter.print(state + ' '); } mprintWriter.print("},\n"); }
206207
*
207208
* private void printInternalTransitions(Map<STATE, Map<LETTER, Set<STATE>>> map) { mprintWriter.println(TAB +
208209
* "internalTransitions = {"); for (Entry<STATE, Map<LETTER, Set<STATE>>> entry : map.entrySet()) { STATE pre =
209-
* entry.getKey(); Map<LETTER, Set<STATE>> transitionsMap = entry.getValue(); if (transitionsMap != null) {//
210-
* state has no outgoing transitions, so nothing has to be printed for (Entry<LETTER, Set<STATE>> entry1 :
210+
* entry.getKey(); Map<LETTER, Set<STATE>> transitionsMap = entry.getValue(); if (transitionsMap != null) {// state
211+
* has no outgoing transitions, so nothing has to be printed for (Entry<LETTER, Set<STATE>> entry1 :
211212
* transitionsMap.entrySet()) { LETTER letter = entry1.getKey(); Set<STATE> succStates = entry1.getValue(); for
212213
* (STATE succ : succStates) { printInternalTransition(pre, letter, succ); } } }
213214
*
214215
* } mprintWriter.println("\t},"); }
215216
*
216-
* private void printInternalTransition(STATE pre, LETTER letter, STATE succ) { mprintWriter.println("\t\t (" +
217-
* pre + ' ' + letter + ' ' + succ + ')' ); }
217+
* private void printInternalTransition(STATE pre, LETTER letter, STATE succ) { mprintWriter.println("\t\t (" + pre
218+
* + ' ' + letter + ' ' + succ + ')' ); }
218219
*/
219220
}

0 commit comments

Comments
 (0)