org.ggp.base.util.prover.aima.substitution.Substitution 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.
The newest version!
package org.ggp.base.util.prover.aima.substitution;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.ggp.base.util.gdl.grammar.GdlTerm;
import org.ggp.base.util.gdl.grammar.GdlVariable;
public final class Substitution
{
private final Map contents;
public Substitution()
{
contents = new HashMap();
}
public Substitution compose(Substitution thetaPrime)
{
Substitution result = new Substitution();
result.contents.putAll(contents);
result.contents.putAll(thetaPrime.contents);
return result;
}
public boolean contains(GdlVariable variable)
{
return contents.containsKey(variable);
}
@Override
public boolean equals(Object o)
{
if ((o != null) && (o instanceof Substitution))
{
Substitution substitution = (Substitution) o;
return substitution.contents.equals(contents);
}
return false;
}
public GdlTerm get(GdlVariable variable)
{
return contents.get(variable);
}
@Override
public int hashCode()
{
return contents.hashCode();
}
public void put(GdlVariable variable, GdlTerm term)
{
contents.put(variable, term);
}
/**
* Creates an identical substitution.
*
* @return A new, identical substitution.
*/
public Substitution copy()
{
Substitution copy = new Substitution();
copy.contents.putAll(contents);
return copy;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("{ ");
for (GdlVariable variable : contents.keySet())
{
sb.append(variable + "/" + contents.get(variable) + " ");
}
sb.append("}");
return sb.toString();
}
public void retainKeys(Set varsInSentence) {
contents.keySet().retainAll(varsInSentence);
}
public int size() {
return contents.size();
}
}