Search in sources :

Example 6 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class FilterIOTest method testSubFilterTransformerRoundTrip.

@SuppressWarnings("rawtypes")
@Test
public void testSubFilterTransformerRoundTrip() throws Exception {
    ColumnFilter columnFilter = new ColumnFilter();
    columnFilter.setColumnName("MyColName");
    columnFilter.setPredicateAndCriterion(Predicate.CONTAINS, "blah");
    AdjacencyTransformer adjacency = new AdjacencyTransformer();
    adjacency.setAction(Action.REPLACE);
    adjacency.setEdgesAre(EdgesAre.INCOMING);
    adjacency.setFilterTarget(What.EDGES);
    adjacency.setOutput(What.EDGES);
    adjacency.getCompositeFilter().append(columnFilter);
    CyTransformerWriter writer = new CyTransformerWriterImpl();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.write(stream, FilterIO.createNamedTransformer("transformer1", adjacency));
    // String serialized = stream.toString("utf-8");
    TransformerManagerImpl transformerManager = new TransformerManagerImpl();
    Map<String, String> properties = Collections.emptyMap();
    transformerManager.registerFilterFactory(new ColumnFilterFactory(), properties);
    transformerManager.registerElementTransformerFactory(new AdjacencyTransformerFactory(), properties);
    CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
    reader.registerTransformerManager(transformerManager, null);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
    NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
    Assert.assertNotNull(transformers);
    Assert.assertEquals(1, transformers.length);
    NamedTransformer<?, ?> namedTransformer = transformers[0];
    Assert.assertNotNull(namedTransformer);
    Assert.assertEquals("transformer1", namedTransformer.getName());
    List children = namedTransformer.getTransformers();
    Assert.assertEquals(1, children.size());
    Transformer child = (Transformer) children.get(0);
    Assert.assertNotNull(child);
    Assert.assertTrue(child instanceof AdjacencyTransformer);
    AdjacencyTransformer adjacency2 = (AdjacencyTransformer) child;
    Assert.assertEquals(adjacency.getAction(), adjacency2.getAction());
    Assert.assertEquals(adjacency.getEdgesAre(), adjacency2.getEdgesAre());
    Assert.assertEquals(adjacency.getFilterTarget(), adjacency2.getFilterTarget());
    Assert.assertEquals(adjacency.getOutput(), adjacency2.getOutput());
    CompositeFilter<?, ?> composite = adjacency2.getCompositeFilter();
    Assert.assertEquals(1, composite.getLength());
    Filter filter = composite.get(0);
    Assert.assertTrue(filter instanceof ColumnFilter);
    ColumnFilter columnFilter2 = (ColumnFilter) filter;
    Assert.assertEquals(columnFilter.getColumnName(), columnFilter2.getColumnName());
    Assert.assertEquals(columnFilter.getPredicate(), columnFilter2.getPredicate());
    Assert.assertEquals(columnFilter.getCriterion(), columnFilter2.getCriterion());
}
Also used : CyTransformerReaderImpl(org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl) Transformer(org.cytoscape.filter.model.Transformer) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AbstractTransformer(org.cytoscape.filter.model.AbstractTransformer) CyTransformerWriter(org.cytoscape.io.write.CyTransformerWriter) CyTransformerWriterImpl(org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AdjacencyTransformerFactory(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformerFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) TopologyFilter(org.cytoscape.filter.internal.filters.topology.TopologyFilter) DegreeFilter(org.cytoscape.filter.internal.filters.degree.DegreeFilter) Filter(org.cytoscape.filter.model.Filter) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) List(java.util.List) TransformerManagerImpl(org.cytoscape.filter.internal.work.TransformerManagerImpl) ColumnFilterFactory(org.cytoscape.filter.internal.filters.column.ColumnFilterFactory) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) Test(org.junit.Test)

Example 7 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class CyTransformerWriterImpl method write.

private void write(JsonGenerator generator, Transformer<?, ?> transformer) throws IOException {
    generator.writeStartObject();
    try {
        generator.writeStringField(FilterIO.ID_FIELD, transformer.getId());
        generator.writeObjectFieldStart(FilterIO.PARAMETERS_FIELD);
        try {
            Map<String, Object> parameters = FilterIO.getParameters(transformer);
            for (Entry<String, Object> entry : parameters.entrySet()) {
                String name = entry.getKey();
                Object value = entry.getValue();
                writeField(generator, name, value);
            }
        } catch (IntrospectionException e) {
            throw new IOException(e);
        } finally {
            generator.writeEndObject();
        }
        if (transformer instanceof CompositeFilter || transformer instanceof SubFilterTransformer) {
            CompositeFilter<?, ?> composite;
            if (transformer instanceof SubFilterTransformer)
                composite = ((SubFilterTransformer<?, ?>) transformer).getCompositeFilter();
            else
                composite = (CompositeFilter<?, ?>) transformer;
            generator.writeArrayFieldStart(FilterIO.TRANSFORMERS_FIELD);
            try {
                for (int i = 0; i < composite.getLength(); i++) {
                    write(generator, composite.get(i));
                }
            } finally {
                generator.writeEndArray();
            }
        }
    } finally {
        generator.writeEndObject();
    }
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) IntrospectionException(java.beans.IntrospectionException) IOException(java.io.IOException) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer)

