Search in sources :

Example 1 with VirtualColumn

use of org.cytoscape.io.internal.util.cytables.model.VirtualColumn in project cytoscape-impl by cytoscape.

the class Cy3SessionReaderImplTest method testRestoreVirtualColumnsWithCircularDependencies.

@Test(expected = Exception.class)
public void testRestoreVirtualColumnsWithCircularDependencies() throws Exception {
    // This should never happen, but let's make sure it doesn't get into an infinite loop
    {
        VirtualColumn vc = new VirtualColumn();
        vc.setName("a");
        vc.setSourceColumn("a");
        vc.setSourceJoinKey("id");
        vc.setTargetJoinKey("id");
        vc.setSourceTable("t2");
        vc.setTargetTable("t3");
        reader.virtualColumns.add(vc);
    }
    {
        VirtualColumn vc = new VirtualColumn();
        vc.setName("a");
        vc.setSourceColumn("a");
        vc.setSourceJoinKey("id");
        vc.setTargetJoinKey("id");
        vc.setSourceTable("t1");
        vc.setTargetTable("t2");
        reader.virtualColumns.add(vc);
    }
    {
        VirtualColumn vc = new VirtualColumn();
        vc.setName("a");
        vc.setSourceColumn("a");
        vc.setSourceJoinKey("id");
        vc.setTargetJoinKey("id");
        vc.setSourceTable("t2");
        vc.setTargetTable("t3");
        reader.virtualColumns.add(vc);
    }
    CyTableFactory tblFactory = tblTestSupport.getTableFactory();
    CyTable tbl1 = tblFactory.createTable("t1", "id", Integer.class, true, true);
    CyTable tbl2 = tblFactory.createTable("t2", "id", Integer.class, true, true);
    CyTable tbl3 = tblFactory.createTable("t3", "id", Integer.class, true, true);
    reader.filenameTableMap.put(tbl1.getTitle(), tbl1);
    reader.filenameTableMap.put(tbl2.getTitle(), tbl2);
    reader.filenameTableMap.put(tbl3.getTitle(), tbl3);
    reader.restoreVirtualColumns();
}
Also used : CyTableFactory(org.cytoscape.model.CyTableFactory) CyTable(org.cytoscape.model.CyTable) VirtualColumn(org.cytoscape.io.internal.util.cytables.model.VirtualColumn) Test(org.junit.Test)

Example 2 with VirtualColumn

use of org.cytoscape.io.internal.util.cytables.model.VirtualColumn in project cytoscape-impl by cytoscape.

the class Cy3SessionReaderImpl method restoreVirtualColumns.

