org.ggp.base.util.prover.logging.ProverCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alloy-ggp-base Show documentation
Show all versions of alloy-ggp-base Show documentation
A modified version of the GGP-Base library for Alloy.
package org.ggp.base.util.prover.logging;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.ggp.base.util.gdl.grammar.GdlSentence;
public final class ProverCache
{
private final Map> contents;
private ProverCache(Map> mapForContents) {
this.contents = mapForContents;
}
public static ProverCache createSingleThreadedCache() {
return new ProverCache(new HashMap>());
}
public static ProverCache createMultiThreadedCache() {
return new ProverCache(new ConcurrentHashMap>());
}
/**
* NOTE: The given sentence must have been renamed with a VariableRenamer.
*/
public boolean contains(GdlSentence renamedSentence)
{
return contents.containsKey(renamedSentence);
}
public List get(GdlSentence sentence, GdlSentence varRenamedSentence)
{
Set cacheContents = contents.get(varRenamedSentence);
if (cacheContents == null) {
return null;
}
Set results = new HashSet();
for (GdlSentence answer : cacheContents)
{
results.add(Unifier.unify(sentence, answer));
}
return new ArrayList(results);
}
public void put(GdlSentence sentence, GdlSentence renamedSentence,
Set answers)
{
Set results = new HashSet();
for (Substitution answer : answers)
{
results.add(Substituter.substitute(sentence, answer));
}
contents.put(renamedSentence, results);
}
}