Kirk Rader  1.0-SNAPSHOT
SymbolicNode.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  * A sentence or predicate.
26  */
27 abstract class SymbolicNode extends Formula {
28 
29  /**
30  * The symbol for this primitive term.
31  */
32  protected String symbol;
33 
34  /**
35  * Initialize {@link #symbol}
36  *
37  * @param symbol
38  * {@link #symbol}
39  */
40  protected SymbolicNode(final String symbol) {
41 
42  this.symbol = symbol;
43 
44  }
45 
46  /**
47  * @see Formula#evaluate
48  */
49  @Override
50  public final boolean evaluate(final Map<Expression, Boolean> values) {
51 
52  final Boolean value = values.get(this);
53 
54  if (value == null) {
55 
56  throw new IllegalArgumentException(String.format("no value specified for %s", toString()));
57 
58  }
59 
60  return value.booleanValue();
61 
62  }
63 
64  /**
65  * @see Formula#getNodes
66  */
67  @Override
68  public final Set<Expression> getNodes() {
69 
70  return getTerminalNodes();
71 
72  }
73 
74  /**
75  * @see Formula#getTerminalNodes
76  */
77  @Override
78  public final Set<Expression> getTerminalNodes() {
79 
80  final Set<Expression> terms = new HashSet<>();
81  terms.add(this);
82  return terms;
83 
84  }
85 
86  /**
87  * @see Graphable#printDigraph
88  */
89  @Override
90  public void printDigraph(final PrintStream stream, final int indent) {
91 
92  printIndentation(stream, indent);
93  stream.println(nodeName + " [ label=\"" + symbol + "\" ]");
94 
95  }
96 
97  /**
98  * @see Formula#toLaTeX
99  */
100  @Override
101  public final void toLaTeX(final PrintStream stream) {
102 
103  stream.print(toString());
104 
105  }
106 
107  /**
108  * @return {@link #symbol}
109  */
110  @Override
111  public String toString() {
112 
113  return symbol;
114 
115  }
116 
117 }
String nodeName
Graphviz digraph node name.
Definition: Expression.java:90
SymbolicNode(final String symbol)
Initialize symbol.
final Set< Expression > getNodes()
A sentence or predicate.
final Set< Expression > getTerminalNodes()
String symbol
The symbol for this primitive term.
final void toLaTeX(final PrintStream stream)
void printDigraph(final PrintStream stream, final int indent)
A formula of the monadic predicate calculus.
Definition: Formula.java:51
final boolean evaluate(final Map< Expression, Boolean > values)
static void printIndentation(final PrintStream stream, final int indent)
Print the specified number of spaces to the given stream.