@Test public void formatter() throws ResultFormattingException { String dots = new ResultFormatterDOT().format("{\n" + " \"columns\" : [ \"source_taxon_name\", \"interaction_type\", \"target_taxon_name\" ],\n" + " \"data\" : [ [ \"Vireo olivaceus\", \"preysOn\", [ \"Diptera\", \"Auchenorrhyncha\", \"Coleoptera\", \"Lepidoptera\", \"Arachnida\" ] ], [ \"Colaptes auratus\", \"preysOn\", [ \"Hymenoptera\" ] ], [ \"Anseriformes\", \"preysOn\", [ \"Cyperaceae\", \"Tracheophyta\", \"marine invertebrates\" ] ], [ \"Geositta\", \"preysOn\", [ \"Insecta\", \"Coelopidae\", \"Diptera\", \"Talitridae\" ] ], [ \"Patagioenas squamosa\", \"preysOn\", [ \"fruit\" ] ], [ \"Ardea cinerea\", \"preysOn\", [ \"Anguilla anguilla\", \"Pollachius virens\", \"Ammodytes tobianus\", \"Zoarces viviparus\", \"Pholis gunnellus\", \"Myoxocephalus scorpius\", \"Pomatoschistus microps\", \"Crangon crangon\", \"Salmo trutta\" ] ], [ \"Setophaga petechia\", \"preysOn\", [ \"Araneae\", \"Insecta\", \"Orthoptera\", \"fruit and seeds\", \"Hemiptera\", \"Diptera\", \"Lepidoptera\", \"Formicidae\", \"Hymenoptera\", \"Coleoptera\", \"Auchenorrhyncha\" ] ] ] }"); assertThat(dots, is(notNullValue())); }
@Override public String format(final String content) throws ResultFormattingException { StringBuilder builder = dotPrefix(); JsonNode results; try { results = RequestHelper.parse(content); } catch (IOException e) { throw new ResultFormattingException("failed to parse", e); } JsonNode columns = results.get("columns"); Map<String, Integer> nameIndex = new HashMap<String, Integer>(); for (int i = 0; i < columns.size(); i++) { nameIndex.put(columns.get(i).getTextValue(), i); } List<ResultField> sourceTaxonResultFields = Arrays.asList(ResultField.SOURCE_TAXON_NAME, ResultField.SOURCE_TAXON_PATH, ResultField.SOURCE_TAXON_EXTERNAL_ID, ResultField.SOURCE_TAXON_PATH_IDS); List<ResultField> targetTaxonResultFields = Arrays.asList(ResultField.TARGET_TAXON_NAME, ResultField.TARGET_TAXON_PATH, ResultField.TARGET_TAXON_EXTERNAL_ID, ResultField.TARGET_TAXON_PATH_IDS); JsonNode rows = results.get("data"); for (JsonNode row : rows) { Integer sourceTaxonIndex = getTaxonLabel(nameIndex, sourceTaxonResultFields); Integer targetTaxonIndex = getTaxonLabel(nameIndex, targetTaxonResultFields); Integer interactionTypeIndex = nameIndex.get(ResultField.INTERACTION_TYPE.getLabel()); if (null != sourceTaxonIndex && null != targetTaxonIndex && null != interactionTypeIndex) { JsonNode sourceTaxon = row.get(sourceTaxonIndex); JsonNode targetTaxon = row.get(targetTaxonIndex); JsonNode type = row.get(interactionTypeIndex); if (sourceTaxon != null && targetTaxon != null && type != null) { String sourceId = getSafeLabel(sourceTaxon.getTextValue()); if (targetTaxon.isArray()) { for (JsonNode targetTaxonItem : targetTaxon) { appendEdge(builder, targetTaxonItem, type, sourceId); } } else { appendEdge(builder, targetTaxon, type, sourceId); } } } } dotSuffix(builder); return builder.toString(); }
ResultFormatterDOT implements ResultFormatter { @Override public String format(final String content) throws ResultFormattingException { StringBuilder builder = dotPrefix(); JsonNode results; try { results = RequestHelper.parse(content); } catch (IOException e) { throw new ResultFormattingException("failed to parse", e); } JsonNode columns = results.get("columns"); Map<String, Integer> nameIndex = new HashMap<String, Integer>(); for (int i = 0; i < columns.size(); i++) { nameIndex.put(columns.get(i).getTextValue(), i); } List<ResultField> sourceTaxonResultFields = Arrays.asList(ResultField.SOURCE_TAXON_NAME, ResultField.SOURCE_TAXON_PATH, ResultField.SOURCE_TAXON_EXTERNAL_ID, ResultField.SOURCE_TAXON_PATH_IDS); List<ResultField> targetTaxonResultFields = Arrays.asList(ResultField.TARGET_TAXON_NAME, ResultField.TARGET_TAXON_PATH, ResultField.TARGET_TAXON_EXTERNAL_ID, ResultField.TARGET_TAXON_PATH_IDS); JsonNode rows = results.get("data"); for (JsonNode row : rows) { Integer sourceTaxonIndex = getTaxonLabel(nameIndex, sourceTaxonResultFields); Integer targetTaxonIndex = getTaxonLabel(nameIndex, targetTaxonResultFields); Integer interactionTypeIndex = nameIndex.get(ResultField.INTERACTION_TYPE.getLabel()); if (null != sourceTaxonIndex && null != targetTaxonIndex && null != interactionTypeIndex) { JsonNode sourceTaxon = row.get(sourceTaxonIndex); JsonNode targetTaxon = row.get(targetTaxonIndex); JsonNode type = row.get(interactionTypeIndex); if (sourceTaxon != null && targetTaxon != null && type != null) { String sourceId = getSafeLabel(sourceTaxon.getTextValue()); if (targetTaxon.isArray()) { for (JsonNode targetTaxonItem : targetTaxon) { appendEdge(builder, targetTaxonItem, type, sourceId); } } else { appendEdge(builder, targetTaxon, type, sourceId); } } } } dotSuffix(builder); return builder.toString(); } }
ResultFormatterDOT implements ResultFormatter { @Override public String format(final String content) throws ResultFormattingException { StringBuilder builder = dotPrefix(); JsonNode results; try { results = RequestHelper.parse(content); } catch (IOException e) { throw new ResultFormattingException("failed to parse", e); } JsonNode columns = results.get("columns"); Map<String, Integer> nameIndex = new HashMap<String, Integer>(); for (int i = 0; i < columns.size(); i++) { nameIndex.put(columns.get(i).getTextValue(), i); } List<ResultField> sourceTaxonResultFields = Arrays.asList(ResultField.SOURCE_TAXON_NAME, ResultField.SOURCE_TAXON_PATH, ResultField.SOURCE_TAXON_EXTERNAL_ID, ResultField.SOURCE_TAXON_PATH_IDS); List<ResultField> targetTaxonResultFields = Arrays.asList(ResultField.TARGET_TAXON_NAME, ResultField.TARGET_TAXON_PATH, ResultField.TARGET_TAXON_EXTERNAL_ID, ResultField.TARGET_TAXON_PATH_IDS); JsonNode rows = results.get("data"); for (JsonNode row : rows) { Integer sourceTaxonIndex = getTaxonLabel(nameIndex, sourceTaxonResultFields); Integer targetTaxonIndex = getTaxonLabel(nameIndex, targetTaxonResultFields); Integer interactionTypeIndex = nameIndex.get(ResultField.INTERACTION_TYPE.getLabel()); if (null != sourceTaxonIndex && null != targetTaxonIndex && null != interactionTypeIndex) { JsonNode sourceTaxon = row.get(sourceTaxonIndex); JsonNode targetTaxon = row.get(targetTaxonIndex); JsonNode type = row.get(interactionTypeIndex); if (sourceTaxon != null && targetTaxon != null && type != null) { String sourceId = getSafeLabel(sourceTaxon.getTextValue()); if (targetTaxon.isArray()) { for (JsonNode targetTaxonItem : targetTaxon) { appendEdge(builder, targetTaxonItem, type, sourceId); } } else { appendEdge(builder, targetTaxon, type, sourceId); } } } } dotSuffix(builder); return builder.toString(); }  }
ResultFormatterDOT implements ResultFormatter { @Override public String format(final String content) throws ResultFormattingException { StringBuilder builder = dotPrefix(); JsonNode results; try { results = RequestHelper.parse(content); } catch (IOException e) { throw new ResultFormattingException("failed to parse", e); } JsonNode columns = results.get("columns"); Map<String, Integer> nameIndex = new HashMap<String, Integer>(); for (int i = 0; i < columns.size(); i++) { nameIndex.put(columns.get(i).getTextValue(), i); } List<ResultField> sourceTaxonResultFields = Arrays.asList(ResultField.SOURCE_TAXON_NAME, ResultField.SOURCE_TAXON_PATH, ResultField.SOURCE_TAXON_EXTERNAL_ID, ResultField.SOURCE_TAXON_PATH_IDS); List<ResultField> targetTaxonResultFields = Arrays.asList(ResultField.TARGET_TAXON_NAME, ResultField.TARGET_TAXON_PATH, ResultField.TARGET_TAXON_EXTERNAL_ID, ResultField.TARGET_TAXON_PATH_IDS); JsonNode rows = results.get("data"); for (JsonNode row : rows) { Integer sourceTaxonIndex = getTaxonLabel(nameIndex, sourceTaxonResultFields); Integer targetTaxonIndex = getTaxonLabel(nameIndex, targetTaxonResultFields); Integer interactionTypeIndex = nameIndex.get(ResultField.INTERACTION_TYPE.getLabel()); if (null != sourceTaxonIndex && null != targetTaxonIndex && null != interactionTypeIndex) { JsonNode sourceTaxon = row.get(sourceTaxonIndex); JsonNode targetTaxon = row.get(targetTaxonIndex); JsonNode type = row.get(interactionTypeIndex); if (sourceTaxon != null && targetTaxon != null && type != null) { String sourceId = getSafeLabel(sourceTaxon.getTextValue()); if (targetTaxon.isArray()) { for (JsonNode targetTaxonItem : targetTaxon) { appendEdge(builder, targetTaxonItem, type, sourceId); } } else { appendEdge(builder, targetTaxon, type, sourceId); } } } } dotSuffix(builder); return builder.toString(); }  @Override String format(final String content); }
ResultFormatterDOT implements ResultFormatter { @Override public String format(final String content) throws ResultFormattingException { StringBuilder builder = dotPrefix(); JsonNode results; try { results = RequestHelper.parse(content); } catch (IOException e) { throw new ResultFormattingException("failed to parse", e); } JsonNode columns = results.get("columns"); Map<String, Integer> nameIndex = new HashMap<String, Integer>(); for (int i = 0; i < columns.size(); i++) { nameIndex.put(columns.get(i).getTextValue(), i); } List<ResultField> sourceTaxonResultFields = Arrays.asList(ResultField.SOURCE_TAXON_NAME, ResultField.SOURCE_TAXON_PATH, ResultField.SOURCE_TAXON_EXTERNAL_ID, ResultField.SOURCE_TAXON_PATH_IDS); List<ResultField> targetTaxonResultFields = Arrays.asList(ResultField.TARGET_TAXON_NAME, ResultField.TARGET_TAXON_PATH, ResultField.TARGET_TAXON_EXTERNAL_ID, ResultField.TARGET_TAXON_PATH_IDS); JsonNode rows = results.get("data"); for (JsonNode row : rows) { Integer sourceTaxonIndex = getTaxonLabel(nameIndex, sourceTaxonResultFields); Integer targetTaxonIndex = getTaxonLabel(nameIndex, targetTaxonResultFields); Integer interactionTypeIndex = nameIndex.get(ResultField.INTERACTION_TYPE.getLabel()); if (null != sourceTaxonIndex && null != targetTaxonIndex && null != interactionTypeIndex) { JsonNode sourceTaxon = row.get(sourceTaxonIndex); JsonNode targetTaxon = row.get(targetTaxonIndex); JsonNode type = row.get(interactionTypeIndex); if (sourceTaxon != null && targetTaxon != null && type != null) { String sourceId = getSafeLabel(sourceTaxon.getTextValue()); if (targetTaxon.isArray()) { for (JsonNode targetTaxonItem : targetTaxon) { appendEdge(builder, targetTaxonItem, type, sourceId); } } else { appendEdge(builder, targetTaxon, type, sourceId); } } } } dotSuffix(builder); return builder.toString(); }  @Override String format(final String content);  }