cass.rollup.processors.v2.graph.collapser.NodeGraph Maven / Gradle / Ivy
package cass.rollup.processors.v2.graph.collapser;
import org.stjs.javascript.Array;
import org.stjs.javascript.JSCollections;
import org.stjs.javascript.Map;
public class NodeGraph {
//CYCLE EQUIVS (1)
//Narrows == isRequiredBy
//CYCLE EQUIVS (2)
//Broadens == Requires
private Array nodeList;
private Map nodeMap;
private Array relationList;
private Map> relationMap;
public NodeGraph() {
nodeList = new Array();
relationList = new Array();
nodeMap = JSCollections.$map();
relationMap = JSCollections.$map();
}
public void addNode(Node n) {
if (nodeMap.$get(n.getId()) == null) {
nodeList.push(n);
nodeMap.$put(n.getId(), n);
}
}
public void createImpliedRelations() throws Exception {
try {
Array relationsToAdd = new Array();
NodeRelation nr;
for (int i = 0; i < relationList.$length(); i++) {
nr = relationList.$get(i);
if (nr.getType() == RelationType.RELATION_TYPE.NARROWS) {
relationsToAdd.push(new NodeRelation(nr.getTarget(), nr.getSource(), RelationType.RELATION_TYPE.BROADENS));
} else if (nr.getType() == RelationType.RELATION_TYPE.REQUIRES) {
relationsToAdd.push(new NodeRelation(nr.getTarget(), nr.getSource(), RelationType.RELATION_TYPE.IS_REQUIRED_BY));
} else if (nr.getType() == RelationType.RELATION_TYPE.BROADENS) {
relationsToAdd.push(new NodeRelation(nr.getTarget(), nr.getSource(), RelationType.RELATION_TYPE.NARROWS));
} else if (nr.getType() == RelationType.RELATION_TYPE.IS_REQUIRED_BY) {
relationsToAdd.push(new NodeRelation(nr.getTarget(), nr.getSource(), RelationType.RELATION_TYPE.REQUIRES));
}
}
NodeRelation nnr;
for (int i = 0; i < relationsToAdd.$length(); i++) {
nnr = relationsToAdd.$get(i);
addRelation(nnr.getSource(), nnr.getTarget(), nnr.getType());
}
}
catch (Exception e) {
throw new Exception ("createImpliedRelations: " + e.toString());
}
}
public void addRelation(Node sourceNode, Node targetNode, RelationType.RELATION_TYPE relationType) throws Exception {
try {
if (sourceNode == null || targetNode == null) return;
Array nodeRelationList;
if (nodeHasRelations(sourceNode)) nodeRelationList = getRelationListForNode(sourceNode);
else {
nodeRelationList = new Array();
relationMap.$put(sourceNode.getId(), nodeRelationList);
}
NodeRelation newNodeRelation = new NodeRelation(sourceNode, targetNode, relationType);
if (!doesRelationAlreadyExist(newNodeRelation, nodeRelationList)) {
nodeRelationList.push(newNodeRelation);
relationList.push(newNodeRelation);
}
}
catch (Exception e) {
throw new Exception ("addRelation: " + e.toString());
}
}
public Array getRelationListForNode(Node n) {
return relationMap.$get(n.getId());
}
public Array getNarrowsIsRequiredByEqualsRelationListForNode(Node n) {
Array retList = new Array();
if (relationMap.$get(n.getId()) != null) {
Array nra = relationMap.$get(n.getId());
NodeRelation nr;
for (int i = 0; i < nra.$length(); i++) {
nr = nra.$get(i);
if (nr.getType()==RelationType.RELATION_TYPE.IS_EQUIVALENT_TO ||
nr.getType()==RelationType.RELATION_TYPE.NARROWS ||
nr.getType()==RelationType.RELATION_TYPE.IS_REQUIRED_BY) {
retList.push(nr);
}
}
}
return retList;
}
public Array getBroadensRequiresEqualsRelationListForNode(Node n) {
Array retList = new Array();
if (relationMap.$get(n.getId()) != null) {
Array nra = relationMap.$get(n.getId());
NodeRelation nr;
for (int i = 0; i < nra.$length(); i++) {
nr = nra.$get(i);
if (nr.getType()==RelationType.RELATION_TYPE.IS_EQUIVALENT_TO ||
nr.getType()==RelationType.RELATION_TYPE.BROADENS ||
nr.getType()==RelationType.RELATION_TYPE.REQUIRES) {
retList.push(nr);
}
}
}
return retList;
}
public Array getNodeList() {
return nodeList;
}
public void setNodeList(Array nodeList) {
this.nodeList = nodeList;
}
public Array getRelationList() {
return relationList;
}
public void setRelationList(Array relationList) {
this.relationList = relationList;
}
public boolean nodeHasRelations(Node n) {
if (relationMap.$get(n.getId()) == null) return false;
return true;
}
private boolean doesRelationAlreadyExist(NodeRelation nodeRelation, Array nodeRelationList) {
NodeRelation nr;
for (int i = 0; i < nodeRelationList.$length(); i++) {
nr = nodeRelationList.$get(i);
if (nodeRelation.getSource().getId()==nr.getSource().getId() &&
nodeRelation.getTarget().getId()==nr.getTarget().getId() &&
nodeRelation.getType()==nr.getType()) return true;
}
return false;
}
//DEBUGING****************************************************************************************************************************
//cant use StringBuffer with stjs...
public String toStringGraphAll() {
String ret = "";
Node n;
for (int i = 0; i < nodeList.$length(); i++) {
n = nodeList.$get(i);
ret = ret + n.toString() + "\n";
}
NodeRelation nr;
for (int i = 0; i < relationList.$length(); i++) {
nr = relationList.$get(i);
ret = ret + nr.toString() + "\n";
}
return ret;
}
//cant use StringBuffer with stjs...
public String toStringGraphByNode() {
String ret = "";
ret = ret + " - TEST HOWDY - \n";
Node n;
Array nra;
NodeRelation nr;
for (int i = 0; i < nodeList.$length(); i++) {
n = nodeList.$get(i);
ret = ret + " --> " + n.toString() + "\n";
if (nodeHasRelations(n)) {
nra = getRelationListForNode(n);
for (int j = 0; j < nra.$length(); j++) {
nr = nra.$get(j);
ret = ret + "\t\t" + nr.toString() + "\n";
}
} else ret = ret + "\t\t---------NO RELATIONSHIPS---------" + "\n";
}
return ret;
}
//cant use StringBuffer with stjs...
public String toStringGraphByNodeSplit() {
String ret = "";
Node n;
Array nra;
NodeRelation nr;
for (int i = 0; i < nodeList.$length(); i++) {
n = nodeList.$get(i);
ret = ret + " --> " + n.toString() + "\n";
if (nodeHasRelations(n)) {
ret = ret + "\t\t=== Narrows/isRequiredBy ===" + "\n";
nra = getNarrowsIsRequiredByEqualsRelationListForNode(n);
for (int j = 0; j < nra.$length(); j++) {
nr = nra.$get(j);
ret = ret + "\t\t" + nr.toString() + "\n";
}
ret = ret + "\t\t=== Broadens/Requires ===" + "\n";
nra = getBroadensRequiresEqualsRelationListForNode(n);
for (int j = 0; j < nra.$length(); j++) {
nr = nra.$get(j);
ret = ret + "\t\t" + nr.toString() + "\n";
}
} else ret = ret + "\t\t---------NO RELATIONSHIPS---------" + "\n";
}
return ret;
}
}