All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.prob.prolog.match.PrologVariableMatch Maven / Gradle / Ivy

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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy