Skip to content

Commit 027c5ee

Browse files
authored
Merge pull request #20 from agupta231/R2
R2 finished
2 parents 447f8e7 + f479d3a commit 027c5ee

13 files changed

Lines changed: 1087 additions & 80 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# Fucking Akash and Bryson
22
/.idea
3+
.DS_Store
4+
/out
5+
*.iml
6+
*.swp

src/AbstractCompoundExpression.java

Lines changed: 164 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import javafx.scene.Node;
2+
import javafx.scene.layout.HBox;
13
import java.util.LinkedList;
24

3-
public abstract class AbstractCompoundExpression implements CompoundExpression{
5+
/**
6+
* Child function of CompoundExpression. Meant to abstract a lot of the code in
7+
* Multiplicative and Additive Expression classes
8+
*/
9+
public abstract class AbstractCompoundExpression implements CompoundExpression, CopyAble {
410
private CompoundExpression parent = null;
511
private LinkedList<Expression> children = new LinkedList<Expression>();
12+
protected boolean focused;
13+
protected Node node;
614

715
/**
816
* Flatten's the expression's children.
@@ -51,38 +59,188 @@ public void setChildren(LinkedList<Expression> children){
5159
* @param child An expression
5260
*/
5361
public void addSubexpression(Expression child){
62+
child.setParent(this);
63+
5464
this.children.add(child);
5565
}
5666

5767
/**
58-
* Will generate a String representation for the the addition
68+
* Will generate a String representation for the current
5969
* operator, with the appropiate number of tabs to properly
6070
* represent it's position in the expression.
6171
* @param indentLevel the indentation level
62-
* @param name the name of the expression
72+
* @param name what the current operator is
6373
* @return a String containing this expression and all its children in hierarchic format.
6474
*/
6575

66-
public String convertToString(int indentLevel, String name){
76+
public String convertToString(int indentLevel, String name) {
6777
StringBuffer sb = new StringBuffer();
6878
Expression.indent(sb,indentLevel);
6979
sb.append(name + "\n");
80+
7081
for(Expression e: this.getChildren()){
7182
sb.append(e.convertToString(indentLevel+1));
7283
}
84+
7385
return sb.toString();
7486
}
7587

88+
/**
89+
* Will generate a String representation for the operator, with no
90+
* space though.
91+
* @param delimiter the operator
92+
* @return a String containing this expresison.
93+
*/
94+
public String convertToStringFlat(String delimiter) {
95+
String outputString = "";
96+
97+
for(int i = 0; i < this.getChildren().size() - 1; i++) {
98+
outputString += ((CopyAble) this.getChildren().get(i)).convertToStringFlat();
99+
outputString += delimiter;
100+
}
101+
102+
outputString += ((CopyAble) this.getChildren().get(this.getChildren().size() - 1)).convertToStringFlat();
103+
104+
return outputString;
105+
}
106+
76107
/**
77108
* Creates a deep copy of the expression
78109
* @param copy, an empty Compound expression that will be returned.
79110
* @return a deep copy of the expression
80111
*/
81112
public AbstractCompoundExpression deepCopy(AbstractCompoundExpression copy){
82-
for(Expression c: this.getChildren()){
83-
copy.addSubexpression(c.deepCopy());
113+
for(Expression c: this.getChildren()) {
114+
copy.addSubexpression(c);
115+
}
116+
for(Expression c: copy.getChildren()) {
117+
c.setParent(copy);
84118
}
119+
120+
copy.node = node;
85121
return copy;
86122
}
87123

124+
/**
125+
* Abstracted function for getNode() for Multiplicative and Additive Expressions
126+
* @param delimiter the operator
127+
* @return a Node represeneting the current Expression
128+
*/
129+
public Node getNode(String delimiter) {
130+
if(node == null) {
131+
final HBox hbox = new HBox();
132+
hbox.getChildren().add(this.getChildren().get(0).getNode());
133+
for (int i = 1; i < this.getChildren().size(); i++) {
134+
hbox.getChildren().add(ExpressionEditor.newLabel(delimiter));
135+
hbox.getChildren().add(this.getChildren().get(i).getNode());
136+
}
137+
if (this.getFocused()) {
138+
hbox.setBorder(RED_BORDER);
139+
}
140+
node = hbox;
141+
return hbox;
142+
}
143+
144+
return node;
145+
}
146+
147+
/**
148+
* Getter for if the current expression is focused.
149+
* @return true if the expression is focused, false
150+
* otherwise
151+
*/
152+
public boolean getFocused() {
153+
return focused;
154+
}
155+
156+
/**
157+
* Setter for if the current expression is focused.
158+
* @param s A boolean representing if the current
159+
* expression is focused or not
160+
*/
161+
public void setFocused(boolean s) {
162+
focused = s;
163+
}
164+
165+
/**
166+
* Will create a copy of the current expression, and transfer
167+
* the Node pointers as well
168+
* @return a copy of Expression with old Node pointers
169+
*/
170+
@Override
171+
public Expression trueCopy() {
172+
if(this.getParent() == null) {
173+
return this.deepCopy();
174+
}
175+
176+
AbstractCompoundExpression parent = ((AbstractCompoundExpression) this.getParent());
177+
AbstractCompoundExpression copy = (AbstractCompoundExpression) parent.trueCopy();
178+
179+
for(Expression e:copy.getChildren()) {
180+
if(e.convertToString(0).equals(this.convertToString(0))) {
181+
return e;
182+
}
183+
}
184+
185+
return null;
186+
}
187+
188+
/**
189+
* Will generate all possible trees that an expression can take
190+
* @param parent Parent of the focused expression
191+
* @param selected the focused expression, represented by the .convertToString(0) method
192+
* @return a LinkedList of Expressions of all of the permutations of the Expression
193+
*/
194+
public static LinkedList<Expression> generateAllPossibleTrees(Expression parent, String selected) {
195+
Expression focused = null;
196+
197+
for(Expression child : ((AbstractCompoundExpression) parent).getChildren()) {
198+
if (child.convertToString(0).equals(selected)) {
199+
focused = child;
200+
break;
201+
}
202+
}
203+
204+
final LinkedList<Expression> children = ((AbstractCompoundExpression) parent).getChildren();
205+
final int childrenSize = children.size();
206+
207+
int nodeIndex = -1;
208+
209+
for(int i = 0; i < childrenSize; i++) {
210+
if (children.get(i) == focused) {
211+
nodeIndex = i;
212+
break;
213+
}
214+
}
215+
216+
if (nodeIndex == -1) {
217+
return new LinkedList<>();
218+
}
219+
220+
children.remove(nodeIndex);
221+
222+
LinkedList<Expression> possibleTrees = new LinkedList<>();
223+
224+
for (int i = 0; i < childrenSize; i++) {
225+
AbstractCompoundExpression tempParent = (AbstractCompoundExpression) ((AbstractCompoundExpression) parent).trueCopy();
226+
LinkedList<Expression> orderedChildren = new LinkedList<>();
227+
228+
for (int j = 0; j < children.size(); j++) {
229+
if (j == i) {
230+
orderedChildren.add(focused);
231+
}
232+
233+
orderedChildren.add(children.get(j));
234+
}
235+
236+
if(i == childrenSize - 1) {
237+
orderedChildren.add(focused);
238+
}
239+
240+
tempParent.setChildren(orderedChildren);
241+
possibleTrees.add(tempParent);
242+
}
243+
244+
return possibleTrees;
245+
}
88246
}

src/AdditiveExpression.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
import javafx.scene.Node;
2+
3+
14
/**
25
* Child of expression that represents an addition operator.
36
*/
4-
public class AdditiveExpression extends CollapsibleExpression{
7+
public class AdditiveExpression extends CollapsibleExpression {
8+
59
/**
610
* Will return a copy of the Additive expression to use
711
* for the GUI.
8-
* @return an copy of the {@link AdditiveExpression}
12+
* @return a copy of the {@link AdditiveExpression}
13+
*/
14+
public Expression deepCopy() {
15+
return super.deepCopy(new AdditiveExpression());
16+
}
17+
18+
/**
19+
* Gives the Node representation of the current Additive Expression
20+
* @return a Node representing the current AdditiveExpression
921
*/
10-
public Expression deepCopy() {
11-
AdditiveExpression copy = new AdditiveExpression();
12-
return super.deepCopy(copy);
13-
}
22+
public Node getNode() {
23+
return super.getNode("+");
24+
}
1425

1526
/**
1627
* Will generate a String representation for the the addition
@@ -19,8 +30,17 @@ public Expression deepCopy() {
1930
* @param indentLevel how many "levels" down the operator is
2031
* @return a String representing the addition operator.
2132
*/
22-
public String convertToString(int indentLevel){
23-
return super.convertToString(indentLevel,"+");
24-
}
33+
public String convertToString(int indentLevel) {
34+
return super.convertToString(indentLevel,"+");
35+
}
2536

26-
}
37+
/**
38+
* Will generate a String representation for the the addition,
39+
* with no whitespace.
40+
* @return a flattened String representing the addition operator.
41+
*/
42+
@Override
43+
public String convertToStringFlat() {
44+
return super.convertToStringFlat("+");
45+
}
46+
}

src/CollapsibleExpression.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import java.util.LinkedList;
2-
3-
public abstract class CollapsibleExpression extends AbstractCompoundExpression {
1+
import javafx.scene.Node;
2+
import javafx.scene.layout.HBox;
43

4+
import java.util.LinkedList;
55

6+
public abstract class CollapsibleExpression extends AbstractCompoundExpression implements Focusable {
67
/**
78
* Flattens the expression in order to make it easier for the GUI to parse.
89
*/
910
public void flatten() {
1011
flattenChildren();
11-
LinkedList<Expression> children = new LinkedList<Expression>();
12-
for(Expression e: this.getChildren())
13-
{
12+
LinkedList<Expression> children = new LinkedList<>();
13+
14+
for(Expression e: this.getChildren()) {
1415
if(e.getClass() == this.getClass()){
1516
CollapsibleExpression tempExp = (CollapsibleExpression)e;
16-
for(Expression exp: tempExp.getChildren())
17-
{
17+
18+
for(Expression exp: tempExp.getChildren()) {
1819
children.add(exp);
1920
}
2021
}
21-
else{
22+
else {
2223
children.add(e);
2324
}
2425
}
2526
this.setChildren(children);
2627
}
27-
2828
}

src/CopyAble.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public interface CopyAble {
2+
/**
3+
* Will create a copy of the current expression, and transfer
4+
* the Node pointers as well
5+
* @return a copy of Expression with old Node pointers
6+
*/
7+
Expression trueCopy();
8+
9+
/**
10+
* Will generate a String representation for the operator, with no
11+
* space though.
12+
* @return a String containing this expresison.
13+
*/
14+
String convertToStringFlat();
15+
}

src/Expression.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
1+
import javafx.scene.layout.*;
2+
import javafx.scene.paint.Color;
3+
import javafx.scene.Node;
4+
15
interface Expression {
6+
/**
7+
* Border for showing a focused expression
8+
*/
9+
Border RED_BORDER = new Border(
10+
new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)
11+
);
12+
13+
/**
14+
* Border for showing a non-focused expression
15+
*/
16+
Border NO_BORDER = null;
17+
18+
/**
19+
* Color used for a "ghosted" expression
20+
*/
21+
Color GHOST_COLOR = Color.LIGHTGREY;
22+
223
/**
324
* Returns the expression's parent.
425
* @return the expression's parent
526
*/
627
CompoundExpression getParent ();
7-
28+
829
/**
9-
* Sets the parent be the specified expression.
10-
* @param parent the CompoundExpression that should be the parent of the target object
11-
*/
30+
* Sets the parent be the specified expression.
31+
* @param parent the CompoundExpression that should be the parent of the target object
32+
*/
1233
void setParent (CompoundExpression parent);
1334

1435
/**
1536
* Creates and returns a deep copy of the expression.
1637
* The entire tree rooted at the target node is copied, i.e.,
1738
* the copied Expression is as deep as possible.
18-
* @return the deep copy
39+
* @return the deep copy
1940
*/
2041
Expression deepCopy ();
2142

43+
44+
/**
45+
* Returns the JavaFX node associated with this expression.
46+
* @return the JavaFX node associated with this expression.
47+
*/
48+
Node getNode ();
49+
2250
/**
2351
* Recursively flattens the expression as much as possible
2452
* throughout the entire tree. Specifically, in every multiplicative

0 commit comments

Comments
 (0)