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

Propagation of one constant implemented. #42

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f50701b
Travis Test
NAG47 Jul 24, 2018
3d88740
added a test
NAG47 Jul 24, 2018
2a66884
test
NAG47 Jul 24, 2018
30cb5c7
commented out all the tests that work
NAG47 Jul 24, 2018
a9f9670
trying to fix test
NAG47 Jul 24, 2018
c654fcc
trying to fix test2
NAG47 Jul 24, 2018
931ae39
backtracking
NAG47 Jul 25, 2018
e9767b1
docker test
NAG47 Jul 26, 2018
f3078a3
docker test 2
NAG47 Jul 26, 2018
8047342
docker test 3
NAG47 Jul 26, 2018
f71ae08
docker test 4
NAG47 Jul 26, 2018
d27d582
fixed build status link
NAG47 Jul 26, 2018
19d8c7c
trying to fix build status icon
NAG47 Jul 26, 2018
6ee96eb
trying to fix build status icon 2
NAG47 Jul 26, 2018
60ba954
trying to fix build status icon 3
NAG47 Jul 26, 2018
fc73599
trying to fix build status icon 4
NAG47 Jul 26, 2018
ccaf0e1
trying to fix build status icon 5
NAG47 Jul 26, 2018
e6d73ae
bonus
NAG47 Jul 26, 2018
439bb1c
build status
NAG47 Jul 28, 2018
872e193
broke a test case
NAG47 Jul 30, 2018
c546323
Merge branch 'master' of github.com:NAG47/green
NAG47 Jul 30, 2018
18dd0fd
trying to fix my buil from not using the latest commit
NAG47 Jul 30, 2018
474ce2b
final
NAG47 Jul 30, 2018
6a20894
final2
NAG47 Jul 31, 2018
10b8bd4
final3
NAG47 Jul 31, 2018
415f9c9
final4
NAG47 Jul 31, 2018
c3a6603
prop test 1
NAG47 Aug 13, 2018
5e2359f
prop test 2
NAG47 Aug 13, 2018
b86ec8f
prop test 3
NAG47 Aug 13, 2018
5f5db27
crying, literal tears
NAG47 Aug 13, 2018
f9ef8db
crying, literal tears 2
NAG47 Aug 13, 2018
d524e94
all smiles :)
NAG47 Aug 13, 2018
9947314
:)))
NAG47 Aug 13, 2018
9772bfb
stuff works yo
NAG47 Aug 13, 2018
8b92337
final tut2, onlly propagation of one c works
NAG47 Aug 13, 2018
dde6aa5
fixed spacing problem
NAG47 Aug 14, 2018
157999c
oops
NAG47 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

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"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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/nag47/green

# Download and extract Z3
RUN mkdir z3
Expand Down
3 changes: 1 addition & 2 deletions README.md
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.

6 changes: 3 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<pathelement location="test"/>
</path>
<property name="green.srcpath" refid="green.srcpath"/>

<!--
Initialization
-->
Expand Down Expand Up @@ -95,8 +95,8 @@
<target name="test">
<echo message="java.library.path = ${z3lib};${cvc3lib}"/>
<mkdir dir="${junit.dir}"/>
<junit fork="yes" printsummary="withOutAndErr" haltonfailure = "yes">
<formatter type="xml"/>
<junit fork="yes" printsummary="withOutAndErr" haltonfailure="yes">
<formatter type="xml" 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
6 changes: 3 additions & 3 deletions src/za/ac/sun/cs/green/expr/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public static enum Operator {
// String Operations
SUBSTRING("SUBSTRING", 3, Fix.POSTFIX),
CONCAT("CONCAT", 2, Fix.POSTFIX),
TRIM("TRIM", 1, Fix.POSTFIX),
TRIM("TRIM", 1, Fix.POSTFIX),
REPLACE("REPLACE", 3, Fix.POSTFIX),
REPLACEFIRST("REPLACEFIRST", 3, Fix.POSTFIX),
REPLACEFIRST("REPLACEFIRST", 3, Fix.POSTFIX),
TOLOWERCASE("TOLOWERCASE", 2, Fix.POSTFIX),
TOUPPERCASE("TOUPPERCASE", 2, Fix.POSTFIX),
TOUPPERCASE("TOUPPERCASE", 2, Fix.POSTFIX),
VALUEOF("VALUEOF", 2, Fix.POSTFIX),
// String Comparators
NOTCONTAINS("NOTCONTAINS", 2, Fix.POSTFIX),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public Expression canonize(Expression expression,
return canonized;
} catch (VisitorException x) {
log.log(Level.SEVERE,
"encountered an exception -- this should not be happening!",
x);
"encountered an exception -- this should not be happening!",x);
}
return null;
}
Expand Down
128 changes: 128 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,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 {

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.

Copy link
Author

Choose a reason for hiding this comment

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

Done!

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));
}
}
}
}
8 changes: 5 additions & 3 deletions test/za/ac/sun/cs/green/EntireSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import za.ac.sun.cs.green.parser.smtlib2.SMTLIB2Scanner0Test;
import za.ac.sun.cs.green.service.bounder.BounderTest;
import za.ac.sun.cs.green.service.canonizer.SATCanonizerTest;
import za.ac.sun.cs.green.service.simplify.OnlyConstantPropagationTest;
import za.ac.sun.cs.green.service.choco.SATChocoTest;
import za.ac.sun.cs.green.service.cvc3.SATCVC3Test;
import za.ac.sun.cs.green.service.factorizer.SATFactorizerTest;
Expand All @@ -36,7 +37,8 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
SATCanonizerTest.class,
SATZ3Test.class
SATZ3Test.class,
OnlyConstantPropagationTest.class
})

public class EntireSuite {
Expand Down Expand Up @@ -75,9 +77,9 @@ public class EntireSuite {
Z3_PATH = z3;
HAS_Z3 = checkZ3Presence();
if (!HAS_Z3) {
System.out.println("Z3 Not Available, no tests for it will be executed");
System.out.println("Z3 Not Available, no tests for it will be executed");
}

}

private static boolean checkCVC3Presence() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ 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);
IntVariable v1 = new IntVariable("aa", 0, 99);
Operation o1 = new Operation(Operation.Operator.LE, c1, c1);
Operation o2 = new Operation(Operation.Operator.LT, v1, c1);
Operation o3 = new Operation(Operation.Operator.AND, o1, o2);
check(o3, "(2<=2)&&(aa<2)", "1*v+-1<0");
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 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)");
}

}