use of org.cytoscape.filter.model.CompositeFilter 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);
}
use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.
the class CyTransformerReaderImpl method readTransformer.
private Transformer<?, ?> readTransformer(JsonParser parser) throws IOException {
JsonToken firstToken = parser.nextToken();
if (firstToken == JsonToken.END_ARRAY) {
return null;
}
if (firstToken != JsonToken.START_OBJECT) {
throw new IOException("Expected: " + JsonToken.START_OBJECT + ". Got: " + firstToken);
}
assertField(parser, FilterIO.ID_FIELD);
String id = parser.nextTextValue();
assertField(parser, FilterIO.PARAMETERS_FIELD);
assertNextToken(parser, JsonToken.START_OBJECT);
Transformer<?, ?> transformer = transformerManager.createTransformer(id);
if (transformer == null && !id.contains(".")) {
transformer = transformerManager.createTransformer("org.cytoscape." + id);
}
if (transformer == null) {
throw new IOException("Unrecognized id: '" + id + "'");
}
Map<String, Object> parameters = readParameters(parser);
assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
FilterIO.applyParameters(parameters, transformer);
if (transformer instanceof CompositeFilter) {
readCompositeFilter(parser, (CompositeFilter<?, ?>) transformer);
} else if (transformer instanceof SubFilterTransformer) {
SubFilterTransformer<?, ?> sft = (SubFilterTransformer<?, ?>) transformer;
readCompositeFilter(parser, sft.getCompositeFilter());
} else {
assertNextToken(parser, JsonToken.END_OBJECT);
}
return transformer;
}
use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.
the class CreateFilterTaskTest method testCreateCompositeFilter.
@Test
public void testCreateCompositeFilter() {
CreateFilterTask task = new CreateFilterTask(serviceRegistrar, "MyFilter");
String subFilterJson = "{ \"id\" : \"ColumnFilter\", \"parameters\" : { \"criterion\" : \"1\", \"columnName\" : \"name\", \"predicate\" : \"CONTAINS\"} }";
task.jsonTunable.json = "{ \"id\" : \"org.cytoscape.CompositeFilter\", \"parameters\" : { \"type\" : \"ALL\" }, \"transformers\" : [ " + subFilterJson + " ] }";
TaskMonitor tm = mock(TaskMonitor.class);
task.run(tm);
verify(tm, times(0)).showMessage(any(), any());
NamedTransformer<CyNetwork, CyIdentifiable> transformer = transformerContainer.getNamedTransformer(task.name);
assertNotNull(transformer);
@SuppressWarnings("rawtypes") CompositeFilter compositeFilter = (CompositeFilter) transformer.getTransformers().get(0);
assertEquals(CompositeFilter.Type.ALL, compositeFilter.getType());
assertEquals(1, compositeFilter.getLength());
ColumnFilter columnFilter = (ColumnFilter) compositeFilter.get(0);
assertEquals(Predicate.CONTAINS, columnFilter.getPredicate());
assertEquals("1", columnFilter.getCriterion());
assertEquals("name", columnFilter.getColumnName());
}
use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.
the class TransformerPanelController method createView.
@Override
public JComponent createView(TransformerPanel parent, Transformer<CyNetwork, CyIdentifiable> transformer, int depth) {
// CompositeFilterImpl needs a CompositeFilterPanel but the top is blank so view will be null
JComponent view = transformerViewManager.createView(transformer);
if (transformer instanceof SubFilterTransformer || transformer instanceof CompositeFilter) {
String addButtonTT = transformerViewManager.getAddButtonTooltip(transformer);
CompositeFilterController controller = CompositeFilterController.createFor(view, addButtonTT);
CompositeFilter<CyNetwork, CyIdentifiable> compositeFilter;
if (transformer instanceof SubFilterTransformer)
compositeFilter = ((SubFilterTransformer<CyNetwork, CyIdentifiable>) transformer).getCompositeFilter();
else
compositeFilter = (CompositeFilter<CyNetwork, CyIdentifiable>) transformer;
return new CompositeFilterPanel<TransformerPanel>(parent, this, controller, compositeFilter, depth);
}
if (view == null)
throw new IllegalArgumentException("view could not be created for: " + transformer.getId());
return view;
}
use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.
the class FilterWorker method doWork.
@Override
public void doWork(ProgressMonitor monitor) {
if (controller == null) {
return;
}
final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
CyNetworkView networkView = applicationManager.getCurrentNetworkView();
CyNetwork network;
if (networkView != null) {
network = networkView.getModel();
} else {
network = applicationManager.getCurrentNetwork();
}
if (network == null) {
return;
}
monitor.setProgress(0);
monitor.setStatusMessage(null);
int nodeCount = 0;
int edgeCount = 0;
int counter = 0;
long startTime = System.currentTimeMillis();
try {
Filter<CyNetwork, CyIdentifiable> filter = controller.getFilter();
if (filter instanceof CompositeFilter) {
// If we have an empty CompositeFilter, bail out.
CompositeFilter<CyNetwork, CyIdentifiable> composite = (CompositeFilter<CyNetwork, CyIdentifiable>) filter;
if (composite.getLength() == 0) {
return;
}
}
if (filter instanceof MemoizableTransformer) {
((MemoizableTransformer) filter).startCaching();
}
try {
List<CyNode> nodeList = network.getNodeList();
List<CyEdge> edgeList = network.getEdgeList();
double total = nodeList.size() + edgeList.size();
List<CyIdentifiable> selected = new ArrayList<>();
List<CyIdentifiable> unselected = new ArrayList<>();
for (CyNode node : nodeList) {
if (monitor.isCancelled()) {
return;
}
boolean accepted = filter.accepts(network, node);
if (accepted) {
selected.add(node);
nodeCount++;
} else {
unselected.add(node);
}
monitor.setProgress(++counter / total);
}
for (CyEdge edge : edgeList) {
if (monitor.isCancelled()) {
return;
}
boolean accepted = filter.accepts(network, edge);
if (accepted) {
selected.add(edge);
edgeCount++;
} else {
unselected.add(edge);
}
monitor.setProgress(++counter / total);
}
// now do the selection
for (CyIdentifiable element : unselected) {
CyRow row = network.getRow(element);
if (row.get(CyNetwork.SELECTED, Boolean.class)) {
row.set(CyNetwork.SELECTED, Boolean.FALSE);
}
}
for (CyIdentifiable element : selected) {
CyRow row = network.getRow(element);
if (!row.get(CyNetwork.SELECTED, Boolean.class)) {
row.set(CyNetwork.SELECTED, Boolean.TRUE);
}
}
} finally {
if (filter instanceof MemoizableTransformer) {
((MemoizableTransformer) filter).clearCache();
}
}
if (networkView != null) {
networkView.updateView();
}
} finally {
long duration = System.currentTimeMillis() - startTime;
monitor.setProgress(1.0);
monitor.setStatusMessage(String.format("Selected %d %s and %d %s in %dms", nodeCount, nodeCount == 1 ? "node" : "nodes", edgeCount, edgeCount == 1 ? "edge" : "edges", duration));
}
}
Aggregations