Example 8 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class FilterPanelController method createView.

@Override
public JComponent createView(FilterPanel parent, Transformer<CyNetwork, CyIdentifiable> filter, int depth) {
    // CompositeFilterImpl needs a CompositeFilterPanel but the top is blank so view will be null
    JComponent view = transformerViewManager.createView(filter);
    if (view instanceof InteractivityChangedListener) {
        ((InteractivityChangedListener) view).handleInteractivityChanged(isInteractive);
    }
    if (filter instanceof CompositeFilter) {
        String addButtonTT = transformerViewManager.getAddButtonTooltip(filter);
        CompositeFilterController controller = CompositeFilterController.createFor(view, addButtonTT);
        return new CompositeFilterPanel<FilterPanel>(parent, this, controller, (CompositeFilter<CyNetwork, CyIdentifiable>) filter, depth);
    }
    if (view == null)
        throw new IllegalArgumentException("view could not be created for: " + filter.getId());
    return view;
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JComponent(javax.swing.JComponent) CyNetwork(org.cytoscape.model.CyNetwork) InteractivityChangedListener(org.cytoscape.filter.view.InteractivityChangedListener) CompositeFilterController(org.cytoscape.filter.internal.filters.composite.CompositeFilterController) CompositeFilterPanel(org.cytoscape.filter.internal.filters.composite.CompositeFilterPanel) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 9 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class FilterIOTest method testLoadOldTopologyFilter.

/**
 * The topology filter was made a composite filter in 3.3. Older versions of cytoscape
 * will export a topology filter without the "transformers" field that is part of
 * composite filters. The filter json parser was enhanced to support this.
 */
@SuppressWarnings("rawtypes")
@Test
public void testLoadOldTopologyFilter() throws IOException {
    TransformerManagerImpl transformerManager = new TransformerManagerImpl();
    Map<String, String> properties = Collections.emptyMap();
    transformerManager.registerFilterFactory(new CompositeFilterFactory<CyNetwork, CyIdentifiable>(CyNetwork.class, CyIdentifiable.class), properties);
    transformerManager.registerFilterFactory(new TopologyFilterFactory(), properties);
    transformerManager.registerFilterFactory(new ColumnFilterFactory(), properties);
    String path = "./src/test/resources/testData/filter/topology-filter-3.2.json";
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    // String contents = new String(encoded);
    // System.out.println(contents);
    CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
    reader.registerTransformerManager(transformerManager, null);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(encoded);
    NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
    Assert.assertNotNull(transformers);
    Assert.assertEquals(1, transformers.length);
    NamedTransformer<?, ?> namedTransformer = transformers[0];
    Assert.assertNotNull(namedTransformer);
    Assert.assertEquals("Default filter", namedTransformer.getName());
    List children = namedTransformer.getTransformers();
    Assert.assertEquals(1, children.size());
    Transformer child = (Transformer) children.get(0);
    Assert.assertNotNull(child);
    Assert.assertTrue(child instanceof CompositeFilter);
    CompositeFilter composite = (CompositeFilter) child;
    Assert.assertEquals(2, composite.getLength());
    Assert.assertTrue(composite.get(0) instanceof TopologyFilter);
    Assert.assertTrue(composite.get(1) instanceof ColumnFilter);
    TopologyFilter topology = (TopologyFilter) composite.get(0);
    Assert.assertEquals(0, topology.getLength());
    Assert.assertEquals(Integer.valueOf(4), topology.getDistance());
    Assert.assertEquals(Integer.valueOf(5), topology.getThreshold());
    ColumnFilter column = (ColumnFilter) composite.get(1);
    Assert.assertEquals("name", column.getColumnName());
}
Also used : CyTransformerReaderImpl(org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) Transformer(org.cytoscape.filter.model.Transformer) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AbstractTransformer(org.cytoscape.filter.model.AbstractTransformer) CyNetwork(org.cytoscape.model.CyNetwork) TopologyFilterFactory(org.cytoscape.filter.internal.filters.topology.TopologyFilterFactory) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) TransformerManagerImpl(org.cytoscape.filter.internal.work.TransformerManagerImpl) ColumnFilterFactory(org.cytoscape.filter.internal.filters.column.ColumnFilterFactory) CyIdentifiable(org.cytoscape.model.CyIdentifiable) TopologyFilter(org.cytoscape.filter.internal.filters.topology.TopologyFilter) Test(org.junit.Test)

