Kirk Rader  1.0-SNAPSHOT
TT.java
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Kirk Rader
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package us.rader.tt.app;
18 
19 import java.io.ByteArrayInputStream;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.PrintStream;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 
28 import us.rader.tt.formula.Formula;
29 
30 /**
31  * Process a well-formed formula of the monadic predicate calculus.
32  */
33 public class TT {
34 
35  /**
36  * Invalid command-line arguments.
37  */
38  private static final int BAD_ARGUMENTS_STATUS = 10;
39 
40  /**
41  * Other run-time error.
42  */
43  private static final int ERROR_STATUS = 30;
44 
45  /**
46  * Number of spaces to indent generated markdown content.
47  */
48  private static final int INDENT = 0;
49 
50  /**
51  * Error logging and debug tracing.
52  */
53  private static final Logger LOGGER;
54  /**
55  * Syntax error in formula.
56  */
57  private static final int SYNTAX_ERROR_STATUS = 20;
58 
59  static {
60 
61  LOGGER = Logger.getLogger(TT.class.getName());
62 
63  }
64 
65  /**
66  * Prevent instantiation of utility class.
67  */
68  private TT() {
69 
70  // nothing to do here
71 
72  }
73 
74  /**
75  * Program entry point.
76  *
77  * @param args
78  * Command-line arguments.
79  */
80  public static void main(final String[] args) {
81 
82  try {
83 
84  switch (args.length) {
85 
86  case 0:
87 
88  tt(System.in, System.out); // NOSONAR
89  break;
90 
91  case 2:
92 
93  tt(args[0], args[1]);
94  break;
95 
96  default:
97 
98  usage(System.err); // NOSONAR
99  System.exit(BAD_ARGUMENTS_STATUS);
100 
101  }
102 
103  } catch (final IllegalArgumentException e) {
104 
105  LOGGER.logp(Level.FINE, TT.class.getName(), "main", "invalid command-line arguments", e);
106  usage(System.err); // NOSONAR
107  System.exit(BAD_ARGUMENTS_STATUS);
108 
109  } catch (final Expression.SyntaxError e) {
110 
111  LOGGER.logp(Level.SEVERE, TT.class.getName(), "main", "syntax error in formula", e);
112  System.exit(SYNTAX_ERROR_STATUS);
113 
114  } catch (final Exception e) {
115 
116  LOGGER.logp(Level.SEVERE, TT.class.getName(), "main", "error processing formula", e);
117  System.exit(ERROR_STATUS);
118 
119  }
120  }
121 
122  /**
123  * Process the well-formed formula from the given stream.
124  *
125  * @param inpuStream
126  * The input stream.
127  *
128  * @param outputStream
129  * The output stream.
130  *
131  * @throws IOException
132  * Thrown if an I/O error occurs.
133  */
134  private static void tt(final InputStream inpuStream, final PrintStream outputStream) throws IOException {
135 
136  final Formula term = Formula.parseFormula(inpuStream);
137  final String wff = term.toString();
138  outputStream.print("# ");
139  outputStream.println(wff);
140  outputStream.println();
141  outputStream.println("## Graphviz");
142  outputStream.println();
143  outputStream.println("```viz");
144  outputStream.println(term.toDigraph(INDENT));
145  outputStream.println("```");
146  outputStream.println();
147  outputStream.println("## LaTex");
148  outputStream.println();
149  outputStream.println("### Preamble");
150  outputStream.println();
151  outputStream.println("The LaTeX generated by this program assumes the following preamble:");
152  outputStream.println();
153  outputStream.println(" \\usepackage{amsmath}");
154  outputStream.println(" \\usepackage{amssymb}");
155  outputStream.println(" \\usepackage{graphicx}");
156  outputStream.println(" \\usepackage{natded}");
157  outputStream.println(" \\newcommand{\\lognot}{\\mathord{\\sim}}");
158  outputStream.println(" \\newcommand{\\uq}{\\mathord{\\bigwedge}}");
159  outputStream.println(" \\newcommand{\\eq}{\\mathord{\\bigvee}}");
160  outputStream.println(" \\newcommand{\\riota}{\\mathrm{\\rotatebox[origin=c]{180}{$\\iota$}}}");
161  outputStream.println();
162  outputStream.println("### Formula");
163  outputStream.println();
164  outputStream.println("```math");
165  outputStream.println(term.toLaTeX());
166  outputStream.println("```");
167  outputStream.println();
168  outputStream.println("## Truth Table");
169  outputStream.println();
170  final String truthTable = term.toTruthTable(INDENT);
171  outputStream.println("```math");
172  outputStream.print(truthTable);
173  outputStream.println("```");
174 
175  }
176 
177  /**
178  * Handle command-line arguments.
179  *
180  * @param command
181  * Command selector string.
182  *
183  * @param parameter
184  * Command parameter string.
185  *
186  * @throws IOException
187  * Thrown if an I/O error occurs.
188  */
189  private static void tt(final String command, final String parameter) throws IOException {
190 
191  switch (command) {
192 
193  case "-f":
194  case "--file": // NOSONAR
195 
196  try (FileInputStream stream = new FileInputStream(parameter)) {
197 
198  tt(stream, System.out); // NOSONAR
199 
200  }
201 
202  break;
203 
204  case "-e":
205  case "--expression": // NOSONAR
206 
207  try (ByteArrayInputStream stream = new ByteArrayInputStream(parameter.getBytes("UTF-8"))) {
208 
209  tt(stream, System.out); // NOSONAR
210 
211  }
212 
213  break;
214 
215  default:
216 
217  throw new IllegalArgumentException();
218 
219  }
220  }
221 
222  /**
223  * Print a usage message to the given stream.
224  *
225  * @param stream
226  * The output stream.
227  */
228  private static void usage(final PrintStream stream) {
229 
230  stream.println("usage:\tjava -jar TT.jar");
231  stream.println("\tjava -jar TT.jar (-f | --file) <input-file>");
232  stream.println("\tjava -jar TT.jar (-e | --expression) <expression>");
233  stream.println();
234  stream.println("if no <input-file> or <expression> is specified, reads from standard input stream");
235 
236  }
237 
238 }
static void usage(final PrintStream stream)
Print a usage message to the given stream.
Definition: TT.java:228
Demonstrate object-oriented design and implementation using a simple Java class library.
static final int ERROR_STATUS
Other run-time error.
Definition: TT.java:43
static void tt(final InputStream inpuStream, final PrintStream outputStream)
Process the well-formed formula from the given stream.
Definition: TT.java:134
static final Logger LOGGER
Error logging and debug tracing.
Definition: TT.java:53
static final int BAD_ARGUMENTS_STATUS
Invalid command-line arguments.
Definition: TT.java:38
final String toDigraph(final int indent)
Definition: Formula.java:505
final String toTruthTable(final int indent)
Definition: Formula.java:550
static void tt(final String command, final String parameter)
Handle command-line arguments.
Definition: TT.java:189
static final int INDENT
Number of spaces to indent generated markdown content.
Definition: TT.java:48
static Formula parseFormula(final BufferedReader reader)
Parse the next Formula from the given buffered input stream.
Definition: Formula.java:112
static void main(final String[] args)
Program entry point.
Definition: TT.java:80
Processor for well-formed formulas of the monadic predicate calculus.
Objects with unique Graphviz node names.
Definition: Expression.java:32
TT()
Prevent instantiation of utility class.
Definition: TT.java:68
Process a well-formed formula of the monadic predicate calculus.
Definition: TT.java:33
Exception thrown when a syntax error is detected while parsing an Expression.
Definition: Expression.java:41
A formula of the monadic predicate calculus.
Definition: Formula.java:51
static final int SYNTAX_ERROR_STATUS
Syntax error in formula.
Definition: TT.java:57