Kirk Rader  1.0-SNAPSHOT
Predicate.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.formula;
18 
19 import java.io.PrintStream;
20 
21 /**
22  * A predicate, i.e. an upper-case letter immediately followed by a lower-case
23  * letter.
24  */
25 final class Predicate extends SymbolicNode {
26 
27  /**
28  * The factor to which this predicate is applied.
29  */
30  private final Expression factor;
31 
32  /**
33  * @param symbol
34  * Symbol for {@link SymbolicNode#SymbolicNode}.
35  *
36  * @param factor
37  * The {@link Expression} denoting the object to which this
38  * {@link Predicate} is applied
39  */
40  public Predicate(final String symbol, final Expression factor) {
41 
42  super(symbol);
43  this.factor = factor;
44 
45  }
46 
47  /**
48  * @see Graphable#printDigraph
49  */
50  @Override
51  public void printDigraph(final PrintStream stream, final int indent) {
52 
53  super.printDigraph(stream, indent);
54  factor.printDigraph(stream, indent);
55  printIndentation(stream, indent);
56  stream.print(nodeName);
57  stream.print(" -> ");
58  stream.println(factor.nodeName);
59 
60  }
61 
62  /**
63  * @see Expression#replaceVariable
64  */
65  @Override
66  public Expression replaceVariable(final Variable boundVariable, final Expression newFactor) {
67 
68  final Expression expression = factor.equals(boundVariable) ? newFactor : factor;
69  return new Predicate(symbol, expression);
70 
71  }
72 
73  /**
74  * @return Symbol immediately followed by variable.
75  */
76  @Override
77  public String toString() {
78 
79  return super.toString() + factor.toString();
80 
81  }
82 
83 }
void printDigraph(PrintStream stream, int indent)
Print the Graphviz nodes and edges for this object to the given stream.
String nodeName
Graphviz digraph node name.
Definition: Expression.java:90
A variable (lower-case alphabetic character) of the monadic predicate calculus.
Definition: Variable.java:27
final boolean equals(final Object obj)
Base equality on the string representation of a formula.
void printDigraph(final PrintStream stream, final int indent)
Definition: Predicate.java:51
A sentence or predicate.
final Expression factor
The factor to which this predicate is applied.
Definition: Predicate.java:30
Objects with unique Graphviz node names.
Definition: Expression.java:32
String symbol
The symbol for this primitive term.
Predicate(final String symbol, final Expression factor)
Definition: Predicate.java:40
Expression replaceVariable(final Variable boundVariable, final Expression newFactor)
Definition: Predicate.java:66
static void printIndentation(final PrintStream stream, final int indent)
Print the specified number of spaces to the given stream.