package models.terms.meta;

import java.util.Map;

import lombok.Getter;
import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Symbol;
import models.algebra.Variable;
import models.terms.RDLTerm;
import models.terms.ResourceConstant;
import models.terms.ResourceVariable;

@Getter
public class MetaResourceVariable extends MetaVariable {
	
	public MetaResourceVariable(Variable variableName, OrderConstraint constraint, Expression order) {
		super(new Symbol("", 0), MetaRDLTerm.TermType.META_RESOURCE_VARIABLE, variableName, constraint, order);
	}
	
	public MetaResourceVariable(Variable variableName) {
		super(new Symbol("", 0), MetaRDLTerm.TermType.META_RESOURCE_VARIABLE, variableName, OrderConstraint.ANY, new Constant("0"));
	}
	
	public MetaResourceVariable(Variable variableName, Expression order) {
		super(new Symbol("", 0), MetaRDLTerm.TermType.META_RESOURCE_VARIABLE, variableName, OrderConstraint.EQ, order);
	}
	
	@Override
	public boolean isMatchedBy(RDLTerm another, Map<Variable, RDLTerm> binding,
			Map<Variable, OrderVariableConstraint> orderConstraint) {
		if ((! (another instanceof ResourceVariable)) && (! (another instanceof ResourceConstant))) {
			return false;
		}
		
		if (! orderConstraintCheck(another, orderConstraint)) {
			return false;
		}
		
		if (! islinearRightNormalizedMatchedBy(another)) {
			return false;
		}
		
		if (! binding.containsKey(this.variableName)) {
			binding.put(this.variableName, another);
			return true;
		}
		return binding.get(this.variableName).equals(another);
	}
}
