Skip to content
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 propogation #39

Open
wants to merge 66 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
bc3f6b3
Update README.md
wiesnerbernard Jul 24, 2018
f90f0fb
Removed commented test20
wiesnerbernard Jul 24, 2018
a3af55d
ChangedExpectedOutput of testcase 20
wiesnerbernard Jul 24, 2018
5a151fd
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
ee8ab49
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
6b00ae4
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
c6b68ba
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
a2e98c4
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
3da952d
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
00677a8
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
535a23a
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
bf5f1ed
Update SATCanonizerTest.java
wiesnerbernard Jul 24, 2018
dd3d3d9
Update build.xml
wiesnerbernard Jul 24, 2018
0e71503
Update .travis.yml
wiesnerbernard Jul 24, 2018
5900cf9
Update .travis.yml
wiesnerbernard Jul 24, 2018
0ea9bc0
Update .travis.yml
wiesnerbernard Jul 24, 2018
5a3a8cf
Update SATCanonizerTest.java
wiesnerbernard Jul 30, 2018
f07162c
Update README.md
wiesnerbernard Jul 30, 2018
de3beb6
Update README.md
wiesnerbernard Jul 31, 2018
8e4355c
Update Dockerfile
wiesnerbernard Jul 31, 2018
1fa086d
Create ConstantPropagation.java
wiesnerbernard Aug 12, 2018
fa2c297
Create ConstantPropagationTest.java
wiesnerbernard Aug 12, 2018
5c3a912
Update ConstantPropagation.java
wiesnerbernard Aug 12, 2018
fcc823e
Update Dockerfile
wiesnerbernard Aug 13, 2018
e766afc
Update Dockerfile
wiesnerbernard Aug 13, 2018
ea62403
Update Dockerfile
wiesnerbernard Aug 13, 2018
2ec4cb9
Update Dockerfile
wiesnerbernard Aug 13, 2018
79181b9
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
d337746
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
fbea38a
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
cfa3e08
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
df7167c
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
39799a6
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
93140fc
Update ConstantPropagationTest.java
wiesnerbernard Aug 13, 2018
8fc0d1c
Update ConstantPropagationTest.java
wiesnerbernard Aug 13, 2018
a227301
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
58768a5
Removed Redundent code and added print statement to check output
wiesnerbernard Aug 13, 2018
1a7a400
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
08cbf64
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
9788f7e
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
ec4db4d
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
74bb9af
Update EntireSuite.java
wiesnerbernard Aug 13, 2018
efd32d3
Update EntireSuite.java
wiesnerbernard Aug 13, 2018
b56d333
Update ConstantPropagationTest.java
wiesnerbernard Aug 13, 2018
934d7ce
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
bf8ddf5
First attempt :)
wiesnerbernard Aug 13, 2018
cbc448b
fixed few errors
wiesnerbernard Aug 13, 2018
6ff87e1
Testing for constant assignment
wiesnerbernard Aug 13, 2018
8106ff8
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
d4c6d9c
Update changes to include all expressions
wiesnerbernard Aug 13, 2018
27d7396
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
0c315b8
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
2482a4b
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
9b53157
Fixed some more erros
wiesnerbernard Aug 13, 2018
9629484
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
14f7b0d
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
f805c9b
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
a05c0c7
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
ea56d3e
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
942a3b1
Fixed errors
wiesnerbernard Aug 13, 2018
9c56978
Added comments
wiesnerbernard Aug 13, 2018
8244804
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
db3e57b
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
7cd261b
Update ConstantPropagation.java
wiesnerbernard Aug 13, 2018
80dc5f4
Create SimplificationConstantPropogationTest.java
wiesnerbernard Aug 14, 2018
527a9cf
Updated commenting
wiesnerbernard Aug 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
language: java

services:
- docker

before_install:
- docker build -t green .

script:
- ant;
- ant test;
- docker run green /bin/sh -c "ant; ant test;"
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ RUN apt install patchelf -y
RUN apt install libgomp1

# Clone down the GreenSolver repository
RUN git clone https://github.com/wvisser/green
RUN git clone https://github.com/wiesnerbernard/green

#
WORKDIR /green
RUN git fetch
RUN git checkout constantPropogation
WORKDIR /

