Neo4jImporter implements AutoCloseable { public void addSegments(final String dataset, final List<Neuron> neuronList, final LocalDateTime timeStamp) { LOG.info("addSegments: entry"); String roiPropertyBaseString = " n.`%s` = TRUE,"; final String segmentText = "MERGE (n:`" + dataset + "-Segment`{bodyId:$bodyId}) " + "ON CREATE SET n.bodyId = $bodyId," + " n:Segment," + " n:" + dataset + "," + " n.name = $name," + " n.type = $type," + " n.instance = $instance," + " n.primaryNeurite = $primaryNeurite," + " n.majorInput = $majorInput," + " n.majorOutput = $majorOutput," + " n.clonalUnit = $clonalUnit," + " n.neurotransmitter = $neurotransmitter," + " n.property = $property," + " n.status = $status," + " n.size = $size," + " n.somaLocation = $somaLocation," + " n.somaRadius = $somaRadius, " + "%s" + " n.timeStamp = $timeStamp"; final String synapseSetText = "MERGE (s:`" + dataset + "-SynapseSet`{datasetBodyId:$datasetBodyId}) ON CREATE SET s.datasetBodyId=$datasetBodyId, s.timeStamp=$timeStamp, s:SynapseSet, s:" + dataset + " \n"; final String segmentContainsSynapseSetText = "MERGE (n:`" + dataset + "-Segment`{bodyId:$bodyId}) \n" + "MERGE (s:`" + dataset + "-SynapseSet`{datasetBodyId:$datasetBodyId}) \n" + "MERGE (n)-[:Contains]->(s)"; final String synapseSetContainsSynapseText = "MERGE (s:`" + dataset + "-Synapse`{location:$location}) \n" + "MERGE (t:`" + dataset + "-SynapseSet`{datasetBodyId:$datasetBodyId}) \n" + "MERGE (t)-[:Contains]->(s) \n"; final String metaNodeString = "MATCH (m:Meta{dataset:$dataset}) SET " + "m.lastDatabaseEdit=$timeStamp"; try (final TransactionBatch batch = getBatch()) { for (final Neuron neuron : neuronList) { StringBuilder roiProperties = updateSuperRoisRoiInfoAndCreateRoiPropertyString(new HashSet<>(), new RoiInfo(), roiPropertyBaseString, neuron.getRois(), "none"); String segmentTextWithRois = String.format(segmentText, roiProperties.toString()); batch.addStatement( new Statement(segmentTextWithRois, parameters( "bodyId", neuron.getId(), "name", neuron.getName(), "type", neuron.getType(), "instance", neuron.getInstance(), "primaryNeurite", neuron.getPrimaryNeurite(), "majorInput", neuron.getMajorInput(), "majorOutput", neuron.getMajorOutput(), "clonalUnit", neuron.getClonalUnit(), "neurotransmitter", neuron.getNeurotransmitter(), "property", neuron.getProperty(), "status", neuron.getStatus(), "size", neuron.getSize(), "somaLocation", neuron.getSomaLocation(), "somaRadius", neuron.getSomaRadius(), "timeStamp", timeStamp)) ); if (neuron.getSynapseLocationSet().size() > 0) { batch.addStatement( new Statement(synapseSetText, parameters( "datasetBodyId", dataset + ":" + neuron.getId(), "timeStamp", timeStamp)) ); batch.addStatement( new Statement(segmentContainsSynapseSetText, parameters( "bodyId", neuron.getId(), "datasetBodyId", dataset + ":" + neuron.getId()))); for (Location synapseLocation : neuron.getSynapseLocationSet()) { batch.addStatement(new Statement(synapseSetContainsSynapseText, parameters( "location", synapseLocation.getAsPoint(), "datasetBodyId", dataset + ":" + neuron.getId() ))); } } } batch.writeTransaction(); } try (final TransactionBatch batch = getBatch()) { batch.addStatement(new Statement( metaNodeString, parameters( "dataset", dataset, "timeStamp", timeStamp ) )); batch.writeTransaction(); } LOG.info("addSegments: exit"); } Neo4jImporter(final DbConfig dbConfig);  Neo4jImporter(final Driver driver); @Override void close(); void prepDatabase(final String dataset); void prepDatabaseForClusterNames(final String dataset); void indexBooleanRoiProperties(String dataset); void createMetaNodeWithDataModelNode(final String dataset,
                                                final float dataModelVersion,
                                                final double preHPThreshold,
                                                final double postHPThreshold,
                                                final boolean addConnectionSetRoiInfoAndWeightHP,
                                                final LocalDateTime timeStamp); void addSynapsesWithRois(final String dataset, final List<Synapse> synapseList, final LocalDateTime timeStamp); void addSynapsesTo(final String dataset, final List<SynapticConnection> synapticConnectionList, final LocalDateTime timeStamp); void addSegments(final String dataset,
                            final List<Neuron> neuronList,
                            final LocalDateTime timeStamp); void addConnectionInfo(final String dataset,
                                  final List<Neuron> neuronList,
                                  final boolean addConnectionSetRoiInfoAndWeightHP,
                                  final double preHPThreshold,
                                  final double postHPThreshold,
                                  final long neuronThreshold); void addSkeletonNodes(final String dataset, final List<Skeleton> skeletonList, final LocalDateTime timeStamp); void addMetaInfo(String dataset, MetaInfo metaInfo, final LocalDateTime timeStamp); static SortedSet<Map.Entry<String, SynapseCounter>> sortRoisByPostCount(Map<String, SynapseCounter> roiSynapseCountMap); static SortedSet<Map.Entry<String, SynapseCounter>> sortRoisByPreCount(Map<String, SynapseCounter> roiSynapseCountMap);  }
@Test public void shouldNotBeAbleToAddDuplicateSegmentsDueToUniquenessConstraint() { Session session = driver.session(); LocalDateTime timeStamp = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS); Neo4jImporter neo4jImporter = new Neo4jImporter(driver); String neuronsJsonPath = "src/test/resources/neuronList.json"; List<Neuron> neuronList = NeuPrintMain.readNeuronsJson(neuronsJsonPath); neo4jImporter.addSegments("test", neuronList, timeStamp); int numberOfSegments2 = session.run("MATCH (n:Segment:test:`test-Segment`) RETURN count(n)").single().get(0).asInt(); Assert.assertEquals(12, numberOfSegments2); }