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 progration #57
Open
XaniNiehaus
wants to merge
12
commits into
wvisser:master
Choose a base branch
from
XaniNiehaus:ConstantProgration
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
12 commits
Select commit
Hold shift + click to select a range
97b688a
Updating readme, pointing it to xaniniehaus's travis
XaniNiehaus ff1b9be
uncommented testcase
Zen115 56f3ecb
updating build.xml
XaniNiehaus b5f3d3a
updating build to output junit in plain format
XaniNiehaus 41f160d
The long test to get docker working starts here
XaniNiehaus d5d3f1a
attempt #2
XaniNiehaus 722f6b9
name is now lowercase
XaniNiehaus 98dc955
this should work
XaniNiehaus caf998c
updating readme to link to travis cl
XaniNiehaus 294ea38
this might get weird
XaniNiehaus eaf94cb
updating docker file to point to my repo, all is well
XaniNiehaus f7fba6b
single propagation works
XaniNiehaus 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 @@ | ||
language: java | ||
|
||
services: | ||
- docker | ||
|
||
before_install: | ||
- docker build -t green . | ||
|
||
|
||
script: | ||
- ant; | ||
- ant test; | ||
- docker run green /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
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
158 changes: 158 additions & 0 deletions
158
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,158 @@ | ||
package za.ac.sun.cs.green.service.simplify; | ||
|
||
import za.ac.sun.cs.green.Green; | ||
import za.ac.sun.cs.green.Instance; | ||
import za.ac.sun.cs.green.expr.*; | ||
import za.ac.sun.cs.green.service.BasicService; | ||
|
||
import java.util.*; | ||
|
||
public class ConstantPropogation extends BasicService { | ||
public ConstantPropogation(Green solver) { | ||
super(solver); | ||
} | ||
|
||
@Override | ||
public Set<Instance> processRequest(Instance instance) { | ||
@SuppressWarnings("unchecked") | ||
Set<Instance> result = (Set<Instance>) instance.getData(getClass()); | ||
if (result == null) { | ||
try { | ||
final Map<Variable, Variable> map = new HashMap<Variable, Variable>(); | ||
final Expression e = instance.getFullExpression(); | ||
ConstantCheckerVisitor checkerVisitor = new ConstantCheckerVisitor(); | ||
e.accept(checkerVisitor); | ||
ConstantPropagatorVisitor propagatorVisitor = new ConstantPropagatorVisitor(checkerVisitor.getMap()); | ||
e.accept(propagatorVisitor); | ||
Expression propagated = propagatorVisitor.getExpression(); | ||
final Instance i = new Instance(getSolver(), instance.getSource(), null, propagated); | ||
result = Collections.singleton(i); | ||
instance.setData(getClass(), result); | ||
} catch (VisitorException e1) { | ||
e1.printStackTrace(); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static class ConstantCheckerVisitor extends Visitor { | ||
|
||
private int location; | ||
private Stack<Expression> stack; | ||
private HashMap<String, Pair> map; | ||
|
||
public ConstantCheckerVisitor() { | ||
location = 0; | ||
stack = new Stack<Expression>(); | ||
map = new HashMap<String, Pair>(); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntConstant constant) { | ||
location++; | ||
stack.push(constant); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntVariable variable) { | ||
location++; | ||
stack.push(variable); | ||
} | ||
|
||
@Override | ||
public void postVisit(Operation operation) throws VisitorException { | ||
location++; | ||
if (operation.getOperator() != Operation.Operator.EQ) { | ||
stack.push(operation); | ||
return; | ||
} | ||
Expression a = stack.pop(); | ||
Expression b = stack.pop(); | ||
if (a instanceof Constant && b instanceof Variable) { | ||
map.put(b.toString(), new Pair(a, location)); | ||
} else if (a instanceof Variable && b instanceof Constant) { | ||
map.put(a.toString(), new Pair(b, location)); | ||
} | ||
stack.push(operation); | ||
} | ||
|
||
public HashMap<String, Pair> getMap() { | ||
return map; | ||
} | ||
} | ||
|
||
private static class ConstantPropagatorVisitor extends Visitor { | ||
|
||
private int location; | ||
private Stack<Expression> stack; | ||
private Map<String, Pair> map; | ||
|
||
public ConstantPropagatorVisitor(HashMap<String, Pair> map) { | ||
location = 0; | ||
stack = new Stack<Expression>(); | ||
this.map = map; | ||
} | ||
|
||
@Override | ||
public void postVisit(IntConstant constant) { | ||
location++; | ||
stack.push(constant); | ||
} | ||
|
||
@Override | ||
public void postVisit(IntVariable variable) { | ||
location++; | ||
stack.push(variable); | ||
} | ||
|
||
@Override | ||
public void postVisit(Operation operation) throws VisitorException { | ||
location++; | ||
Expression a = stack.pop(); | ||
Expression b = stack.pop(); | ||
if (map.containsKey(a.toString())) { | ||
if (location != map.get(a.toString()).getGetLocation()) { | ||
Expression temp = new Operation(operation.getOperator(), a, map.get(b.toString()).getExpr()); | ||
stack.push(temp); | ||
return; | ||
} | ||
stack.push(new Operation(operation.getOperator(), b, a)); | ||
} else if (map.containsKey(b.toString())) { | ||
if (location != map.get(b.toString()).getGetLocation()) { | ||
Expression temp = new Operation(operation.getOperator(), map.get(b.toString()).getExpr(), a); | ||
stack.push(temp); | ||
return; | ||
} | ||
stack.push(new Operation(operation.getOperator(), b, a)); | ||
} else { | ||
Expression temp = new Operation(operation.getOperator(), b, a); | ||
stack.push(temp); | ||
} | ||
|
||
} | ||
|
||
public Expression getExpression() { | ||
return stack.pop(); | ||
} | ||
|
||
} | ||
|
||
private static class Pair { | ||
Expression expr; | ||
int location; | ||
|
||
Pair(Expression expr, int location) { | ||
this.expr = expr; | ||
this.location = location; | ||
} | ||
|
||
public int getGetLocation() { | ||
return location; | ||
} | ||
|
||
public Expression getExpr() { | ||
return expr; | ||
} | ||
} | ||
|
||
} |
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
74 changes: 74 additions & 0 deletions
74
test/za/ac/sun/cs/green/service/simplify/OnlyConstantPropogationTest.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,74 @@ | ||
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 OnlyConstantPropogationTest { | ||
|
||
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.
Some inline comments and Javadoc would go a long way.