Commit 498f9ea7 authored by Carlos Galindo's avatar Carlos Galindo
Browse files

Constraints: remove NodeConstraints

- Now Constraints is just a stack of EdgeConstraint.
parent 28a21900
Loading
Loading
Loading
Loading
+0 −67
Original line number Diff line number Diff line
package edg.constraint;

import edg.graph.EDG;
import edg.graph.Edge;
import edg.slicing.Phase;

import java.util.List;

public class AddNodeConstraint extends EdgeConstraint {
	private final NodeConstraint nodeConstraint;

	public AddNodeConstraint(NodeConstraint nodeConstraint)
	{
		this.nodeConstraint = nodeConstraint;
	}

	public boolean equals(Object object)
	{
		if (object == this)
			return true;
		if (!(object instanceof AddNodeConstraint))
			return false;

		final AddNodeConstraint constraint = (AddNodeConstraint) object;

		return this.nodeConstraint.equals(constraint.nodeConstraint);
	}
	public String toString()
	{
		return this.nodeConstraint.toString();
	}

	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, int productionDepth)
	{
		constraints.nodeConstraints.add(this.nodeConstraint);
		return super.wrap(constraints);
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AccessConstraint topConstraint, int productionDepth)
	{
		constraints.nodeConstraints.add(this.nodeConstraint);
		return super.wrap(constraints);
	}

	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, LiteralConstraint topConstraint, int productionDepth) {
		constraints.nodeConstraints.add(this.nodeConstraint);
		return super.wrap(constraints);
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, GrammarConstraint topConstraint, int productionDepth)
	{
		super.check(phase, Phase.SummaryGeneration);

		constraints.nodeConstraints.add(this.nodeConstraint);
		return super.wrap(constraints);
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, SeekingConstraint topConstraint, int productionDepth)
	{
		constraints.nodeConstraints.add(this.nodeConstraint);
		return super.wrap(constraints);
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AsteriskConstraint topConstraint, int productionDepth)
	{
		super.check(phase, Phase.SummaryGeneration);

		constraints.nodeConstraints.add(this.nodeConstraint);
		return super.wrap(constraints);
	}
}
 No newline at end of file
