Commit 6d875e93 authored by DominikE's avatar DominikE

Build dependency graph on load

parent 2f972c37
Pipeline #2523 passed with stage
in 41 seconds
......@@ -26,6 +26,7 @@ import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.traverse.TopologicalOrderIterator;
import edu.tigers.moduli.exceptions.DependencyException;
import edu.tigers.moduli.exceptions.InitModuleException;
import edu.tigers.moduli.exceptions.LoadModulesException;
import edu.tigers.moduli.exceptions.ModuleNotFoundException;
......@@ -42,6 +43,7 @@ public class Moduli
{
private static final Logger log = Logger.getLogger(Moduli.class.getName());
private final Map<Class<? extends AModule>, AModule> modules = new HashMap<>();
private List<AModule> orderedModules = new ArrayList<>();
private SubnodeConfiguration globalConfiguration;
private ModulesStateVariable modulesState = new ModulesStateVariable();
private XMLConfiguration config;
......@@ -87,13 +89,15 @@ public class Moduli
* @param xmlFile (module-)configuration-file
* @throws LoadModulesException an error occurs... Can't continue.
*/
public void loadModules(final String xmlFile) throws LoadModulesException
public void loadModules(final String xmlFile) throws LoadModulesException, DependencyException
{
modules.clear();
modulesState.set(ModulesState.NOT_LOADED);
loadModulesFromFile(xmlFile);
DirectedGraph<AModule, DefaultEdge> dependencyGraph = buildDependencyGraph();
new TopologicalOrderIterator<>(dependencyGraph).forEachRemaining(orderedModules::add);
modulesState.set(ModulesState.RESOLVED);
}
......@@ -214,10 +218,9 @@ public class Moduli
{
try
{
// --- get modules from configuration-file ---
loadModules(filename);
log.debug("Loaded config: " + filename);
} catch (final LoadModulesException e)
} catch (final LoadModulesException | DependencyException e)
{
log.error(e.getMessage() + " (moduleConfigFile: '" + filename
+ "') ", e);
......@@ -234,9 +237,6 @@ public class Moduli
@SuppressWarnings("unchecked")
public void startModules() throws InitModuleException, StartModuleException
{
DirectedGraph<AModule, DefaultEdge> dependencyGraph = buildDependencyGraph();
List<AModule> orderedModules = new ArrayList<>();
new TopologicalOrderIterator<>(dependencyGraph).forEachRemaining(orderedModules::add);
initModules(orderedModules);
startUpModules(orderedModules);
......@@ -245,7 +245,7 @@ public class Moduli
}
private DirectedGraph<AModule, DefaultEdge> buildDependencyGraph() throws InitModuleException
private DirectedGraph<AModule, DefaultEdge> buildDependencyGraph() throws DependencyException
{
try
{
......@@ -258,7 +258,7 @@ public class Moduli
AModule dependency = modules.get(dependencyId);
if (dependency == null)
{
throw new InitModuleException(
throw new DependencyException(
"Dependency " + dependencyId + " is required by " + module + ", but not started.");
}
dependencyGraph.addVertex(dependency);
......@@ -268,7 +268,7 @@ public class Moduli
return dependencyGraph;
} catch (IllegalArgumentException e)
{
throw new InitModuleException("Cycle in dependencies: ", e);
throw new DependencyException("Cycle in dependencies: ", e);
}
}
......
......@@ -26,4 +26,14 @@ public class DependencyException extends Exception
{
super(msg);
}
/**
* @param message
* @param cause
*/
public DependencyException(final String message, final Throwable cause)
{
super(message, cause);
}
}
......@@ -10,7 +10,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import edu.tigers.moduli.exceptions.InitModuleException;
import edu.tigers.moduli.exceptions.DependencyException;
import edu.tigers.moduli.listenerVariables.ModulesState;
import edu.tigers.moduli.modules.ConfiguredTestModule;
import edu.tigers.moduli.modules.TestModule;
......@@ -105,19 +105,17 @@ public class ModuliTest
}
@Test(expected = InitModuleException.class)
@Test(expected = DependencyException.class)
public void testCyclicConfiguration() throws Exception
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "cyclic_config.xml");
moduli.startModules();
moduli.loadModules(MODULE_CONFIG_PATH + "cyclic_config.xml");
}
@Test(expected = InitModuleException.class)
@Test(expected = DependencyException.class)
public void testUnresolvedDependencyConfiguration() throws Exception
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "unresolved_dependency_config.xml");
moduli.startModules();
moduli.loadModules(MODULE_CONFIG_PATH + "unresolved_dependency_config.xml");
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment