All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openl.rules.dependency.graph.DependencyRulesGraph Maven / Gradle / Ivy

There is a newer version: 5.27.9
Show newest version
package org.openl.rules.dependency.graph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.jgrapht.DirectedGraph;
import org.jgrapht.EdgeFactory;
import org.jgrapht.graph.DefaultDirectedGraph;

import org.openl.binding.BindingDependencies;
import org.openl.exception.OpenlNotCheckedException;
import org.openl.types.IOpenMethod;
import org.openl.types.impl.ExecutableMethod;

/**
 * Dependency rules graph, implemented using {@link org.jgrapht.graph.DefaultDirectedGraph}. Vertexes are represented as
 * {@link ExecutableMethod}, edges {@link DirectedEdge}.
 *
 * @author DLiauchuk
 */
public class DependencyRulesGraph implements DirectedGraph> {

    private DirectedGraph> graph;

    public DependencyRulesGraph(List rulesMethods) {
        createGraph();
        fill(rulesMethods);
    }

    public DependencyRulesGraph() {
        createGraph();
    }

    private void createGraph() {
        EdgeFactory> edgeFactory = new DirectedEdgeFactory<>(
                DirectedEdge.class);
        graph = new DefaultDirectedGraph<>(edgeFactory);
    }

    private void fill(List rulesMethods) {
        if (rulesMethods != null && !rulesMethods.isEmpty()) {
            for (ExecutableMethod method : rulesMethods) {
                graph.addVertex(method);
                BindingDependencies dependencies = method.getDependencies();
                if (dependencies != null) {
                    Set dependentMethods = dependencies.getRulesMethods();
                    if (dependentMethods != null) {
                        for (ExecutableMethod dependentMethod : dependentMethods) {
                            graph.addVertex(dependentMethod);
                            graph.addEdge(method, dependentMethod);
                        }
                    }
                }
            }
        }
    }

    @Override
    public DirectedEdge addEdge(ExecutableMethod sourceVertex, ExecutableMethod targetVertex) {
        return graph.addEdge(sourceVertex, targetVertex);
    }

    @Override
    public boolean addEdge(ExecutableMethod sourceVertex,
                           ExecutableMethod targetVertex,
                           DirectedEdge e) {
        return graph.addEdge(sourceVertex, targetVertex, e);
    }

    @Override
    public boolean addVertex(ExecutableMethod v) {
        return graph.addVertex(v);
    }

    @Override
    public boolean containsEdge(DirectedEdge e) {
        return graph.containsEdge(e);
    }

    @Override
    public boolean containsEdge(ExecutableMethod sourceVertex, ExecutableMethod targetVertex) {
        return graph.containsEdge(sourceVertex, targetVertex);
    }

    @Override
    public boolean containsVertex(ExecutableMethod v) {
        return graph.containsVertex(v);
    }

    @Override
    public Set> edgeSet() {
        return graph.edgeSet();
    }

    @Override
    public Set> edgesOf(ExecutableMethod vertex) {
        return graph.edgesOf(vertex);
    }

    @Override
    public Set> getAllEdges(ExecutableMethod sourceVertex,
                                                           ExecutableMethod targetVertex) {
        return graph.getAllEdges(sourceVertex, targetVertex);
    }

    @Override
    public DirectedEdge getEdge(ExecutableMethod sourceVertex, ExecutableMethod targetVertex) {
        return graph.getEdge(sourceVertex, targetVertex);
    }

    @Override
    public EdgeFactory> getEdgeFactory() {
        return graph.getEdgeFactory();
    }

    @Override
    public ExecutableMethod getEdgeSource(DirectedEdge e) {
        return graph.getEdgeSource(e);
    }

    @Override
    public ExecutableMethod getEdgeTarget(DirectedEdge e) {
        return graph.getEdgeTarget(e);
    }

    @Override
    public double getEdgeWeight(DirectedEdge e) {
        return graph.getEdgeWeight(e);
    }

    @Override
    public boolean removeAllEdges(Collection> edges) {
        return graph.removeAllEdges(edges);
    }

    @Override
    public Set> removeAllEdges(ExecutableMethod sourceVertex,
                                                              ExecutableMethod targetVertex) {
        return graph.removeAllEdges(sourceVertex, targetVertex);
    }

    @Override
    public boolean removeAllVertices(Collection vertices) {
        return graph.removeAllVertices(vertices);
    }

    @Override
    public boolean removeEdge(DirectedEdge e) {
        return graph.removeEdge(e);
    }

    @Override
    public DirectedEdge removeEdge(ExecutableMethod sourceVertex, ExecutableMethod targetVertex) {
        return graph.removeEdge(sourceVertex, targetVertex);
    }

    @Override
    public boolean removeVertex(ExecutableMethod v) {
        return graph.removeVertex(v);
    }

    @Override
    public Set vertexSet() {
        return graph.vertexSet();
    }

    @Override
    public int inDegreeOf(ExecutableMethod arg0) {
        return graph.inDegreeOf(arg0);
    }

    @Override
    public Set> incomingEdgesOf(ExecutableMethod arg0) {
        return graph.incomingEdgesOf(arg0);
    }

    @Override
    public int outDegreeOf(ExecutableMethod arg0) {
        return graph.outDegreeOf(arg0);
    }

    @Override
    public Set> outgoingEdgesOf(ExecutableMethod arg0) {
        return graph.outgoingEdgesOf(arg0);
    }

    /**
     * Filter incoming methods, finding {@link ExecutableMethod} and create graph.
     *
     * @param methods {@link IOpenMethod}
     * @return {@link DependencyRulesGraph} graph representing dependencies between executable rules methods.
     */
    public static DependencyRulesGraph filterAndCreateGraph(Collection methods) {
        List rulesMethods = new ArrayList<>();
        if (methods != null && !methods.isEmpty()) {
            for (IOpenMethod method : methods) {
                if (method instanceof ExecutableMethod) {
                    rulesMethods.add((ExecutableMethod) method);
                }
            }
            return new DependencyRulesGraph(rulesMethods);
        } else {
            throw new OpenlNotCheckedException("There is no rules for building graph.");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy