Skip to content

Commit

Permalink
[EAK-427] Fixed Topological sort
Browse files Browse the repository at this point in the history
  • Loading branch information
AlKaliada committed Sep 19, 2023
1 parent f194305 commit 14e1fe3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ public static <T> List<T> sortHandlers(List<T> handlers) {
if (!_Default.class.equals(handles.before())) {
Orderable<T> before = findSibling(handles.before().getName(), orderableHandlers);
orderableHandlers.get(i).getBefore().add(before);
if (before != null) {
before.getAfter().add(orderableHandlers.get(i));
}
}
if (!_Default.class.equals(handles.after())) {
Orderable<T> after = findSibling(handles.after().getName(), orderableHandlers);
orderableHandlers.get(i).getAfter().add(after);
if (after != null) {
after.getBefore().add(0, orderableHandlers.get(i));
}
}
}

Expand Down Expand Up @@ -122,7 +128,7 @@ public static List<Source> sortMembers(List<Source> sources) {
list);
list.get(i).getBefore().add(before);
if (before != null) {
before.getAfter().add(0, list.get(i));
before.getAfter().add(list.get(i));
}
}
ClassMember classMemberAfter = place.after();
Expand All @@ -135,7 +141,7 @@ public static List<Source> sortMembers(List<Source> sources) {
list);
list.get(i).getAfter().add(after);
if (after != null) {
after.getBefore().add(list.get(i));
after.getBefore().add(0, list.get(i));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ private Deque<Orderable<T>> after(Orderable<T> orderable, List<Orderable<T>> val
while (!after.isEmpty()) {
deque.addFirst(after.removeLast());
}
Deque<Orderable<T>> before = before(orderable1, values);
while (!before.isEmpty()) {
Orderable<T> tOrderable = before.removeFirst();
if (!deque.contains(tOrderable)) {
deque.addLast(tOrderable);
}
}
}
}
deque.addLast(orderable);
if (!deque.contains(orderable)) {
deque.addLast(orderable);
}
return deque;
}

Expand All @@ -113,13 +122,22 @@ private Deque<Orderable<T>> before(Orderable<T> orderable, List<Orderable<T>> va
for (Orderable<T> orderable1 : orderable.getBefore()) {
if (!values.contains(orderable1)) {
values.add(orderable1);
Deque<Orderable<T>> after = after(orderable1, values);
while (!after.isEmpty()) {
deque.addFirst(after.removeLast());
}
Deque<Orderable<T>> before = before(orderable1, values);
while (!before.isEmpty()) {
deque.addLast(before.removeFirst());
Orderable<T> tOrderable = before.removeFirst();
if (!deque.contains(tOrderable)) {
deque.addLast(tOrderable);
}
}
}
}
deque.addFirst(orderable);
if (!deque.contains(orderable)) {
deque.addFirst(orderable);
}
return deque;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void accept(Source source, Target target) {
}
}

@Handles(value = WidgetAnnotationForOrderingTest.class, before = CustomHandler3.class, after = CustomHandler1.class)
@Handles(value = WidgetAnnotationForOrderingTest.class, before = CustomHandler2.class, after = CustomHandler1.class)
public static class CustomHandler1 implements Handler {

@Override
Expand All @@ -78,7 +78,7 @@ public void accept(Source source, Target target) {
}
}

