Added new abstraction layer
This commit is contained in:
@@ -26,6 +26,7 @@ import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
@@ -176,81 +177,65 @@ public class AcquisitionMain {
|
||||
* @param nogui Flag whether to run the scan with a GUI
|
||||
* @param variables Table of scan variables
|
||||
*/
|
||||
public static void run(File file, Integer iterations, boolean autoclose, boolean nogui){
|
||||
public static void run(final File file, Integer iterations, boolean autoclose, boolean nogui){
|
||||
|
||||
// Initialize application
|
||||
ApplicationConfigurator ac = new ApplicationConfigurator();
|
||||
ac.initializeApplication();
|
||||
|
||||
if(!file.exists()){
|
||||
throw new RuntimeException("File "+file.getAbsolutePath()+" does not exist");
|
||||
}
|
||||
|
||||
EDescriptor edescriptor = null;
|
||||
ServiceLoader<EDescriptorProvider> providers = ServiceLoader.load(EDescriptorProvider.class);
|
||||
for (EDescriptorProvider provider : providers) {
|
||||
try{
|
||||
edescriptor = provider.loadDescriptor(file);
|
||||
// Once the first provider can interpret stop iteration of the providers
|
||||
break;
|
||||
}
|
||||
catch(Exception e){
|
||||
logger.log(Level.FINEST, provider.getClass().getName()+ " is not able to read provided descriptor files", e);
|
||||
}
|
||||
}
|
||||
|
||||
Configuration c;
|
||||
try {
|
||||
c = ModelManager.unmarshall(file);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to deserialize configuration: "+e.getMessage(), e);
|
||||
}
|
||||
|
||||
// Set data file name
|
||||
// Determine name used for the data file
|
||||
String name = file.getName();
|
||||
name = name.replaceAll("\\.xml$", "");
|
||||
|
||||
if(c.getData()!=null){
|
||||
Data data = c.getData();
|
||||
// Only update filename if no name is specified in xml file
|
||||
if(data.getFileName()==null){
|
||||
data.setFileName(name);
|
||||
}
|
||||
}
|
||||
else{
|
||||
Data data = new Data();
|
||||
data.setFileName(name);
|
||||
c.setData(data);
|
||||
}
|
||||
|
||||
|
||||
// Override number of executions
|
||||
if(iterations != null){
|
||||
c.setNumberOfExecution(iterations);
|
||||
}
|
||||
// Fix configuration if iterations is specified with 0 and no iterations option is specified
|
||||
if(c.getNumberOfExecution()==0){
|
||||
c.setNumberOfExecution(1);
|
||||
}
|
||||
|
||||
// Create/get acquisition engine
|
||||
final Acquisition acquisition = new Acquisition(new DefaultChannelService(), new AcquisitionConfiguration());
|
||||
|
||||
boolean vis = false;
|
||||
// Only register data visualization task/processor if there are visualizations
|
||||
if(c.getVisualization().size()>0 && !nogui){
|
||||
vis=true;
|
||||
}
|
||||
// TODO
|
||||
// boolean vis = false;
|
||||
// // Only register data visualization task/processor if there are visualizations
|
||||
// if(c.getVisualization().size()>0 && !nogui){
|
||||
// vis=true;
|
||||
// }
|
||||
|
||||
EventBus b = new AsyncEventBus(Executors.newSingleThreadExecutor());
|
||||
|
||||
EContainer ec = null;
|
||||
ServiceLoader<EContainerFactory> factories = ServiceLoader.load(EContainerFactory.class);
|
||||
for (EContainerFactory factory : factories) {
|
||||
if(factory.supportsEDescriptor(edescriptor)){
|
||||
ec = factory.createContainer(edescriptor, b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
final EContainer econtainer = ec;
|
||||
|
||||
acquisition.initalize(b, c);
|
||||
|
||||
econtainer.initialize();
|
||||
|
||||
Visualizer visualizer = null;
|
||||
// Only register data visualization task/processor if there are visualizations
|
||||
if(vis){
|
||||
|
||||
visualizer = new Visualizer(VisualizationMapper.mapVisualizations(c.getVisualization()));
|
||||
b.register(visualizer);
|
||||
|
||||
// TODO eventually set update on delimiter/dim boundary here
|
||||
|
||||
// If there is a continous dimension only update plot at the end of a line
|
||||
if(c.getScan() != null && c.getScan().getCdimension()!=null){
|
||||
visualizer.setUpdateAtStreamElement(false);
|
||||
visualizer.setUpdateAtStreamDelimiter(true);
|
||||
visualizer.setUpdateAtEndOfStream(true);
|
||||
}
|
||||
}
|
||||
// TODO
|
||||
// if(vis){
|
||||
//
|
||||
// visualizer = new Visualizer(VisualizationMapper.mapVisualizations(c.getVisualization()));
|
||||
// b.register(visualizer);
|
||||
//
|
||||
// // TODO eventually set update on delimiter/dim boundary here
|
||||
//
|
||||
// // If there is a continous dimension only update plot at the end of a line
|
||||
// if(c.getScan() != null && c.getScan().getCdimension()!=null){
|
||||
// visualizer.setUpdateAtStreamElement(false);
|
||||
// visualizer.setUpdateAtStreamDelimiter(true);
|
||||
// visualizer.setUpdateAtEndOfStream(true);
|
||||
// }
|
||||
// }
|
||||
|
||||
// GUI GUI GUI GUI GUI GUI GUI
|
||||
ProgressPanel progressPanel = null;
|
||||
@@ -267,7 +252,7 @@ public class AcquisitionMain {
|
||||
opanel.add(p);
|
||||
}
|
||||
|
||||
final JFrame frame = new JFrame("FDA: "+acquisition.getDatafileName());
|
||||
final JFrame frame = new JFrame("FDA: "+file.getName());
|
||||
frame.setSize(1200,800);
|
||||
|
||||
// Create progress panel
|
||||
@@ -277,7 +262,7 @@ public class AcquisitionMain {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
try {
|
||||
acquisition.abort();
|
||||
econtainer.abort();
|
||||
} catch (Exception e1) {
|
||||
logger.log(Level.SEVERE, "Exception occured while aborting scan", e1);
|
||||
}
|
||||
@@ -294,15 +279,15 @@ public class AcquisitionMain {
|
||||
frame.addWindowListener(new WindowAdapter(){
|
||||
@Override
|
||||
public void windowClosing(WindowEvent we){
|
||||
if(acquisition.isActive()){
|
||||
if(econtainer.isActive()){
|
||||
// Abort acquisition
|
||||
acquisition.abort();
|
||||
econtainer.abort();
|
||||
}
|
||||
|
||||
// Wait until acquisition is aborted. Maximum wait 10*100milliseconds before forcefully
|
||||
// terminate application
|
||||
int count=0;
|
||||
while(acquisition.isActive()){
|
||||
while(econtainer.isActive()){
|
||||
if(count == 10){
|
||||
break;
|
||||
}
|
||||
@@ -363,9 +348,9 @@ public class AcquisitionMain {
|
||||
|
||||
abortedViaSignal = true;
|
||||
// Abort acquisition engine
|
||||
if(acquisition.isActive()){
|
||||
if(econtainer.isActive()){
|
||||
// Abort acquisition
|
||||
acquisition.abort();
|
||||
econtainer.abort();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -373,20 +358,20 @@ public class AcquisitionMain {
|
||||
|
||||
|
||||
// Run acquisition engine
|
||||
try {
|
||||
// try {
|
||||
if(visualizer != null){
|
||||
// Start visualization
|
||||
visualizer.configure();
|
||||
}
|
||||
|
||||
acquisition.execute();
|
||||
econtainer.execute();
|
||||
|
||||
} catch (InterruptedException e1) {
|
||||
throw new RuntimeException("Acquisition was interrupted",e1);
|
||||
}
|
||||
finally{
|
||||
acquisition.destroy();
|
||||
}
|
||||
// } catch (InterruptedException e1) {
|
||||
// throw new RuntimeException("Acquisition was interrupted",e1);
|
||||
// }
|
||||
// finally{
|
||||
econtainer.destroy();
|
||||
// }
|
||||
|
||||
|
||||
// GUI GUI GUI GUI GUI GUI GUI
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package ch.psi.fda.aq;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
import ch.psi.fda.EContainer;
|
||||
import ch.psi.fda.model.v1.Configuration;
|
||||
import ch.psi.jcae.ChannelService;
|
||||
|
||||
public class XScanContainer implements EContainer {
|
||||
|
||||
private final Acquisition acquisition;
|
||||
|
||||
private EventBus bus;
|
||||
private Configuration xscanConfiguration;
|
||||
|
||||
public XScanContainer(ChannelService cservice, AcquisitionConfiguration config, EventBus bus, Configuration xscanConfiguration){
|
||||
acquisition = new Acquisition(cservice, config);
|
||||
this.bus = bus;
|
||||
this.xscanConfiguration = xscanConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
acquisition.initalize(bus, xscanConfiguration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
try {
|
||||
acquisition.execute();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort() {
|
||||
acquisition.abort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
acquisition.abort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
acquisition.destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return acquisition.isActive();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ch.psi.fda.aq;
|
||||
|
||||
import ch.psi.fda.EDescriptor;
|
||||
import ch.psi.fda.model.v1.Configuration;
|
||||
|
||||
public class XScanDescriptor implements EDescriptor {
|
||||
|
||||
private final Configuration configuration;
|
||||
|
||||
public XScanDescriptor(Configuration configuration){
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
public Configuration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package ch.psi.fda.aq;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import ch.psi.fda.EDescriptor;
|
||||
import ch.psi.fda.EDescriptorProvider;
|
||||
import ch.psi.fda.model.ModelManager;
|
||||
import ch.psi.fda.model.v1.Configuration;
|
||||
import ch.psi.fda.model.v1.Data;
|
||||
|
||||
public class XScanDescriptorProvider implements EDescriptorProvider {
|
||||
|
||||
@Override
|
||||
public EDescriptor loadDescriptor(File... files) {
|
||||
|
||||
|
||||
if(files.length<1 || files[0]==null){
|
||||
throw new IllegalArgumentException("There need to be at lease one file specified");
|
||||
}
|
||||
File file = files[0];
|
||||
|
||||
if(!file.exists()){
|
||||
throw new IllegalArgumentException("File "+file.getAbsolutePath()+" does not exist");
|
||||
}
|
||||
|
||||
Configuration c;
|
||||
try {
|
||||
c = ModelManager.unmarshall(file);
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedOperationException("Unable to deserialize configuration: "+e.getMessage(), e);
|
||||
}
|
||||
|
||||
// Set data file name
|
||||
// Determine name used for the data file
|
||||
String name = file.getName();
|
||||
name = name.replaceAll("\\.xml$", "");
|
||||
|
||||
if(c.getData()!=null){
|
||||
Data data = c.getData();
|
||||
// Only update filename if no name is specified in xml file
|
||||
if(data.getFileName()==null){
|
||||
data.setFileName(name);
|
||||
}
|
||||
}
|
||||
else{
|
||||
Data data = new Data();
|
||||
data.setFileName(name);
|
||||
c.setData(data);
|
||||
}
|
||||
|
||||
|
||||
// // Override number of executions
|
||||
// if(iterations != null){
|
||||
// c.setNumberOfExecution(iterations);
|
||||
// }
|
||||
// Fix configuration if iterations is specified with 0 and no iterations option is specified
|
||||
if(c.getNumberOfExecution()==0){
|
||||
c.setNumberOfExecution(1);
|
||||
}
|
||||
|
||||
return new XScanDescriptor(c);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package ch.psi.fda.aq;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
import ch.psi.fda.EContainer;
|
||||
import ch.psi.fda.EContainerFactory;
|
||||
import ch.psi.fda.EDescriptor;
|
||||
import ch.psi.jcae.ChannelService;
|
||||
import ch.psi.jcae.impl.DefaultChannelService;
|
||||
|
||||
public class XScanFactory implements EContainerFactory {
|
||||
|
||||
// TODO need to be injected
|
||||
private ChannelService cservice = new DefaultChannelService();
|
||||
|
||||
private AcquisitionConfiguration config = new AcquisitionConfiguration();
|
||||
|
||||
@Override
|
||||
public boolean supportsEDescriptor(EDescriptor descriptor) {
|
||||
return (descriptor instanceof XScanDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EContainer createContainer(EDescriptor descriptor, EventBus bus) {
|
||||
|
||||
if(! (descriptor instanceof XScanDescriptor)){
|
||||
throw new IllegalArgumentException("Descriptor of type "+descriptor.getClass().getName()+" is not supported - descriptor need to be of type "+XScanDescriptor.class);
|
||||
}
|
||||
|
||||
XScanDescriptor xdescriptor = (XScanDescriptor) descriptor;
|
||||
|
||||
return new XScanContainer(cservice, config, bus, xdescriptor.getConfiguration());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
ch.psi.fda.aq.XScanFactory
|
||||
@@ -0,0 +1 @@
|
||||
ch.psi.fda.aq.XScanDescriptorProvider
|
||||
Reference in New Issue
Block a user