Author: ruschein
Date: 2011-07-05 09:10:08 -0700 (Tue, 05 Jul 2011)
New Revision: 26017

Modified:
   core3/io-impl/trunk/pom.xml
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderFactory.java
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVTableWriterFactory.java
   
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml
   core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml
   
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
Log:
Added equation support for CSV files.

Modified: core3/io-impl/trunk/pom.xml
===================================================================
--- core3/io-impl/trunk/pom.xml 2011-07-04 21:32:50 UTC (rev 26016)
+++ core3/io-impl/trunk/pom.xml 2011-07-05 16:10:08 UTC (rev 26017)
@@ -117,6 +117,11 @@
        <dependencies>
                <dependency>
                        <groupId>org.cytoscape</groupId>
+                       <artifactId>equations-api</artifactId>
+                       <version>3.0.0-alpha3-SNAPSHOT</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.cytoscape</groupId>
                        <artifactId>io-api</artifactId>
                        <version>3.0.0-alpha5-SNAPSHOT</version>
                </dependency>
@@ -137,11 +142,6 @@
                </dependency>
                <dependency>
                        <groupId>org.cytoscape</groupId>
-                       <artifactId>presentation-api</artifactId>
-                       <version>3.0.0-alpha4-SNAPSHOT</version>
-               </dependency>
-               <dependency>
-                       <groupId>org.cytoscape</groupId>
                        <artifactId>core-task-api</artifactId>
                        <version>3.0.0-alpha4-SNAPSHOT</version>
                </dependency>
@@ -155,11 +155,6 @@
                        <artifactId>default-mappings</artifactId>
                        <version>3.0.0-alpha4-SNAPSHOT</version>
                </dependency>
-               <dependency>
-                       <groupId>org.cytoscape</groupId>
-                       <artifactId>equations-api</artifactId>
-                       <version>3.0.0-alpha2</version>
-               </dependency>
 
                <!-- 3rd party libraries -->
                <!-- <dependency>
@@ -174,12 +169,13 @@
                        
<artifactId>com.springsource.com.lowagie.text</artifactId>
                        <version>2.0.8</version>
                </dependency>
-               
+<!--           
                <dependency>
                        <groupId>com.sun.xml.bind</groupId>
                        <artifactId>jaxb-impl</artifactId>
                        <version>2.2.1</version>
                </dependency>
+-->
                <dependency>
                        <groupId>cytoscape-temp</groupId>
                        <artifactId>opencsv</artifactId>
@@ -207,16 +203,20 @@
                        <artifactId>freehep-graphicsio-ps</artifactId>
                        <version>2.1.1</version>
                </dependency>
+<!--
                <dependency>
                        <groupId>cytoscape-temp</groupId>
                        <artifactId>freehep-graphics-base</artifactId>
                        <version>2.1.3</version>
                </dependency>
+-->
+<!--
                <dependency>
                        <groupId>cytoscape-temp</groupId>
                        <artifactId>freehep-io</artifactId>
                        <version>2.1</version>
                </dependency>
+-->
 
                <!-- For Testing -->
                <dependency>

Modified: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
 2011-07-04 21:32:50 UTC (rev 26016)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
 2011-07-05 16:10:08 UTC (rev 26017)
@@ -1,14 +1,19 @@
 package org.cytoscape.io.internal.read.datatable;
 
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.cytoscape.equations.Equation;
+import org.cytoscape.equations.EquationCompiler;
 import org.cytoscape.io.read.CyTableReader;
 import org.cytoscape.model.CyRow;
 import org.cytoscape.model.CyTable;
@@ -17,24 +22,30 @@
 
 import au.com.bytecode.opencsv.CSVReader;
 
+
 public class CSVCyReader implements CyTableReader {
+       private final static Pattern classPattern = 
Pattern.compile("([^<>]+)(<(.*?)>)?");
 
-       private Pattern classPattern = Pattern.compile("([^<>]+)(<(.*?)>)?");
-       
        private final InputStream stream;
        private final boolean readSchema;
+       private final boolean handleEquations;
        private final CyTableFactory tableFactory;
-       
+       private final EquationCompiler compiler;
+
        private boolean isCanceled;
        private CyTable table;
 
+       public CSVCyReader(final InputStream stream, final boolean readSchema,
+                          final boolean handleEquations, final CyTableFactory 
tableFactory,
+                          final EquationCompiler compiler)
+       {
+               this.stream          = stream;
+               this.readSchema      = readSchema;
+               this.handleEquations = handleEquations;
+               this.tableFactory    = tableFactory;
+               this.compiler        = compiler;
+       }
 
-       public CSVCyReader(InputStream stream, boolean readSchema, 
CyTableFactory tableFactory) {
-               this.stream = stream;
-               this.readSchema = readSchema;
-               this.tableFactory = tableFactory;
-       }
-       
        @Override
        public void cancel() {
                isCanceled = true;
@@ -48,9 +59,15 @@
        }
 
        CyTable createTable(CSVReader reader, TableInfo info) throws 
IOException, SecurityException {
-               ColumnInfo[] columns = info.getColumns();
-               CyTable table = tableFactory.createTable(info.getTitle(), 
columns[0].getName(), columns[0].getType(), info.isPublic(), info.isMutable());
-               
+               final ColumnInfo[] columns = info.getColumns();
+               final CyTable table = tableFactory.createTable(info.getTitle(), 
columns[0].getName(),
+                                                              
columns[0].getType(), info.isPublic(),
+                                                              
info.isMutable());
+
+               final Map<String, Class<?>> variableNameToTypeMap = new 
HashMap<String, Class<?>>();
+               for (final ColumnInfo colInfo : columns)
+                       variableNameToTypeMap.put(colInfo.getName(), 
colInfo.getType());
+
                for (int i = 1; i < columns.length; i++) {
                        ColumnInfo column = columns[i];
                        Class<?> type = column.getType();
@@ -62,17 +79,33 @@
                }
                String[] values = reader.readNext();
                while (values != null) {
-                       if (isCanceled) {
+                       if (isCanceled)
                                return null;
-                       }
+
                        Object key = parseValue(columns[0].getType(), null, 
values[0]);
                        CyRow row = table.getRow(key);
                        for (int i = 1; i < values.length; i++) {
                                ColumnInfo column = columns[i];
                                String name = column.getName();
-                               Object value = parseValue(column.getType(), 
column.getListElementType(), values[i]);
-                               if (value != null) {
-                                       row.set(name, value);
+                               if (handleEquations && 
values[i].startsWith("=")) {
+                                       final Class<?> type = 
variableNameToTypeMap.remove(name);
+                                       try {
+                                               if (!compiler.compile(values[i],
+                                                                     
variableNameToTypeMap))
+                                                       throw new 
IOException("Error while reading \""
+                                                                             + 
info.getTitle()
+                                                                             + 
"\" cant compile equation because: "
+                                                                             + 
compiler.getLastErrorMsg());
+                                               final Equation equation = 
compiler.getEquation();
+                                               row.set(name, equation);
+                                       } catch (final Exception e) {
+                                               throw new 
IOException(e.getMessage(), e.getCause());
+                                       }
+                                       variableNameToTypeMap.put(name, type);
+                               } else {
+                                       Object value = 
parseValue(column.getType(), column.getListElementType(), values[i]);
+                                       if (value != null)
+                                               row.set(name, value);
                                }
                        }
                        values = reader.readNext();
@@ -88,9 +121,9 @@
                                list.add(parseValue(listElementType, null, 
item));
                        }
                        return list;
-               } else if (type.equals(String.class)) {
+               } else if (type.equals(String.class))
                        return value;
-               } else {
+               else {
                        try {
                                Method method = type.getMethod("valueOf", 
String.class);
                                return method.invoke(null, value);
@@ -99,7 +132,7 @@
                        }
                }
        }
-       
+
        TableInfo readHeader(CSVReader reader) throws IOException, 
ClassNotFoundException {
                String[] values = reader.readNext();
                TableInfo table = new TableInfo();

Modified: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderFactory.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderFactory.java
  2011-07-04 21:32:50 UTC (rev 26016)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderFactory.java
  2011-07-05 16:10:08 UTC (rev 26017)
@@ -1,28 +1,38 @@
 package org.cytoscape.io.internal.read.datatable;
 
+
 import java.io.InputStream;
 
+import org.cytoscape.equations.EquationCompiler;
 import org.cytoscape.io.CyFileFilter;
 import org.cytoscape.io.read.InputStreamTaskFactory;
 import org.cytoscape.model.CyTableFactory;
 import org.cytoscape.work.TaskIterator;
 
+
 public class CSVCyReaderFactory implements InputStreamTaskFactory {
-
        private final CyFileFilter filter;
        private InputStream stream;
-       private boolean readSchema;
-       private CyTableFactory tableFactory;
+       private final boolean readSchema;
+       private final boolean handleEquations;
+       private final CyTableFactory tableFactory;
+       private final EquationCompiler compiler;
 
-       public CSVCyReaderFactory(CyFileFilter filter, boolean readSchema, 
CyTableFactory tableFactory) {
-               this.filter = filter;
-               this.readSchema = readSchema;
-               this.tableFactory = tableFactory;
+       public CSVCyReaderFactory(final CyFileFilter filter, final boolean 
readSchema,
+                                 final boolean handleEquations, final 
CyTableFactory tableFactory,
+                                 final EquationCompiler compiler)
+       {
+               this.filter          = filter;
+               this.readSchema      = readSchema;
+               this.handleEquations = handleEquations;
+               this.tableFactory    = tableFactory;
+               this.compiler        = compiler;
        }
        
        @Override
        public TaskIterator getTaskIterator() {
-               return new TaskIterator(new CSVCyReader(stream, readSchema, 
tableFactory));
+               return new TaskIterator(new CSVCyReader(stream, readSchema, 
handleEquations,
+                                                       tableFactory, 
compiler));
        }
 
        @Override
@@ -34,5 +44,4 @@
        public void setInputStream(InputStream stream, String inputName) {
                this.stream = stream;
        }
-
 }

Modified: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
    2011-07-04 21:32:50 UTC (rev 26016)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
    2011-07-05 16:10:08 UTC (rev 26017)
@@ -1,5 +1,6 @@
 package org.cytoscape.io.internal.write.datatable.csv;
 
+
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.util.ArrayList;
@@ -17,17 +18,21 @@
 
 import au.com.bytecode.opencsv.CSVWriter;
 
+
 public class CSVCyWriter implements CyWriter {
-
        private final OutputStream outputStream;
        private final CyTable table;
        private final boolean writeSchema;
        private boolean isCanceled;
+       private final boolean handleEquations;
 
-       public CSVCyWriter(OutputStream outputStream, CyTable table, boolean 
writeSchema) {
-               this.outputStream = outputStream;
-               this.table = table;
-               this.writeSchema = writeSchema;
+       public CSVCyWriter(final OutputStream outputStream, final CyTable table,
+                          final boolean writeSchema, final boolean 
handleEquations)
+       {
+               this.outputStream    = outputStream;
+               this.table           = table;
+               this.writeSchema     = writeSchema;
+               this.handleEquations = handleEquations;
        }
 
        @Override
@@ -62,7 +67,7 @@
                        writer.flush();
                }
        }
-       
+
        private void writeSchema(CSVWriter writer, List<CyColumn> columns) {
                String[] values = new String[columns.size()];
                for (int i = 0; i < columns.size(); i++) {
@@ -77,7 +82,7 @@
                writer.writeNext(values);
                values = new String[2];
                values[0] = table.getTitle();
-               
+
                StringBuilder builder = new StringBuilder();
                if (table.isPublic()) {
                        builder.append("public");
@@ -94,12 +99,22 @@
 
        private void writeValues(CSVWriter writer, Collection<CyColumn> 
columns) {
                for (CyRow row : table.getAllRows()) {
-                       if (isCanceled) {
+                       if (isCanceled)
                                return;
-                       }
+
                        String[] values = new String[columns.size()];
                        int index = 0;
                        for (CyColumn column : columns) {
+                               if (handleEquations) {
+                                       final Object rawValue = 
row.getRaw(column.getName());
+                                       if (rawValue instanceof String
+                                           && 
((String)rawValue).startsWith("="))
+                                       {
+                                               values[index++] = 
(String)rawValue;
+                                               continue;
+                                       }
+                               }
+
                                Class<?> type = column.getType();
                                if (type.equals(List.class)) {
                                        StringBuilder builder = new 
StringBuilder();

Modified: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVTableWriterFactory.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVTableWriterFactory.java
  2011-07-04 21:32:50 UTC (rev 26016)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVTableWriterFactory.java
  2011-07-05 16:10:08 UTC (rev 26017)
@@ -1,20 +1,25 @@
 package org.cytoscape.io.internal.write.datatable.csv;
 
+
 import org.cytoscape.io.CyFileFilter;
 import org.cytoscape.io.internal.write.datatable.AbstractCyTableWriterFactory;
 import org.cytoscape.io.write.CyWriter;
 
+
 public class CSVTableWriterFactory extends AbstractCyTableWriterFactory {
-
        private final boolean writeSchema;
+       private final boolean handleEquations;
 
-       public CSVTableWriterFactory(CyFileFilter fileFilter, boolean 
writeSchema) {
+       public CSVTableWriterFactory(final CyFileFilter fileFilter, final 
boolean writeSchema,
+                                    final boolean handleEquations)
+       {
                super(fileFilter);
-               this.writeSchema = writeSchema;
+               this.writeSchema     = writeSchema;
+               this.handleEquations = handleEquations;
        }
        
        @Override
        public CyWriter getWriterTask() {
-               return new CSVCyWriter(outputStream, table, writeSchema);
+               return new CSVCyWriter(outputStream, table, writeSchema, 
handleEquations);
        }
 }

Modified: 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml
===================================================================
--- 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml  
    2011-07-04 21:32:50 UTC (rev 26016)
+++ 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml  
    2011-07-05 16:10:08 UTC (rev 26017)
@@ -7,6 +7,8 @@
 
 
        <!-- Import services -->
+       <osgi:reference id="compilerServiceRef" 
interface="org.cytoscape.equations.EquationCompiler" />
+
        <osgi:reference id="cyEventHelperRef" 
interface="org.cytoscape.event.CyEventHelper" />
        
        <osgi:reference id="cyLayoutsServiceRef" 
interface="org.cytoscape.view.layout.CyLayoutAlgorithmManager" />

Modified: 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml
===================================================================
--- core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml   
2011-07-04 21:32:50 UTC (rev 26016)
+++ core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml   
2011-07-05 16:10:08 UTC (rev 26017)
@@ -514,12 +514,14 @@
                
class="org.cytoscape.io.internal.write.datatable.csv.CSVTableWriterFactory">
                <constructor-arg ref="csvFilter" />
                <constructor-arg value="false" type="boolean" />
+               <constructor-arg value="false" type="boolean" />
        </bean>
 
        <bean id="sessionTableWriterFactory"
                
class="org.cytoscape.io.internal.write.datatable.csv.CSVTableWriterFactory">
                <constructor-arg ref="sessionTableFilter" />
                <constructor-arg value="true" type="boolean" />
+               <constructor-arg value="true" type="boolean" />
        </bean>
        
        <bean id="vizmapWriterFactory"
@@ -576,7 +578,9 @@
        <bean id="sessionTableReaderFactory" 
class="org.cytoscape.io.internal.read.datatable.CSVCyReaderFactory">
                <constructor-arg ref="sessionTableFilter"/>
                <constructor-arg value="true"/>
+               <constructor-arg value="true"/>
                <constructor-arg ref="cyTableFactoryServiceRef"/>
+               <constructor-arg ref="compilerServiceRef"/>
        </bean>
        
        <!-- All Reader Managers -->

Modified: 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
===================================================================
--- 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
     2011-07-04 21:32:50 UTC (rev 26016)
+++ 
core3/io-impl/trunk/src/test/java/org/cytoscape/io/internal/read/datatable/CSVCyReaderTest.java
     2011-07-05 16:10:08 UTC (rev 26017)
@@ -1,5 +1,6 @@
 package org.cytoscape.io.internal.read.datatable;
 
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -20,9 +21,10 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
+
 public class CSVCyReaderTest {
        @Mock TaskMonitor taskMonitor;
-       
+
        private CyTableFactory tableFactory;
 
        @Before
@@ -31,15 +33,17 @@
                tableFactory = tableTestSupport.getDataTableFactory();
                MockitoAnnotations.initMocks(this);
        }
-       
+
        InputStream createStream(String data) throws 
UnsupportedEncodingException {
                return new ByteArrayInputStream(data.getBytes("UTF-8"));
        }
-       
+
        @Test
        public void testReadSimple() throws Exception {
                String data = "SUID\r\njava.lang.Long\r\ntest 
table,\"public\\,mutable\"\r\n5\r\n6";
-               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               CSVCyReader reader = new CSVCyReader(createStream(data), true,
+                                                    /* handleEquations = */ 
false, tableFactory,
+                                                    null);
                reader.run(taskMonitor);
                CyTable[] tables = reader.getCyTables();
                assertNotNull(tables);
@@ -55,33 +59,39 @@
                assertEquals(Mutability.MUTABLE, table.getMutability());
                assertEquals("test table", table.getTitle());
        }
-       
+
        @Test
        public void testReadString() throws Exception {
                String data = 
"SUID,name\r\njava.lang.Long,java.lang.String\r\ntest 
table,\"public\\,mutable\"\r\n1,Alice\r\n2,Bob\r\n3,Carol";
-               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               CSVCyReader reader = new CSVCyReader(createStream(data), true,
+                                                    /* handleEquations = */ 
false, tableFactory,
+                                                    null);
                reader.run(taskMonitor);
                CyTable[] tables = reader.getCyTables();
                CyTable table = tables[0];
                CyRow row = table.getRow(3L);
                assertEquals("Carol", row.get("name", String.class));
        }
-       
+
        @Test
        public void testReadDouble() throws Exception {
                String data = 
"SUID,weight\r\njava.lang.Long,java.lang.Double\r\ntest 
table,\"public\\,mutable\"\r\n0,0.56\r\n-5,-1.234";
-               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               CSVCyReader reader = new CSVCyReader(createStream(data), true,
+                                                    /* handleEquations = */ 
false, tableFactory,
+                                                    null);
                reader.run(taskMonitor);
                CyTable[] tables = reader.getCyTables();
                CyTable table = tables[0];
                CyRow row = table.getRow(-5L);
                assertEquals((Double) (-1.234), row.get("weight", 
Double.class));
        }
-       
+
        @Test
        public void testReadBoolean() throws Exception {
                String data = 
"SUID,hidden\r\njava.lang.Long,java.lang.Boolean\r\ntest 
table,\"public\\,mutable\"\r\n30,true\r\n40,false\r\n50,true";
-               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               CSVCyReader reader = new CSVCyReader(createStream(data), true,
+                                                    /* handleEquations = */ 
false, tableFactory,
+                                                    null);
                reader.run(taskMonitor);
                CyTable[] tables = reader.getCyTables();
                CyTable table = tables[0];
@@ -92,7 +102,9 @@
        @Test
        public void testReadList() throws Exception {
                String data = 
"SUID,list\r\njava.lang.Long,java.util.List<java.lang.String>\r\ntest 
table,\"public\\,mutable\"\r\n1,\"a\rb\rc\"";
-               CSVCyReader reader = new CSVCyReader(createStream(data), true, 
tableFactory);
+               CSVCyReader reader = new CSVCyReader(createStream(data), true,
+                                                    /* handleEquations = */ 
false, tableFactory,
+                                                    null);
                reader.run(taskMonitor);
                CyTable[] tables = reader.getCyTables();
                CyTable table = tables[0];

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