# Download and extract Z3
RUN mkdir z3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![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/wiesnerbernard/green.svg?branch=master)](https://travis-ci.org/wiesnerbernard/green?branch=master)

Notes:

Expand Down
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<env key="LD_LIBRARY_PATH" value="lib"/>
<sysproperty key="java.library.path" value="${z3lib}:${cvc3lib}"/>
<classpath refid="green.classpath"/>
<formatter type="plain" usefile="false"/>
</junit>
<junitreport todir="${junit.dir}">
<fileset dir="${junit.dir}">
Expand Down
144 changes: 144 additions & 0 deletions src/za/ac/sun/cs/green/service/simplify/ConstantPropagation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
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 {
private int invocations = 0;

public ConstantPropagation(Green solver) {
super(solver);
}

/**
* Taken from SATCanonizerService.java
*/
@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;
}

/**
* Taken from SATCanonizerService.java
*/
@Override
public void report(Reporter reporter) {
reporter.report(getClass().getSimpleName(), "invocations = " + invocations);
}

/**
* Based off of the same method in SATCanonizerService.java
* @param expression: expression to propagate
* map: hashmap of variables
* @return updated expression
*/
public Expression propagate(Expression expression,
Map<Variable, Variable> map) {
try {
log.log(Level.FINEST, "Before Propagation: " + expression);
invocations++;
OrderingVisitor orderingVisitor = new OrderingVisitor();
expression.accept(orderingVisitor);
expression = orderingVisitor.getExpression();
log.log(Level.FINEST, "After Propagation: " + expression);
return expression;
} catch (VisitorException x) {
log.log(Level.SEVERE,
"encountered an exception -- this should not be happening!",
x);
}
return null;
}

/**
* Class to handle propagation of constants
*/
private static class OrderingVisitor extends Visitor {

private Map<IntVariable, IntConstant> hashmap;
private Stack<Expression> stack;

public OrderingVisitor() {
stack = new Stack<Expression>();
hashmap = new HashMap<IntVariable, IntConstant>(); //Added a hash map to store constants propagated to variables
}

public Expression getExpression() {
return stack.pop();
}

@Override
public void postVisit(IntConstant constant) {
stack.push(constant);
}

@Override
public void postVisit(IntVariable variable) {
stack.push(variable);
}

/**
* This method propagates a constant through the expression, replacing all instances of a certain variable
* with a constant value.
* @param operation: the current operator that must be handled
*/
@Override
public void postVisit(Operation operation) throws VisitorException {
Operation.Operator op = operation.getOperator();
if (stack.size() >= 2) {
Expression right = stack.pop();
Expression left = stack.pop();
if (op == Operation.Operator.EQ) { //checks for '=='
//if there is a variable to the right of the operator and an integer integer to the left
//then assign the integer value to a key corresponding to the variable
if (right instanceof IntConstant && left instanceof IntVariable) {
hashmap.put((IntVariable) left, (IntConstant) right);
}
//form the new operation and push it to the stack
Operation nop = new Operation(operation.getOperator(), left, right);
stack.push(nop);
} else {
// If the the current expression is not of the type above create a new expression and push
// it to the stack
if (hashmap.containsKey(left)) {
left = hashmap.get(left);
} else if (hashmap.containsKey(right)) {
right = hashmap.get(right);
}
Operation nop = new Operation(operation.getOperator(), left, right);
stack.push(nop);
}
}
}
}
}
6 changes: 4 additions & 2 deletions test/za/ac/sun/cs/green/EntireSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
import za.ac.sun.cs.green.util.ParallelSATTest;
import za.ac.sun.cs.green.util.SetServiceTest;
import za.ac.sun.cs.green.util.SetTaskManagerTest;
import za.ac.sun.cs.green.service.simplify.ConstantPropagationTest;

@RunWith(Suite.class)
@Suite.SuiteClasses({
SATCanonizerTest.class,
SATZ3Test.class
//SATCanonizerTest.class,
//SATZ3Test.class
ConstantPropagationTest.class
})

public class EntireSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void test19() {
Operation o1 = new Operation(Operation.Operator.LE, c1, c1);
check(o1, "2<=2", "0==0");
}
/*

@Test
public void test20() {
IntConstant c1 = new IntConstant(2);
Expand All @@ -303,5 +303,4 @@ public void test20() {
Operation o3 = new Operation(Operation.Operator.AND, o1, o2);
check(o3, "(2<=2)&&(aa<2)", "1*v+-1<0");
}
*/
}
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 ConstantPropagationTest {

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)");
}

}
Loading