protected void restoreVirtualColumns() throws Exception {
    if (virtualColumns == null)
        return;
    final Queue<VirtualColumn> queue = new LinkedList<>();
    queue.addAll(virtualColumns);
    // Will be used to prevent infinite loops if there are circular references or missing table/columns
    // First column marked as depending on a missing column
    VirtualColumn markedColumn = null;
    int lastSize = queue.size();
    while (!queue.isEmpty()) {
        if (cancelled)
            return;
        final VirtualColumn vcData = queue.poll();
        final CyTable tgtTable = filenameTableMap.get(vcData.getTargetTable());
        final String colName = vcData.getName();
        if (tgtTable.getColumn(colName) == null) {
            final CyTable srcTable = filenameTableMap.get(vcData.getSourceTable());
            final String srcColName = vcData.getSourceColumn();
            final String tgtJoinKey = vcData.getTargetJoinKey();
            if (srcTable.getColumn(srcColName) != null && tgtTable.getColumn(tgtJoinKey) != null) {
                try {
                    tgtTable.addVirtualColumn(colName, srcColName, srcTable, tgtJoinKey, vcData.isImmutable());
                    // Reset it!
                    markedColumn = null;
                } catch (Exception e) {
                    throw new Exception("Error restoring virtual column \"" + colName + "\" in table \"" + tgtTable + "\"(" + vcData.getTargetTable() + ")--source table: \"" + srcTable + "\"(" + vcData.getSourceTable() + ")", e);
                }
            } else {
                queue.add(vcData);
                if (markedColumn == null) {
                    // Mark this element and save the queue's size
                    markedColumn = vcData;
                    lastSize = queue.size();
                } else if (vcData == markedColumn && queue.size() == lastSize) {
                    // The iteration reached the same marked column again and the queue's size hasn't decreased,
                    // which means that the remaining elements in the queue cannot be resolved
                    final StringBuilder msg = new StringBuilder("Cannot restore the following virtual columns because of missing or circular dependencies: ");
                    String prefix = "";
                    for (final VirtualColumn vc : queue) {
                        msg.append(prefix + vc.getTargetTable() + "." + vc.getName());
                        prefix = ", ";
                    }
                    throw new Exception(msg.toString());
                }
            }
        }
    }
}
Also used : CyTable(org.cytoscape.model.CyTable) VirtualColumn(org.cytoscape.io.internal.util.cytables.model.VirtualColumn) LinkedList(java.util.LinkedList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 3 with VirtualColumn

use of org.cytoscape.io.internal.util.cytables.model.VirtualColumn in project cytoscape-impl by cytoscape.

the class Cy3SessionReaderImplTest method testRestoreVirtualColumns.

@Test
public void testRestoreVirtualColumns() throws Exception {
    VirtualColumn vcA1 = new VirtualColumn();
    vcA1.setName("vcA1");
    vcA1.setSourceColumn("cA");
    vcA1.setSourceJoinKey("id");
    vcA1.setTargetJoinKey("id");
    vcA1.setSourceTable("Tbl1");
    vcA1.setTargetTable("Tbl2");
    vcA1.setImmutable(false);
    VirtualColumn vcA2 = new VirtualColumn();
    vcA2.setName("vcA2");
    vcA2.setSourceColumn("vcA1");
    vcA2.setSourceJoinKey("id");
    vcA2.setTargetJoinKey("id");
    vcA2.setSourceTable("Tbl2");
    vcA2.setTargetTable("Tbl3");
    vcA2.setImmutable(false);
    VirtualColumn vcA3 = new VirtualColumn();
    vcA3.setName("vcA3");
    vcA3.setSourceColumn("vcA2");
    vcA3.setSourceJoinKey("id");
    vcA3.setTargetJoinKey("id");
    vcA3.setSourceTable("Tbl3");
    vcA3.setTargetTable("Tbl4");
    vcA3.setImmutable(false);
    VirtualColumn vcB = new VirtualColumn();
    vcB.setName("cB");
    vcB.setSourceColumn("cB");
    vcB.setSourceJoinKey("cA");
    vcB.setTargetJoinKey("vcA3");
    vcB.setSourceTable("Tbl5");
    vcB.setTargetTable("Tbl4");
    vcB.setImmutable(true);
    CyTableFactory tblFactory = tblTestSupport.getTableFactory();
    CyTable tbl1 = tblFactory.createTable("Tbl1", "id", Integer.class, false, true);
    tbl1.createColumn("cA", String.class, true);
    CyTable tbl2 = tblFactory.createTable("Tbl2", "id", Integer.class, true, true);
    CyTable tbl3 = tblFactory.createTable("Tbl3", "id", Integer.class, true, false);
    CyTable tbl4 = tblFactory.createTable("Tbl4", "id", Integer.class, true, false);
    CyTable tbl5 = tblFactory.createTable("Tbl5", "cA", String.class, true, false);
    tbl5.createColumn("cB", Boolean.class, true);
    reader.filenameTableMap.put(tbl1.getTitle(), tbl1);
    reader.filenameTableMap.put(tbl2.getTitle(), tbl2);
    reader.filenameTableMap.put(tbl3.getTitle(), tbl3);
    reader.filenameTableMap.put(tbl4.getTitle(), tbl4);
    reader.filenameTableMap.put(tbl5.getTitle(), tbl5);
    // Set this order to see if it works when virtual columns depend on other virtual columns
    // the virtual column "vcA3" (used as target join key) hasn't been created yet
    reader.virtualColumns.add(vcB);
    // the virtual column "vcA2" hasn't been created yet
    reader.virtualColumns.add(vcA3);
    // the virtual column "vcA1" hasn't been created yet
    reader.virtualColumns.add(vcA2);
    reader.virtualColumns.add(vcA1);
    reader.restoreVirtualColumns();
    // --- Test ---
    {
        CyColumn c = tbl2.getColumn("vcA1");
        assertNotNull(c);
        assertTrue(c.getVirtualColumnInfo().isVirtual());
        assertEquals("cA", c.getVirtualColumnInfo().getSourceColumn());
        assertEquals(tbl1, c.getVirtualColumnInfo().getSourceTable());
        assertFalse(c.isImmutable());
    }
    {
        CyColumn c = tbl3.getColumn("vcA2");
        assertNotNull(c);
        assertTrue(c.getVirtualColumnInfo().isVirtual());
        assertEquals("vcA1", c.getVirtualColumnInfo().getSourceColumn());
        assertEquals(tbl2, c.getVirtualColumnInfo().getSourceTable());
        assertFalse(c.isImmutable());
    }
    {
        CyColumn c = tbl4.getColumn("vcA3");
        assertNotNull(c);
        assertTrue(c.getVirtualColumnInfo().isVirtual());
        assertEquals("vcA2", c.getVirtualColumnInfo().getSourceColumn());
        assertEquals(tbl3, c.getVirtualColumnInfo().getSourceTable());
        assertFalse(c.isImmutable());
    }
    {
        CyColumn c = tbl4.getColumn("cB");
        assertNotNull(c);
        assertTrue(c.getVirtualColumnInfo().isVirtual());
        assertEquals("cB", c.getVirtualColumnInfo().getSourceColumn());
        assertEquals(tbl5, c.getVirtualColumnInfo().getSourceTable());
        assertTrue(c.isImmutable());
    }
}
Also used : CyTableFactory(org.cytoscape.model.CyTableFactory) CyTable(org.cytoscape.model.CyTable) CyColumn(org.cytoscape.model.CyColumn) VirtualColumn(org.cytoscape.io.internal.util.cytables.model.VirtualColumn) Test(org.junit.Test)

Example 4 with VirtualColumn

use of org.cytoscape.io.internal.util.cytables.model.VirtualColumn in project cytoscape-impl by cytoscape.

the class CyTablesXMLWriter method buildModel.

private CyTables buildModel() {
    CyTables model = new CyTables();
    VirtualColumns virtualColumns = new VirtualColumns();
    model.setVirtualColumns(virtualColumns);
    List<VirtualColumn> columns = virtualColumns.getVirtualColumn();
    for (CyTableMetadata metadata : tables) {
        CyTable table = metadata.getTable();
        String targetTable = tableFileNamesBySUID.get(table.getSUID());
        if (targetTable == null) {
            continue;
        }
        for (CyColumn cyColumn : table.getColumns()) {
            VirtualColumnInfo info = cyColumn.getVirtualColumnInfo();
            if (!info.isVirtual()) {
                continue;
            }
            String sourceTable = tableFileNamesBySUID.get(info.getSourceTable().getSUID());
            if (sourceTable == null) {
                // log this
                continue;
            }
            VirtualColumn column = new VirtualColumn();
            column.setName(cyColumn.getName());
            column.setSourceColumn(info.getSourceColumn());
            column.setSourceTable(sourceTable);
            column.setSourceJoinKey(info.getSourceJoinKey());
            column.setTargetTable(targetTable);
            column.setTargetJoinKey(info.getTargetJoinKey());
            columns.add(column);
        }
    }
    return model;
}
Also used : CyTable(org.cytoscape.model.CyTable) CyTables(org.cytoscape.io.internal.util.cytables.model.CyTables) CyColumn(org.cytoscape.model.CyColumn) CyTableMetadata(org.cytoscape.model.CyTableMetadata) VirtualColumnInfo(org.cytoscape.model.VirtualColumnInfo) VirtualColumn(org.cytoscape.io.internal.util.cytables.model.VirtualColumn) VirtualColumns(org.cytoscape.io.internal.util.cytables.model.VirtualColumns)

Aggregations

VirtualColumn (org.cytoscape.io.internal.util.cytables.model.VirtualColumn)4 CyTable (org.cytoscape.model.CyTable)4 CyColumn (org.cytoscape.model.CyColumn)2 CyTableFactory (org.cytoscape.model.CyTableFactory)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LinkedList (java.util.LinkedList)1 CyTables (org.cytoscape.io.internal.util.cytables.model.CyTables)1 VirtualColumns (org.cytoscape.io.internal.util.cytables.model.VirtualColumns)1 CyTableMetadata (org.cytoscape.model.CyTableMetadata)1 VirtualColumnInfo (org.cytoscape.model.VirtualColumnInfo)1