Search in sources :

Example 1 with Filter

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

the class CyTransformerReaderImpl method readCompositeFilter.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void readCompositeFilter(JsonParser parser, CompositeFilter composite) throws IOException {
    JsonToken firstToken = parser.nextToken();
    if (firstToken == JsonToken.END_OBJECT) {
        return;
    }
    if (firstToken != JsonToken.FIELD_NAME) {
        throw new IOException("Expected: " + JsonToken.FIELD_NAME + ". Got: " + firstToken);
    }
    assertEquals(FilterIO.TRANSFORMERS_FIELD, parser.getCurrentName());
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    while (true) {
        Filter filter = (Filter) readTransformer(parser);
        if (filter == null) {
            break;
        }
        composite.append(filter);
    }
    assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
    assertNextToken(parser, JsonToken.END_OBJECT);
}
Also used : Filter(org.cytoscape.filter.model.Filter) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException)

Example 2 with Filter

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

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

the class ColumnFilter method accepts.

@SuppressWarnings("unchecked")
@Override
public boolean accepts(CyNetwork context, CyIdentifiable element) {
    if (columnName == null || rawCriterion == null || predicate == null) {
        // This filter is incomplete.
        return false;
    }
    Class<?> tableType = getTableType();
    if (tableType != null && !tableType.isAssignableFrom(element.getClass())) {
        return false;
    }
    CyRow row = context.getRow(element);
    CyTable table = row.getTable();
    CyColumn column = table.getColumn(columnName);
    if (column == null) {
        return false;
    }
    Class<?> columnType = column.getType();
    if (columnType.equals(List.class)) {
        Class<?> listElementType = column.getListElementType();
        if (String.class.equals(listElementType)) {
            List<String> list = row.getList(columnName, String.class);
            return listMatch(anyMatch, list, item -> stringDelegate.accepts(stringCriterion, lowerCaseCriterion, item, caseSensitive));
        } else if (Number.class.isAssignableFrom(listElementType)) {
            List<Number> list = (List<Number>) row.getList(columnName, listElementType);
            return listMatch(anyMatch, list, number -> numericDelegate.accepts(lowerBound, upperBound, number));
        } else if (Boolean.class.equals(listElementType)) {
            List<Boolean> list = (List<Boolean>) row.getList(columnName, listElementType);
            if (list != null) {
                for (Boolean value : list) {
                    if (value != null && booleanCriterion.equals(value)) {
                        return true;
                    }
                }
            }
        }
    } else if (columnType.equals(String.class)) {
        String value = row.get(columnName, String.class);
        return stringDelegate.accepts(stringCriterion, lowerCaseCriterion, value, caseSensitive);
    } else if (Number.class.isAssignableFrom(columnType)) {
        Number value = row.get(columnName, (Class<Number>) columnType);
        return numericDelegate.accepts(lowerBound, upperBound, value);
    } else if (Boolean.class.equals(columnType)) {
        Boolean value = row.get(columnName, Boolean.class);
        return booleanCriterion.equals(value);
    }
    return false;
}
Also used : Predicate(org.cytoscape.filter.predicates.Predicate) Transformers(org.cytoscape.filter.transformers.Transformers) StringPredicateDelegate(org.cytoscape.filter.internal.predicates.StringPredicateDelegate) AbstractTransformer(org.cytoscape.filter.model.AbstractTransformer) CyNode(org.cytoscape.model.CyNode) ListSingleSelection(org.cytoscape.work.util.ListSingleSelection) ValidatableTransformer(org.cytoscape.filter.model.ValidatableTransformer) CyRow(org.cytoscape.model.CyRow) ValidationWarning(org.cytoscape.filter.model.ValidationWarning) List(java.util.List) NumericPredicateDelegate(org.cytoscape.filter.internal.predicates.NumericPredicateDelegate) ValidationWarning.warn(org.cytoscape.filter.model.ValidationWarning.warn) CyIdentifiable(org.cytoscape.model.CyIdentifiable) PredicateDelegates(org.cytoscape.filter.internal.predicates.PredicateDelegates) Tunable(org.cytoscape.work.Tunable) ListChangeListener(org.cytoscape.work.util.ListChangeListener) CyNetwork(org.cytoscape.model.CyNetwork) CyColumn(org.cytoscape.model.CyColumn) Filter(org.cytoscape.filter.model.Filter) ListSelection(org.cytoscape.work.util.ListSelection) CyEdge(org.cytoscape.model.CyEdge) Collections(java.util.Collections) CyTable(org.cytoscape.model.CyTable) CyTable(org.cytoscape.model.CyTable) CyColumn(org.cytoscape.model.CyColumn) List(java.util.List) CyRow(org.cytoscape.model.CyRow)

Example 4 with Filter

use of org.cytoscape.filter.model.Filter 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

Filter (org.cytoscape.filter.model.Filter)4 List (java.util.List)3 AbstractTransformer (org.cytoscape.filter.model.AbstractTransformer)3 CompositeFilter (org.cytoscape.filter.model.CompositeFilter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ColumnFilter (org.cytoscape.filter.internal.filters.column.ColumnFilter)2 DegreeFilter (org.cytoscape.filter.internal.filters.degree.DegreeFilter)2 TopologyFilter (org.cytoscape.filter.internal.filters.topology.TopologyFilter)2 AdjacencyTransformer (org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer)2 TransformerManagerImpl (org.cytoscape.filter.internal.work.TransformerManagerImpl)2 NamedTransformer (org.cytoscape.filter.model.NamedTransformer)2 Transformer (org.cytoscape.filter.model.Transformer)2 CyTransformerReaderImpl (org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl)2 CyTransformerWriterImpl (org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl)2 CyTransformerWriter (org.cytoscape.io.write.CyTransformerWriter)2 CyIdentifiable (org.cytoscape.model.CyIdentifiable)2 CyNetwork (org.cytoscape.model.CyNetwork)2 Test (org.junit.Test)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1