Author: mes
Date: 2012-01-24 15:30:26 -0800 (Tue, 24 Jan 2012)
New Revision: 28095

Added:
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/WeakMapList.java
Modified:
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableFactoryImpl.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/NameSetListener.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumnAdder.java
Log:
now avoiding a lot of service registration when adding subnetworks

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
 2012-01-24 23:00:09 UTC (rev 28094)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
 2012-01-24 23:30:26 UTC (rev 28095)
@@ -48,6 +48,8 @@
 import org.cytoscape.model.SUIDFactory;
 import org.cytoscape.model.events.ColumnCreatedListener;
 import org.cytoscape.model.events.RowsSetListener;
+import org.cytoscape.model.events.NetworkAddedListener;
+import org.cytoscape.model.events.NetworkAddedEvent;
 import org.cytoscape.model.subnetwork.CyRootNetwork;
 import org.cytoscape.model.subnetwork.CySubNetwork;
 import org.cytoscape.service.util.CyServiceRegistrar;
@@ -91,16 +93,19 @@
        private final CyTableManagerImpl tableMgr;
        private final CyNetworkTableManagerImpl networkTableMgr;
        private final CyTableFactory tableFactory;
-       private final CyServiceRegistrar serviceRegistrar;
        private final boolean publicTables;
+       private final VirtualColumnAdder columnAdder;
+       private final NameSetListener nameSetListener;
+       private final NetworkAddedListenerDelegator 
networkAddedListenerDelegator;
 
