Skip to content

Commit

Permalink
Merge pull request #249 from DawnScience/DAQ-146
Browse files Browse the repository at this point in the history
[DAQ-146] support adding nexus object to a collection
  • Loading branch information
gerring committed May 23, 2016
2 parents 6f9579b + ca81b11 commit 23ae97e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.util.Arrays;

import org.eclipse.dawnsci.nexus.NXcollection;
import org.eclipse.dawnsci.nexus.NXdetector;
import org.eclipse.dawnsci.nexus.NXentry;
import org.eclipse.dawnsci.nexus.NXinstrument;
Expand All @@ -31,8 +33,6 @@
import org.eclipse.dawnsci.nexus.builder.NexusObjectProvider;
import org.eclipse.dawnsci.nexus.builder.appdef.NexusApplicationBuilder;
import org.eclipse.dawnsci.nexus.builder.data.NexusDataBuilder;
import org.eclipse.dawnsci.nexus.builder.impl.DefaultNexusFileBuilder;
import org.eclipse.dawnsci.nexus.builder.impl.MapBasedMetadataProvider;
import org.eclipse.dawnsci.nexus.validation.NexusValidationException;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -151,6 +151,27 @@ public void testAdd_namedGroup() throws NexusException {
assertThat(instrument.getPositioner("x"), is(sameInstance(positionerProvider.getNexusObject())));
}

@Test
public void testAdd_positionerWithCollectionName() throws NexusException {
entryBuilder.addDefaultGroups();
assertThat(nxEntry.getNumberOfGroupNodes(), is(3));
NXinstrument instrument = nxEntry.getInstrument();
assertThat(instrument.getNumberOfGroupNodes(), is(0));

AbstractNexusObjectProvider<NXpositioner> positioner = new TestPositioner("xPos");
positioner.setCollectionName("scannables");
entryBuilder.add(positioner);

assertThat(nxEntry.getNumberOfGroupNodes(), is(3));
assertThat(instrument.getNumberOfGroupNodes(), is(1));

NXcollection collection = instrument.getCollection("scannables");
assertNotNull(collection);
NXpositioner xPositioner = (NXpositioner) collection.getGroupNode("xPos");
assertNotNull(xPositioner);
assertThat(xPositioner, is(sameInstance(positioner.getNexusObject())));
}

@Test
public void testAdd_samplePositioner() throws NexusException {
entryBuilder.addDefaultGroups();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public abstract class AbstractNexusObjectProvider<N extends NXobject> implements

private String defaultAxisDataFieldName = null;

private String collectionName = null;

private NexusBaseClass category = null;

private Boolean useDeviceNameInNXdata = null;
Expand Down Expand Up @@ -461,6 +463,14 @@ public void setCategory(NexusBaseClass category) {
this.category = category;
}

public String getCollectionName() {
return collectionName;
}

public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}

@Override
public Integer getDefaultAxisDimension(String primaryDataFieldName, String dataFieldName) {
PrimaryDataFieldModel dataFieldModel = getPrimaryDataFieldModel(primaryDataFieldName, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.List;

import org.eclipse.dawnsci.nexus.NXcollection;
import org.eclipse.dawnsci.nexus.NXdata;
import org.eclipse.dawnsci.nexus.NXinstrument;
import org.eclipse.dawnsci.nexus.NXobject;
Expand Down Expand Up @@ -88,6 +89,16 @@ public interface NexusObjectProvider<N extends NXobject> extends NexusEntryModif
*/
public NexusBaseClass getCategory();

/**
* Returns the name of the collection for this {@link NexusObjectProvider}. When adding
* a nexus object to a {@link NexusEntryBuilder}, if this method does not return
* <code>null</code>, then the nexus object will be added to the {@link NXcollection} with this
* name within the group that it would have been added to otherwise. The collection will be
* created if it does not already exist.
* @return collection name or <code>null</code>
*/
public String getCollectionName();

/**
* Returns the name of the external HDF5 file that this device writes
* its data to, or <code>null</code> if none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.util.List;

import org.eclipse.dawnsci.analysis.api.tree.DataNode;
import org.eclipse.dawnsci.analysis.api.tree.GroupNode;
import org.eclipse.dawnsci.analysis.api.tree.NodeLink;
import org.eclipse.dawnsci.nexus.NXcollection;
import org.eclipse.dawnsci.nexus.NXdata;
import org.eclipse.dawnsci.nexus.NXentry;
import org.eclipse.dawnsci.nexus.NXinstrument;
Expand Down Expand Up @@ -66,10 +68,11 @@ public class DefaultNexusEntryBuilder implements NexusEntryBuilder {
* Creates a new {@link DefaultNexusEntryBuilder}. This constructor should only be called
* by {@link DefaultNexusFileBuilder}.
* @param nexusNodeFactory node factory
* @param entryName TODO
* @param entryName name of entry
* @param nxEntry entry to wrap
*/
protected DefaultNexusEntryBuilder(final NexusNodeFactory nexusNodeFactory, String entryName, final NXentry nxEntry) {
protected DefaultNexusEntryBuilder(final NexusNodeFactory nexusNodeFactory,
String entryName, final NXentry nxEntry) {
this.nexusNodeFactory = nexusNodeFactory;
this.nxEntry = nxEntry;
this.entryName = entryName;
Expand Down Expand Up @@ -250,7 +253,6 @@ public DataNode getDataNode(String relativePath) throws NexusException {

/**
* Adds the default groups for the entry. Subclasses may override as appropriate.
* @return
*/
@Override
public void addDefaultGroups() {
Expand Down Expand Up @@ -310,10 +312,13 @@ protected <N extends NXobject> void addGroupToNexusTree(NexusObjectProvider<N> n
final String name = nexusObjectProvider.getName();
final NexusBaseClass category = nexusObjectProvider.getCategory();

// find the parent group
NXobject parentGroup = null;
if (category != null) {
// if a category is specified, the parent group is the first group for this category
parentGroup = findGroupForCategory(category);
} else {
// otherwise the parent group is the first group we can add this type of object to
for (final NXobject group : defaultGroups) {
if (group.canAddChild(nexusObject)) {
parentGroup = group;
Expand All @@ -324,10 +329,33 @@ protected <N extends NXobject> void addGroupToNexusTree(NexusObjectProvider<N> n
throw new NexusException("Cannot find a parent group that accepts a " + nexusObject.getNexusBaseClass());
}
}

// if a collection name is specified, get the parent collection - creating it if necessary
String collectionName = nexusObjectProvider.getCollectionName();
if (collectionName != null) {
parentGroup = getCollection(parentGroup, collectionName);
}

parentGroup.addGroupNode(name, nexusObject);
}
}

private NXcollection getCollection(NXobject group, String collectionName) {
NXcollection collection = null;

GroupNode collectionGroup = group.getGroupNode(collectionName);
if (collectionGroup == null) {
collection = nexusNodeFactory.createNXcollection();
group.addGroupNode(collectionName, collection);
} else if (collectionGroup instanceof NXcollection) {
collection = (NXcollection) collectionGroup;
} else {
throw new IllegalArgumentException("Cannot add collection " + collectionName +
". A child group with that name already exists");
}

return collection;
}

private NXobject findGroupForCategory(NexusBaseClass category) throws NexusException {
if (category == NexusBaseClass.NX_ENTRY) {
Expand Down

0 comments on commit 23ae97e

Please sign in to comment.