za.ac.sun.cs.green.service.CountService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of green Show documentation
Show all versions of green Show documentation
Universal interface to decision procedures, constraint solvers, model counters, and related tools
The newest version!
package za.ac.sun.cs.green.service;
import java.util.Set;
import org.apfloat.Apint;
import za.ac.sun.cs.green.Instance;
import za.ac.sun.cs.green.Green;
import za.ac.sun.cs.green.util.Reporter;
public abstract class CountService extends BasicService {
private static final String SERVICE_KEY = "COUNT:";
private int invocationCount = 0;
private int cacheHitCount = 0;
private int cacheMissCount = 0;
private long timeConsumption = 0;
public CountService(Green solver) {
super(solver);
}
@Override
public void report(Reporter reporter) {
reporter.setContext(getClass().getSimpleName());
reporter.report("invocationCount", invocationCount);
reporter.report("cacheHitCount", cacheHitCount);
reporter.report("cacheMissCount", cacheMissCount);
reporter.report("timeConsumption", timeConsumption);
}
@Override
public Object allChildrenDone(Instance instance, Object result) {
return instance.getData(getClass());
}
@Override
public Set processRequest(Instance instance) {
Apint result = (Apint) instance.getData(getClass());
if (result == null) {
result = solve0(instance);
if (result != null) {
instance.setData(getClass(), result);
}
}
return null;
}
private Apint solve0(Instance instance) {
invocationCount++;
// String key = SERVICE_KEY + instance.getFullExpression().getString();
String key = SERVICE_KEY + instance.getFullExpression().toString();
Apint result = store.getApfloatInteger(key);
if (result == null) {
cacheMissCount++;
result = solve1(instance);
if (result != null) {
store.put(key, result);
}
} else {
cacheHitCount++;
}
return result;
}
private Apint solve1(Instance instance) {
long startTime = System.currentTimeMillis();
Apint result = solve(instance);
timeConsumption += System.currentTimeMillis() - startTime;
return result;
}
protected abstract Apint solve(Instance instance);
}