summaryrefslogtreecommitdiff
path: root/RGens/src/main/java/bjc/rgens/server/ReaderState.java
blob: 3f5021e33d413ba963a3e476b2a53a583ca1420d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package bjc.rgens.server;

import java.util.function.Supplier;

import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
import bjc.utils.gen.WeightedGrammar;

/**
 * Represents the internal state of reader
 * 
 * @author ben
 *
 */
public class ReaderState {
	private WeightedGrammar<String>			currentGrammar;
	private String							currentRule;
	private boolean							isUniform;

	private IList<String> exports;

	/**
	 * Create a new reader state
	 */
	public ReaderState() {
		currentGrammar = new WeightedGrammar<>();

		// Grammars start out uniform
		isUniform = true;

		exports = new FunctionalList<>();
	}

	public void addExport(String export) {
		exports.add(export);
	}

	public IList<String> getExports() {
		return exports;
	}

	/**
	 * Get the rule names for the current grammar
	 * 
	 * @return The rule names for the current grammar
	 */
	public IList<String> getRuleNames() {
		return currentGrammar.getRuleNames();
	}

	/**
	 * Check if this reader is currently in uniform mode
	 * 
	 * @return Whether this grammar is in uniform mode
	 */
	public boolean isUniform() {
		return isUniform;
	}

	/**
	 * Set the current grammar to be the specified one
	 * 
	 * @param newWorkingGrammar
	 *            The new grammar to use
	 */
	public void setCurrentGrammar(WeightedGrammar<String> newWorkingGrammar) {
		currentGrammar = newWorkingGrammar;
	}

	/**
	 * Set the rule currently being worked on
	 * 
	 * @param ruleName
	 *            The rule currently being worked on
	 */
	public void setCurrentRule(String ruleName) {
		currentRule = ruleName;
	}

	/**
	 * Set the initial rule of this grammar
	 * 
	 * @param ruleName
	 *            The initial rule of this grammar
	 */
	public void setInitialRule(String ruleName) {
		currentGrammar.setInitialRule(ruleName);
	}

	/**
	 * Toggle this uniformity setting for this grammar
	 */
	public void toggleUniformity() {
		isUniform = !isUniform;
	}

	/**
	 * Add a case to the current grammar
	 * 
	 * @param ruleProbability
	 *            The probability for this case to occur
	 * @param ruleParts
	 *            The parts that make up this case
	 */
	public void addCase(int ruleProbability, IList<String> ruleParts) {
		currentGrammar.addCase(currentRule, ruleProbability, ruleParts);
	}

	/**
	 * Add a special rule to the grammar
	 *
	 * @param ruleName The name of the special rule
	 * @param cse The special case for the rule
	 */
	public void addSpecialRule(String ruleName, Supplier<IList<String>> cse) {
		currentGrammar.addSpecialRule(ruleName, cse);
	}

	/**
	 * Start editing a new rule in the current grammar
	 * 
	 * @param ruleName
	 *            The name of the new rule to edit
	 */
	public void startNewRule(String ruleName) {
		currentGrammar.addRule(ruleName);

		currentRule = ruleName;
	}

	/**
	 * Convert this package of state into a weighted grammar
	 * 
	 * @return The grammar represented by this state
	 */
	public WeightedGrammar<String> getGrammar() {
		return currentGrammar;
	}

	/**
	 * Prefix a token onto all of the cases for the specified rule
	 * 
	 * @param ruleName
	 *            The rule to do prefixing on
	 * @param prefixToken
	 *            The token to prefix onto each case
	 * @param additionalProbability
	 *            The probability modification of the prefixed cases
	 */
	public void prefixRule(String ruleName, String prefixToken,
			int additionalProbability) {
		currentGrammar.prefixRule(ruleName, prefixToken,
				additionalProbability);
	}

	/**
	 * Delete a rule from the current grammar
	 * 
	 * @param ruleName
	 *            The name of the rule to delete
	 */
	public void deleteRule(String ruleName) {
		currentGrammar.deleteRule(ruleName);
	}

	/**
	 * Suffix a token onto all of the cases for the specified rule
	 * 
	 * @param ruleName
	 *            The rule to do suffixing on
	 * @param suffixToken
	 *            The token to suffix onto each case
	 * @param additionalProbability
	 *            The probability modification of the suffixed cases
	 */
	public void suffixRule(String ruleName, String suffixToken,
			int additionalProbability) {
		currentGrammar.suffixRule(ruleName, suffixToken,
				additionalProbability);
	}
}