Commit 5ff027cd authored by Chris Coughlin's avatar Chris Coughlin

Message metadata cleanup

parent ca69d3ac
......@@ -12,6 +12,10 @@
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.metadata>1.8</maven.compiler.metadata>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>com.emphysic.myriad</groupId>
<artifactId>core</artifactId>
<dependencies>
......
......@@ -32,7 +32,7 @@ public class ROI implements Serializable {
/**
* A text tag for including descriptive metadata
*/
protected String source;
protected String metadata;
/**
* Creates a new Region of Interest from the specified data.
......@@ -72,18 +72,18 @@ public class ROI implements Serializable {
/**
* Sets the descriptive metadata for this ROI
* @param source new metadata
* @param metadata new metadata
*/
public void setSource(String source) {
this.source = source;
public void setMetadata(String metadata) {
this.metadata = metadata;
}
/**
* Returns the current metadata for this ROI
* @return current metadata
*/
public String getSource() {
return source;
public String getMetadata() {
return metadata;
}
/**
......@@ -96,10 +96,10 @@ public class ROI implements Serializable {
@Override
public String toString() {
StringBuilder sb = new StringBuilder("&source=");
sb.append(source);
StringBuilder sb = new StringBuilder();
sb.append(metadata);
if (!origin.isEmpty()) {
sb.append("&origin=").append(origin.get(0));
sb.append("&roiorigin=").append(origin.get(0));
for (int i=1; i<origin.size(); i++) {
sb.append("x").append(origin.get(i));
}
......
......@@ -91,7 +91,7 @@ public class PipelineDemo {
if (isFlaw) {
ROI newROI = new ROI(window, slidingWindow.getXoffset(), slidingWindow.getYoffset());
// Store the step in the metadata
newROI.setSource(Integer.toString(step));
newROI.setMetadata(Integer.toString(step));
foundROIs.add(newROI);
}
} catch (ArithmeticException ae) {
......@@ -110,7 +110,7 @@ public class PipelineDemo {
for (ROI roi : foundROIs) {
// Coordinates of ROI are relative to their input - need to upscale to the original data dimensions
List<Integer> origin = roi.getOrigin();
int upScale = scaler * Integer.parseInt(roi.getSource());
int upScale = scaler * Integer.parseInt(roi.getMetadata());
int lowX = DatasetUtils.safeIdx(upScale * origin.get(0), dataset.getWidth());
int lowY = DatasetUtils.safeIdx(upScale * origin.get(1), dataset.getHeight());
int highX = DatasetUtils.safeIdx(lowX + upScale * windowSize, dataset.getWidth());
......
......@@ -62,8 +62,8 @@ public class ROITest {
public void setSource() throws Exception {
roi = new ROI(dataset, 0);
String src = Long.toString(new Date().getTime());
roi.setSource(src);
assertEquals(src, roi.getSource());
roi.setMetadata(src);
assertEquals(src, roi.getMetadata());
}
@Test
......
......@@ -31,8 +31,7 @@ public class DataIngestorActor extends UntypedActor {
IODataset dataset = FileSniffer.read(f, true);
if (dataset != null) {
log.info(getSelf() + ": successfully read " + f + " sending to " + getSender());
DatasetMessage response = new DatasetMessage(dataset,
DatasetMessage.genMetadataEntry("source", f.getAbsolutePath()));
DatasetMessage response = new DatasetMessage(dataset, ((FileMessage) message).getMetadata());
getSender().tell(response, getSelf());
if (dataset instanceof ImageDataset) {
// Multi-frame
......
......@@ -48,7 +48,11 @@ public class PyramidActor extends UntypedActor {
Dataset current = new Dataset(((DatasetMessage) message).getDataset());
int step = 0;
do {
String md = ((DatasetMessage) message).genMetadata("pstep", Integer.toString(step));
String md = ((DatasetMessage) message).genMetadata("pyramid",
"pscale" + Integer.toString(gpo.getScaleFactor()) +
"pwsize" + Integer.toString(gpo.getWindowSize()) +
"pstep" + Integer.toString(step)
);
getSender().tell(new DatasetMessage(current, md), getSelf());
log.info(getSelf() + " sent pyramid step to " + getSender());
step++;
......
......@@ -43,7 +43,7 @@ public class ROIActor extends UntypedActor {
// Look for ROI
ROIMessage ROIMessage;
Dataset payload = ((DatasetMessage) message).getDataset();
String md = ((DatasetMessage) message).genMetadata("flawfinder", ROIFinder.toString());
String md = ((DatasetMessage) message).genMetadata("roifinder", ROIFinder.toString());
ROI ROI = null;
if (ROIFinder.isROI(payload)) {
sb.append("ROI found");
......
......@@ -25,12 +25,12 @@ public class ReporterActor extends UntypedActor {
StringBuilder sb = new StringBuilder("Received ROI report from " + getSender() + ": ");
ROI f = ((ROIMessage) message).getROI();
if (f != null) {
sb.append("** FLAW FOUND ** ").append(((ROIMessage) message).getMetaData());
sb.append("** FLAW FOUND ** ").append(((ROIMessage) message).getMetadata());
getSender().tell(message, getSelf());
} else {
sb.append(" no ROI found ");
}
sb.append(((ROIMessage) message).getMetaData());
sb.append(((ROIMessage) message).getMetadata());
log.info(sb.toString());
}
}
......
......@@ -83,7 +83,7 @@ public class ReporterActorPool extends LinkedWorkerPool {
if (rois.size() > 0) {
log.info("**** Final ROI Find Report ****");
for (ROIMessage f : rois) {
log.info(f.getMetaData());
log.info(f.getMetadata());
}
} else {
log.info("No flaws");
......
......@@ -92,7 +92,7 @@ public class SlidingWindowActor extends UntypedActor {
log.info(getSelf() + ": offset " + slider.getXoffset() + "," + slider.getYoffset() + " " + ((DatasetMessage) message).getDataset().getSize());
if (next != null) {
String md = ((DatasetMessage) message).genMetadata("window",
"xoff" + slider.getXoffset() + "yoff" + slider.getYoffset());
"xoff" + slider.getXoffset() + "yoff" + slider.getYoffset() + "w" + slider.getWindowWidth() + "h" + slider.getWindowHeight());
log.info(getSelf() + " sending window to " + next);
next.tell(new DatasetMessage(current, md), getSelf());
} else {
......
......@@ -6,22 +6,14 @@ package com.emphysic.myriad.network.messages;
import com.emphysic.myriad.core.data.io.Dataset;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* DatasetMessage - immutable message for transmitting a Dataset payload
*/
public class DatasetMessage implements Serializable {
public class DatasetMessage extends ImmutableMessage {
/**
* Dataset payload
*/
protected final Dataset dataset;
/**
* Metadata
*/
protected final String metadata;
/**
* Creates a new DatasetMessage
......@@ -29,8 +21,8 @@ public class DatasetMessage implements Serializable {
* @param metadata metadata
*/
public DatasetMessage(Dataset dataset, String metadata) {
super(metadata);
this.dataset = dataset;
this.metadata = metadata;
}
/**
......@@ -55,14 +47,7 @@ public class DatasetMessage implements Serializable {
* @param addlMetadata additional metadata
*/
public DatasetMessage(DatasetMessage orig, String addlMetadata) {
String origMetadata = orig.getMetadata();
if (origMetadata == null) {
origMetadata = addlMetadata;
} else {
origMetadata = origMetadata + addlMetadata;
}
this.dataset = orig.getDataset();
this.metadata = origMetadata;
this(orig.getDataset(), orig.getMetadata() == null ? addlMetadata: orig.getMetadata() + addlMetadata);
}
/**
......@@ -72,68 +57,4 @@ public class DatasetMessage implements Serializable {
public Dataset getDataset() {
return dataset;
}
/**
* Retrieves the metadata
* @return metadata associated with this payload, or null if none
*/
public String getMetadata() {
return metadata;
}
/**
* Convenience function to split metadata of the form &amp;key1=val1&amp;key2=val2... into a Map
* @param mData original metadata
* @return key:value map of metadata
*/
public static Map<String, String> getMetadata(String mData) {
HashMap<String, String> md = new HashMap<>();
String[] entries = mData.split("&");
for (String entry : entries) {
String[] pair = entry.split("=");
if (pair.length == 2) {
md.put(pair[0], pair[1]);
}
}
return md;
}
/**
* Convenience function to add a key-value pair to the current instance's metadata. Metadata will
* be of the form this.getMetadata()&amp;key=val
* @param key key
* @param val value
* @return new metadata
*/
public String genMetadata(String key, String val) {
return genMetadata(this, key, val);
}
/**
* Convenience function to add a key-value metadata pair to a DatasetMessage's metadata
* @param orig source of existing metadata
* @param key key
* @param val value
* @return String of the form orig.getMetadata()&amp;key=val
*/
public static String genMetadata(DatasetMessage orig, String key, String val) {
if (orig != null) {
String md = orig.getMetadata();
StringBuilder sb = new StringBuilder(md);
sb.append("&");
sb.append(genMetadataEntry(key, val));
return sb.toString();
}
return genMetadataEntry(key, val);
}
/**
* Convenience function to generate a key=val pair.
* @param key key
* @param val value
* @return String of the form key=val
*/
public static String genMetadataEntry(String key, String val) {
return key + "=" + val;
}
}
......@@ -5,23 +5,27 @@
package com.emphysic.myriad.network.messages;
import java.io.File;
import java.io.Serializable;
/**
* FileMessage - immutable message with a File payload
*/
public class FileMessage implements Serializable {
public class FileMessage extends ImmutableMessage {
/**
* Payload
*/
protected final File file;
private final File file;
public FileMessage(File file, String metadata) {
super(metadata);
this.file = file;
}
/**
* Creates a new FileMessage
* @param file payload
*/
public FileMessage(File file) {
this.file = file;
this(file, genMetadata(null, "source", file.getAbsolutePath()));
}
/**
......@@ -29,7 +33,7 @@ public class FileMessage implements Serializable {
* @param fileName payload
*/
public FileMessage(String fileName) {
this.file = new File(fileName);
this(new File(fileName));
}
/**
......
/*
* Copyright (c) 2016 Emphysic LLC. All rights reserved.
*/
package com.emphysic.myriad.network.messages;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* ImmutableMessage - base class for Myriad messages.
* Created by ccoughlin on 9/28/2016.
*/
public abstract class ImmutableMessage implements Serializable {
/**
* Metadata
*/
final String metadata;
ImmutableMessage(String metadata) {
this.metadata = metadata;
}
/**
* Retrieves the metadata
* @return metadata associated with this payload, or null if none
*/
public String getMetadata() {
return metadata;
}
/**
* Convenience function to split metadata of the form &amp;key1=val1&amp;key2=val2... into a Map
* @param mData original metadata
* @return key:value map of metadata
*/
public static Map<String, String> getMetadata(String mData) {
HashMap<String, String> md = new HashMap<>();
String[] entries = mData.split("&");
for (String entry : entries) {
String[] pair = entry.split("=");
if (pair.length == 2) {
md.put(pair[0], pair[1]);
}
}
return md;
}
/**
* Convenience function to create a new metadata String from this message's metadata and a new
* key=val pair. This instance's metadata is not modified.
* @param key key
* @param val value
* @return new metadata String of the form this.getMetadata();&amp;key=val
*/
public String genMetadata(String key, String val) {
return genMetadata(this, key, val);
}
/**
* Convenience function to create a new metadata String from a message's metadata and a new
* key=val pair. The original message is not modified.
* @param orig metadata of existing metadata
* @param key key
* @param val value
* @return String of the form orig.getMetadata()&amp;key=val
*/
public static String genMetadata(ImmutableMessage orig, String key, String val) {
StringBuilder sb = new StringBuilder();
if (orig != null) {
String md = orig.getMetadata();
if (md != null) {
sb.append(md);
}
}
sb.append("&");
sb.append(genMetadataEntry(key, val));
return sb.toString();
}
/**
* Convenience function to generate a key=val pair.
* @param key key
* @param val value
* @return String of the form key=val
*/
public static String genMetadataEntry(String key, String val) {
return key + "=" + val;
}
}
......@@ -7,21 +7,15 @@ package com.emphysic.myriad.network.messages;
import com.emphysic.myriad.core.data.roi.ROI;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
/**
* ROIMessage - immutable message with a Region of Interest (ROI) payload.
*/
@Slf4j
public class ROIMessage implements Serializable {
public class ROIMessage extends ImmutableMessage {
/**
* Payload
*/
protected final ROI ROI;
/**
* Metadata associated with the payload
*/
protected final String metadata;
/**
* Creates a new ROIMessage
......@@ -29,8 +23,8 @@ public class ROIMessage implements Serializable {
* @param metadata metadata
*/
public ROIMessage(ROI ROI, String metadata) {
super(metadata);
this.ROI = ROI;
this.metadata = metadata;
}
/**
......@@ -56,14 +50,6 @@ public class ROIMessage implements Serializable {
return ROI;
}
/**
* Retrieves this message's metadata
* @return metadata or null if none
*/
public String getMetaData() {
return metadata;
}
@Override
public String toString() {
return metadata;
......
......@@ -4,10 +4,15 @@
package com.emphysic.myriad.network.messages;
import java.io.Serializable;
/**
* ShutdownMessage - informs the Myriad components that a shutdown has been requested
*/
public class ShutdownMessage implements Serializable {
public class ShutdownMessage extends ImmutableMessage {
public ShutdownMessage(String metadata) {
super(metadata);
}
public ShutdownMessage() {
this(null);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment