Search in sources :

Example 1 with DependencyGraphException

use of com.voxelgameslib.voxelgameslib.exception.DependencyGraphException in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method checkDependencies.

private boolean checkDependencies() {
    List<Class<? extends Feature>> orderedFeatures = new ArrayList<>();
    List<Class<? extends Feature>> added = new ArrayList<>();
    List<Class<? extends Feature>> missingSoftDependencies = new ArrayList<>();
    try {
        Graph<Class<? extends Feature>> graph = new Graph<>(orderedFeatures::add);
        // add all dependencies to the graph
        for (Feature feature : getFeatures()) {
            for (Class<? extends Feature> dependency : feature.getDependencies()) {
                if (dependency.equals(feature.getClass())) {
                    log.severe(feature.getName() + " tried to depend on itself...");
                    continue;
                }
                graph.addDependency(feature.getClass(), dependency);
                added.add(feature.getClass());
                added.add(dependency);
                try {
                    getFeature(dependency);
                } catch (NoSuchFeatureException ex) {
                    log.severe("could not find dependency " + dependency.getName() + " for feature " + feature.getClass().getName() + " in phase " + getName());
                    return false;
                }
            }
            for (Class<? extends Feature> dependency : feature.getSoftDependencies()) {
                if (dependency.equals(feature.getClass())) {
                    log.severe(feature.getName() + " tried to depend on itself...");
                    continue;
                }
                graph.addDependency(feature.getClass(), dependency);
                added.add(feature.getClass());
                added.add(dependency);
                try {
                    getFeature(dependency);
                } catch (NoSuchFeatureException ex) {
                    missingSoftDependencies.add(dependency);
                }
            }
        }
        // add features that have no dependency connection to any other feature. they can't be left out alone!
        for (Feature feature : getFeatures()) {
            if (!added.contains(feature.getClass())) {
                orderedFeatures.add(feature.getClass());
            }
        }
        added.clear();
        if (graph.size() != 0) {
            // do the magic! (but only if there are actually nodes on the graph)
            graph.generateDependencies();
        }
    } catch (DependencyGraphException ex) {
        log.severe("error while trying to generate dependency graph: " + ex.getMessage());
        ex.printStackTrace();
        return false;
    }
    // no need to keep stuff that isn't present
    orderedFeatures.removeAll(missingSoftDependencies);
    if (features.size() != orderedFeatures.size()) {
        throw new RuntimeException("WTF HAPPENED HERE?!" + features.size() + " " + orderedFeatures.size());
    }
    // reverse order because dependencies need to be run before dependend features
    Collections.reverse(orderedFeatures);
    // remap classes to features
    features = orderedFeatures.stream().map((Function<Class, Feature>) this::getFeature).collect(Collectors.toList());
    return true;
}
Also used : Function(java.util.function.Function) Graph(com.voxelgameslib.voxelgameslib.graph.Graph) NoSuchFeatureException(com.voxelgameslib.voxelgameslib.exception.NoSuchFeatureException) DependencyGraphException(com.voxelgameslib.voxelgameslib.exception.DependencyGraphException) ArrayList(java.util.ArrayList) Feature(com.voxelgameslib.voxelgameslib.feature.Feature)

Aggregations

DependencyGraphException (com.voxelgameslib.voxelgameslib.exception.DependencyGraphException)1 NoSuchFeatureException (com.voxelgameslib.voxelgameslib.exception.NoSuchFeatureException)1 Feature (com.voxelgameslib.voxelgameslib.feature.Feature)1 Graph (com.voxelgameslib.voxelgameslib.graph.Graph)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1