Search in sources :

Example 1 with NamedTransformer

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

the class FilterIO method readTransformers.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void readTransformers(File file, AbstractPanel panel) throws IOException {
    try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
        CyTransformerReader reader = serviceRegistrar.getService(CyTransformerReader.class);
        NamedTransformer<CyNetwork, CyIdentifiable>[] transformers = (NamedTransformer<CyNetwork, CyIdentifiable>[]) reader.read(stream);
        AbstractPanelController controller = panel.getController();
        controller.addNamedTransformers(panel, transformers);
    }
}
Also used : AbstractPanelController(org.cytoscape.filter.internal.view.AbstractPanelController) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) BufferedInputStream(java.io.BufferedInputStream) CyNetwork(org.cytoscape.model.CyNetwork) FileInputStream(java.io.FileInputStream) CyTransformerReader(org.cytoscape.io.read.CyTransformerReader) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 2 with NamedTransformer

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

the class FilterIOTest method testSerializationRoundTrip.

@Test
public void testSerializationRoundTrip() throws Exception {
    DegreeFilter degreeFilter = new DegreeFilter();
    degreeFilter.setCriterion(new Double[] { 0.2, 5.5 });
    degreeFilter.setEdgeType(CyEdge.Type.ANY);
    DummyFilter dummyFilter = new DummyFilter();
    CyTransformerWriter writer = new CyTransformerWriterImpl();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.write(stream, FilterIO.createNamedTransformer("filter1", degreeFilter), FilterIO.createNamedTransformer("filter2", dummyFilter));
    String serialized = stream.toString("utf-8");
    TransformerManagerImpl transformerManager = new TransformerManagerImpl();
    Map<String, String> properties = Collections.emptyMap();
    transformerManager.registerFilterFactory(new DegreeFilterFactory(), properties);
    transformerManager.registerFilterFactory(new DummyFilterFactory(), properties);
    CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
    reader.registerTransformerManager(transformerManager, null);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
    NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
    ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
    writer.write(stream2, transformers);
    String serialized2 = stream2.toString("utf-8");
    Assert.assertEquals(serialized, serialized2);
}
Also used : CyTransformerReaderImpl(org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl) CyTransformerWriter(org.cytoscape.io.write.CyTransformerWriter) CyTransformerWriterImpl(org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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) TransformerManagerImpl(org.cytoscape.filter.internal.work.TransformerManagerImpl) Test(org.junit.Test)

Example 3 with NamedTransformer

use of org.cytoscape.filter.model.NamedTransformer 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 4 with NamedTransformer

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

the class TransformerJsonTunable method getTransformer.

public NamedTransformer<CyNetwork, CyIdentifiable> getTransformer(String name, CyTransformerReader transformerReader) {
    String wrapper = "[ { \"name\" : \"%s\", \"transformers\" : [ %s ] } ]";
    String fullJson = String.format(wrapper, name, json);
    byte[] bytes = fullJson.getBytes();
    try (InputStream in = new ByteArrayInputStream(bytes)) {
        @SuppressWarnings("unchecked") NamedTransformer<CyNetwork, CyIdentifiable>[] transformers = (NamedTransformer<CyNetwork, CyIdentifiable>[]) transformerReader.read(in);
        if (transformers == null || transformers.length == 0 || transformers[0] == null) {
            return null;
        }
        return transformers[0];
    } catch (Exception e) {
        return null;
    }
}
Also used : NamedTransformer(org.cytoscape.filter.model.NamedTransformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CyNetwork(org.cytoscape.model.CyNetwork) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 5 with NamedTransformer

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

the class CyTransformerReaderImpl method read.

@Override
public NamedTransformer<?, ?>[] read(InputStream stream) throws IOException {
    JsonParser parser = factory.createParser(stream);
    try {
        assertNextToken(parser, JsonToken.START_ARRAY);
        List<NamedTransformer<?, ?>> namedTransformers = new ArrayList<NamedTransformer<?, ?>>();
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
            assertField(parser, FilterIO.NAME_FIELD);
            String name = parser.nextTextValue();
            assertField(parser, FilterIO.TRANSFORMERS_FIELD);
            assertNextToken(parser, JsonToken.START_ARRAY);
            List<Transformer<?, ?>> transformers = new ArrayList<Transformer<?, ?>>();
            while (true) {
                Transformer<?, ?> transformer = readTransformer(parser);
                if (transformer == null) {
                    break;
                }
                transformers.add(transformer);
            }
            assertNextToken(parser, JsonToken.END_OBJECT);
            namedTransformers.add(FilterIO.createNamedTransformer(name, transformers));
        }
        return namedTransformers.toArray(new NamedTransformer[namedTransformers.size()]);
    } finally {
        parser.close();
    }
}
Also used : Transformer(org.cytoscape.filter.model.Transformer) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) ArrayList(java.util.ArrayList) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

NamedTransformer (org.cytoscape.filter.model.NamedTransformer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 TransformerManagerImpl (org.cytoscape.filter.internal.work.TransformerManagerImpl)4 Transformer (org.cytoscape.filter.model.Transformer)4 CyTransformerReaderImpl (org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl)4 CyIdentifiable (org.cytoscape.model.CyIdentifiable)4 CyNetwork (org.cytoscape.model.CyNetwork)4 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 List (java.util.List)3 ColumnFilter (org.cytoscape.filter.internal.filters.column.ColumnFilter)3 DegreeFilter (org.cytoscape.filter.internal.filters.degree.DegreeFilter)3 TopologyFilter (org.cytoscape.filter.internal.filters.topology.TopologyFilter)3 AdjacencyTransformer (org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer)3 AbstractTransformer (org.cytoscape.filter.model.AbstractTransformer)3 CompositeFilter (org.cytoscape.filter.model.CompositeFilter)3 CyTransformerWriterImpl (org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl)3 CyTransformerWriter (org.cytoscape.io.write.CyTransformerWriter)3 ColumnFilterFactory (org.cytoscape.filter.internal.filters.column.ColumnFilterFactory)2 DegreeFilterFactory (org.cytoscape.filter.internal.filters.degree.DegreeFilterFactory)2