Skip to content
AsteriskConstraint.java 3.8 KiB
Newer Older
Carlos Galindo's avatar
Carlos Galindo committed
package edg.constraint;

import edg.graph.EDG;
Carlos Galindo's avatar
Carlos Galindo committed
import edg.graph.Edge;
import edg.slicing.Phase;

import java.util.List;

Carlos Galindo's avatar
Carlos Galindo committed
public class AsteriskConstraint extends EdgeConstraint
{
	private static AsteriskConstraint constraint = new AsteriskConstraint();
	public static AsteriskConstraint getConstraint()
	{
		return AsteriskConstraint.constraint;
	}

	private AsteriskConstraint()
	{
		
	}

	public boolean equals(Object object)
	{
		return object instanceof AsteriskConstraint;
	}
	public String toString()
	{
		return "*";
	}

	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, int productionDepth)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		return super.wrap(constraints);
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AccessConstraint topConstraint, int productionDepth)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		if (phase.isInstanceof(Phase.Slicing))
			return super.wrap(new Constraints());
		super.check(phase, Phase.SummaryGeneration);

		if (existsPreviousAsteriskConstraint(constraints))
			return super.wrap(this.popToAsteriskConstraint(constraints));

Carlos Galindo's avatar
Carlos Galindo committed
		if (topConstraint.operation == AccessConstraint.Operation.Remove)
			return super.wrap(super.push(phase, constraints));
		super.check(topConstraint.operation, AccessConstraint.Operation.Add);

		final Constraints newConstraints = super.pop(constraints);
		if (newConstraints.isEdgeConstraintsEmpty())
			return super.wrap(newConstraints);

		final EdgeConstraint peekConstraint = newConstraints.peekEdgeConstraint();
		return super.resolve(phase, edg, edge, newConstraints, peekConstraint, productionDepth);
Carlos Galindo's avatar
Carlos Galindo committed
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, GrammarConstraint topConstraint, int productionDepth)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		super.check(phase, Phase.SummaryGeneration);

		if (existsPreviousAsteriskConstraint(constraints))
			return super.wrap(this.popToAsteriskConstraint(constraints));

Carlos Galindo's avatar
Carlos Galindo committed
		return super.wrap(super.push(phase, constraints));
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, SeekingConstraint topConstraint, int productionDepth)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		if (phase.isInstanceof(Phase.Slicing))
			return super.wrap(new Constraints());
		super.check(phase, Phase.SummaryGeneration);

		if (existsPreviousAsteriskConstraint(constraints))
			return super.wrap(this.popToAsteriskConstraint(constraints));

Carlos Galindo's avatar
Carlos Galindo committed
		if (topConstraint.operation == SeekingConstraint.Operation.LetThrough || topConstraint.operation == SeekingConstraint.Operation.Remove)
			return super.wrap(super.push(phase, constraints));
		super.check(topConstraint.operation, SeekingConstraint.Operation.Add);

		final Constraints newConstraints = super.pop(constraints);
		if (newConstraints.isEdgeConstraintsEmpty())
			return super.wrap(newConstraints);

		final EdgeConstraint peekConstraint = newConstraints.peekEdgeConstraint();
		return super.resolve(phase, edg, edge, newConstraints, peekConstraint, productionDepth);
Carlos Galindo's avatar
Carlos Galindo committed
	}
		
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AsteriskConstraint topConstraint, int productionDepth)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		super.check(phase, Phase.SummaryGeneration);
		return super.wrap(constraints);
	}

	private boolean existsPreviousAsteriskConstraint(Constraints constraints){
		boolean previousAsterisk = false;
		for(Constraint c : constraints.getEdgeConstraints()) {
			if (c instanceof AsteriskConstraint)
				previousAsterisk = true;
			if (previousAsterisk && c instanceof GrammarConstraint)
				previousAsterisk = false;
		}
		return previousAsterisk;
	}

	private Constraints popToAsteriskConstraint(Constraints constraints){
		Constraint topConstraint = constraints.getEdgeConstraints().peek();
		while(!(topConstraint instanceof AsteriskConstraint)){
			constraints.getEdgeConstraints().pop();
			topConstraint = constraints.getEdgeConstraints().peek();
		}
		return constraints;
	}
Carlos Galindo's avatar
Carlos Galindo committed
}