@Handles(value = WidgetAnnotationForOrderingTest.class, before = CustomHandler0.class, after = CustomHandler3.class)
@Handles(value = WidgetAnnotationForOrderingTest.class, after = CustomHandler3.class)
public static class CustomHandler3 implements Handler {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public class TopologicalSorterTest {

private static final List<String> CYCLED_GRAPH_SEQUENCE_2 = Arrays.asList(
"Handler0",
"Handler1",
"Handler6",
"Handler5",
"Handler2",
"Handler3",
"Handler4",
"Handler5",
"Handler6");
"Handler1");

private static final List<String> CYCLED_GRAPH_SEQUENCE_1 = Arrays.asList(
"Handler0",
"Handler1",
"Handler2",
"Handler3",
"Handler1",
"Handler4");

private static final List<String> SIMPLE_CYCLED_GRAPH_SEQUENCE = Arrays.asList(
Expand All @@ -62,10 +62,10 @@ public class TopologicalSorterTest {
"Handler1",
"Handler3",
"Handler2",
"Handler4",
"Handler6",
"Handler0",
"Handler7",
"Handler4",
"Handler0",
"Handler9",
"Handler5",
"Handler8");
Expand Down Expand Up @@ -98,19 +98,19 @@ public void testGraphWithCycleInside1() {
List<Orderable<String>> list = getList(5);

list.get(0).getBefore().add(list.get(1));
list.get(1).getAfter().add(0, list.get(0));
list.get(1).getAfter().add(list.get(0));

list.get(1).getBefore().add(list.get(2));
list.get(2).getAfter().add(0, list.get(1));
list.get(2).getAfter().add(list.get(1));

list.get(2).getBefore().add(list.get(3));
list.get(3).getAfter().add(0, list.get(2));
list.get(3).getAfter().add(list.get(2));

list.get(3).getBefore().add(list.get(1));
list.get(1).getAfter().add(0, list.get(3));
list.get(1).getAfter().add(list.get(3));

list.get(4).getAfter().add(list.get(1));
list.get(1).getBefore().add(list.get(4));
list.get(1).getBefore().add(0, list.get(4));

List<String> answer = getSortedByValues(list);

Expand All @@ -123,25 +123,25 @@ public void testGraphWithCycleInside2() {
List<Orderable<String>> list = getList(7);

list.get(0).getBefore().add(list.get(1));
list.get(1).getAfter().add(0, list.get(0));
list.get(1).getAfter().add(list.get(0));

list.get(1).getBefore().add(list.get(2));
list.get(2).getAfter().add(0, list.get(1));
list.get(2).getAfter().add(list.get(1));

list.get(1).getAfter().add(list.get(3));
list.get(3).getBefore().add(list.get(1));
list.get(3).getBefore().add(0, list.get(1));

list.get(2).getBefore().add(list.get(3));
list.get(3).getAfter().add(0, list.get(2));
list.get(3).getAfter().add(list.get(2));

list.get(3).getBefore().add(list.get(4));
list.get(4).getAfter().add(0, list.get(3));
list.get(4).getAfter().add(list.get(3));

list.get(5).getBefore().add(list.get(1));
list.get(1).getAfter().add(0, list.get(5));
list.get(1).getAfter().add(list.get(5));

list.get(6).getBefore().add(list.get(1));
list.get(1).getAfter().add(0, list.get(6));
list.get(1).getAfter().add(list.get(6));

List<String> answer = getSortedByValues(list);

Expand All @@ -167,25 +167,25 @@ public void testExampleGraph1() {
List<Orderable<String>> list = getList(10);

list.get(1).getBefore().add(list.get(0));
list.get(0).getAfter().add(0, list.get(1));
list.get(0).getAfter().add(list.get(1));

list.get(2).getBefore().add(list.get(6));
list.get(6).getAfter().add(0, list.get(2));
list.get(6).getAfter().add(list.get(2));

list.get(3).getBefore().add(list.get(2));
list.get(2).getAfter().add(0, list.get(3));
list.get(2).getAfter().add(list.get(3));

list.get(4).getAfter().add(list.get(2));
list.get(2).getBefore().add(list.get(4));
list.get(2).getBefore().add(0, list.get(4));

list.get(6).getBefore().add(list.get(0));
list.get(0).getAfter().add(0, list.get(6));
list.get(0).getAfter().add(list.get(6));

list.get(7).getAfter().add(list.get(6));
list.get(6).getBefore().add(list.get(7));
list.get(6).getBefore().add(0, list.get(7));

list.get(9).getBefore().add(list.get(5));
list.get(5).getAfter().add(0, list.get(9));
list.get(5).getAfter().add(list.get(9));

List<String> answerValues = getSortedByValues(list);

Expand All @@ -207,10 +207,10 @@ public void testExampleGraph3() {
List<Orderable<String>> list = getList(4);

list.get(2).getBefore().add(list.get(0));
list.get(0).getAfter().add(0, list.get(2));
list.get(0).getAfter().add( list.get(2));

list.get(3).getBefore().add(list.get(0));
list.get(0).getAfter().add(0, list.get(3));
list.get(0).getAfter().add(list.get(3));

List<String> answerValues = getSortedByValues(list);

Expand All @@ -222,10 +222,10 @@ public void testExampleGraph4() {
List<Orderable<String>> list = getList(4);

list.get(2).getAfter().add(list.get(0));
list.get(0).getBefore().add(list.get(2));
list.get(0).getBefore().add(0, list.get(2));

list.get(3).getAfter().add(list.get(0));
list.get(0).getBefore().add(list.get(3));
list.get(0).getBefore().add(0, list.get(3));

List<String> answerValues = getSortedByValues(list);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
<items jcr:primaryType="nt:unstructured">
<field1 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field3 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field8 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field9 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field2 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field5 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field4 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field6 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field0 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field7 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field0 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field9 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field5 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
<field8 jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
</items>
</column>
</items>
Expand Down

0 comments on commit 14e1fe3

Please sign in to comment.