Kirk Rader  1.0-SNAPSHOT
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Package Functions | Static Private Attributes | List of all members
us.rader.tt.formula.Expression Class Referenceabstract

Objects with unique Graphviz node names. More...

Inheritance diagram for us.rader.tt.formula.Expression:
Inheritance graph

Classes

class  SyntaxError
 Exception thrown when a syntax error is detected while parsing an Expression. More...
 

Public Member Functions

final boolean equals (final Object obj)
 Base equality on the string representation of a formula. More...
 
abstract Set< ExpressiongetNodes ()
 
abstract Set< ExpressiongetTerminalNodes ()
 
final int hashCode ()
 Base the hash code on the string representation of a formula. More...
 
abstract Expression replaceVariable (Variable boundVariable, Expression factor)
 Replace boundVariable with factor everywhere in this Expression. More...
 
final String toLaTeX () throws IOException
 
abstract void toLaTeX (PrintStream stream)
 Print the LaTeX representation of this object to the given stream. More...
 
- Public Member Functions inherited from us.rader.tt.formula.Graphable
void printDigraph (PrintStream stream, int indent)
 Print the Graphviz nodes and edges for this object to the given stream. More...
 

Static Public Member Functions

static Expression parseFactor (final BufferedReader reader) throws IOException
 Parse the next Expression from the given buffered input stream. More...
 
static Expression parseFactor (final InputStream stream) throws IOException
 Parse the next Expression from the given buffered input stream. More...
 
static Expression parseFactor (final String factor) throws IOException
 Parse the next Expression from the given buffered input stream. More...
 

Protected Member Functions

 Expression ()
 Initialize nodeName. More...
 

Static Protected Member Functions

static Expression parseVariableBindingOperator (final BufferedReader reader, final Class<? extends VariableBindingOperator > type, final char terminator) throws IOException
 Parse the next VariableBindingOperator or Description from the given buffered input stream. More...
 
static void printIndentation (final PrintStream stream, final int indent)
 Print the specified number of spaces to the given stream. More...
 
static int skipWhitespace (final BufferedReader reader) throws IOException
 Discard white space from the given buffered input stream. More...
 

Protected Attributes

String nodeName
 Graphviz digraph node name. More...
 

Static Package Functions

 [static initializer]
 

Static Private Attributes

static final String ENCODING = "UTF-8"
 Character encoding for file and streams. More...
 
static long counter
 Make nodeName unique per instance. More...
 

Detailed Description

Objects with unique Graphviz node names.

Definition at line 32 of file Expression.java.

Constructor & Destructor Documentation

◆ Expression()

us.rader.tt.formula.Expression.Expression ( )
protected

Initialize nodeName.

Note that the combination of counter and the depth-first, left-to-right traversal of the nodes while parsing an expression results in monotonically increasing values for nodeName that can then be used to sort the columns in a truth table.

See also
Formula::toTruthTable

Definition at line 102 of file Expression.java.

References us.rader.tt.formula.Expression.counter.

102  {
103 
104  synchronized (Expression.class) {
105 
106  nodeName = "node" + ++counter;
107 
108  }
109  }
Expression()
Initialize nodeName.
String nodeName
Graphviz digraph node name.
Definition: Expression.java:90
static long counter
Make nodeName unique per instance.
Definition: Expression.java:74

Member Function Documentation

◆ [static initializer]()

us.rader.tt.formula.Expression.[static initializer] ( )
staticpackage

◆ equals()

final boolean us.rader.tt.formula.Expression.equals ( final Object  obj)

Base equality on the string representation of a formula.

See also
hashCode

Definition at line 302 of file Expression.java.

References us.rader.tt.formula.Expression.getNodes(), and us.rader.tt.formula.Expression.getTerminalNodes().

Referenced by us.rader.tt.formula.test.ExpressionTest.notEqualToNonExpressionTest(), us.rader.tt.formula.test.ExpressionTest.notEqualToNullTest(), us.rader.tt.formula.Identity.replaceFactor(), us.rader.tt.formula.Predicate.replaceVariable(), and us.rader.tt.formula.Variable.replaceVariable().

302  {
303 
304  if (obj == null) {
305 
306  return false;
307 
308  }
309 
310  if (!(obj instanceof Expression)) {
311 
312  return false;
313 
314  }
315 
316  return toString().equals(obj.toString());
317 
318  }
Expression()
Initialize nodeName.

◆ getNodes()

abstract Set<Expression> us.rader.tt.formula.Expression.getNodes ( )
abstract
Returns
The set of nodes for this formula (i.e. the columns of its truth table).

Referenced by us.rader.tt.formula.Expression.equals(), us.rader.tt.formula.Identity.getNodes(), and us.rader.tt.formula.Formula.toTruthTable().

◆ getTerminalNodes()

abstract Set<Expression> us.rader.tt.formula.Expression.getTerminalNodes ( )
abstract

◆ hashCode()

final int us.rader.tt.formula.Expression.hashCode ( )

Base the hash code on the string representation of a formula.

See also
equals

Definition at line 337 of file Expression.java.

References us.rader.tt.formula.Expression.replaceVariable().

