Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #1014

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion com.avaloq.tools.ddk.xtext.expression/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Require-Bundle: org.eclipse.xtext,
org.eclipse.jdt.launching,
org.eclipse.core.resources,
org.eclipse.core.runtime,
org.eclipse.xtext.xbase.lib;bundle-version="2.14.0"
org.eclipse.xtext.xbase.lib,
com.avaloq.tools.ddk
Import-Package: org.apache.logging.log4j,
org.apache.log4j
Export-Package: com.avaloq.tools.ddk.xtext.expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;

import com.avaloq.tools.ddk.util.Graph;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Bundle-Vendor: Avaloq Group AG
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Fragment-Host: com.avaloq.tools.ddk.xtext.generator
Require-Bundle: com.avaloq.tools.ddk.test.core,
Require-Bundle: com.avaloq.tools.ddk.test.core,
com.avaloq.tools.ddk.xtext.expression,
com.avaloq.tools.ddk.xtext.test.core,
org.eclipse.xtend,
Expand All @@ -16,7 +16,8 @@ Require-Bundle: com.avaloq.tools.ddk.test.core,
org.junit,
org.mockito,
com.avaloq.tools.ddk.xtext.ui,
org.eclipse.xtext.xtext.generator
org.eclipse.xtext.xtext.generator,
com.avaloq.tools.ddk
Export-Package: com.avaloq.tools.ddk.xtext.generator.test.generator
Import-Package: org.eclipse.xtext.ui.resource
Automatic-Module-Name: com.avaloq.tools.ddk.xtext.generator.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@

import org.junit.Test;

import com.avaloq.tools.ddk.xtext.expression.generator.Graph;
import com.avaloq.tools.ddk.util.Graph;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;


@SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage")
@SuppressWarnings({"nls", "PMD.JUnitAssertionsShouldIncludeMessage"})
public class GraphTest {
// CHECKSTYLE:CONSTANTS-OFF

private static Graph<String> create(final Multimap<String, String> graph) {
Graph<String> g = new Graph<String>();
for (Map.Entry<String, String> entry : graph.entries()) {
String from = entry.getKey();
String to = entry.getValue();
g.addNode(from);
g.addNode(to);
g.addEdge(from, to);
}
return g;
}

@Test
public void testTopologicalSorting() {
Multimap<String, String> graph = HashMultimap.create();
Expand All @@ -42,7 +54,7 @@ public void testTopologicalSorting() {
graph.put("8", "9");
graph.put("8", "10");

List<String> sorted = Graph.create(graph).sort();
List<String> sorted = create(graph).sort();
assertSorting(graph, sorted);
}

Expand All @@ -51,7 +63,7 @@ public void testDependencyCycle() {
Multimap<String, String> graph = ImmutableMultimap.of("1", "2", "2", "3", "3", "1");

try {
Graph.create(graph).sort();
create(graph).sort();
fail();
} catch (IllegalStateException e) {
assertTrue(true); // NOPMD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
* Contributors:
* Avaloq Group AG - initial API and implementation
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.expression.generator;
package com.avaloq.tools.ddk.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;


/**
* A generic graph which can be topologically sorted.
Expand All @@ -38,8 +35,8 @@ public class Graph<T> {
*/
private static class Node<T> {
private final T ref;
private final Set<Node<T>> inEdges = Sets.newLinkedHashSet();
private final Set<Node<T>> outEdges = Sets.newLinkedHashSet();
private final Set<Node<T>> inEdges = new LinkedHashSet<>();
private final Set<Node<T>> outEdges = new LinkedHashSet<>();

Node(final T ref) {
this.ref = ref;
Expand All @@ -64,7 +61,7 @@ public String toString() {
}
}

private final Map<T, Node<T>> nodes = Maps.newLinkedHashMap();
private final Map<T, Node<T>> nodes = new LinkedHashMap<>();

/**
* Helper method to create a new graph.
Expand All @@ -83,27 +80,6 @@ public static <T> Graph<T> create(final Iterable<T> nodes) {
return g;
}

/**
* Helper method to create a new graph.
*
* @param <T>
* node type
* @param graph
* graph as multiset where values represent targets of edges with key as source
* @return graph
*/
public static <T> Graph<T> create(final Multimap<T, T> graph) {
Graph<T> g = new Graph<T>();
for (Map.Entry<T, T> entry : graph.entries()) {
T from = entry.getKey();
T to = entry.getValue();
g.addNode(from);
g.addNode(to);
g.addEdge(from, to);
}
return g;
}

/**
* Adds a node to this graph.
*
Expand Down Expand Up @@ -140,13 +116,11 @@ public Graph<T> addEdge(final T from, final T to) {
* @return sorted graph
*/
public List<T> sort() {
// TODO try to sort to something as close as possible to the original

// L <- Empty list that will contain the sorted elements
List<Node<T>> l = Lists.newArrayList();
List<Node<T>> l = new ArrayList<>();

// S <- Set of all nodes with no incoming edges
Set<Node<T>> s = Sets.newLinkedHashSet();
Set<Node<T>> s = new LinkedHashSet<>();
for (Node<T> n : nodes.values()) {
if (n.inEdges.isEmpty()) {
s.add(n);
Expand Down Expand Up @@ -180,16 +154,11 @@ public List<T> sort() {
// Check to see if all edges are removed
for (Node<T> n : nodes.values()) {
if (!n.inEdges.isEmpty()) {
throw new IllegalStateException("Cycle present, topological sort not possible: " + n.ref + " -> " + n.inEdges);
throw new IllegalStateException("Cycle present, topological sort not possible: " + n.ref + " -> " + n.inEdges); //$NON-NLS-1$ //$NON-NLS-2$
}
}

return Lists.transform(l, new Function<Node<T>, T>() {
@Override
public T apply(final Node<T> from) {
return from.ref;
}
});
return l.stream().map(f -> f.ref).toList();
}

}