Search in sources :

Example 1 with Graph

use of org.jgrapht.Graph in project waltz by khartec.

the class ShortestPath method main.

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    LogicalFlowDao logicalFlowDao = ctx.getBean(LogicalFlowDao.class);
    ApplicationDao applicationDao = ctx.getBean(ApplicationDao.class);
    List<LogicalFlow> allActive = logicalFlowDao.findAllActive();
    Graph<EntityReference, DefaultEdge> g = createGraph(allActive);
    Application targetApp = findFirstMatchByCode(applicationDao, targetAssetCode);
    Stream.of(sourceAssetCodes).map(assetCode -> findFirstMatchByCode(applicationDao, assetCode)).filter(Objects::nonNull).map(sourceApp -> {
        System.out.printf("Route from: %s (%s)\n----------------------\n", sourceApp.name(), ExternalIdValue.orElse(sourceApp.assetCode(), ""));
        return sourceApp.entityReference();
    }).filter(sourceRef -> {
        if (!g.containsVertex(sourceRef)) {
            System.out.println("No flows defined for application\n\n");
            return false;
        }
        return true;
    }).map(sourceRef -> findShortestPath(g, sourceRef, targetApp.entityReference())).filter(route -> {
        if (route == null) {
            System.out.println("No route found\n\n");
            return false;
        }
        return true;
    }).forEach(route -> {
        List<DefaultEdge> edgeList = route.getEdgeList();
        Set<Long> appIds = edgeList.stream().flatMap(e -> Stream.of(g.getEdgeSource(e).id(), g.getEdgeTarget(e).id())).collect(toSet());
        Map<Long, Application> appsById = MapUtilities.indexBy(a -> a.id().get(), applicationDao.findByIds(appIds));
        edgeList.forEach(edge -> {
            Application source = appsById.get(g.getEdgeSource(edge).id());
            Application target = appsById.get(g.getEdgeTarget(edge).id());
            System.out.printf("%s (%s) -> %s (%s) \n", source.name(), ExternalIdValue.orElse(source.assetCode(), ""), target.name(), ExternalIdValue.orElse(target.assetCode(), ""));
        });
        System.out.println();
        System.out.println();
    });
}
Also used : MapUtilities(org.finos.waltz.common.MapUtilities) LogicalFlowDao(org.finos.waltz.data.logical_flow.LogicalFlowDao) LogicalFlow(org.finos.waltz.model.logical_flow.LogicalFlow) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) Application(org.finos.waltz.model.application.Application) Set(java.util.Set) DIConfiguration(org.finos.waltz.service.DIConfiguration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DijkstraShortestPath(org.jgrapht.alg.shortestpath.DijkstraShortestPath) Objects(java.util.Objects) ApplicationDao(org.finos.waltz.data.application.ApplicationDao) GraphPath(org.jgrapht.GraphPath) List(java.util.List) ExternalIdValue(org.finos.waltz.model.external_identifier.ExternalIdValue) Stream(java.util.stream.Stream) SingleSourcePaths(org.jgrapht.alg.interfaces.ShortestPathAlgorithm.SingleSourcePaths) Map(java.util.Map) Graph(org.jgrapht.Graph) EntityReference(org.finos.waltz.model.EntityReference) Collectors.toSet(java.util.stream.Collectors.toSet) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DefaultEdge(org.jgrapht.graph.DefaultEdge) ApplicationDao(org.finos.waltz.data.application.ApplicationDao) LogicalFlowDao(org.finos.waltz.data.logical_flow.LogicalFlowDao) LogicalFlow(org.finos.waltz.model.logical_flow.LogicalFlow) EntityReference(org.finos.waltz.model.EntityReference) Application(org.finos.waltz.model.application.Application)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Stream (java.util.stream.Stream)1 MapUtilities (org.finos.waltz.common.MapUtilities)1 ApplicationDao (org.finos.waltz.data.application.ApplicationDao)1 LogicalFlowDao (org.finos.waltz.data.logical_flow.LogicalFlowDao)1 EntityReference (org.finos.waltz.model.EntityReference)1 Application (org.finos.waltz.model.application.Application)1 ExternalIdValue (org.finos.waltz.model.external_identifier.ExternalIdValue)1 LogicalFlow (org.finos.waltz.model.logical_flow.LogicalFlow)1 DIConfiguration (org.finos.waltz.service.DIConfiguration)1 Graph (org.jgrapht.Graph)1 GraphPath (org.jgrapht.GraphPath)1 SingleSourcePaths (org.jgrapht.alg.interfaces.ShortestPathAlgorithm.SingleSourcePaths)1 DijkstraShortestPath (org.jgrapht.alg.shortestpath.DijkstraShortestPath)1 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1