337  {
338 
339  return toString().hashCode();
340 
341  }

◆ parseFactor() [1/3]

static Expression us.rader.tt.formula.Expression.parseFactor ( final BufferedReader  reader) throws IOException
static

Parse the next Expression from the given buffered input stream.

Parameters
readerThe buffered input stream.
Returns
The Expression
Exceptions
IOExceptionThrown if an I/O error occurs.

Definition at line 122 of file Expression.java.

References us.rader.tt.formula.Expression.parseVariableBindingOperator(), us.rader.tt.formula.Expression.skipWhitespace(), and us.rader.tt.formula.Expression.SyntaxError.SyntaxError().

Referenced by us.rader.tt.formula.test.ExpressionTest.equalToSameExpressionTest(), us.rader.tt.formula.test.DescriptionTest.instantiateTest(), us.rader.tt.formula.test.ExpressionTest.notEqualToNonExpressionTest(), us.rader.tt.formula.test.ExpressionTest.notEqualToNullTest(), us.rader.tt.formula.test.ExpressionTest.parseBadFactorTest(), us.rader.tt.formula.test.ExpressionTest.parseEmptyTest(), us.rader.tt.formula.Expression.parseFactor(), us.rader.tt.formula.Formula.parseIdentity(), us.rader.tt.formula.Formula.parseSymbolicNode(), us.rader.tt.formula.test.DescriptionTest.replaceVariableTest(), and us.rader.tt.formula.test.DescriptionTest.toLaTeXTest().

122  {
123 
124  final int c = skipWhitespace(reader);
125 
126  if (c == -1) {
127 
128  throw new SyntaxError("unexpected end of input parsing factor");
129 
130  }
131 
132  if (c == ':') {
133 
134  return parseVariableBindingOperator(reader, Description.class, ':');
135 
136  }
137 
138  if (Character.isLowerCase(c)) {
139 
140  return new Variable(Character.toString((char) c));
141 
142  }
143 
144  throw new SyntaxError(String.format("Expression expected, got '%s'", Character.toString((char) c)));
145 
146  }
static int skipWhitespace(final BufferedReader reader)
Discard white space from the given buffered input stream.
static Expression parseVariableBindingOperator(final BufferedReader reader, final Class<? extends VariableBindingOperator > type, final char terminator)
Parse the next VariableBindingOperator or Description from the given buffered input stream...

◆ parseFactor() [2/3]

static Expression us.rader.tt.formula.Expression.parseFactor ( final InputStream  stream) throws IOException
static

Parse the next Expression from the given buffered input stream.

Parameters
streamThe input stream.
Returns
The Expression
Exceptions
IOExceptionThrown if an I/O error occurs.

Definition at line 159 of file Expression.java.

References us.rader.tt.formula.Expression.parseFactor().

159  {
160 
161  try (InputStreamReader streamReader = new InputStreamReader(stream, ENCODING)) {
162 
163  try (BufferedReader reader = new BufferedReader(streamReader)) {
164 
165  return parseFactor(reader);
166 
167  }
168  }
169  }
static final String ENCODING
Character encoding for file and streams.
Definition: Expression.java:63
static Expression parseFactor(final BufferedReader reader)
Parse the next Expression from the given buffered input stream.

◆ parseFactor() [3/3]

static Expression us.rader.tt.formula.Expression.parseFactor ( final String  factor) throws IOException
static

Parse the next Expression from the given buffered input stream.

Parameters
factorThe string representation of the Expression to parse.
Returns
The Expression
Exceptions
IOExceptionThrown if an I/O error occurs.

Definition at line 182 of file Expression.java.

References us.rader.tt.formula.Expression.parseFactor().

182  {
183 
184  try (ByteArrayInputStream stream = new ByteArrayInputStream(factor.getBytes(ENCODING))) {
185 
186  return parseFactor(stream);
187 
188  }
189  }
static final String ENCODING
Character encoding for file and streams.
Definition: Expression.java:63
static Expression parseFactor(final BufferedReader reader)
Parse the next Expression from the given buffered input stream.

◆ parseVariableBindingOperator()

static Expression us.rader.tt.formula.Expression.parseVariableBindingOperator ( final BufferedReader  reader,
final Class<? extends VariableBindingOperator type,
final char  terminator 
) throws IOException
staticprotected

Parse the next VariableBindingOperator or Description from the given buffered input stream.

Parameters
readerThe buffered input stream.
typeThe type of VariableBindingOperator to create.
terminatorThe terminator symbol for the operator.
Returns
The Connective
Exceptions
IOExceptionThrown if an I/O error occurs.

Definition at line 209 of file Expression.java.

References us.rader.tt.formula.Formula.parseFormula(), us.rader.tt.formula.Expression.skipWhitespace(), and us.rader.tt.formula.Expression.SyntaxError.SyntaxError().

Referenced by us.rader.tt.formula.Expression.parseFactor(), and us.rader.tt.formula.Formula.parseOtherFormula().

210  {
211 
212  try {
213 
214  int c = skipWhitespace(reader);
215 
216  if (c == -1) {
217 
218  throw new SyntaxError(String.format("unexpected end of input parsing %s", type.getName()));
219 
220  }
221 
222  final String v = Character.toString((char) c);
223 
224  if (!Character.isLowerCase(c)) {
225 
226  reader.reset();
227  throw new SyntaxError(String.format("expected a variable, got \"%s\"", v));
228 
229  }
230 
231  c = skipWhitespace(reader);
232 
233  if (c != terminator) {
234 
235  reader.reset();
236  throw new SyntaxError(String.format("expected \"%s\", got \"%s\"", Character.toString(terminator), v));
237 
238  }
239 
240  final Expression operand = Formula.parseFormula(reader);
241  final Constructor<?> constructor = type.getConstructor(Variable.class, Formula.class);
242  return (Expression) constructor.newInstance(new Variable(v), operand);
243 
244  } catch (ReflectiveOperationException | SecurityException e) {
245 
246  throw new IllegalStateException(e);
247 
248  }
249 
250  }
Expression()
Initialize nodeName.
static int skipWhitespace(final BufferedReader reader)
Discard white space from the given buffered input stream.

◆ printIndentation()

static void us.rader.tt.formula.Expression.printIndentation ( final PrintStream  stream,
final int  indent 
)
staticprotected

Print the specified number of spaces to the given stream.

Parameters
streamThe output stream.
indentThe number of characters to print.

Definition at line 261 of file Expression.java.

Referenced by us.rader.tt.formula.Predicate.printDigraph(), us.rader.tt.formula.Variable.printDigraph(), us.rader.tt.formula.VariableBindingOperator.printDigraph(), us.rader.tt.formula.SymbolicNode.printDigraph(), us.rader.tt.formula.Operator.printDigraph(), us.rader.tt.formula.Identity.printDigraph(), us.rader.tt.formula.Formula.printTruthTableRow(), us.rader.tt.formula.Formula.toDigraph(), and us.rader.tt.formula.Formula.toTruthTable().

261  {
262 
263  for (int i = 0; i < indent; ++i) {
264 
265  stream.print(' ');
266 
267  }
268  }

◆ replaceVariable()

abstract Expression us.rader.tt.formula.Expression.replaceVariable ( Variable  boundVariable,
Expression  factor 
)
abstract

◆ skipWhitespace()

static int us.rader.tt.formula.Expression.skipWhitespace ( final BufferedReader  reader) throws IOException
staticprotected

Discard white space from the given buffered input stream.

Parameters
readerThe buffered input stream.
Returns
The next non-whitespace character or -1.
Exceptions
IOExceptionThrown if an I/O error occurs.

Definition at line 281 of file Expression.java.

Referenced by us.rader.tt.formula.Formula.parseConnective(), us.rader.tt.formula.Expression.parseFactor(), us.rader.tt.formula.Formula.parseFormula(), us.rader.tt.formula.Formula.parseIdentity(), us.rader.tt.formula.Formula.parseSymbolicNode(), and us.rader.tt.formula.Expression.parseVariableBindingOperator().

281  {
282 
283  int c;
284 
285  do {
286 
287  reader.mark(1);
288  c = reader.read();
289 
290  } while (c != -1 && Character.isWhitespace(c));
291 
292  return c;
293 
294  }

◆ toLaTeX() [1/2]

final String us.rader.tt.formula.Expression.toLaTeX ( ) throws IOException
Returns
The LaTeX representation of this object
Exceptions
IOExceptionThrow if an I/O error occurs.

Definition at line 363 of file Expression.java.

References us.rader.tt.formula.Expression.ENCODING.

Referenced by us.rader.tt.formula.Identity.toLaTeX(), us.rader.tt.formula.test.DescriptionTest.toLaTeXTest(), and us.rader.tt.app.TT.tt().

363  {
364 
365  try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
366 
367  try (PrintStream printStream = new PrintStream(stream, false, ENCODING)) {
368 
369  toLaTeX(printStream);
370  printStream.flush();
371  return new String(stream.toByteArray(), ENCODING);
372 
373  }
374  }
375  }
static final String ENCODING
Character encoding for file and streams.
Definition: Expression.java:63

◆ toLaTeX() [2/2]

abstract void us.rader.tt.formula.Expression.toLaTeX ( PrintStream  stream)
abstract

Print the LaTeX representation of this object to the given stream.

Parameters
streamThe output stream.

Member Data Documentation

◆ counter

long us.rader.tt.formula.Expression.counter
staticprivate

Make nodeName unique per instance.

Using a sequential counter rather than, for example, a hash allows nodeName to be used for sorting columns in a truth table.

See also
Expression
Formula::toTruthTable

Definition at line 74 of file Expression.java.

Referenced by us.rader.tt.formula.Expression.Expression().

◆ ENCODING

final String us.rader.tt.formula.Expression.ENCODING = "UTF-8"
staticprivate

Character encoding for file and streams.

Definition at line 63 of file Expression.java.

Referenced by us.rader.tt.formula.Expression.toLaTeX().

◆ nodeName

String us.rader.tt.formula.Expression.nodeName
protected

The documentation for this class was generated from the following file: