Author: abeld
Date: 2009-03-15 06:19:49 -0700 (Sun, 15 Mar 2009)
New Revision: 16271

Modified:
   
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/ContinuousMapping.java
   
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/DiscreteMapping.java
   
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/PassthroughMappingCalculator.java
   
core3/vizmap-api/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/MappingCalculator.java
   
core3/vizmap-impl/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/internal/VisualStyleImpl.java
Log:
valuecalculator branch: implement 'valuecalculator' idea.
Note: although this code compiles, I haven't tested it. I have a hunch not 
enough type-checking is done.


Modified: 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/ContinuousMapping.java
===================================================================
--- 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/ContinuousMapping.java
        2009-03-15 12:28:55 UTC (rev 16270)
+++ 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/ContinuousMapping.java
        2009-03-15 13:19:49 UTC (rev 16271)
@@ -151,50 +151,10 @@
                this.attrName = attrName;
        }
 
-       public <T, V extends GraphObject> void apply(ViewColumn<T> column, 
List<? extends View<V>> views){
-               if (views.size() < 1)
-                       return; // empty list, nothing to do
-               CyRow row = views.get(0).getSource().attrs(); // to check 
types, have to peek at first view instance
-               // check types:
-               Class<?> attrType = 
row.getDataTable().getColumnTypeMap().get(attrName);
-               Class<?> vpType = vp.getType();
-               if (vpType.isAssignableFrom(rangeClass) && 
Number.class.isAssignableFrom(attrType)){
-                       doMap(views, column, (Class<Number>)attrType); // due 
to check in previous line, this is a safe cast
-               } else {
-                       throw new IllegalArgumentException("Mapping 
"+toString()+" can't map from attribute type "+attrType+" to VisualProperty 
"+vp+" of type "+vp.getType());
-               }
+       public <T, V> V valueFor(T attributeValue, Class<V> type){
+               return (V) getRangeValue((Number) attributeValue); 
        }
-       /** 
-        * Read attribute from row, map it and apply it.
-        * 
-        * types are guaranteed to be correct (? FIXME: check this)
-        * 
-        * Putting this in a separate method makes it possible to make it 
type-parametric.
-        * 
-        * @param <T> the type-parameter of the ViewColumn column
-        * @param <K> the type-parameter of the domain of the mapping (the 
object read as an attribute value has to be is-a K)
-        * @param <V> the type-parameter of the View
-        */
-       private <T,K extends Number, V extends GraphObject> void doMap(final 
List<? extends View<V>> views, ViewColumn<T> column, Class<K> attrType){
-               // aggregate changes to be made in these:
-               Map<View<V>, T> valuesToSet = new HashMap<View<V>, T>();
-               List<View<V>> valuesToClear = new ArrayList<View<V>>();
-
-               for (View<V> v: views){
-                       CyRow row = v.getSource().attrs();
-                       if (row.contains(attrName, attrType) ){
-                               // skip Views where source attribute is not 
defined;
-                               // ViewColumn will automatically substitute the 
per-VS or global default, as appropriate
-
-                               final K attrValue = (K) 
v.getSource().attrs().get(attrName, attrType);
-                               final T value = (T) getRangeValue(attrValue); 
// FIXME: make getRangeValue type-parametric, so this shouldn't be needed (??) 
-                               valuesToSet.put(v, value);
-                       } else { // remove value so that default value will be 
used:
-                               valuesToClear.add(v);
-                       }
-               }
-               column.setValues(valuesToSet, valuesToClear);
-       }
+       
        private Object getRangeValue(Number domainValue) {
                ContinuousMappingPoint firstPoint = points.get(0);
                Number minDomain = new Double(firstPoint.getValue());

Modified: 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/DiscreteMapping.java
===================================================================
--- 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/DiscreteMapping.java
  2009-03-15 12:28:55 UTC (rev 16270)
+++ 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/DiscreteMapping.java
  2009-03-15 13:19:49 UTC (rev 16271)
@@ -109,56 +109,13 @@
                return vp;
        }
 
-       public <T, V extends GraphObject> void apply(ViewColumn<T> column, 
List<? extends View<V>> views){
-               if (views.size() < 1)
-                       return; // empty list, nothing to do
-               CyRow row = views.get(0).getSource().attrs(); // to check 
types, have to peek at first view instance
-               // check types:
-               Class<?> attrType = 
row.getDataTable().getColumnTypeMap().get(attrName);
-               Class<?> vpType = vp.getType();
-               if (vpType.isAssignableFrom(rangeClass)){
-                       // FIXME: should check here? or does that not matter?
-                       // if (keyClass.isAssignableFrom(attrType)) 
-                       doMap(views, column, attrType);
+       public <T, V> V valueFor(T attributeValue, Class<V> type){
+               if (treeMap.containsKey(attributeValue)){
+                       return  (V) treeMap.get(attributeValue);
                } else {
-                       throw new IllegalArgumentException("Mapping 
"+toString()+" can't map from attribute type "+attrType+" to VisualProperty 
"+vp+" of type "+vp.getType());
+                       return null;
                }
        }
-       /** 
-        * Read attribute from row, map it and apply it.
-        * 
-        * types are guaranteed to be correct (? FIXME: check this)
-        * 
-        * Putting this in a separate method makes it possible to make it 
type-parametric.
-        * 
-        * @param <T> the type-parameter of the ViewColumn column
-        * @param <K> the type-parameter of the key stored in the mapping (the 
object read as an attribute value has to be is-a K)
-        * @param <V> the type-parameter of the View
-        */
-       private <T,K, V extends GraphObject> void doMap(final List<? extends 
View<V>> views, ViewColumn<T> column, Class<K> attrType){
-               // aggregate changes to be made in these:
-               Map<View<V>, T> valuesToSet = new HashMap<View<V>, T>();
-               List<View<V>> valuesToClear = new ArrayList<View<V>>();
-
-               for (View<V> v: views){
-                       CyRow row = v.getSource().attrs();
-                       if (row.contains(attrName, attrType) ){
-                               // skip Views where source attribute is not 
defined;
-                               // ViewColumn will automatically substitute the 
per-VS or global default, as appropriate
-                               
-                               final K key = (K) 
v.getSource().attrs().get(attrName, attrType);
-                               if (treeMap.containsKey(key)){
-                                       final T value = (T) treeMap.get(key);
-                                       valuesToSet.put(v, value);
-                               } else { // remove value so that default value 
will be used:
-                                       valuesToClear.add(v);
-                               }                                       
-                       } else { // remove value so that default value will be 
used:
-                               valuesToClear.add(v);
-                       }
-               }
-               column.setValues(valuesToSet, valuesToClear);
-       }
        
        /**
         * Gets Value for Specified Key.

Modified: 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/PassthroughMappingCalculator.java
===================================================================
--- 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/PassthroughMappingCalculator.java
     2009-03-15 12:28:55 UTC (rev 16270)
+++ 
core3/default-mappingcalculators/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/mappings/PassthroughMappingCalculator.java
     2009-03-15 13:19:49 UTC (rev 16271)
@@ -99,57 +99,18 @@
                return vp;
        }
 
-       /**
-        *  DOCUMENT ME!
-        *
-        * @param v DOCUMENT ME!
-        */
-       public <T, V extends GraphObject> void apply(ViewColumn<T> column, 
List<? extends View<V>> views){
-               if (views.size() < 1)
-                       return; // empty list, nothing to do
-               CyRow row = views.get(0).getSource().attrs();
-               Class<?> attrType = 
row.getDataTable().getColumnTypeMap().get(attributeName);
-               // since attributes can only store certain types of Objects, it 
is enough to test for these:
-               Class<?> vpType = vp.getType();
-               // FIXME: also check that column's vp is internally-stored vp!
-               if (vpType.isAssignableFrom(attrType)){ // can simply copy 
object without any conversion
-                       // aggregate changes to be made in these:
-                       Map<View<V>, T> valuesToSet = new HashMap<View<V>, T>();
-                       List<View<V>> valuesToClear = new ArrayList<View<V>>();
-                       for (View<V> v: views){
-                               row = v.getSource().attrs();
-                               if (row.contains(attributeName, attrType) ){
-                                       // skip Views where source attribute is 
not defined; ViewColumn will automatically substitute the per-VS or global 
default, as appropriate 
-                                       final T value = (T) 
row.get(attributeName, attrType);
-                                       valuesToSet.put(v, value);
-                               } else { // remove value so that default value 
will be used:
-                                       valuesToClear.add(v);
-                               }
-                       }
-                       column.setValues(valuesToSet, valuesToClear);
-               } else if (String.class.isAssignableFrom(vpType)){
-                       // can convert any object to string, so no need to 
check attribute type
-                       // also, since we have to convert the Object here, 
can't use checkAndDoCopy()
-                       ViewColumn<String> c = (ViewColumn<String>) column; // 
have  to cast here, even though previous check ensures that T is 
java.util.String
+       public <T, V> V valueFor(T attributeValue, Class<V> vpType){
+               if (String.class.isAssignableFrom(vpType)){
+                       return (V) attributeValue.toString();
+               } else 
+                       return (V) attributeValue;
 
-                       // aggregate changes to be made in these:
-                       Map<View<V>, String> valuesToSet = new HashMap<View<V>, 
String>();
-                       List<View<V>> valuesToClear = new ArrayList<View<V>>();
-
-                       for (View<V> v: views){
-                               row = v.getSource().attrs();
-                               if (row.contains(attributeName, attrType) ){
-                                       // skip Views where source attribute is 
not defined; ViewColumn will automatically substitute the per-VS or global 
default, as appropriate 
-                                       final Object value = (Object) 
row.get(attributeName, attrType);
-                                       valuesToSet.put(v, value.toString());
-                               } else { // remove value so that default value 
will be used:
-                                       valuesToClear.add(v);
-                               }
-                       }
-                       c.setValues(valuesToSet, valuesToClear);
-               } else {        
-                       throw new IllegalArgumentException("Mapping 
"+toString()+" can't map from attribute type "+attrType+" to VisualProperty 
"+vp+" of type "+vp.getType()); 
+               /* This might be a more elegant way to do this: (but needs a 
Class<T> type-token parameter)
+               if (vpType.isAssignableFrom(attrType){
+                       return (V) attributeValue;
+               } else if (String.class.isAssignableFrom(vpType)){
+                       return (V) attributeValue.toString();
                }
-               // FIXME: handle List & Map
+               */
        }
 }

Modified: 
core3/vizmap-api/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/MappingCalculator.java
===================================================================
--- 
core3/vizmap-api/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/MappingCalculator.java
 2009-03-15 12:28:55 UTC (rev 16270)
+++ 
core3/vizmap-api/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/MappingCalculator.java
 2009-03-15 13:19:49 UTC (rev 16271)
@@ -82,14 +82,17 @@
        VisualProperty<?> getVisualProperty();
 
        /**
-        *  Since current MappingCalculators map from Attributes to
-        *  VisualProperties, have to restrict View<?> to those
-        *  generic types that have CyAttributes; currently this is
-        *  GraphObject.
-        *
-        * @param <V> DOCUMENT ME!
-        * @param column DOCUMENT ME!
-        * @param views DOCUMENT ME!
+        * Calculate the VisualProperty value for a given attribute value.
+        * Returns null if there is no calculated value, in this case the 
default value will be used.
+        * 
+        * Callers of this method are responsible for ensuring type-safety.
+        * 
+        * For efficiency, implementations of this methods should make no 
checks that T and V are
+        * correct for the given implementation: that will be checked and thus 
correctness ensured by
+        * callers of this method. 
+        * 
+        * @param <T> the type-parameter of the attribute being mapped from
+        * @param V the type of the return value (a VisualProperty 
type-parameter)
         */
-       <T, V extends GraphObject> void apply(ViewColumn<T> column, List<? 
extends View<V>> views);
+       <T, V> V valueFor(T attributeValue, Class<V> type);
 }

Modified: 
core3/vizmap-impl/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/internal/VisualStyleImpl.java
===================================================================
--- 
core3/vizmap-impl/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/internal/VisualStyleImpl.java
 2009-03-15 12:28:55 UTC (rev 16270)
+++ 
core3/vizmap-impl/branches/valuecalculator/src/main/java/org/cytoscape/vizmap/internal/VisualStyleImpl.java
 2009-03-15 13:19:49 UTC (rev 16271)
@@ -39,6 +39,7 @@
 import org.cytoscape.model.CyEdge;
 import org.cytoscape.model.CyNetwork;
 import org.cytoscape.model.CyNode;
+import org.cytoscape.model.CyRow;
 import org.cytoscape.model.GraphObject;
 
 import org.cytoscape.view.model.CyNetworkView;
@@ -50,6 +51,7 @@
 import org.cytoscape.vizmap.MappingCalculator;
 import org.cytoscape.vizmap.VisualStyle;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
@@ -131,11 +133,8 @@
                perVSDefaults.put(vp, value);
        }
 
-       // ??
        /**
-        *  DOCUMENT ME!
-        *
-        * @param view DOCUMENT ME!
+        *  {...@inheritdoc}
         */
        public void apply(final CyNetworkView view) {
                final List<View<CyNode>> nodeviews = view.getCyNodeViews();
@@ -151,11 +150,7 @@
 
        // note: can't use applyImpl(List<View<?>>views ... ) because that does 
not compile
        /**
-        *  DOCUMENT ME!
         *
-        * @param <T> DOCUMENT ME!
-        * @param views DOCUMENT ME!
-        * @param visualProperties DOCUMENT ME!
         */
        public <T extends GraphObject> void applyImpl(final CyNetworkView view, 
final List<View<T>> views,
                                                      final Collection<? 
extends VisualProperty<?>> visualProperties) {
@@ -164,7 +159,7 @@
                }
        }
        /**
-        *  DOCUMENT ME!
+        *  I think this needs to be in a separate method to allow making it 
generic -- abeld
         *
         * @param <T> DOCUMENT ME!
         * @param views DOCUMENT ME!
@@ -178,12 +173,50 @@
                        column.setDefaultValue(perVSDefault);
                }
                if (c != null) {
-                       c.apply(column, views);
+                       String attrName = c.getMappingAttributeName();
+                       if (views.size() < 1)
+                               return; // empty list, nothing to do
+                       CyRow row = views.get(0).getSource().attrs(); // to 
check types, have to peek at first view instance
+                       // check types:
+                       Class<?> attrType = 
row.getDataTable().getColumnTypeMap().get(attrName);
+                       Class<T> vpType = vp.getType();
+
+                       if (true) {
+                               doMap(views, c, attrName, attrType, column, 
vpType);
+                       } else {
+                               throw new IllegalArgumentException("Mapping 
"+toString()+" can't map from attribute type "+attrType+" to VisualProperty 
"+vp+" of type "+vp.getType());
+                       }
                } else {
-                       // reset all rows to allow usage of default value:
-                       for (View<V>v: views){
-                               column.clearValue(v);
+                       column.setValues(new HashMap<View<V>, T>(), views);
+               }
+       }
+       /**
+        * I think this needs to be in a separate method to allow making it 
generic -- abeld
+        *
+        * @param T the VisualProperty type being mapped to
+        * @param A the attribute type being mapped from
+        * @param V the view type for which the mapping is made
+        */
+       private <T, A, V extends GraphObject> void doMap(final List<? extends 
View<V>> views,  MappingCalculator calc,
+                       String attrName, Class<A> attrType, ViewColumn<T> 
column, Class<T> visualType){
+               // aggregate changes to be made in these:
+               Map<View<V>, T> valuesToSet = new HashMap<View<V>, T>();
+               List<View<V>> valuesToClear = new ArrayList<View<V>>();
+
+               for (View<V> v: views){
+                       CyRow row = v.getSource().attrs();
+                       if (row.contains(attrName, attrType) ){ // skip Views 
where source attribute is not defined;
+                                                                               
                        // ViewColumn will automatically substitute the per-VS 
or global default, as appropriate
+                               T value = calc.valueFor((A) 
v.getSource().attrs().get(attrName, attrType), visualType);
+                               if (value != null){ // 'null' value mean 
'revert to default'
+                                       valuesToSet.put(v, value);
+                               } else { // remove value so that default value 
will be used:
+                                       valuesToClear.add(v);
+                               }                                       
+                       } else { // remove value so that default value will be 
used:
+                               valuesToClear.add(v);
                        }
                }
+               column.setValues(valuesToSet, valuesToClear);
        }
 }


--~--~---------~--~----~------------~-------~--~----~
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