View Javadoc

1   package org.molwind.chemical.view;
2   
3   /*
4    * This file is part of Molwind.
5    *
6    * Molwind is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Molwind is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Iterator;
23  import java.util.TreeMap;
24  
25  import java.util.Collection;
26  import java.awt.Dimension;
27  import java.awt.geom.Point2D;
28  
29  import org.molwind.graph.DefaultEntityEdge;
30  import org.molwind.graph.EntityGraph;
31  import org.molwind.graph.EntityEdge;
32  import org.molwind.graph.DefaultEntityGraph;
33  import org.molwind.model.LayeredPosition;
34  import org.molwind.model.RelationshipResolver;
35  import org.molwind.graph.EntityVertex;
36  import org.molwind.util.MolwindLogger;
37  import org.molwind.view.TopologyManager;
38  import org.molwind.view.AbstractTopologyManager;
39  import org.molwind.model.PlacedWorldEntity;
40  
41  import org.apache.commons.collections15.Transformer;
42  
43  import org.openscience.cdk.tools.MFAnalyser;
44  import org.openscience.cdk.interfaces.IMolecule;
45  
46  import edu.uci.ics.jung.graph.SparseGraph;
47  import edu.uci.ics.jung.algorithms.layout.SpringLayout2;
48  import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
49  import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
50  import edu.uci.ics.jung.algorithms.layout.AggregateLayout;
51  import edu.uci.ics.jung.graph.util.EdgeType;
52  import edu.uci.ics.jung.graph.Graph; 
53  import edu.uci.ics.jung.graph.util.Pair;
54  import edu.uci.ics.jung.visualization.BasicVisualizationServer;
55  
56  
57  
58  
59  /**
60   * ChemicalTopologyManager determines and places visible chemical structures.
61   *
62   * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
63   * @version 1.0
64   */
65  public class ChemicalTopologyManager extends AbstractTopologyManager {
66  
67      private Iterator entityIterator;
68      private RelationshipResolver relationshipResolver;
69      private TreeMap entities;
70  
71      /**
72       * Creates a new topology manager to place chemical entities.
73       */
74      public ChemicalTopologyManager() {
75  	super();
76          entities = null;
77      }
78      
79      
80      private SparseGraph<EntityVertex,EntityEdge> linkToGraph(EntityVertex[] entityArray){
81  	SparseGraph<EntityVertex,EntityEdge> resultGraph = new SparseGraph<EntityVertex,EntityEdge>();
82  	
83  	for(int i = 0;i<entityArray.length;i++){
84  	    resultGraph.addVertex(entityArray[i]);
85  	    for(int j = i+1;j<entityArray.length;j++){
86  		Pair<EntityVertex> endpoints = new Pair<EntityVertex>(entityArray[i],entityArray[j]);
87  		int edgeWeight = entityArray[i].getEntity().getDimension()+ entityArray[j].getEntity().getDimension();
88  		DefaultEntityEdge edge = new DefaultEntityEdge(entityArray[i],entityArray[j],edgeWeight);
89  		resultGraph.addEdge(edge,endpoints,EdgeType.DIRECTED);
90  		
91  		//parentGraph.addEdge(endpoints,EdgeType.DIRECTED);
92  	    }
93  	    
94  	}
95  	
96  	return resultGraph;
97  	
98      }
99      
100     private EntityVertex[] getChildren(SparseGraph<EntityVertex,EntityEdge> graph,EntityVertex vertex){
101 	EntityVertex[] result = new EntityVertex[graph.getIncidentEdges(vertex).size()];
102 	int i = 0;
103 	for(Object edgeObject:graph.getIncidentEdges(vertex)){
104 	    EntityEdge edge =(EntityEdge)edgeObject;
105 	    result[i]=edge.getV1();
106 	    i++;
107 	}
108 	
109 	
110 	return result;
111     }
112     
113     //TODO Method is not well implemented yet
114     public void calculatePositions() {
115         RelationshipResolver resolver = getRelationshipResolver();
116         if (resolver == null) {
117             MolwindLogger.warn("Cannot place entities: "
118                     + "missing relationship resolver", null);
119             return;
120         }
121         DefaultEntityGraph defaultGraph =(DefaultEntityGraph) resolver.resolve();
122 	
123 	SparseGraph graph = defaultGraph.getGraph();
124 	
125 	
126 	MolwindLogger.debug( "Entity graph:\n"+graph );
127 
128 	Transformer<EntityEdge,Integer> transformer = new Transformer<EntityEdge,Integer>(){
129 	    public Integer transform(EntityEdge edge){
130 		return edge.getEdgeWeight();
131 	    }
132 	};
133 	int size = 1000;
134 
135 	for(int i = defaultGraph.getLayerCount()-1;i>0;i--){
136 	    EntityVertex[] parentLayer = defaultGraph.getLayer(i-1);
137 	    EntityVertex[] childrenLayer = defaultGraph.getLayer(i);
138 	    
139 	    SparseGraph<EntityVertex,EntityEdge> parentGraph,childGraph,wholeGraph;
140 	    wholeGraph = new SparseGraph<EntityVertex,EntityEdge>();
141 	    parentGraph = linkToGraph(parentLayer);
142 	    childGraph =  new SparseGraph<EntityVertex,EntityEdge>();
143 	    
144 	    
145 	    for(EntityVertex vertex:parentLayer)
146 		wholeGraph.addVertex(vertex);
147 	    
148 	    for(EntityVertex vertex:childrenLayer)
149 		wholeGraph.addVertex(vertex);
150 	    
151 	    SpringLayout2<EntityVertex,EntityEdge> springLayout = new SpringLayout2<EntityVertex,EntityEdge>(parentGraph,transformer);
152 	    //Maybe a factor for RepulsionRange needed
153 	    springLayout.setRepulsionRange(300);
154 	    springLayout.setSize(new Dimension(size,size));
155 	    springLayout.initialize();
156 	    for(int j = 0;j<100;j++)
157 		springLayout.step();
158 	    //  BasicVisualizationServer basicVisualizationServer = new BasicVisualizationServer(springLayout);
159 	    for(EntityVertex vertex:parentLayer){
160 		vertex.setPosition((Point2D.Double)springLayout.transform(vertex));
161 	    }
162 	    
163 	    
164 	    AggregateLayout aLayout = new AggregateLayout(new SpringLayout2(wholeGraph));
165 	    try{
166 		for(EntityVertex vertex : parentLayer){
167 		    childGraph = linkToGraph(getChildren(graph,vertex));
168 		    
169 		    int circleSize = 0;
170 		    for(EntityVertex childVertex:childGraph.getVertices()){
171 			
172 			circleSize +=childVertex.getEntity().getDimension();
173 			//	childGraph.addVertex(children[i]);
174 		    }
175 		    
176 		    if(circleSize < 40)
177 			circleSize = 40;
178 		    circleSize *=2;
179 		    
180 		    ISOMLayout<EntityVertex,EntityEdge> isomLayout = new ISOMLayout<EntityVertex,EntityEdge>(childGraph);
181 		    
182 		    // BasicVisualizationServer vv2 = new BasicVisualizationServer(aLayout);
183 		    
184 		    isomLayout.setInitializer(aLayout);
185 		    isomLayout.setSize(new Dimension(circleSize,circleSize));
186 		    
187 		    aLayout.put(isomLayout,vertex.getPosition());
188 		    // vv2.repaint();
189 		    for(EntityVertex childVertex:childGraph.getVertices()){
190 			
191 		
192 			
193 			childVertex.setPosition((Point2D.Double)aLayout.transform(childVertex));
194 		    }
195 		    
196 		}
197 	    }catch(java.util.ConcurrentModificationException e){
198 		
199 	    }
200 	    size *=4;
201 	}
202 	
203 	int o = 0;
204 	
205 	for(EntityVertex vertex:defaultGraph.getGraph().getVertices()){
206 	    o++;
207 	    System.out.println(vertex.getPosition());
208 	}
209 	System.out.println("OOO: " + o);
210 	
211 	//defaultGraph.viewGraph(2000);
212 	
213         // TODO Complete method, seems unfinished
214     }
215 
216 
217     /**
218      * Determines the visible entities for the given position.
219      *
220      * @param position
221      *      the position (longitude/latitude/level)
222      * @return
223      *      an (possibly empty) iterator of entities
224      */
225     public Iterator getVisibleEntities(final LayeredPosition position) {
226         if (entities == null) {
227             calculatePositions();
228         }
229         return visibleIterator(position);
230     }
231 
232     private Iterator visibleIterator(final LayeredPosition position) {
233         // TODO Needs implemention
234         return (new ArrayList()).iterator();
235     }
236 
237 }