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

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

There is a newer version: 2.13.5
Show newest version
package de.prob.prolog.match;

import java.util.HashMap;
import java.util.Map;

import de.prob.prolog.term.PrologTerm;

/**
 * This class and its subclasses are used to do pattern-matching on Prolog
 * terms. It is a kind of unification.
 * 
 * This base class matches every {@link PrologTerm}.
 */
public class PrologMatch {
	private final String name;

	public PrologMatch anon() {
		return named(null);
	}

	public PrologMatch named(final String name) {
		return new PrologMatch(name);
	}

	protected PrologMatch(final String name) {
		this.name = name;
	}

	public final boolean matches(final PrologTerm term,
			final Map hits) {
		boolean isMatch = isMatch(term, hits);
		if (isMatch && hits != null && name != null) {
			hits.put(name, term);
		}
		return isMatch;
	}

	protected boolean isMatch(final PrologTerm term,
			final Map hits) {
		return true;
	}

	public final Map getMatches(final PrologTerm term) {
		Map hits = new HashMap();
		boolean matches = matches(term, hits);
		return matches ? hits : null;
	}

	public final boolean matches(final PrologTerm term) {
		return matches(term, null);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy