package models.dataConstraintModel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import models.algebra.Expression;
import models.algebra.Type;
import models.algebra.Symbol;
import models.algebra.Term;
public class ResourcePath extends Symbol {
protected ResourcePath parent = null;
protected ResourceHierarchy resourceHierarchy = null;
protected List<Map.Entry<Expression, Expression>> pathParams = null;
public ResourcePath(String fullResourceName) {
super(fullResourceName);
this.parent = null;
this.resourceHierarchy = new ResourceHierarchy(null, fullResourceName);
this.pathParams = new ArrayList<>();
}
public ResourcePath(String fullResourceName, ResourceHierarchy resourceHierarchy) {
super(fullResourceName);
this.parent = null;
this.resourceHierarchy = resourceHierarchy;
this.pathParams = new ArrayList<>();
}
public ResourcePath(ResourcePath parent, String leafResourceName) {
super(parent.toString() + "." + leafResourceName);
this.parent = parent;
this.resourceHierarchy = new ResourceHierarchy(parent.getResourceHierarchy(), leafResourceName);
this.pathParams = parent.getPathParamsAndConstraints();
}
public ResourcePath(ResourcePath parent, String leafResourceName, ResourceHierarchy resourceHierarchy) {
super(parent.toString() + "." + leafResourceName);
this.parent = parent;
this.resourceHierarchy = resourceHierarchy;
this.pathParams = parent.getPathParamsAndConstraints();
}
public ResourcePath(ResourcePath parent, Expression exp) {
super(parent.toString() + ".{" + exp + "}");
this.parent = parent;
this.resourceHierarchy = new ResourceHierarchy(parent.getResourceHierarchy(), exp);
this.pathParams = new ArrayList<>(parent.getPathParamsAndConstraints());
this.pathParams.add(new AbstractMap.SimpleEntry<>(exp, null));
}
public ResourcePath(ResourcePath parent, Expression exp, Expression constraint) {
super(parent.toString() + ".{" + exp + "=" + constraint + "}");
this.parent = parent;
this.resourceHierarchy = new ResourceHierarchy(parent.getResourceHierarchy(), exp);
this.pathParams = new ArrayList<>(parent.getPathParamsAndConstraints());
this.pathParams.add(new AbstractMap.SimpleEntry<>(exp, constraint));
}
public ResourcePath(ResourcePath parent, Expression exp, ResourceHierarchy resourceHierarchy) {
super(parent.toString() + ".{" + exp + "}");
this.parent = parent;
this.resourceHierarchy = resourceHierarchy;
this.pathParams = new ArrayList<>(parent.getPathParamsAndConstraints());
this.pathParams.add(new AbstractMap.SimpleEntry<>(exp, null));
}
public ResourcePath(ResourcePath parent, Expression exp, Expression constraint, ResourceHierarchy resourceHierarchy) {
super(parent.toString() + ".{" + exp + "=" + constraint + "}");
this.parent = parent;
this.resourceHierarchy = resourceHierarchy;
this.pathParams = new ArrayList<>(parent.getPathParamsAndConstraints());
this.pathParams.add(new AbstractMap.SimpleEntry<>(exp, constraint));
}
public ResourcePath(ResourcePath another) {
super(another.name);
if (another.parent != null) {
this.parent = new ResourcePath(another.parent);
} else {
this.parent = null;
}
this.resourceHierarchy = another.resourceHierarchy;
this.pathParams = new ArrayList<>(another.getPathParamsAndConstraints());
}
public ResourceHierarchy getResourceHierarchy() {
return resourceHierarchy;
}
public void setResourceHierarchy(ResourceHierarchy resourceHierarchy) {
this.resourceHierarchy = resourceHierarchy;
}
public String getLeafResourceName() {
return resourceHierarchy.getResourceName();
}
public int getNumberOfParameters() {
return resourceHierarchy.getTotalNumParameters();
}
public models.algebra.Type getResourceStateType() {
return resourceHierarchy.getResourceStateType();
}
public void setResourceStateType(models.algebra.Type resourceStateType) {
this.resourceHierarchy.setResourceStateType(resourceStateType);
}
public List<Expression> getPathParams() {
List<Expression> params = new ArrayList<>();
for (Map.Entry<Expression, Expression> paramEnt: this.pathParams) {
params.add(paramEnt.getKey());
}
return params;
}
public List<Map.Entry<Expression, Expression>> getPathParamsAndConstraints() {
return pathParams;
}
public void setPathParams(List<Map.Entry<Expression, Expression>> pathParams) {
this.pathParams = pathParams;
}
public void addPathParam(Expression pathParam) {
pathParams.add(new AbstractMap.SimpleEntry<>(pathParam, null));
}
public void addPathParamWithConstraint(Expression pathParam, Expression pathConstraint) {
pathParams.add(new AbstractMap.SimpleEntry<>(pathParam, pathConstraint));
}
public void replacePathParam(int i, Expression pathParam, Expression pathConstraint) {
if (i < pathParams.size()) {
pathParams.set(i, new AbstractMap.SimpleEntry<>(pathParam, pathConstraint));
parent.replacePathParam(i, pathParam, pathConstraint);
}
}
public boolean endsWithParam() {
if (resourceHierarchy.getNumParameters() > 0) return true;
return false;
}
public Expression getLastParam() {
if (endsWithParam()) {
return pathParams.get(pathParams.size() - 1).getKey();
}
return null;
}
public Map.Entry<Expression, Expression> getLastParamAndConstraint() {
if (endsWithParam()) {
return pathParams.get(pathParams.size() - 1);
}
return null;
}
public ResourcePath getParent() {
return parent;
}
public ResourcePath getRoot() {
if (parent == null) return this;
return parent.getRoot();
}
public ResourcePath getCommonPrefix(ResourcePath another) {
Set<ResourcePath> ancestors = new HashSet<>();
while (another != null) {
ancestors.add(another);
another = another.getParent();
}
ResourcePath curPath = this;
while (!ancestors.contains(curPath)) {
curPath = curPath.getParent();
if (curPath == null) return null;
}
return curPath;
}
public boolean isAncestorOf(ResourcePath another) {
Set<ResourcePath> ancestors = new HashSet<>();
while (another != null) {
ancestors.add(another);
another = another.getParent();
}
return ancestors.contains(this);
}
public String toString() {
return resourceHierarchy.toString(pathParams);
}
public String toResourcePath() {
List<String> params = new ArrayList<>();
String[] sideEffects = new String[] {""};
for (Map.Entry<Expression, Expression> param: pathParams) {
params.add("{" + param.getKey().toString() + "}");
}
return resourceHierarchy.toResourcePath(params);
}
}