package models.dataConstraintModel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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<Expression> pathParams = null;
public ResourcePath(String resourceName) {
super(resourceName);
this.parent = null;
this.resourceHierarchy = new ResourceHierarchy(null, resourceName);
this.pathParams = new ArrayList<>();
}
public ResourcePath(ResourcePath parent, String resourceName) {
super(parent.toString() + "." + resourceName);
this.parent = parent;
this.resourceHierarchy = new ResourceHierarchy(parent.getResourceHierarchy(), resourceName);
this.pathParams = parent.getPathParams();
}
public ResourcePath(ResourcePath parent, Expression exp) {
super(parent.toString() + ".{" + exp + "}");
this.parent = parent;
this.resourceHierarchy = new ResourceHierarchy(parent.getResourceHierarchy(), 1);
this.pathParams = new ArrayList<>(parent.getPathParams());
this.pathParams.add(exp);
}
public ResourcePath(ResourcePath parent, List<Expression> pathParams) {
super(parent.toString() + ".{" + pathParams.toString() + "}");
this.parent = parent;
this.resourceHierarchy = new ResourceHierarchy(parent.getResourceHierarchy(), pathParams.size());
this.pathParams = new ArrayList<>(parent.getPathParams());
this.pathParams.addAll(pathParams);
}
public ResourceHierarchy getResourceHierarchy() {
return resourceHierarchy;
}
public void setResourceHierarchy(ResourceHierarchy resourceHierarchy) {
this.resourceHierarchy = resourceHierarchy;
}
public String getResourceName() {
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() {
return pathParams;
}
public void setPathParams(List<Expression> pathParams) {
this.pathParams = pathParams;
}
public void addPathParam(Expression pathParam) {
pathParams.add(pathParam);
}
public ResourcePath getParent() {
return parent;
}
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 String toString() {
return resourceHierarchy.toString(pathParams);
}
//
// public boolean equals(Object another) {
// if (!(another instanceof ResourcePath)) return false;
// return toString().equals(((ResourcePath) another).toString());
// }
//
// public int hashCode() {
// return toString().hashCode();
// }
}