+27 −9
Original line number Diff line number Diff line
/*
 * EDG, a library to generate and slice Expression Dependence Graphs.
 * Copyright (c) 2021-2023. David Insa, Carlos Galindo, Sergio Pérez, Josep Silva, Salvador Tamarit.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package edg.constraint;

import edg.graph.EDG;
@@ -98,10 +116,10 @@ public class AsteriskConstraint extends EdgeConstraint
		super.check(topConstraint.operation, SeekingConstraint.Operation.Add);

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

		final EdgeConstraint peekConstraint = newConstraints.peekEdgeConstraint();
		final EdgeConstraint peekConstraint = newConstraints.peek();
		return super.resolve(phase, edg, edge, newConstraints, peekConstraint, productionDepth);
	}
		
@@ -113,7 +131,7 @@ public class AsteriskConstraint extends EdgeConstraint

	private boolean existsPreviousAsteriskConstraint(Constraints constraints){
		boolean previousAsterisk = false;
		for(Constraint c : constraints.getEdgeConstraints()) { // Traverse from bottom to top of the stack
		for(Constraint c : constraints) { // Traverse from bottom to top of the stack
			if (c instanceof AsteriskConstraint)
				previousAsterisk = true;
			if (previousAsterisk && c instanceof GrammarConstraint)
@@ -124,20 +142,20 @@ public class AsteriskConstraint extends EdgeConstraint

	private boolean isPreviousConstraintAsteriskConstraint(Constraints constraints){
		Constraints constraintsClone = (Constraints) constraints.clone();
		constraintsClone.popEdgeConstraint();
		constraintsClone.pop();

		if (constraintsClone.isEdgeConstraintsEmpty())
		if (constraintsClone.isEmpty())
			return false;

		Constraint c = constraintsClone.popEdgeConstraint();
		Constraint c = constraintsClone.pop();
		return c instanceof AsteriskConstraint;
	}

	private Constraints popToAsteriskConstraint(Constraints constraints){
		Constraint topConstraint = constraints.getEdgeConstraints().peek();
		Constraint topConstraint = constraints.peek();
		while(!(topConstraint instanceof AsteriskConstraint)){
			constraints.getEdgeConstraints().pop();
			topConstraint = constraints.getEdgeConstraints().peek();
			constraints.pop();
			topConstraint = constraints.peek();
		}
		return constraints;
	}
+0 −74
Original line number Diff line number Diff line
package edg.constraint;

import edg.slicing.Phase;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

public class ConstraintFactory
{
	private static ConstraintFactory factory = new ConstraintFactory();
	public static ConstraintFactory getFactory()
	{
		return ConstraintFactory.factory;
	}

	private enum Type { DataConstructor, ListComprehension, List, AddNode, Asterisk, Empty, Phase }
	private Map<Type, Set<Constraint>> constraints = new Hashtable<>();

	private ConstraintFactory()
	{
		for (Type type : Type.values())
			this.constraints.put(type, new HashSet<>());
		this.constraints.get(Type.Asterisk).add(AsteriskConstraint.getConstraint());
		this.constraints.get(Type.Empty).add(EmptyConstraint.getConstraint());
	}

	private Constraint getConstraint(Type type, Constraint constraint)
	{
		final Set<Constraint> constraints = this.constraints.get(type);

		for (Constraint oldConstraint : constraints)
			if (oldConstraint.equals(constraint))
				return oldConstraint;

		constraints.add(constraint);
		return constraint;
	}

	public DataConstructorConstraint getDataConstructorConstraint(AccessConstraint.Operation operation, String index)
	{
		final DataConstructorConstraint constraint = new DataConstructorConstraint(operation, index);
		return (DataConstructorConstraint) this.getConstraint(Type.DataConstructor, constraint);
	}
	public ListComprehensionConstraint getListComprehensionConstraint(AccessConstraint.Operation operation)
	{
		final ListComprehensionConstraint constraint = new ListComprehensionConstraint(operation);
		return (ListComprehensionConstraint) this.getConstraint(Type.ListComprehension, constraint);
	}
	public ListConstraint getListConstraint(AccessConstraint.Operation operation)
	{
		final ListConstraint constraint = new ListConstraint(operation);
		return (ListConstraint) this.getConstraint(Type.List, constraint);
	}
	public AddNodeConstraint getAddNodeConstraint(NodeConstraint nodeConstraint)
	{
		final AddNodeConstraint constraint = new AddNodeConstraint(nodeConstraint);
		return (AddNodeConstraint) this.getConstraint(Type.AddNode, constraint);
	}
	public AsteriskConstraint getAsteriskConstraint()
	{
		return (AsteriskConstraint) this.constraints.get(Type.Asterisk).iterator().next();
	}
	public EmptyConstraint getEmptyConstraint()
	{
		return (EmptyConstraint) this.constraints.get(Type.Empty).iterator().next();
	}
	public PhaseConstraint getPhaseConstraint(Phase... phases)
	{
		final PhaseConstraint constraint = new PhaseConstraint(phases);
		return (PhaseConstraint) this.getConstraint(Type.Phase, constraint);
	}
}
 No newline at end of file
+29 −83
Original line number Diff line number Diff line
/*
 * EDG, a library to generate and slice Expression Dependence Graphs.
 * Copyright (c) 2021-2023. David Insa, Carlos Galindo, Sergio Pérez, Josep Silva, Salvador Tamarit.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package edg.constraint;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class Constraints {
public class Constraints extends Stack<EdgeConstraint> {
	public enum Type {Node, Edge}

	protected Set<NodeConstraint> nodeConstraints = new HashSet<>();
	protected Stack<EdgeConstraint> edgeConstraints = new Stack<>();

	public Set<NodeConstraint> getNodeConstraints()
	{
		return this.nodeConstraints;
	}

	public void clearNodeConstraints()
	{
		this.nodeConstraints.clear();
	}

	public EdgeConstraint pushEdgeConstraint(EdgeConstraint constraint)
	{
		return this.edgeConstraints.push(constraint);
	}
	public EdgeConstraint popEdgeConstraint()
	{
		return this.edgeConstraints.pop();
	}
	public EdgeConstraint peekEdgeConstraint()
	{
		return this.edgeConstraints.peek();
	}
	public Stack<EdgeConstraint> getEdgeConstraints()
	{
		return this.edgeConstraints;
	}
	public EdgeConstraint getEdgeConstraint(int index)
	{
		return this.edgeConstraints.get(index);
	}

	public boolean isEdgeConstraintsEmpty()
	{
		return this.edgeConstraints.isEmpty();
	}
	public int sizeEdgeConstraints()
	{
		return this.edgeConstraints.size();
	}

	public Object clone()
	{
		final Constraints constraints = new Constraints();

		constraints.nodeConstraints.addAll(this.nodeConstraints);
		constraints.edgeConstraints.addAll(this.edgeConstraints);

		return constraints;
	}
	public boolean equals(Object object)
	{
		if (!this.nodeConstraints.isEmpty())
			System.out.println("Sergio sabe que aqui no hay nada...");
	public boolean equals(Object object) {
		if (object == this)
			return true;
		if (!(object instanceof Constraints))
			return false;

		final Constraints constraints = (Constraints) object;

		if (this.nodeConstraints.size() != constraints.nodeConstraints.size())
			return false;
		if (this.edgeConstraints.size() != constraints.edgeConstraints.size())
			return false;
		if (!this.nodeConstraints.equals(constraints.nodeConstraints))
			return false;
		return this.edgeConstraints.equals(constraints.edgeConstraints);
		return object instanceof Constraints
				&& this.size() == ((Constraints) object).size()
				&& super.equals(object);
	}

	public boolean isSuffix(Constraints constraints)
	{
		if (this.edgeConstraints.size() > constraints.edgeConstraints.size())
		if (this.size() > constraints.size())
			return false;
		if (this.edgeConstraints.equals(constraints.edgeConstraints))
		if (this.equals(constraints))
			return true;

		Stack<EdgeConstraint> edgeConstraints = constraints.getEdgeConstraints();
		for (int i = 1; i <= this.edgeConstraints.size(); i++) {
			EdgeConstraint thisEC = this.edgeConstraints.get(this.edgeConstraints.size() - i);
			EdgeConstraint constraintsEC = edgeConstraints.get(edgeConstraints.size() - i);
		for (int i = 1; i <= size(); i++) {
			EdgeConstraint thisEC = get(size() - i);
			EdgeConstraint constraintsEC = constraints.get(constraints.size() - i);
			if (!thisEC.equals(constraintsEC))
				return false;
		}
		return true;
	}

	public int hashCode()
	{
		return this.nodeConstraints.size() + this.edgeConstraints.size();
	}
}
+34 −26
Original line number Diff line number Diff line
/*
 * EDG, a library to generate and slice Expression Dependence Graphs.
 * Copyright (c) 2021-2023. David Insa, Carlos Galindo, Sergio Pérez, Josep Silva, Salvador Tamarit.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package edg.constraint;

import edg.config.Config;
@@ -20,9 +38,9 @@ public abstract class EdgeConstraint extends Constraint
		else if (topConstraint instanceof LiteralConstraint)
			return this.resolve(phase, edg, edge, constraints, (LiteralConstraint) topConstraint, productionDepth);
		else if (topConstraint instanceof EmptyConstraint)
			return this.resolve(phase, edg, edge, constraints, (EmptyConstraint) topConstraint, productionDepth);
			throw new RuntimeException("The empty constraint should not be on the stack");
		else if (topConstraint instanceof PhaseConstraint)
			return this.resolve(phase, edg, edge, constraints, (PhaseConstraint) topConstraint, productionDepth);
			throw new RuntimeException("The phase constraint should not be on the stack");
		else if (topConstraint instanceof SeekingConstraint)
			return this.resolve(phase, edg, edge, constraints, (SeekingConstraint) topConstraint, productionDepth);
		else if (topConstraint instanceof AsteriskConstraint)
@@ -37,42 +55,32 @@ public abstract class EdgeConstraint extends Constraint
	protected abstract List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AccessConstraint topConstraint, int productionDepth);
	protected abstract List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, LiteralConstraint topConstraint, int productionDepth);

	protected final List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AddNodeConstraint topConstraint, int productionDepth)
	{
		throw new RuntimeException("The add node constraint should not be on the stack");
	}
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, ListComprehensionConstraint topConstraint, int productionDepth)
	{
	protected List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, ListComprehensionConstraint topConstraint, int productionDepth) {
		return this.resolve(phase, edg, edge, constraints, (AccessConstraint) topConstraint, productionDepth);
	}
	protected final List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, EmptyConstraint topConstraint, int productionDepth)
	{
		throw new RuntimeException("The empty constraint should not be on the stack");
	}
	protected final List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, PhaseConstraint topConstraint, int productionDepth)
	{
		throw new RuntimeException("The phase constraint should not be on the stack");
	}

	protected abstract List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, SeekingConstraint topConstraint, int productionDepth);
	protected abstract List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, AsteriskConstraint topConstraint, int productionDepth);
	protected abstract List<Constraints> resolve(Phase phase, EDG edg, Edge edge, Constraints constraints, GrammarConstraint topConstraint, int productionDepth);

	protected Constraints pop(Constraints constraints)
	{
		constraints.popEdgeConstraint();

	protected Constraints pop(Constraints constraints) {
		constraints.pop();
		return constraints;
	}
	protected Constraints push(Phase phase, Constraints constraints)
	{
		if (constraints.sizeEdgeConstraints() == Config.MAX_STACK_SIZE) {

	protected Constraints push(Phase phase, Constraints constraints) {
		if (constraints.size() == Config.MAX_STACK_SIZE) {
			if (phase == Phase.Tabular)
				constraints.edgeConstraints.remove(0);
				constraints.remove(0);
			else if (phase.isInstanceof(Phase.Slicing))
				throw new StackOverflowError();
		}
		constraints.pushEdgeConstraint(this);

		constraints.push(this);
		return constraints;
	}

	@Override
	public int hashCode() {
		return getClass().hashCode();
	}
}
Loading