de.prob.prolog.match.PrologVariableMatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prologlib Show documentation
Show all versions of prologlib Show documentation
Part of the ProB Parser library
The newest version!
package de.prob.prolog.match;
import java.util.Map;
import de.prob.prolog.output.PrologTermOutput;
import de.prob.prolog.term.PrologTerm;
import de.prob.prolog.term.VariablePrologTerm;
/**
* Matches a Prolog variable.
*/
public final class PrologVariableMatch extends PrologMatch {
private final String varName;
/**
* Matches on a Prolog variable with the given name
*
* @param name the name, if null
it will not be checked
*/
private PrologVariableMatch(final String name, final String varName) {
super(name);
if (varName != null && !PrologTermOutput.isValidPrologVariable(varName)) {
throw new IllegalArgumentException("varName");
}
this.varName = varName;
}
public static PrologVariableMatch anonVar() {
return namedVar(null);
}
public static PrologVariableMatch anonVar(String varName) {
return namedVar(null, varName);
}
public static PrologVariableMatch namedVar(String name) {
return namedVar(name, null);
}
public static PrologVariableMatch namedVar(String name, String varName) {
return new PrologVariableMatch(name, varName);
}
@Override
protected boolean isMatch(PrologTerm term, Map hits) {
boolean match = term instanceof VariablePrologTerm;
if (match && varName != null) {
match = varName.equals(((VariablePrologTerm) term).getName());
}
return match;
}
}