Example 10 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class FilterIOTest method testNestedFilterRoundTrip.

@SuppressWarnings("rawtypes")
@Test
public void testNestedFilterRoundTrip() throws Exception {
    DegreeFilter degreeFilter = new DegreeFilter();
    degreeFilter.setCriterion(new Double[] { 0.2, 5.5 });
    degreeFilter.setEdgeType(CyEdge.Type.ANY);
    CompositeFilter<CyNetwork, CyIdentifiable> composite = new CompositeFilterImpl<CyNetwork, CyIdentifiable>(CyNetwork.class, CyIdentifiable.class);
    composite.setType(Type.ANY);
    composite.append(degreeFilter);
    CyTransformerWriter writer = new CyTransformerWriterImpl();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.write(stream, FilterIO.createNamedTransformer("filter1", composite));
    String serialized = stream.toString("utf-8");
    TransformerManagerImpl transformerManager = new TransformerManagerImpl();
    Map<String, String> properties = Collections.emptyMap();
    transformerManager.registerFilterFactory(new DegreeFilterFactory(), properties);
    transformerManager.registerFilterFactory(new CompositeFilterFactory<CyNetwork, CyIdentifiable>(CyNetwork.class, CyIdentifiable.class), properties);
    CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
    reader.registerTransformerManager(transformerManager, null);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
    NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
    Assert.assertNotNull(transformers);
    Assert.assertEquals(1, transformers.length);
    NamedTransformer<?, ?> namedTransformer = transformers[0];
    Assert.assertNotNull(namedTransformer);
    Assert.assertEquals("filter1", namedTransformer.getName());
    List children = namedTransformer.getTransformers();
    Assert.assertEquals(1, children.size());
    Transformer child = (Transformer) children.get(0);
    Assert.assertNotNull(child);
    Assert.assertTrue(child instanceof CompositeFilter);
    CompositeFilter composite2 = (CompositeFilter) child;
    Assert.assertEquals(composite.getType(), composite2.getType());
    Assert.assertEquals(composite.getLength(), composite2.getLength());
    Filter filter = composite2.get(0);
    Assert.assertTrue(filter instanceof DegreeFilter);
    DegreeFilter degreeFilter2 = (DegreeFilter) filter;
    Assert.assertArrayEquals((Number[]) degreeFilter.getCriterion(), (Number[]) degreeFilter2.getCriterion());
    Assert.assertEquals(degreeFilter.getEdgeType(), degreeFilter2.getEdgeType());
}
Also used : CyTransformerReaderImpl(org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) Transformer(org.cytoscape.filter.model.Transformer) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AbstractTransformer(org.cytoscape.filter.model.AbstractTransformer) CyNetwork(org.cytoscape.model.CyNetwork) CyTransformerWriter(org.cytoscape.io.write.CyTransformerWriter) CyTransformerWriterImpl(org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompositeFilterImpl(org.cytoscape.filter.internal.filters.composite.CompositeFilterImpl) DegreeFilterFactory(org.cytoscape.filter.internal.filters.degree.DegreeFilterFactory) DegreeFilter(org.cytoscape.filter.internal.filters.degree.DegreeFilter) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) TopologyFilter(org.cytoscape.filter.internal.filters.topology.TopologyFilter) DegreeFilter(org.cytoscape.filter.internal.filters.degree.DegreeFilter) Filter(org.cytoscape.filter.model.Filter) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) List(java.util.List) TransformerManagerImpl(org.cytoscape.filter.internal.work.TransformerManagerImpl) CyIdentifiable(org.cytoscape.model.CyIdentifiable) Test(org.junit.Test)

Aggregations

CompositeFilter (org.cytoscape.filter.model.CompositeFilter)10 CyIdentifiable (org.cytoscape.model.CyIdentifiable)6 CyNetwork (org.cytoscape.model.CyNetwork)6 ColumnFilter (org.cytoscape.filter.internal.filters.column.ColumnFilter)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 TopologyFilter (org.cytoscape.filter.internal.filters.topology.TopologyFilter)3 AdjacencyTransformer (org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer)3 TransformerManagerImpl (org.cytoscape.filter.internal.work.TransformerManagerImpl)3 AbstractTransformer (org.cytoscape.filter.model.AbstractTransformer)3 Filter (org.cytoscape.filter.model.Filter)3 NamedTransformer (org.cytoscape.filter.model.NamedTransformer)3 SubFilterTransformer (org.cytoscape.filter.model.SubFilterTransformer)3 Transformer (org.cytoscape.filter.model.Transformer)3 CyTransformerReaderImpl (org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JComponent (javax.swing.JComponent)2