001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.algorithmanimator.core.atoms;
005    
006    //import static graphlab.library.event.GraphEvent.nameType.NEW_GRAPH;
007    
008    import graphlab.graph.atributeset.GraphAttrSet;
009    import graphlab.graph.graph.EdgeModel;
010    import graphlab.graph.graph.GraphModel;
011    import graphlab.graph.graph.GraphPoint;
012    import graphlab.graph.graph.VertexModel;
013    import graphlab.library.BaseEdge;
014    import graphlab.library.BaseVertex;
015    import graphlab.library.event.Event;
016    import graphlab.library.event.GraphEvent;
017    import static graphlab.library.event.GraphEvent.EventType.NEW_GRAPH;
018    import graphlab.library.event.typedef.BaseGraphEvent;
019    import graphlab.platform.core.BlackBoard;
020    import graphlab.plugins.algorithmanimator.core.AtomAnimator;
021    
022    import java.util.HashMap;
023    import java.util.Iterator;
024    
025    /**
026     * @author Azin Azadi
027     */
028    public class NewGraph implements AtomAnimator<BaseGraphEvent> {
029        public boolean isAnimatable(Event event) {
030            if (event instanceof GraphEvent) {
031                if (((GraphEvent) event).eventType == NEW_GRAPH)
032                    return true;
033            }
034            return false;
035        }
036    
037        public BaseGraphEvent animate(BaseGraphEvent event, BlackBoard b) {
038            BlackBoard bb = graphlab.plugins.main.core.actions.graph.NewGraph.doJob(b);
039    //        System.out.println("blackboard:"+bb);
040            Object o = bb.getData(GraphAttrSet.name);
041            //*********************** \/ \/ \/ \/ \/ \/ \/ 
042    //        System.out.println(o.getClass().getName());              //print mikone esme classe graph ro
043    //        System.out.println("instance:" +(o instanceof Graph));   //print mikone: false
044            GraphModel g = ((GraphModel) o);
045            //hich vaght 2 chap nemishe, iani too khate bala moshkeli hast
046    //        System.out.println("2");
047            HashMap<BaseVertex, VertexModel> map = new HashMap<BaseVertex, VertexModel>();
048            for (BaseVertex v : event.graph) {
049                VertexModel vv = new VertexModel();
050                map.put(v, vv);
051                //vv.setModel((VertexModel) v);
052                g.insertVertex(vv);
053                vv.setLocation(new GraphPoint(Math.random() * 200, Math.random() * 200));
054            }
055            Iterator<BaseEdge<BaseVertex>> ie = event.graph.edgeIterator();
056            while (ie.hasNext()) {
057                BaseEdge e = ie.next();
058                VertexModel src = map.get(e.source);
059                VertexModel dest = map.get(e.target);
060                EdgeModel ee = new EdgeModel(src, dest);
061                g.insertEdge(ee);
062            }
063            return event;
064        }
065    }