From 1a57385d0382f4580f5baeeb8dbb1e1007f34051 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 23 Aug 2010 11:28:29 +0000 Subject: [PATCH] Better logic for creating graphs git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17464 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../dependencies/CreateDependencyGraph.java | 210 ++++++++++-------- 1 file changed, 115 insertions(+), 95 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/dependencies/CreateDependencyGraph.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/dependencies/CreateDependencyGraph.java index 4a287d55..2215ca01 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/dependencies/CreateDependencyGraph.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/dependencies/CreateDependencyGraph.java @@ -2,10 +2,12 @@ package org.simantics.sysdyn.ui.dependencies; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.graphviz.Edge; @@ -17,100 +19,118 @@ import org.simantics.sysdyn.SysdynResource; public class CreateDependencyGraph implements Read { - Resource root; - HashMap nodes; - boolean isInverted; - int levels; - - public CreateDependencyGraph(Resource root, int levels, boolean isInverted) { - this.root = root; - this.isInverted = isInverted; - this.levels = levels; - } - - @Override - public Graph perform(ReadGraph g) throws DatabaseException { - nodes = new HashMap(); - Graph graph = new Graph(); - graph.setRankdir("LR"); - ModelingResources mr = ModelingResources.getInstance(g); - Resource element = g.getPossibleObject(root, mr.ElementToComponent); - if (element != null) { - root = element; - } - SysdynResource sr = SysdynResource.getInstance(g); - if (g.isInstanceOf(root, sr.IndependentVariable) || g.isInstanceOf(root, sr.Input)) { - Collection resources = new ArrayList(); - resources.add(root); - for (int i = 0; i < levels && !resources.isEmpty(); i++) { - Collection newResources = new ArrayList(); - for(Resource r : resources) { - newResources.addAll(getDependencies(g, graph, r)); - } - resources = new ArrayList(newResources); - } - } - return graph; - } - - private Collection getDependencies(ReadGraph g, IGraph graph, Resource r) throws DatabaseException{ - Collection dependants = new ArrayList(); - SysdynResource sr = SysdynResource.getInstance(g); - Node n; - if (!nodes.containsKey(r)) { - n = new Node(graph, getName(g, r)); - setShape(g, r, n); - n.set("style", "filled"); - n.setFillColor("#d3d3d3"); - nodes.put(r, n); - } - // stop here if element is a module - if (g.isInstanceOf(r, sr.Module)) - return dependants; - - Resource headRelation = sr.IsHeadOf; - Resource tailRelation = sr.HasTail; - if(isInverted) { - headRelation = sr.IsTailOf; - tailRelation = sr.HasHead; - } - - Collection dependencies = g.getObjects(r, headRelation); - for(Resource d : dependencies) { - Resource dependant = g.getPossibleObject(d, tailRelation); - if(dependant == null) - continue; - if(g.isInstanceOf(dependant, sr.Cloud)) { - break; - } - if( !nodes.containsKey(dependant)) { - n= new Node(graph, getName(g, dependant)); - setShape(g, dependant, n); - nodes.put(dependant, n); - dependants.add(dependant); - } else { - n = nodes.get(dependant); - } - if(isInverted) - new Edge(graph, nodes.get(r), n); - else - new Edge(graph, n, nodes.get(r)); - } - return dependants; - } - - private void setShape(ReadGraph g, Resource r, Node n) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(g); - if(g.isInstanceOf(r, sr.Stock)) - n.setShape("rectangle"); - else if(g.isInstanceOf(r, sr.Module)) - n.setShape("rectangle"); - else - n.setShape("ellipse"); - } - - private String getName(ReadGraph g, Resource r) throws DatabaseException { - return (String)g.getRelatedValue(r, g.getBuiltins().HasName); - } + Resource root; + HashMap nodes; + boolean isInverted; + int levels; + + public CreateDependencyGraph(Resource root, int levels, boolean isInverted) { + this.root = root; + this.isInverted = isInverted; + this.levels = levels; + } + + @Override + public Graph perform(ReadGraph g) throws DatabaseException { + + nodes = new HashMap(); + Graph graph = new Graph(); + graph.setRankdir("LR"); + ModelingResources mr = ModelingResources.getInstance(g); + Resource element = g.getPossibleObject(root, mr.ElementToComponent); + if (element != null) { + root = element; + } + SysdynResource sr = SysdynResource.getInstance(g); + if (g.isInstanceOf(root, sr.IndependentVariable) || g.isInstanceOf(root, sr.Input)) { + setRoot(g, graph, root); + Collection resources = new ArrayList(); + resources.add(root); + for (int i = 0; i < levels && !resources.isEmpty(); i++) { + Collection newResources = new ArrayList(); + for(Resource r : resources) { + newResources.addAll(setDependencies(g, graph, r)); + } + resources = new ArrayList(newResources); + } + } + return graph; + } + + private void setRoot(ReadGraph g, IGraph graph, Resource root) throws DatabaseException { + Node n; + n = new Node(graph, getName(g, root)); + setShape(g, root, n); + n.set("style", "filled"); + n.setFillColor("#d3d3d3"); + nodes.put(root, n); + } + + + private Collection getDependants(ReadGraph g, IGraph graph, Resource r, Resource toDependency, Resource fromDependency, Resource connectionType) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + + Collection dependencies = g.syncRequest(new ObjectsWithType(r, toDependency, connectionType)); + + Collection dependants = new ArrayList(); + Node n; + for(Resource d : dependencies) { + Resource dependant = g.getPossibleObject(d, fromDependency); + if(dependant == null) + continue; + if(g.isInstanceOf(dependant, sr.Cloud)) { + break; + } + if( !nodes.containsKey(dependant)) { + n= new Node(graph, getName(g, dependant)); + setShape(g, dependant, n); + nodes.put(dependant, n); + dependants.add(dependant); + } + + if(isInverted) { + new Edge(graph, nodes.get(r), nodes.get(dependant)); + } else { + new Edge(graph, nodes.get(dependant), nodes.get(r)); + } + } + return dependants; + } + private Collection setDependencies(ReadGraph g, IGraph graph, Resource r) throws DatabaseException{ + SysdynResource sr = SysdynResource.getInstance(g); + + // stop here if element is a module + if (g.isInstanceOf(r, sr.Module)) + return Collections.emptyList(); + + Collection dependants; + if(isInverted) + dependants = getDependants(g, graph, r, sr.IsTailOf, sr.HasHead, sr.Dependency); + else { + dependants = getDependants(g, graph, r, sr.IsHeadOf, sr.HasTail, sr.Dependency); + if (g.isInstanceOf(r, sr.Stock)) { + dependants.addAll(getDependants(g, graph, r, sr.IsTailOf, sr.HasHead, sr.Flow)); + dependants.addAll(getDependants(g, graph, r, sr.IsHeadOf, sr.HasTail, sr.Flow)); + } + } + + return dependants; + } + + private void setShape(ReadGraph g, Resource r, Node n) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + if(g.isInstanceOf(r, sr.Stock)) + n.setShape("rectangle"); + else if(g.isInstanceOf(r, sr.Module)) { + n.setShape("rectangle"); + n.setStyle("rounded"); + } + else + n.setShape("ellipse"); + } + + private String getName(ReadGraph g, Resource r) throws DatabaseException { + return (String)g.getRelatedValue(r, g.getBuiltins().HasName); + } } -- 2.47.1