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
Propagation of one constant implemented. #42
Open
NAG47
wants to merge
37
commits into
wvisser:master
Choose a base branch
from
NAG47: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.
Open
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
f50701b
Travis Test
NAG47 3d88740
added a test
NAG47 2a66884
test
NAG47 30cb5c7
commented out all the tests that work
NAG47 a9f9670
trying to fix test
NAG47 c654fcc
trying to fix test2
NAG47 931ae39
backtracking
NAG47 e9767b1
docker test
NAG47 f3078a3
docker test 2
NAG47 8047342
docker test 3
NAG47 f71ae08
docker test 4
NAG47 d27d582
fixed build status link
NAG47 19d8c7c
trying to fix build status icon
NAG47 6ee96eb
trying to fix build status icon 2
NAG47 60ba954
trying to fix build status icon 3
NAG47 fc73599
trying to fix build status icon 4
NAG47 ccaf0e1
trying to fix build status icon 5
NAG47 e6d73ae
bonus
NAG47 439bb1c
build status
NAG47 872e193
broke a test case
NAG47 c546323
Merge branch 'master' of github.com:NAG47/green
NAG47 18dd0fd
trying to fix my buil from not using the latest commit
NAG47 474ce2b
final
NAG47 6a20894
final2
NAG47 10b8bd4
final3
NAG47 415f9c9
final4
NAG47 c3a6603
prop test 1
NAG47 5e2359f
prop test 2
NAG47 b86ec8f
prop test 3
NAG47 5f5db27
crying, literal tears
NAG47 f9ef8db
crying, literal tears 2
NAG47 d524e94
all smiles :)
NAG47 9947314
:)))
NAG47 9772bfb
stuff works yo
NAG47 8b92337
final tut2, onlly propagation of one c works
NAG47 dde6aa5
fixed spacing problem
NAG47 157999c
oops
NAG47 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 | ||
|
||
services: | ||
- docker | ||
|
||
before_install: | ||
- docker build -t nag47/dock . | ||
|
||
language: java | ||
|
||
script: | ||
- ant; | ||
- ant test; | ||
- docker run nag47/dock /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 @@ | ||
[![Build Status](https://travis-ci.org/wvisser/green.svg?branch=master)](https://travis-ci.org/wvisser/green.svg?branch=master) | ||
[![Build Status](https://travis-ci.org/NAG47/green.png)](https://travis-ci.org/NAG47/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
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
128 changes: 128 additions & 0 deletions
128
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,128 @@ | ||
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 { | ||
|
||
/** | ||
* Number of times the slicer has been invoked. | ||
*/ | ||
private int invocations = 0; | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public void report(Reporter reporter) { | ||
reporter.report(getClass().getSimpleName(), "invocations = " + invocations); | ||
} | ||
|
||
public Expression propagate(Expression expression, | ||
Map<Variable, Variable> map) { | ||
|
||
try { | ||
log.log(Level.FINEST, "Before Canonization: " + expression); | ||
invocations++; | ||
PropagationVisitor propagationVisitor = new PropagationVisitor(); | ||
expression.accept(propagationVisitor); | ||
Expression propagated = propagationVisitor.getExpression(); | ||
return propagated; | ||
} catch (VisitorException x) { | ||
log.log(Level.SEVERE,"encountered an exception -- this should not be happening!",x); | ||
} | ||
return null; | ||
} | ||
|
||
private static class PropagationVisitor extends Visitor { | ||
|
||
private Stack<Expression> stack; | ||
|
||
private IntVariable var; | ||
|
||
private IntConstant const1; | ||
|
||
public PropagationVisitor() { | ||
stack = new Stack<Expression>(); | ||
} | ||
|
||
public Expression getExpression() { | ||
return stack.pop(); | ||
} | ||
|
||
@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 = stack.pop(); | ||
Expression l = stack.pop(); | ||
if (op == Operation.Operator.EQ) { | ||
if (r instanceof IntVariable && l instanceof IntConstant) { | ||
var = (IntVariable) r; | ||
const1 = (IntConstant) l; | ||
} else if (l instanceof IntVariable && r instanceof IntConstant) { | ||
var = (IntVariable) l; | ||
const1 = (IntConstant) r; | ||
} | ||
stack.push(new Operation(op, l, r)); | ||
} else { | ||
if (r instanceof IntVariable) { | ||
if (((IntVariable) r).getName().equals(var.getName())) { | ||
r = const1; | ||
} | ||
} | ||
if (l instanceof IntVariable) { | ||
if (((IntVariable) l).getName().equals(var.getName())) { | ||
l = const1; | ||
} | ||
} | ||
stack.push(new Operation(op, l, r)); | ||
} | ||
} | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
test/za/ac/sun/cs/green/service/simplify/OnlyConstantPropagationTest.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 OnlyConstantPropagationTest { | ||
|
||
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.ConstantPropagation"); | ||
//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 fix your indentation, tabs or tabs. You can't have spaces.
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!