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 propagation #46

Open
wants to merge 78 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
9e133e2
Changed README.md
Jul 24, 2018
1135bec
Uncommented last testcase in SATCanonizerTest.java
Jul 24, 2018
8e12dbb
Uncommented last testcase in SATCanonizerTest.java
Jul 24, 2018
61c2acd
1st try
Jul 24, 2018
f72b81b
Added more verbose messages
Jul 24, 2018
65a71f7
Revert code to ensure solution validity
Jul 24, 2018
4318cd8
The problem was indeed the conical form in Test 20
Jul 24, 2018
6e43337
Tweaked .travis.yml file in accordance with example in Travis documen…
Jul 24, 2018
f23aad4
Tweaked .travis.yml file in accordance with example in Travis documen…
Jul 24, 2018
fe3380f
.tarvis.yml
Jul 24, 2018
428bcf6
Updated repo path in Dockerfile
Jul 24, 2018
c827ffc
Edited script syntax in .yml file
Jul 24, 2018
8400e3b
Changed tabbing in .yml file
Jul 24, 2018
8d5d61a
Added -v verbose option to ant
Jul 24, 2018
64c5b4c
Removed -v verbose option to ant
Jul 24, 2018
4d5c9fd
Added -S silent option to ant
Jul 24, 2018
d7c70f2
cd green/
Jul 24, 2018
2c7da1c
Running in script
Jul 24, 2018
a20c0e1
Still tweaking .yml
Jul 24, 2018
37f7471
Still tweaking .yml
Jul 24, 2018
6d2cd4b
Simplifying formatter output
Jul 24, 2018
8fb4cdc
No longer trying to cd to green/
Jul 24, 2018
d4fc64d
Attempting to further clean output
Jul 24, 2018
213c97a
Reverted
Jul 24, 2018
eaad222
Added the Deliverable 1 text file to repo.
Jul 31, 2018
969d1b6
Update README.md
apvanniekerk Jul 31, 2018
7349a24
Added the link to repo in Deliverable 1.
Jul 31, 2018
fb2fc0d
Merge branch 'master' of https://github.com/apvanniekerk/green
Jul 31, 2018
8198238
Ensured click-through on build status
Jul 31, 2018
1399ded
Broke test to test output
Jul 31, 2018
a9d8a58
Branched to ConstantPropagation and fixed deliberatly broken test
Jul 31, 2018
c33bb7b
Changed Dockerfile to clone new branch
Jul 31, 2018
a917f73
Kept tweaking Dockerfile
Jul 31, 2018
8a12b8f
Kept tweaking Dockerfile
Jul 31, 2018
9119698
Started
Aug 13, 2018
667b838
Adding new tests
Aug 13, 2018
b203634
Adding new test
Aug 13, 2018
bdb5517
Checking that OnlyConstantPropagationTest is being run
Aug 13, 2018
c7bcd17
Dumped in canonizer code
Aug 13, 2018
8a90ffe
Only running propagation tests
Aug 13, 2018
8ce8493
Dummy test
Aug 13, 2018
9ad5e0b
Porting
Aug 13, 2018
b85b682
Rather running all tests
Aug 13, 2018
4e42157
For all tests
Aug 13, 2018
28ea697
Chasin' errors
Aug 13, 2018
7c0e51e
Chasin' errors
Aug 13, 2018
6479c84
Chasin' errors
Aug 13, 2018
db0c3e3
Chasin' errors
Aug 13, 2018
c032773
Chasin' errors
Aug 13, 2018
611e537
Chasin' errors
Aug 13, 2018
be5edef
Chasin' errors
Aug 13, 2018
cb5b56c
Going to have to have multiple Visitors.
Aug 13, 2018
107b206
Populate variable 'name'-'value' hashmap and attemp to order simmilar…
Aug 14, 2018
860f78f
Chaisin' errors
Aug 14, 2018
a1d42ac
Chaisin' errors
Aug 14, 2018
da1892e
Chaisin' errors
Aug 14, 2018
e602316
Chaisin' errors
Aug 14, 2018
968d038
Chaisin' errors
Aug 14, 2018
13e80f8
Chaisin' errors
Aug 14, 2018
3f4cd42
Chaisin' errors
Aug 14, 2018
611a206
Chaisin' errors
Aug 14, 2018
79f4a1d
Chaisin' errors
Aug 14, 2018
8e65be5
Chaisin' errors
Aug 14, 2018
f460baa
Chaisin' errors
Aug 14, 2018
8bf860e
Chaisin' errors
Aug 14, 2018
582e6d7
Starting propagation.
Aug 14, 2018
8de3831
Starting propagation pt 2
Aug 14, 2018
71f26db
Chaisin' Errors
Aug 14, 2018
246d67d
Chaisin' Errors
Aug 14, 2018
03813a0
Chaisin' Errors
Aug 14, 2018
e41a38f
Chaisin' Errors
Aug 14, 2018
d2cae94
Chaisin' Errors
Aug 14, 2018
864d9da
Chaisin' Errors
Aug 14, 2018
5800a25
Chaisin' Errors
Aug 14, 2018
84212c8
Chaisin' Errors
Aug 14, 2018
0b8b362
Chaisin' Errors
Aug 14, 2018
1fefc99
Chaisin' Errors
Aug 14, 2018
3a2cde3
Chaisin' Errors
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
11 changes: 9 additions & 2 deletions .travis.yml
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"
18 changes: 18 additions & 0 deletions Deliverable1.txt
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.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ 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/apvanniekerk/green.git
WORKDIR /green/
RUN git fetch
RUN git checkout ConstantPropagation
WORKDIR /

# Download and extract Z3
RUN mkdir z3
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[![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/apvanniekerk/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

6 changes: 3 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@

<target name="test">
<echo message="java.library.path = ${z3lib};${cvc3lib}"/>
<mkdir dir="${junit.dir}"/>
<junit fork="yes" printsummary="withOutAndErr" haltonfailure = "yes">
<formatter type="xml"/>
<mkdir dir="${junit.dir}"/>
<junit fork="yes" printsummary="on" haltonfailure = "yes">
<formatter type="plain" usefile="false"/>
<test name="za.ac.sun.cs.green.EntireSuite" todir="${junit.dir}"/>
<env key="DYLD_LIBRARY_PATH" value="lib"/>
<env key="LD_LIBRARY_PATH" value="lib"/>
Expand Down
293 changes: 293 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,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>();

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address post haste!


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 {

Choose a reason for hiding this comment

The 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());
}
}
}
}
10 changes: 7 additions & 3 deletions test/za/ac/sun/cs/green/EntireSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@
import za.ac.sun.cs.green.service.slicer.SATSlicerTest;
import za.ac.sun.cs.green.service.z3.SATZ3JavaTest;
import za.ac.sun.cs.green.service.z3.SATZ3Test;

import za.ac.sun.cs.green.service.simplify.OnlyConstantPropagationTest;

import za.ac.sun.cs.green.util.ParallelSATTest;
import za.ac.sun.cs.green.util.SetServiceTest;
import za.ac.sun.cs.green.util.SetTaskManagerTest;

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

public class EntireSuite {

Expand Down
Loading