summaryrefslogtreecommitdiff
path: root/RGens/src/main/java/bjc/rgens/server/GrammarServer.java
blob: 1a47de222cd0c5d35482ee4a38daa1cc59caf2fe (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package bjc.rgens.server;

import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.IMap;
import bjc.utils.gen.WeightedGrammar;

import java.io.File;

import java.util.Scanner;

public class GrammarServer {
	private Scanner scn;

	private IMap<String, WeightedGrammar<String>> loadedGrammars;
	private IMap<String, WeightedGrammar<String>> exportedRules;
	
	private GrammarServerEngine eng;
	
	public GrammarServer(Scanner scn) {
		this.scn = scn;

		this.loadedGrammars = new FunctionalMap<>();
		this.exportedRules  = new FunctionalMap<>();

		eng = new GrammarServerEngine(loadedGrammars, exportedRules);
		
		ServerGrammarReader.setExportedRules(exportedRules);
	}

	public static void main(String[] args) {
		System.out.println("GrammarServer 1.0");

		Scanner scn = new Scanner(System.in);

		GrammarServer serv = new GrammarServer(scn);

		CLIArgsParser.parseArgs(args, serv.eng);

		System.out.print("Enter a command (m for help): ");

		char comm = scn.nextLine().charAt(0);

		while(comm != 'e') {
			switch(comm) {
				case 'm':
					System.out.println("GrammarServer Commands:");
					System.out.println("\tm: Print command help");
					System.out.println("\te: Exit GrammarServer");
					System.out.println("\tl: Load from file");
					System.out.println("\ts: Show information");
					System.out.println("\tg: Generate text");
					break;
				case 'g':
					serv.generateMode();
					break;
				case 's':
					serv.showMode();
					break;
				case 'l':
					serv.loadMode();
					break;
				default:
					System.out.println("? Unrecognized Command");
			}

			System.out.print("Enter a command (m for help): ");

			comm = scn.nextLine().charAt(0);
		}

		System.out.println("GrammarServer exiting");
	}

	

	private void loadMode() {
		System.out.println("Entering Load Mode");

		System.out.print("(Load Mode) Enter a command (m for help): ");
		char comm = scn.nextLine().charAt(0);

		while(comm != 'e') {
			switch(comm) {
				case 'm':
					System.out.println("GrammarServer Load Mode Commands");
					System.out.println("\tm: Show command help");
					System.out.println("\te: Exit Load Mode");
					System.out.println("\tg: Load grammar from a file");
					System.out.println("\tl: Load long rule from a file");
					System.out.println("\tc: Load configuration from a file");
					break;
				case 'g':
					loadGrammar();
					break;
				case 'c':
					loadConfig();
					break;
				case 'l':
					loadLongRule();
					break;
				default:
					System.out.println("? Unrecognized Command");
			}

			System.out.print("(Load Mode) Enter a command (m for help): ");
			comm = scn.nextLine().charAt(0);
		}

		System.out.println("Exiting Load Mode");
	}

	private void loadGrammar() {
		System.out.print("Enter path to load grammar from: ");
	
		String grammarPath = scn.nextLine().trim();
	
		File grammarFile = new File(grammarPath);
	
		String grammarName = grammarFile.getName().trim();
	
		grammarName = grammarName.substring(0, grammarName.lastIndexOf("."));
	
		System.out.printf("Enter grammar name or press enter for"
				+ " the default (%s): ", grammarName);
	
		String inputName = scn.nextLine();
	
		if(!inputName.equals("")) {
			grammarName = inputName;
		}
	
		eng.doLoadGrammar(grammarName, grammarPath);
	
		return;
	}

	private void loadLongRule() {
		System.out.print("Enter the file to load a long rule from: ");

		String fileName = scn.nextLine().trim();
		File ruleFile = new File(fileName);

		String tempName = ruleFile.getName();
		tempName = tempName.substring(0, tempName.lastIndexOf('.'));

		System.out.printf("Enter the name for the long rule (default %s): ", tempName);

		String ruleName = scn.nextLine().trim();

		if(ruleName.equals("")) {
			ruleName = tempName;
		}

		eng.doLoadLongRule(ruleName, fileName);
	}

	private void loadConfig() {
		System.out.print("Enter the file to load configuration from: ");
		
		String fileName = scn.nextLine().trim();

		eng.doLoadConfig(fileName);
	}

	private void showMode() {
		System.out.println("Entering Show Mode");

		System.out.print("(Show Mode) Enter a command (m for help): ");

		char comm = scn.nextLine().charAt(0);

		while(comm != 'e') {
			switch(comm) {
				case 'm':
					System.out.println("GrammarServer Show Mode Commands: ");
					System.out.println("\tm: Show command help");
					System.out.println("\tl: Show loaded grammars");
					System.out.println("\tr: Show rules from a grammar");
					System.out.println("\tx: Show exported rules");
					System.out.println("\te: Exit Show Mode");
					break;
				case 'r':
					showGrammarRules();
					break;
				case 'x':
					eng.doShowExportedRules();
					break;
				case 'l':
					eng.doShowLoadedGrammars();
					break;
				default:
					System.out.println("? Unrecognized command");
					break;
			}

			System.out.print("(Show Mode) Enter a command (m for help): ");

			comm = scn.nextLine().charAt(0);
		}



		System.out.println("Exiting Show Mode");
	}

	private void generateMode() {
		System.out.println("Entering Generate Mode");

		System.out.print("(Generate Mode) Enter a command (m for help): ");

		char comm = scn.nextLine().charAt(0);

		while(comm != 'e') {
			switch(comm) {
				case 'm':
					System.out.println("GrammarServer Generate Mode Commands: ");
					System.out.println("\tm: Show command help");
					System.out.println("\tx: Generate from exported rules");
					System.out.println("\tr: Generate from a grammar");
					System.out.println("\te: Exit Generate Mode");
					break;
				case 'x':
					generateExportedRule();
					break;
				case 'r':
					generateGrammar();
					break;
				default:
					System.out.println("? Unrecognized command");
			}

			System.out.print("(Generate Mode) Enter a command (m for help): ");

			comm = scn.nextLine().charAt(0);
		}

		System.out.println("Exiting Generate Mode");
	}

	private void generateExportedRule() {
		System.out.print("Enter the name of the rule to generate"
				+ " (l to list, enter to cancel): ");

		String ruleName = scn.nextLine().trim();

		while(true) {
			if(ruleName.equals("")) break;

			if(ruleName.equals("l")) {
				eng.doShowExportedRules();
			} else if (exportedRules.containsKey(ruleName)) {
				eng.doGenerateExportedRule(ruleName);

				System.out.print("Generate again from this rule? (yes/no) (yes by default): ");

				String resp = scn.nextLine().trim();

				if(resp.equalsIgnoreCase("yes") || resp.equals("")) {
					continue;
				}
			} else {
				System.out.println("? Unrecognized external rule");
			}

			System.out.print("Enter the name of the rule to generate"
					+ " (l to list, enter to cancel): ");

			ruleName = scn.nextLine().trim();
		}
	}

	private void generateGrammar() {
		System.out.print("Enter the name of the grammar to generate from (l to list, enter to cancel): ");

		String grammarName = scn.nextLine().trim();

		while(true) {
			if(grammarName.equals("")) break;

			if(grammarName.equals("l")) {
				eng.doShowLoadedGrammars();
			} else if(loadedGrammars.containsKey(grammarName)) {
				WeightedGrammar<String> currentGram = loadedGrammars.get(grammarName);

				System.out.print("Enter the name of the rule to generate"
						+ " (l to list, enter to cancel): ");

				String ruleName = scn.nextLine().trim();

				while(true) {
					if(ruleName.equals("")) break;

					if(ruleName.equals("l")) {
						eng.doShowGrammarRules(grammarName);
					} else if (currentGram.hasRule(ruleName)) {
						eng.doGenerateGrammar(currentGram, ruleName);

						System.out.print("Generate again from this rule? (yes/no) (yes by default): ");

						String resp = scn.nextLine().trim();

						if(resp.equalsIgnoreCase("yes") || resp.equals("")) {
							continue;
						}
					} else {
						System.out.println("? Unrecognized grammar rule");
					}

					System.out.print("Enter the name of the rule to generate"
							+ " (l to list, enter to cancel): ");

					ruleName = scn.nextLine().trim();
				}

			} else {
				System.out.println("? Unrecognized grammar name");
			}

			System.out.print("Enter the name of the grammar to generate from "
					+ "(l to list, enter to cancel): ");

			grammarName = scn.nextLine().trim();
		}
	}

	private void showGrammarRules() {
		System.out.print("Enter the name of the grammar (l to list): ");
		String gramName = scn.nextLine().trim();
	
		do {
			if(gramName.equals("")) break;
	
			if(gramName.equals("l")) {
				eng.doShowLoadedGrammars();
			} else if (loadedGrammars.containsKey(gramName)) {
				eng.doShowGrammarRules(gramName);
				break;
			} else {
				System.out.println("? Unrecognized grammar name");
			}
	
			System.out.print("Enter the name of the grammar (l to list): ");
			gramName = scn.nextLine().trim();
		} while(true);
	}
}