Kirk Rader  1.0-SNAPSHOT
Operator.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.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 
25 /**
26  * A non-terminal {@link Formula} of the monadic predicate calculus.
27  */
28 abstract class Operator extends Formula {
29 
30  /**
31  * The connective's operands.
32  */
33  protected List<Formula> operands;
34 
35  /**
36  * @param operands
37  * {@link #operands}
38  */
39  protected Operator(final Formula... operands) {
40 
41  this.operands = new ArrayList<>();
42 
43  for (final Formula operand : operands) {
44 
45  this.operands.add(operand);
46 
47  }
48  }
49 
50  /**
51  * @see Expression#getNodes
52  */
53  @Override
54  public Set<Expression> getNodes() {
55 
56  final Set<Expression> nodes = new HashSet<>();
57  nodes.add(this);
58 
59  for (final Formula operand : operands) {
60 
61  nodes.addAll(operand.getNodes());
62 
63  }
64 
65  return nodes;
66 
67  }
68 
69  /**
70  * @see Expression#getTerminalNodes
71  */
72  @Override
73  public Set<Expression> getTerminalNodes() {
74 
75  final Set<Expression> terms = new HashSet<>();
76 
77  for (final Formula operand : operands) {
78 
79  terms.addAll(operand.getTerminalNodes());
80 
81  }
82 
83  return terms;
84 
85  }
86 
87  /**
88  * @see Graphable#printDigraph
89  */
90  @Override
91  public void printDigraph(final PrintStream stream, final int indent) {
92 
93  printIndentation(stream, indent);
94  stream.println(nodeName + " [ label=\"" + getOperator() + "\" ]");
95 
96  for (final Formula operand : operands) {
97 
98  operand.printDigraph(stream, indent);
99 
100  }
101 
102  printIndentation(stream, indent);
103  stream.print(nodeName + " -> {");
104 
105  for (final Formula operand : operands) {
106 
107  stream.print(" " + operand.nodeName);
108 
109  }
110 
111  stream.println(" }");
112 
113  }
114 
115  /**
116  * @return The operator symbol for this connective.
117  */
118  protected abstract String getOperator();
119 
120  /**
121  * @param stream
122  * The output stream.
123  */
124  protected abstract void printLaTeXOperator(PrintStream stream);
125 
126 }
abstract String getOperator()
String nodeName
Graphviz digraph node name.
Definition: Expression.java:90
abstract void printLaTeXOperator(PrintStream stream)
List< Formula > operands
The connective&#39;s operands.
Definition: Operator.java:33
Set< Expression > getNodes()
Definition: Operator.java:54
Set< Expression > getTerminalNodes()
Definition: Operator.java:73
Operator(final Formula... operands)
Definition: Operator.java:39
A formula of the monadic predicate calculus.
Definition: Formula.java:51
A non-terminal Formula of the monadic predicate calculus.
Definition: Operator.java:28
void printDigraph(final PrintStream stream, final int indent)
Definition: Operator.java:91
static void printIndentation(final PrintStream stream, final int indent)
Print the specified number of spaces to the given stream.