/** * Parses a row from the result object and transforms it into a GraphModel * * @param data the data to transform, given as a map * @return the data transformed to an {@link GraphModel} */ @Override public GraphModel adapt(Map<String, Object> data) { // These two sets keep track of which nodes and edges have already been built, so we don't redundantly // write the same node or relationship entity many times. final Set<Long> nodeIdentities = new HashSet<>(); final Set<Long> edgeIdentities = new HashSet<>(); DefaultGraphModel graphModel = new DefaultGraphModel(); for (Map.Entry<String, Object> mapEntry : data.entrySet()) { final Object value = mapEntry.getValue(); String resultKey = mapEntry.getKey(); boolean generatedNodes = AdapterUtils.describesGeneratedNode(resultKey); adaptInternal(nodeIdentities, edgeIdentities, graphModel, value, generatedNodes); } return graphModel; }
DefaultGraphModel buildGraphModel() { if (this.graphModel != null) { throw new IllegalStateException("GraphModel already built!"); } this.graphModel = new DefaultGraphModel(); graphModel.addNodes(nodeModels.toArray(new NodeModel[0])); graphModel.addRelationships(relationshipModels.toArray(new RelationshipModel[0])); return graphModel; }
/** * Retrieves a mapped entity from this sessions mapping context after a call to {@link GraphEntityMapper#map(Class, List)}. * If there's no mapped entity, this method tries to find the {@link NodeModel} with the same id in the {@link GraphModel} which * has been the base of the mapping process. * * @param id The id of the entity to retrieve * @param graphModel The graph model that has been used for mapping * @return An entity or a NodeModel * @throws RuntimeException When neither entity nor {@link NodeModel} can be found */ private Object getEntityOrNodeModel(Long id, DefaultGraphModel graphModel) { return Optional.ofNullable(mappingContext.getNodeEntity(id)).orElseGet(() -> graphModel.findNode(id).orElseThrow(() -> new RuntimeException("Lost NodeModel for id " + id)) ); }
void buildNode(Object node, DefaultGraphModel graphModel, Set<Long> nodeIdentities, boolean generatedNode) { long nativeId = nodeId(node); if (nodeIdentities.contains(nativeId)) { return; } nodeIdentities.add(nativeId); NodeModel nodeModel = new NodeModel(nativeId); List<String> labels = labels(node); nodeModel.setLabels(labels.toArray(new String[0])); nodeModel.setProperties(convertArrayPropertiesToIterable(properties(node))); nodeModel.setGeneratedNode(generatedNode); graphModel.addNode(nodeModel); }
void buildRelationship(Object relationship, DefaultGraphModel graphModel, Set<Long> edgeIdentities) { if (edgeIdentities.contains(relationshipId(relationship))) { return; } edgeIdentities.add(relationshipId(relationship)); RelationshipModel edgeModel = new RelationshipModel(); edgeModel.setId(relationshipId(relationship)); edgeModel.setType(relationshipType(relationship)); edgeModel.setStartNode(startNodeId(relationship)); edgeModel.setEndNode(endNodeId(relationship)); edgeModel.setProperties(convertArrayPropertiesToIterable(properties(relationship))); graphModel.addRelationship(edgeModel); }
/** * Reads the next row from the result object and transforms it into a RowModel object * * @param data the data to transform, given as a map * @return @return the data transformed to an {@link RowModel} */ public GraphRowModel adapt(Map<String, Object> data) { assert (columns != null); Set<Long> nodeIdentities = new HashSet(); Set<Long> edgeIdentities = new HashSet(); GraphModel graphModel = new DefaultGraphModel(); List<String> variables = new ArrayList<>(); List<Object> values = new ArrayList<>(); // there is no guarantee that the objects in the data are ordered the same way as required by the columns // so we use the columns information to extract them in the correct order for post-processing. Iterator<String> iterator = columns.iterator(); adapt(iterator, data, graphModel, variables, values, nodeIdentities, edgeIdentities); DefaultRowModel rowModel = new DefaultRowModel(values.toArray(new Object[]{}), variables.toArray(new String[]{})); return new DefaultGraphRowModel(graphModel, rowModel.getValues()); }
@Override public <T> Iterable<T> map(Class<T> type, Response<GraphModel> model) { List<GraphModel> listOfGraphModels = model.toList(); model.close(); BiFunction<GraphModel, Long, Boolean> isNotGeneratedNode = (graphModel, nativeId) -> { Optional<Node> node = ((DefaultGraphModel) graphModel).findNode(nativeId); if (!node.isPresent()) { return true; // Native id describes a relationship } return node.map(n -> !((NodeModel) n).isGeneratedNode()).get(); }; return delegate.map(type, listOfGraphModels, isNotGeneratedNode); } }
@Override public GraphRowModel adapt(Map<String, Object> data) { if (columns == null) { throw new ResultProcessingException("Column data cannot be null!"); } Set<Long> nodeIdentities = new HashSet<>(); Set<Long> edgeIdentities = new HashSet<>(); GraphModel graphModel = new DefaultGraphModel(); List<String> variables = new ArrayList<>(); List<Object> values = new ArrayList<>(); // there is no guarantee that the objects in the data are ordered the same way as required by the columns // so we use the columns information to extract them in the correct order for post-processing. Iterator<String> iterator = columns.iterator(); adapt(iterator, data, graphModel, variables, values, nodeIdentities, edgeIdentities); DefaultRowModel rowModel = new DefaultRowModel(values.toArray(new Object[] {}), variables.toArray(new String[] {})); return new DefaultGraphRowModel(graphModel, rowModel.getValues()); }
/** * Reads the next row from the result object and transforms it into a RowModel object * * @param data the data to transform, given as a map * @return the data transformed to an {@link RowModel} */ public GraphRowModel adapt(Map<String, Object> data) { if (columns == null) { throw new ResultProcessingException("Result columns should not be null"); } Set<Long> nodeIdentities = new HashSet<>(); Set<Long> edgeIdentities = new HashSet<>(); DefaultGraphModel graphModel = new DefaultGraphModel(); List<String> variables = new ArrayList<>(); List<Object> values = new ArrayList<>(); // there is no guarantee that the objects in the data are ordered the same way as required by the columns // so we use the columns information to extract them in the correct order for post-processing. Iterator<String> iterator = columns.iterator(); adapt(iterator, data, graphModel, variables, values, nodeIdentities, edgeIdentities); DefaultRowModel rowModel = new DefaultRowModel(values.toArray(new Object[] {}), variables.toArray(new String[] {})); return new DefaultGraphRowModel(graphModel, rowModel.getValues()); }
/** * Reads the next row from the result object and transforms it into a RowModel object * * @param data the data to transform, given as a map * @return the data transformed to an {@link RowModel} */ public GraphRowModel adapt(Map<String, Object> data) { if (columns == null) { throw new ResultProcessingException("Column data cannot be null!"); } Set<Long> nodeIdentities = new HashSet<>(); Set<Long> edgeIdentities = new HashSet<>(); GraphModel graphModel = new DefaultGraphModel(); List<String> variables = new ArrayList<>(); List<Object> values = new ArrayList<>(); // there is no guarantee that the objects in the data are ordered the same way as required by the columns // so we use the columns information to extract them in the correct order for post-processing. Iterator<String> iterator = columns.iterator(); adapt(iterator, data, graphModel, variables, values, nodeIdentities, edgeIdentities); DefaultRowModel rowModel = new DefaultRowModel(values.toArray(new Object[] {}), variables.toArray(new String[] {})); return new DefaultGraphRowModel(graphModel, rowModel.getValues()); }
final Set<Long> edgeIdentities = new HashSet<>(); GraphModel graphModel = new DefaultGraphModel();
GraphModel model = new DefaultGraphModel(); JGraph graph = new JGraph(model);