+
        /**
         * Creates a new ArrayGraph object.
         * @param eh The CyEventHelper used for firing events.
         */
        public ArrayGraph(final CyEventHelper eh, final CyTableManagerImpl 
tableMgr,
-                                         final CyNetworkTableManagerImpl 
networkTableMgr,
-                                         final CyTableFactory tableFactory,
+                         final CyNetworkTableManagerImpl networkTableMgr,
+                         final CyTableFactory tableFactory,
                          final CyServiceRegistrar serviceRegistrar, final 
boolean publicTables)
        {
                this.eventHelper = eh;
@@ -108,7 +113,6 @@
                this.networkTableMgr = networkTableMgr;
                this.tableFactory = tableFactory;
                this.publicTables = publicTables;
-               this.serviceRegistrar = serviceRegistrar;
                suid = SUIDFactory.getNextSUID();
                numSubNetworks = 0;
                nodeCount = 0;
@@ -117,14 +121,21 @@
                nodePointers = new ArrayList<NodePointer>();
                edgePointers = new ArrayList<EdgePointer>();
 
+               columnAdder = new VirtualColumnAdder();
+               serviceRegistrar.registerService(columnAdder, 
ColumnCreatedListener.class, new Properties());
+               nameSetListener = new NameSetListener();
+               serviceRegistrar.registerService(nameSetListener, 
RowsSetListener.class, new Properties());
+               networkAddedListenerDelegator = new 
NetworkAddedListenerDelegator();
+               serviceRegistrar.registerService(networkAddedListenerDelegator, 
NetworkAddedListener.class, new Properties());
+
                netTables = createNetworkTables(suid); 
                getCyRow().set(CyTableEntry.NAME, "");
                nodeTables = createNodeTables(suid); 
                edgeTables = createEdgeTables(suid); 
 
-        networkTableMgr.setTableMap(CyNetwork.class, this, netTables);
-        networkTableMgr.setTableMap(CyNode.class, this, nodeTables);
-        networkTableMgr.setTableMap(CyEdge.class, this, edgeTables);
+               networkTableMgr.setTableMap(CyNetwork.class, this, netTables);
+               networkTableMgr.setTableMap(CyNode.class, this, nodeTables);
+               networkTableMgr.setTableMap(CyEdge.class, this, edgeTables);
 
                subNetworks = new ArrayList<CySubNetwork>();
 
@@ -200,12 +211,10 @@
 
                // Now add a listener for column created events to add
                // virtual columns to any subsequent source columns added.
-               serviceRegistrar.registerService(new 
VirtualColumnAdder(srcTable,tgtTable), 
-                                                ColumnCreatedListener.class, 
new Properties());
+               columnAdder.addInterestedTables(srcTable,tgtTable);
 
                // Another listener tracks changes to the NAME column in local 
tables
-               serviceRegistrar.registerService(new 
NameSetListener(srcTable,tgtTable),
-                                                RowsSetListener.class, new 
Properties());
+               nameSetListener.addInterestedTables(srcTable,tgtTable);
        }
 
        /**
@@ -944,7 +953,7 @@
                final Map<String,CyTable> newNodeTable = 
createNodeTables(newSUID);
                final Map<String,CyTable> newEdgeTable = 
createEdgeTables(newSUID);
                final ArraySubGraph sub = new 
ArraySubGraph(this,newSUID,newId,eventHelper,newNetTable,newNodeTable,newEdgeTable,tableMgr);
-               serviceRegistrar.registerAllServices(sub, new Properties());
+               networkAddedListenerDelegator.addListener(sub); 
                subNetworks.add(sub);
                networkTableMgr.setTableMap(CyNetwork.class, sub, newNetTable);
                networkTableMgr.setTableMap(CyNode.class, sub, newNodeTable);
@@ -1072,4 +1081,17 @@
        public CyRow getRow(CyTableEntry t) {
                return getRow(t,CyNetwork.DEFAULT_ATTRS);
        }
+
+    private class NetworkAddedListenerDelegator implements 
NetworkAddedListener {
+        List<NetworkAddedListener> listeners = new 
ArrayList<NetworkAddedListener>();
+        public void addListener(NetworkAddedListener l) {
+            listeners.add(l);
+        }
+        public void handleEvent(NetworkAddedEvent e) {
+            for (NetworkAddedListener l : listeners)
+                l.handleEvent(e);
+        }
+    }
+
 }
+

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableFactoryImpl.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableFactoryImpl.java
 2012-01-24 23:00:09 UTC (rev 28094)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableFactoryImpl.java
 2012-01-24 23:30:26 UTC (rev 28095)
@@ -33,10 +33,13 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Properties;
+import java.lang.ref.WeakReference;
 
 import org.cytoscape.model.CyTable;
 import org.cytoscape.model.CyTable.SavePolicy;
 import org.cytoscape.model.CyTableFactory;
+import org.cytoscape.model.events.TableAddedListener;
+import org.cytoscape.model.events.TableAddedEvent;
 import org.cytoscape.equations.Interpreter;
 import org.cytoscape.event.CyEventHelper;
 import org.cytoscape.service.util.CyServiceRegistrar;
@@ -51,6 +54,7 @@
        private final CyEventHelper help;
        private final Interpreter interpreter;
        private final CyServiceRegistrar serviceRegistrar;
+       private final WeakEventDelegator eventDelegator; 
 
        public CyTableFactoryImpl(final CyEventHelper help, final Interpreter 
interpreter,
                                  final CyServiceRegistrar serviceRegistrar)
@@ -58,14 +62,32 @@
                this.help             = help;
                this.interpreter      = interpreter;
                this.serviceRegistrar = serviceRegistrar;
+               this.eventDelegator = new WeakEventDelegator();
+               this.serviceRegistrar.registerService(eventDelegator, 
TableAddedListener.class, new Properties()); 
        }
 
        public CyTable createTable(final String name, final String primaryKey, 
final Class<?> primaryKeyType,
                                   final boolean pub, final boolean isMutable)
        {
-               final CyTable table = new CyTableImpl(name, primaryKey, 
primaryKeyType, pub, isMutable,
+               final CyTableImpl table = new CyTableImpl(name, primaryKey, 
primaryKeyType, pub, isMutable,
                                                      SavePolicy.SESSION_FILE, 
help, interpreter);
-               serviceRegistrar.registerAllServices(table, new Properties());
+               eventDelegator.addListener(table);
                return table;
        }
+
+       private class WeakEventDelegator implements TableAddedListener {
+               List<WeakReference<TableAddedListener>> tables = new 
ArrayList<WeakReference<TableAddedListener>>();  
+
+               public void addListener(TableAddedListener t) {
+                       tables.add(new WeakReference<TableAddedListener>(t));
+               }
+               
+               public void handleEvent(TableAddedEvent e) {
+                       for ( WeakReference<TableAddedListener> ref : tables ) {
+                               TableAddedListener l = ref.get();
+                               if ( l != null )
+                                       l.handleEvent(e);
+                       }
+               }
+       }
 }

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/NameSetListener.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/NameSetListener.java
    2012-01-24 23:00:09 UTC (rev 28094)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/NameSetListener.java
    2012-01-24 23:30:26 UTC (rev 28095)
@@ -38,6 +38,9 @@
 import org.cytoscape.model.events.RowsSetEvent;
 import org.cytoscape.model.events.RowSetRecord;
 
+import java.util.List;
+import java.util.ArrayList;
+
 /**
  * Any time that the CyTableEntry.NAME column is set
  * in a local table, update the shared table with the
@@ -45,32 +48,37 @@
  */
 class NameSetListener implements RowsSetListener {
 
-       private final CyTable shared;
-       private final CyTable local;
+       private final WeakMapList<CyTable,CyTable> tables; 
 
-       NameSetListener(CyTable shared, CyTable local) {
+       NameSetListener() {
+               tables = new WeakMapList<CyTable,CyTable>();
+       }
+
+       public void handleEvent(RowsSetEvent e) {
+               CyTable local = e.getSource();  
+               List<CyTable> sharedList = tables.get(local);
+               for ( CyTable shared : sharedList ) {
+                       for ( RowSetRecord record : e.getPayloadCollection() ) {
+                               // assume payload collection is for same column
+                               if ( 
!record.getColumn().equals(CyTableEntry.NAME) )
+                                       continue;
+                               CyRow r = shared.getRow( record.getRow().get( 
CyTableEntry.SUID, Long.class ) );
+                               if ( r != null ) 
+                                       r.set(CyRootNetwork.SHARED_NAME, 
record.getValue());
+                       }
+               }
+       }
+
+    public void addInterestedTables(CyTable local, CyTable shared) {
                if ( shared == null )
                        throw new NullPointerException("source table is null");
                if ( local == null )
                        throw new NullPointerException("target table is null");
                if ( shared == local )
                        throw new IllegalArgumentException("source and target 
tables cannot be the same!");
-               this.shared = shared;
-               this.local = local;
-       }
 
-       public void handleEvent(RowsSetEvent e) {
-               if ( e.getSource() != local )
-                       return;
-               for ( RowSetRecord record : e.getPayloadCollection() ) {
-                       // assume payload collection is for same column
-                       if ( !record.getColumn().equals(CyTableEntry.NAME) )
-                               return;
+               tables.put(local,shared);
+    }
 
-                       CyRow r = shared.getRow( record.getRow().get( 
CyTableEntry.SUID, Long.class ) );
-                       if ( r != null ) 
-                               r.set(CyRootNetwork.SHARED_NAME, 
record.getValue());
-               }
-       }
 }
 

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumnAdder.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumnAdder.java
 2012-01-24 23:00:09 UTC (rev 28094)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/VirtualColumnAdder.java
 2012-01-24 23:30:26 UTC (rev 28095)
@@ -35,34 +35,38 @@
 import org.cytoscape.model.events.ColumnCreatedEvent;
 import org.cytoscape.model.events.ColumnCreatedListener;
 
+import java.util.List;
+import java.util.ArrayList;
 /**
  * Any time that the source table adds a column, the
  * target table add it as a virtual column.
  */
 class VirtualColumnAdder implements ColumnCreatedListener {
 
-       private final CyTable src;
-       private final CyTable tgt;
+       private final WeakMapList<CyTable,CyTable> tables;
 
-       VirtualColumnAdder(CyTable src, CyTable tgt) {
+       VirtualColumnAdder() {
+               tables = new WeakMapList<CyTable,CyTable>(); 
+       }
+
+       public void handleEvent(ColumnCreatedEvent e) {
+               CyTable src = e.getSource();
+               List<CyTable> targets = tables.get( src );
+               String srcName = e.getColumnName();
+               CyColumn srcCol = src.getColumn(srcName);
+
+               for ( CyTable tgt : targets )
+                       
tgt.addVirtualColumn(srcName,srcName,src,CyTableEntry.SUID,srcCol.isImmutable());
+       }
+
+       public void addInterestedTables(CyTable src, CyTable tgt) {
                if ( src == null )
                        throw new NullPointerException("source table is null");
                if ( tgt == null )
                        throw new NullPointerException("target table is null");
                if ( src == tgt )
                        throw new IllegalArgumentException("source and target 
tables cannot be the same!");
-               this.src = src;
-               this.tgt = tgt;
+               tables.put(src,tgt);
        }
-
-       public void handleEvent(ColumnCreatedEvent e) {
-               if ( e.getSource() != src )
-                       return;
-               String srcName = e.getColumnName();
-               if ( tgt.getColumn(srcName) != null )
-                       return;
-               CyColumn srcCol = src.getColumn(srcName);
-               
tgt.addVirtualColumn(srcName,srcName,src,CyTableEntry.SUID,srcCol.isImmutable());
-       }
 }
 

Added: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/WeakMapList.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/WeakMapList.java
                                (rev 0)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/WeakMapList.java
        2012-01-24 23:30:26 UTC (rev 28095)
@@ -0,0 +1,74 @@
+
+/*
+ Copyright (c) 2008, 2010-2011, The Cytoscape Consortium (www.cytoscape.org)
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.model.internal;
+
+
+import org.cytoscape.model.CyTable;
+import org.cytoscape.model.CyColumn;
+import org.cytoscape.model.CyTableEntry;
+import org.cytoscape.model.events.ColumnCreatedEvent;
+import org.cytoscape.model.events.ColumnCreatedListener;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.WeakHashMap;
+
+import java.lang.ref.WeakReference;
+
+/**
+ * A WeakHashMap where the keys point to lists of WeakReferences.
+ */
+class WeakMapList<S,T> {
+
+       WeakHashMap<S,List<WeakReference<T>>> data = new 
WeakHashMap<S,List<WeakReference<T>>>();
+
+       public List<T> get(S src) {
+               List<WeakReference<T>> list = data.get(src);
+               List<T> ret = new ArrayList<T>();
+               if ( list == null )
+                       return ret; 
+               for (WeakReference<T> ref : list) {
+                       T t = ref.get();
+                       if ( t != null )
+                               ret.add(t);
+               }
+               return ret;
+       }
+
+
+       public void put(S src, T tgt) {
+               List<WeakReference<T>> refs = data.get(src);
+               if ( refs == null ) {
+                       refs = new ArrayList<WeakReference<T>>();
+                       data.put(src,refs);
+               }
+               refs.add( new WeakReference<T>( tgt ) );
+       }
+}
+

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to