Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(output): cleaner initialization of output generators #403

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,6 @@ public abstract class OutputGeneratorLoader {
private Collection<String> interactionTypes;
private File configurationDirectory;

/**
* this method is called just after a new instance of a derived output generator config was created.
* subclasses should call this method at first, and then proceed with reading custom parameters from the configuration
*
* @param rti the {@link RtiAmbassador} of the federation
* @param config output generator configuration
* @param configurationDirectory output generator configuration directory path
*/
public void initialize(RtiAmbassador rti, HierarchicalConfiguration<ImmutableNode> config, File configurationDirectory) throws Exception {
this.rti = rti;
this.id = ConfigHelper.getId(config);
this.updateInterval = ConfigHelper.getUpdateInterval(config);
this.handleStartTime = ConfigHelper.getHandleStartTime(config);
this.handleEndTime = ConfigHelper.getHandleEndTime(config);
this.interactionTypes = ConfigHelper.getSubscriptions(config);
this.configurationDirectory = configurationDirectory;
}


/**
* Returns the output generator identifier.
*
Expand Down Expand Up @@ -122,7 +103,35 @@ public File getConfigurationDirectory() {
}

/**
* Factory method for creating actual output generator.
* this method is called just after a new instance of a derived output generator config was created.
*
* @param rti the {@link RtiAmbassador} of the federation
* @param config output generator configuration
* @param configurationDirectory output generator configuration directory path
*/
public final void initialize(RtiAmbassador rti, HierarchicalConfiguration<ImmutableNode> config, File configurationDirectory) throws Exception {
this.rti = rti;
this.id = ConfigHelper.getId(config);
this.updateInterval = ConfigHelper.getUpdateInterval(config);
this.handleStartTime = ConfigHelper.getHandleStartTime(config);
this.handleEndTime = ConfigHelper.getHandleEndTime(config);
this.interactionTypes = ConfigHelper.getSubscriptions(config);
this.configurationDirectory = configurationDirectory;

configure(config);
}

/**
* Subclasses implement this method to read additional custom configuration from the
* given {@link HierarchicalConfiguration} tree.
*
* @param config the configuration tree for the output generator
*/
protected abstract void configure(HierarchicalConfiguration<ImmutableNode> config);

/**
* Factory method for creating actual output generator based on the previously
* read configuration ({@link #configure}).
*
* @return the actual output generator
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.mosaic.fed.output.generator.file.write.WriteByFile;
import org.eclipse.mosaic.fed.output.generator.file.write.WriteByFileCompress;
import org.eclipse.mosaic.fed.output.generator.file.write.WriteByLog;
import org.eclipse.mosaic.rti.api.RtiAmbassador;

import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.tree.ImmutableNode;
Expand Down Expand Up @@ -72,6 +71,17 @@ public InteractionFormatter getInteractionFormatter() {
return this.interactionFormatter;
}

@Override
public void configure(HierarchicalConfiguration<ImmutableNode> config) {
try {
this.writer = this.getWrite(config);
this.interactionFormatter = this.createInteractionFormatter(config);
} catch (Exception e) {
log.error("Exception", e);
throw new RuntimeException("Caused by OutputGenerator " + getId(), e);
}
}

private InteractionFormatter createInteractionFormatter(HierarchicalConfiguration<ImmutableNode> sub)
throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException {
String separatorInput = sub.getString(SEPARATOR);
Expand Down Expand Up @@ -146,18 +156,6 @@ private Write getWrite(HierarchicalConfiguration<ImmutableNode> sub) throws IOEx
return ret;
}

@Override
public void initialize(RtiAmbassador rti, HierarchicalConfiguration<ImmutableNode> config, File configurationDirectory) throws Exception {
super.initialize(rti, config, configurationDirectory);
try {
this.writer = this.getWrite(config);
this.interactionFormatter = this.createInteractionFormatter(config);
} catch (Exception e) {
log.error("Exception", e);
throw new Exception("Caused by OutputGenerator " + getId(), e);
}
}

@Override
public AbstractOutputGenerator createOutputGenerator() {
return new FileOutput(this.writer, this.getInteractionFormatter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@

import org.eclipse.mosaic.fed.output.ambassador.AbstractOutputGenerator;
import org.eclipse.mosaic.fed.output.ambassador.OutputGeneratorLoader;
import org.eclipse.mosaic.rti.api.RtiAmbassador;

import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.tree.ImmutableNode;

import java.io.File;

public class WebsocketVisualizerLoader extends OutputGeneratorLoader {

private int port;

@Override
public void initialize(RtiAmbassador rti, HierarchicalConfiguration<ImmutableNode> config, File configurationDirectory) throws Exception {
super.initialize(rti, config, configurationDirectory);
public void configure(HierarchicalConfiguration<ImmutableNode> config) {
port = config.getInt("port");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

import static org.mockito.Mockito.spy;

import org.eclipse.mosaic.fed.output.ambassador.AbstractOutputGenerator;
import org.eclipse.mosaic.fed.output.ambassador.OutputGeneratorLoader;
import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.tree.ImmutableNode;

/**
* An extension of {@link OutputGeneratorLoader} for testing purposes.
*/
public class TestOutputGeneratorLoader extends OutputGeneratorLoader {

@Override
protected void configure(HierarchicalConfiguration<ImmutableNode> config) {
// nothing to do
}

@Override
public AbstractOutputGenerator createOutputGenerator() {
return spy(new AbstractOutputGenerator() {});
Expand Down
Loading