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
Propogation of constants. #43
Open
dijkstradev
wants to merge
14
commits into
wvisser:master
Choose a base branch
from
dijkstradev:master
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.
+239
−25
Open
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c6b4cae
Uncommented test20 - break build
2988506
test20 fixed
e712e88
Initial docker attempt
cd9b963
Docker attempt
1d16a26
Docker attempt v1
6e8326a
Docker attempt v2
ffc16f0
Failure test
890d3a2
Docker attempt v3
e15b104
Docker based test - done
b678003
Dockerfile edit - repo name change
dd3fd0e
Prop test setup
51ed9fb
Propagation done
0c85294
Cleanup
cbba05f
Cleanup - tabs/spaces
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,11 @@ | ||
sudo: required | ||
services: | ||
- docker | ||
|
||
language: java | ||
|
||
before_install: | ||
- docker build -t dijkstradev/z3green . | ||
|
||
script: | ||
- ant; | ||
- ant test; | ||
- docker run dijkstradev/z3green /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
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,7 @@ | ||
[](https://travis-ci.org/wvisser/green.svg?branch=master) | ||
[](https://travis-ci.org/dijkstradev/green) | ||
|
||
Notes: | ||
|
||
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. | ||
|
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
106 changes: 106 additions & 0 deletions
106
src/za/ac/sun/cs/green/service/simplify/ConstantPropogation.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,106 @@ | ||
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 ConstantPropogation extends BasicService { | ||
|
||
public ConstantPropogation(Green solver){ | ||
super(solver); | ||
} | ||
|
||
@Override | ||
public Set<Instance> processRequest(Instance instance) { | ||
Expression expression = instance.getFullExpression(); | ||
log.log(Level.FINEST, "Before propogation: " + expression); | ||
final Expression e = propegate(expression); | ||
final Instance i = new Instance(getSolver(), instance.getSource(), null, e); | ||
log.log(Level.FINEST, "After propogation: " + i.getExpression()); | ||
return Collections.singleton(i); | ||
} | ||
|
||
private Expression propegate(Expression expression){ | ||
try { | ||
PropogationVisitor propogationVisitor = new PropogationVisitor(); | ||
expression.accept(propogationVisitor); | ||
return propogationVisitor.getExpression(); | ||
} catch (VisitorException ex){ | ||
log.log(Level.SEVERE, "This should not be happening - probably will.",ex); | ||
} | ||
return null; | ||
} | ||
|
||
private static class PropogationVisitor extends Visitor { | ||
|
||
private Map<Expression, Expression> constants; | ||
private Stack<Expression> stack; | ||
|
||
public Expression getExpression() { | ||
return stack.pop(); | ||
} | ||
|
||
public PropogationVisitor() { | ||
stack = new Stack<Expression>(); | ||
this.constants = new HashMap<Expression, Expression>(); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntConstant constant) { | ||
stack.push(constant); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntVariable variable) { | ||
stack.push(variable); | ||
} | ||
|
||
@Override | ||
public void postVisit(Operation operation) throws VisitorException { | ||
Operation.Operator op = operation.getOperator(); | ||
|
||
Expression r = propegateForward(stack.pop()); | ||
Expression l = propegateForward(stack.pop()); | ||
|
||
if (op.equals(Operation.Operator.EQ)) { | ||
extractEquality(r, l); | ||
extractEquality(l, r); | ||
} | ||
|
||
stack.push(new Operation(op, l, r)); | ||
} | ||
|
||
private void extractEquality(Expression r, Expression l) { | ||
if ((r instanceof IntVariable) && (l instanceof IntConstant)) { | ||
constants.putIfAbsent(r, l); | ||
} | ||
} | ||
|
||
private Expression propegateForward(Expression e) { | ||
IntConstant var; | ||
return (var = (IntConstant) constants.get(e)) != null | ||
&& e instanceof IntVariable ? var : e; | ||
} | ||
} | ||
|
||
} |
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
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
76 changes: 76 additions & 0 deletions
76
test/za/ac/sun/cs/green/service/simplify/ConstantPropogationTest.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,76 @@ | ||
package za.ac.sun.cs.green.service.simplify; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.Properties; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
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.expr.IntConstant; | ||
import za.ac.sun.cs.green.expr.IntVariable; | ||
import za.ac.sun.cs.green.expr.Operation; | ||
import za.ac.sun.cs.green.util.Configuration; | ||
|
||
public class ConstantPropogationTest { | ||
|
||
public static Green solver; | ||
|
||
@BeforeClass | ||
public static void initialize() { | ||
solver = new Green(); | ||
Properties props = new Properties(); | ||
props.setProperty("green.services", "sat"); | ||
props.setProperty("green.service.sat", "(simplify sink)"); | ||
//props.setProperty("green.service.sat", "(canonize sink)"); | ||
props.setProperty("green.service.sat.simplify", | ||
"za.ac.sun.cs.green.service.simplify.ConstantPropogation"); | ||
//props.setProperty("green.service.sat.canonize", | ||
// "za.ac.sun.cs.green.service.canonizer.SATCanonizerService"); | ||
|
||
props.setProperty("green.service.sat.sink", | ||
"za.ac.sun.cs.green.service.sink.SinkService"); | ||
Configuration config = new Configuration(solver, props); | ||
config.configure(); | ||
} | ||
|
||
private void finalCheck(String observed, String expected) { | ||
assertEquals(expected, observed); | ||
} | ||
|
||
private void check(Expression expression, String expected) { | ||
Instance i = new Instance(solver, null, null, expression); | ||
Expression e = i.getExpression(); | ||
assertTrue(e.equals(expression)); | ||
assertEquals(expression.toString(), e.toString()); | ||
|
||
Object result = i.request("sat"); | ||
|
||
assertNotNull(result); | ||
assertEquals(Instance.class, result.getClass()); | ||
Instance j = (Instance) result; | ||
finalCheck(j.getExpression().toString(), expected); | ||
} | ||
|
||
@Test | ||
public void test00() { | ||
IntVariable x = new IntVariable("x", 0, 99); | ||
IntVariable y = new IntVariable("y", 0, 99); | ||
IntVariable z = new IntVariable("z", 0, 99); | ||
IntConstant c = new IntConstant(1); | ||
IntConstant c10 = new IntConstant(10); | ||
IntConstant c3 = new IntConstant(3); | ||
Operation o1 = new Operation(Operation.Operator.EQ, x, c); // o1 : x = 1 | ||
Operation o2 = new Operation(Operation.Operator.ADD, x, y); // o2 : (x + y) | ||
Operation o3 = new Operation(Operation.Operator.EQ, o2, c10); // o3 : x+y = 10 | ||
Operation o4 = new Operation(Operation.Operator.AND, o1, o3); // o4 : x = 1 && (x+y) = 10 | ||
check(o4, "(x==1)&&((1+y)==10)"); | ||
} | ||
|
||
} |
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 stick to one style convention, tabs or spaces. Try and format the rest of the code. It's good to keep to the convention used in the original code, in this case, it's tabs.
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.
Done!