Issue #552 Timeout exception, max recursion level fix => fixGraph removed

This commit is contained in:
Jindra Petk
2014-03-29 09:50:50 +01:00
parent b7df434e48
commit 7ffb188800

View File

@@ -76,8 +76,6 @@ public class Graph {
List<GraphPart> visited = new ArrayList<>();
for (GraphPart head : heads) {
time = head.setTime(time, ordered, visited);
fixGraph(localData, head);
makeMulti(head, new ArrayList<GraphPart>());
}
}
@@ -91,181 +89,6 @@ public class Graph {
}
}
private void fixGraph(BaseLocalData localData, GraphPart part) throws InterruptedException {
//if(true) return;
try {
while (fixGraphOnce(localData, part, new ArrayList<GraphPart>(), false)) {
}
} catch (InterruptedException ex) {
throw ex;
} catch (Exception | Error ex) {
String s = ex.toString();
//ignore
}
}
private boolean fixGraphOnce(BaseLocalData localData, GraphPart part, List<GraphPart> visited, boolean doChildren) throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
if (visited.contains(part)) {
return false;
}
visited.add(part);
boolean fixed = false;
int i = 0;
GraphPath lastpref;
boolean modify = true;
int prvni = -1;
if (!doChildren) {
List<GraphPart> uniqueRefs = new ArrayList<>();
for (GraphPart r : part.refs) {
if (!uniqueRefs.contains(r)) {
uniqueRefs.add(r);
}
}
loopi:
for (; i <= part.path.length(); i++) {
lastpref = null;
int pos = -1;
for (GraphPart r : uniqueRefs) {
pos++;
if (r.path.rootName.equals("e") && !part.path.rootName.equals("e")) {
continue;
}
if (part.leadsTo(localData, this, code, r, new ArrayList<Loop>())) {
modify = false;
continue;
}
prvni = pos;
if (i > r.path.length()) {
i--;
break loopi;
}
if (lastpref == null) {
lastpref = r.path.parent(i);
} else {
if (!r.path.startsWith(lastpref)) {
i--;
break loopi;
}
}
}
}
if (i > part.path.length()) {
i = part.path.length();
}
if (modify && ((uniqueRefs.size() > 1) && (prvni >= 0))) {
GraphPath prvniUniq = uniqueRefs.get(prvni).path;
GraphPath newpath = prvniUniq.parent(i);
if (!part.path.equals(newpath)) {
if (part.path.startsWith(newpath) && ((newpath.length() == prvniUniq.length()) || (prvniUniq.getKey(newpath.length()) == part.path.getKey(newpath.length())))) {
GraphPath origPath = part.path;
GraphPart p = part;
part.path = newpath;
while (p.nextParts.size() == 1) {
p = p.nextParts.get(0);
if (!p.path.equals(origPath)) {
break;
}
p.path = newpath;
}
fixGraphOnce(localData, part, new ArrayList<GraphPart>(), true);
fixed = true;
}
}
}
} else {
if (!fixed) {
if (part.nextParts.size() == 1) {
if (!(part.path.rootName.equals("e") && (!part.nextParts.get(0).path.rootName.equals("e")))) {
if (part.nextParts.get(0).path.length() > part.path.length()) {
part.nextParts.get(0).path = part.path;
fixed = true;
}
}
}
if (part.nextParts.size() > 1) {
for (int j = 0; j < part.nextParts.size(); j++) {
GraphPart npart = part.nextParts.get(j);
if (npart.path.length() > part.path.length() + 1) {
npart.path = part.path.sub(j, part.end);
fixed = true;
}
}
}
}
}
if (part.nextParts.size() == 2) {
if (part.nextParts.get(1).leadsTo(localData, this, code, part.nextParts.get(0), new ArrayList<Loop>() /*visited*/)) {
fixGraphOnce(localData, part.nextParts.get(1), visited, doChildren);
fixGraphOnce(localData, part.nextParts.get(0), visited, doChildren);
} else {
fixGraphOnce(localData, part.nextParts.get(0), visited, doChildren);
fixGraphOnce(localData, part.nextParts.get(1), visited, doChildren);
}
} else {
for (int j = part.nextParts.size() - 1; j >= 0; j--) {
GraphPart p = part.nextParts.get(j);
fixGraphOnce(localData, p, visited, doChildren);
}
}
return fixed;
}
private void makeMulti(GraphPart part, List<GraphPart> visited) {
if (true) {
return;
}
if (visited.contains(part)) {
return;
}
visited.add(part);
GraphPart p = part;
List<GraphPart> multiList = new ArrayList<>();
multiList.add(p);
while ((p.nextParts.size() == 1) && (p.nextParts.get(0).refs.size() == 1)) {
p = p.nextParts.get(0);
multiList.add(p);
}
if (multiList.size() > 1) {
GraphPartMulti gpm = new GraphPartMulti(multiList);
gpm.refs = part.refs;
GraphPart lastPart = multiList.get(multiList.size() - 1);
gpm.nextParts = lastPart.nextParts;
for (GraphPart next : gpm.nextParts) {
int index = next.refs.indexOf(lastPart);
if (index == -1) {
continue;
}
next.refs.remove(lastPart);
next.refs.add(index, gpm);
}
for (GraphPart parent : part.refs) {
if (parent.start == -1) {
continue;
}
int index = parent.nextParts.indexOf(part);
if (index == -1) {
continue;
}
parent.nextParts.remove(part);
parent.nextParts.add(index, gpm);
}
}
for (int i = 0; i < part.nextParts.size(); i++) {
makeMulti(part.nextParts.get(i), visited);
}
}
public GraphPart deepCopy(GraphPart part, List<GraphPart> visited, List<GraphPart> copies) {
if (visited == null) {