forked from GreenSolver/green
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constant propagation #46
Open
apvanniekerk
wants to merge
78
commits into
wvisser:master
Choose a base branch
from
apvanniekerk:ConstantPropagation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
78 commits
Select commit
Hold shift + click to select a range
9e133e2
Changed README.md
1135bec
Uncommented last testcase in SATCanonizerTest.java
8e12dbb
Uncommented last testcase in SATCanonizerTest.java
61c2acd
1st try
f72b81b
Added more verbose messages
65a71f7
Revert code to ensure solution validity
4318cd8
The problem was indeed the conical form in Test 20
6e43337
Tweaked .travis.yml file in accordance with example in Travis documen…
f23aad4
Tweaked .travis.yml file in accordance with example in Travis documen…
fe3380f
.tarvis.yml
428bcf6
Updated repo path in Dockerfile
c827ffc
Edited script syntax in .yml file
8400e3b
Changed tabbing in .yml file
8d5d61a
Added -v verbose option to ant
64c5b4c
Removed -v verbose option to ant
4d5c9fd
Added -S silent option to ant
d7c70f2
cd green/
2c7da1c
Running in script
a20c0e1
Still tweaking .yml
37f7471
Still tweaking .yml
6d2cd4b
Simplifying formatter output
8fb4cdc
No longer trying to cd to green/
d4fc64d
Attempting to further clean output
213c97a
Reverted
eaad222
Added the Deliverable 1 text file to repo.
969d1b6
Update README.md
apvanniekerk 7349a24
Added the link to repo in Deliverable 1.
fb2fc0d
Merge branch 'master' of https://github.com/apvanniekerk/green
8198238
Ensured click-through on build status
1399ded
Broke test to test output
a9d8a58
Branched to ConstantPropagation and fixed deliberatly broken test
c33bb7b
Changed Dockerfile to clone new branch
a917f73
Kept tweaking Dockerfile
8a12b8f
Kept tweaking Dockerfile
9119698
Started
667b838
Adding new tests
b203634
Adding new test
bdb5517
Checking that OnlyConstantPropagationTest is being run
c7bcd17
Dumped in canonizer code
8a90ffe
Only running propagation tests
8ce8493
Dummy test
9ad5e0b
Porting
b85b682
Rather running all tests
4e42157
For all tests
28ea697
Chasin' errors
7c0e51e
Chasin' errors
6479c84
Chasin' errors
db0c3e3
Chasin' errors
c032773
Chasin' errors
611e537
Chasin' errors
be5edef
Chasin' errors
cb5b56c
Going to have to have multiple Visitors.
107b206
Populate variable 'name'-'value' hashmap and attemp to order simmilar…
860f78f
Chaisin' errors
a1d42ac
Chaisin' errors
da1892e
Chaisin' errors
e602316
Chaisin' errors
968d038
Chaisin' errors
13e80f8
Chaisin' errors
3f4cd42
Chaisin' errors
611a206
Chaisin' errors
79f4a1d
Chaisin' errors
8e65be5
Chaisin' errors
f460baa
Chaisin' errors
8bf860e
Chaisin' errors
582e6d7
Starting propagation.
8de3831
Starting propagation pt 2
71f26db
Chaisin' Errors
246d67d
Chaisin' Errors
03813a0
Chaisin' Errors
e41a38f
Chaisin' Errors
d2cae94
Chaisin' Errors
864d9da
Chaisin' Errors
5800a25
Chaisin' Errors
84212c8
Chaisin' Errors
0b8b362
Chaisin' Errors
1fefc99
Chaisin' Errors
3a2cde3
Chaisin' Errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
sudo: required | ||
|
||
language: java | ||
|
||
services: | ||
- docker | ||
|
||
before_install: | ||
- docker build -t riaan . | ||
|
||
script: | ||
- ant; | ||
- ant test; | ||
- docker run riaan /bin/sh -c "ant; ant test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
AP van Niekerk | ||
16579089 | ||
|
||
REPO: https://github.com/apvanniekerk/green | ||
|
||
For Task 1, in order to have verbose debug output for the test.dist.dir variable, I added this line to the target in the build.xml file: | ||
|
||
<echo message="${test.dist.dir}"/> | ||
|
||
This then showed: | ||
|
||
Before Canonization: (aa<2)&&(2<=2) | ||
[junit] [20180724 09:14:35][za.ac.sun.cs.green.service.canonizer.SATCanonizerService canonize][FINEST] After Canonization: ((1*v0)+-1)<=0 | ||
[junit] | ||
BUILD FAILED | ||
/home/travis/build/apvanniekerk/green/build.xml:99: Test za.ac.sun.cs.green.EntireSuite failed | ||
|
||
So after changing the conical form from "1*v+-1<0" to the canonized form given by JUNIT, the build succeeded. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
[](https://travis-ci.org/wvisser/green.svg?branch=master) | ||
[](https://travis-ci.org/apvanniekerk/green) | ||
|
||
Notes: | ||
AP van Niekerk - 16579089 - 2018 | ||
|
||
The first step is to update "build.properties" with your local | ||
settings. You do not need to set z3 and latte, but in that case | ||
some unit tests won't run. | ||
|
||
Green project for tuts in RW344. Forked from wvisser/green | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
293 changes: 293 additions & 0 deletions
293
src/za/ac/sun/cs/green/service/simplify/ConstantPropagation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,293 @@ | ||
package za.ac.sun.cs.green.service.simplify; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.SortedMap; | ||
import java.util.SortedSet; | ||
import java.util.Stack; | ||
import java.util.TreeMap; | ||
import java.util.TreeSet; | ||
import java.util.logging.Level; | ||
|
||
import za.ac.sun.cs.green.Instance; | ||
import za.ac.sun.cs.green.Green; | ||
import za.ac.sun.cs.green.expr.Expression; | ||
import za.ac.sun.cs.green.service.BasicService; | ||
import za.ac.sun.cs.green.util.Reporter; | ||
import za.ac.sun.cs.green.expr.Constant; | ||
import za.ac.sun.cs.green.expr.IntConstant; | ||
import za.ac.sun.cs.green.expr.IntVariable; | ||
import za.ac.sun.cs.green.expr.Operation; | ||
import za.ac.sun.cs.green.expr.Variable; | ||
import za.ac.sun.cs.green.expr.Visitor; | ||
import za.ac.sun.cs.green.expr.VisitorException; | ||
|
||
public class ConstantPropagation extends BasicService { | ||
|
||
public ConstantPropagation(Green solver) { | ||
super(solver); | ||
} | ||
|
||
@Override | ||
public Set<Instance> processRequest(Instance instance) { | ||
@SuppressWarnings("unchecked") | ||
Set<Instance> result = (Set<Instance>) instance.getData(getClass()); | ||
if (result == null) { | ||
final Map<Variable, Variable> map = new HashMap<Variable, Variable>(); | ||
final Expression e = propagate(instance.getFullExpression(), map); | ||
final Instance i = new Instance(getSolver(), instance.getSource(), null, e); | ||
result = Collections.singleton(i); | ||
instance.setData(getClass(), result); | ||
} | ||
return result; | ||
} | ||
|
||
public Expression propagate(Expression expression, | ||
Map<Variable, Variable> map) { | ||
|
||
Map<Variable, Constant> varValues;// = new HashMap<Variable, Constant>(); | ||
|
||
try { | ||
log.log(Level.FINEST, "Before Collection:\n" + expression); | ||
|
||
CollectionVisitor collectionVisitor = new CollectionVisitor(); | ||
expression.accept(collectionVisitor); | ||
Expression propagated = collectionVisitor.getExpression(); | ||
varValues = collectionVisitor.getVarValues(); | ||
|
||
log.log(Level.FINEST, "After Collection:\n" + propagated); | ||
log.log(Level.FINEST, "Before Propagation:\n" + expression); | ||
|
||
PropagationVisitor propagationVisitor = new PropagationVisitor(varValues); | ||
expression.accept(propagationVisitor); | ||
propagated = propagationVisitor.getExpression(); | ||
|
||
log.log(Level.FINEST, "After Propagation:\n" + propagated); | ||
return propagated; | ||
} catch (VisitorException x) { | ||
log.log(Level.SEVERE, | ||
"encountered an exception -- the sky is falling!", | ||
x); | ||
} | ||
return null; | ||
} | ||
|
||
private static class PropagationVisitor extends Visitor { | ||
|
||
private Stack<Expression> stack; | ||
|
||
private Map<Variable, Constant> varValues; | ||
|
||
public PropagationVisitor(Map<Variable, Constant> varVals) { | ||
stack = new Stack<Expression>(); | ||
varValues = varVals; | ||
} | ||
|
||
public Expression getExpression() { | ||
return stack.pop(); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntConstant constant) { | ||
stack.push(constant); | ||
System.out.println( "Propagation Visitor pushed \" " + constant + " \" (constant) to stack."); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntVariable variable) { | ||
stack.push(variable); | ||
System.out.println("Propagation Visitor pushed \" " + variable + " \" (variable) to stack."); | ||
} | ||
|
||
@Override | ||
public void postVisit(Operation operation) throws VisitorException { | ||
Operation.Operator op = operation.getOperator(); | ||
Operation.Operator nop = null; | ||
switch(op) { | ||
case EQ: | ||
nop = Operation.Operator.EQ; | ||
break; | ||
case NE: | ||
nop = Operation.Operator.NE; | ||
break; | ||
case GT: | ||
nop = Operation.Operator.GT; | ||
break; | ||
case GE: | ||
nop = Operation.Operator.GE; | ||
break; | ||
case LT: | ||
nop = Operation.Operator.LT; | ||
break; | ||
case LE: | ||
nop = Operation.Operator.LE; | ||
break; | ||
default: | ||
System.out.println("Default reached switching on operator -- get outa Dodge!"); | ||
break; | ||
} | ||
if ((nop != null) && (nop.equals(Operation.Operator.EQ))) { | ||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
if ((r instanceof Variable) && (l instanceof Variable)) { | ||
//TODO: If one of the values are in the map, another can be added. Kinda a second pass thing. | ||
System.out.println("Instance of variable being able to be added to map. Maby next pass."); | ||
stack.push(new Operation(op, l, r)); | ||
//System.out.println("Propagation Visitor pushed \"" + operation +"\" (operation) to stack."); | ||
System.out.println("Propagation Visitor pushed \"" + l + " " + op + " " + r + "\" (operation) to stack."); | ||
} else { | ||
System.out.println("Operator was EQ, but allas, no variables could be added to map. Maby next pass."); | ||
stack.push(new Operation(op, l, r)); | ||
//System.out.println("Propagation Visitor pushed \"" + operation +"\" (operation) to stack."); | ||
System.out.println("Propagation Visitor pushed \"" + l + " " + op + " " + r + "\" (operation) to stack."); | ||
} | ||
} /*else if ((nop != null) && !(nop.equals(Operation.Operator.EQ))) { | ||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
if ((r instanceof IntVariable) && (l instanceof IntVariable) && (((IntVariable) r).getName().compareTo(((IntVariable) l).getName()) < 0)) { | ||
stack.push(new Operation(nop, r, l)); | ||
} else if ((r instanceof IntVariable) && (l instanceof IntConstant)) { | ||
stack.push(new Operation(nop, r, l)); | ||
} else { | ||
stack.push(operation); | ||
} | ||
} else if (op.getArity() == 2){ | ||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
stack.push(new Operation(op, l, r)); | ||
System.out.println("Collection Visitor pushed \"" + l + " " + op + " " + r +"\" (operation) to stack."); | ||
}*/ else if (op!= null) { | ||
//for (int i = op.getArity(); i > 0; i--) { | ||
// stack.pop(); | ||
//} | ||
//stack.push(operation); | ||
|
||
//TODO: Another second pass opportunity | ||
|
||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
|
||
if (varValues.containsKey(r)) { | ||
r = varValues.get(r); | ||
System.out.println("Propagation Visitor replaced \"" + r +"\" with \"" + varValues.get(r) + "\""); | ||
} | ||
|
||
if (varValues.containsKey(l)) { | ||
l = varValues.get(l); | ||
System.out.println("Propagation Visitor replaced \"" + l +"\" with \"" + varValues.get(l) + "\""); | ||
} | ||
|
||
stack.push(new Operation(op, l, r)); | ||
System.out.println("Propagation Visitor pushed \"" + l + " " + op + " " + r + "\" (operation) to stack."); | ||
} | ||
} | ||
|
||
} | ||
|
||
private static class CollectionVisitor extends Visitor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some Javadoc, maybe some inline comments. |
||
|
||
private Stack<Expression> stack; | ||
|
||
private Map<Variable, Constant> map;// = new HashMap<Variable, Constant>(); | ||
|
||
public CollectionVisitor() { | ||
stack = new Stack<Expression>(); | ||
map = new HashMap<Variable, Constant>(); | ||
} | ||
|
||
public Map getVarValues() { | ||
return map; | ||
} | ||
|
||
public Expression getExpression() { | ||
return stack.pop(); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntConstant constant) { | ||
stack.push(constant); | ||
System.out.println( "Collection Visitor pushed \" " + constant + " \" (constant) to stack."); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntVariable variable) { | ||
stack.push(variable); | ||
System.out.println("Collection Visitor pushed \" " + variable + " \" (variable) to stack."); | ||
} | ||
|
||
@Override | ||
public void postVisit(Operation operation) throws VisitorException { | ||
Operation.Operator op = operation.getOperator(); | ||
Operation.Operator nop = null; | ||
switch(op) { | ||
case EQ: | ||
nop = Operation.Operator.EQ; | ||
break; | ||
case NE: | ||
nop = Operation.Operator.NE; | ||
break; | ||
case GT: | ||
nop = Operation.Operator.GT; | ||
break; | ||
case GE: | ||
nop = Operation.Operator.GE; | ||
break; | ||
case LT: | ||
nop = Operation.Operator.LT; | ||
break; | ||
case LE: | ||
nop = Operation.Operator.LE; | ||
break; | ||
default: | ||
System.out.println("Default reached switching on operator -- get outa Dodge!"); | ||
break; | ||
} | ||
if ((nop != null) && (nop.equals(Operation.Operator.EQ))) { | ||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
if ((r instanceof Constant) && (l instanceof Variable)) { | ||
map.put((Variable) l, (Constant) r); | ||
System.out.println(l + " == "+ r + ": Variable \"" + l + "\" added to map."); | ||
stack.push(new Operation(op, l, r)); | ||
System.out.println("Collection Visitor pushed \"" + l + " " + op + " " + r +"\" (operation) to stack."); | ||
} else if ((r instanceof Variable) && (l instanceof Constant)) { | ||
map.put((Variable) r, (Constant) l); | ||
System.out.println(l + " == "+ r + ": Variable \"" + r + "\" added to map."); | ||
stack.push(new Operation(op, l, r)); | ||
System.out.println("Collection Visitor pushed \"" + l + " " + op + " " + r +"\" (operation) to stack."); | ||
} else { | ||
System.out.println("Operator was EQ, but allas, no variables could be added to map. Maby next pass."); | ||
stack.push(operation); | ||
System.out.println("Collection Visitor pushed \"" + operation +"\" (operation) to stack."); | ||
} | ||
} /**/else if ((nop != null) && !(nop.equals(Operation.Operator.EQ))) { | ||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
if ((r instanceof IntVariable) && (l instanceof IntVariable) && (((IntVariable) r).getName().compareTo(((IntVariable) l).getName()) < 0)) { | ||
stack.push(new Operation(nop, r, l)); | ||
} else if ((r instanceof IntVariable) && (l instanceof IntConstant)) { | ||
stack.push(new Operation(nop, r, l)); | ||
} else { | ||
stack.push(operation); | ||
} | ||
} else if (op.getArity() == 2){ | ||
Expression r = stack.pop(); | ||
Expression l = stack.pop(); | ||
stack.push(new Operation(op, l, r)); | ||
System.out.println("Collection Visitor pushed \"" + l + " " + op + " " + r +"\" (operation) to stack."); | ||
} else { | ||
for (int i = op.getArity(); i > 0; i--) { | ||
stack.pop(); | ||
} | ||
stack.push(operation); | ||
System.out.println("Collection Visitor pushed \"" + operation +"\" (operation) to stack."); | ||
} | ||
System.out.println("Following variables collected:"); | ||
for (Map.Entry<Variable, Constant> entry: map.entrySet()) { | ||
System.out.println("Variable: " + entry.getKey() + " Constant: " + entry.getValue()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try and remove any unused code rather than uncommenting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will address post haste!