Kirk Rader  1.0-SNAPSHOT
Identity.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 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23 
24 /**
25  * Identity, i.e. _factor_ = _factor_.
26  */
27 final class Identity extends Formula {
28 
29  /**
30  * First {@link Expression}
31  */
32  private final Expression factor1;
33 
34  /**
35  * Second {@link Expression}
36  */
37  private final Expression factor2;
38 
39  /**
40  * Initialize factors.
41  *
42  * @param factor1
43  * {@link #factor1}
44  *
45  * @param factor2
46  * {@link #factor2}
47  */
48  public Identity(final Expression factor1, final Expression factor2) {
49 
50  this.factor1 = factor1;
51  this.factor2 = factor2;
52 
53  }
54 
55  /**
56  * see {@link Formula#evaluate}
57  */
58  @Override
59  public boolean evaluate(final Map<Expression, Boolean> values) {
60 
61  final Boolean value = values.get(this);
62 
63  if (value == null) {
64 
65  throw new IllegalArgumentException(String.format("no value specified for %s", toString()));
66 
67  }
68 
69  return value.booleanValue();
70 
71  }
72 
73  /**
74  * @see Formula#getNodes
75  */
76  @Override
77  public Set<Expression> getNodes() {
78 
79  final Set<Expression> nodes = new HashSet<>();
80  nodes.add(this);
81  nodes.addAll(factor1.getNodes());
82  nodes.addAll(factor2.getNodes());
83  return nodes;
84 
85  }
86 
87  /**
88  * @see Formula#getTerminalNodes
89  */
90  @Override
91  public Set<Expression> getTerminalNodes() {
92 
93  final Set<Expression> nodes = new HashSet<>();
94  nodes.add(this);
95  return nodes;
96 
97  }
98 
99  /**
100  * @see Graphable#printDigraph
101  */
102  @Override
103  public void printDigraph(final PrintStream stream, final int indent) {
104 
105  factor1.printDigraph(stream, indent);
106  factor2.printDigraph(stream, indent);
107  printIndentation(stream, indent);
108  stream.print(nodeName);
109  stream.println(" [ label=\"=\" ]");
110  stream.print(nodeName);
111  stream.print(" -> { ");
112  stream.print(factor1.nodeName);
113  stream.print(' ');
114  stream.print(factor2.nodeName);
115  stream.println(" }");
116 
117  }
118 
119  /**
120  * @see Expression#replaceVariable
121  */
122  @Override
123  public Expression replaceVariable(final Variable boundVariable, final Expression factor) {
124 
125  return new Identity(replaceFactor(boundVariable, factor1, factor),
126  replaceFactor(boundVariable, factor2, factor));
127 
128  }
129 
130  /**
131  * @see Expression#toLaTeX
132  */
133  @Override
134  public void toLaTeX(final PrintStream stream) {
135 
136  factor1.toLaTeX(stream);
137  stream.print(" = ");
138  factor2.toLaTeX(stream);
139 
140  }
141 
142  /**
143  * @see Object#toString
144  */
145  @Override
146  public String toString() {
147 
148  return factor1.toString() + "=" + factor2.toString();
149 
150  }
151 
152  /**
153  * Return `newFactor` if `oldFactor` equals `variable.
154  *
155  * Otherwise, return `oldFactor.`
156  *
157  * @param variable
158  * The bound variable being replaced.
159  *
160  * @param oldFactor
161  * The current {@link Expression} being considered for
162  * replacement.
163  *
164  * @param newFactor
165  * The new {@link Expression} in the replacement.
166  *
167  * @return `oldFactor` or `newFactor`.
168  */
169  private Expression replaceFactor(final Variable variable, final Expression oldFactor, final Expression newFactor) {
170 
171  if (oldFactor instanceof Variable && oldFactor.equals(variable)) {
172 
173  return newFactor;
174 
175  }
176 
177  return oldFactor;
178 
179  }
180 
181 }
Expression replaceFactor(final Variable variable, final Expression oldFactor, final Expression newFactor)
Return newFactor if oldFactor equals `variable.
Definition: Identity.java:169
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
Set< Expression > getTerminalNodes()
Definition: Identity.java:91
Identity(final Expression factor1, final Expression factor2)
Initialize factors.
Definition: Identity.java:48
abstract Set< Expression > getNodes()
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.
final Expression factor1
First Expression.
Definition: Identity.java:32
void toLaTeX(final PrintStream stream)
Definition: Identity.java:134
Set< Expression > getNodes()
Definition: Identity.java:77
final Expression factor2
Second Expression.
Definition: Identity.java:37
Objects with unique Graphviz node names.
Definition: Expression.java:32
Expression replaceVariable(final Variable boundVariable, final Expression factor)
Definition: Identity.java:123
A formula of the monadic predicate calculus.
Definition: Formula.java:51
void printDigraph(final PrintStream stream, final int indent)
Definition: Identity.java:103
static void printIndentation(final PrintStream stream, final int indent)
Print the specified number of spaces to the given stream.
boolean evaluate(final Map< Expression, Boolean > values)
see Formula#evaluate
Definition: Identity.java:59