From cb02279a4c81d89991451e2c34267f2e620fd380 Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa Date: Tue, 5 May 2015 15:59:04 +0200 Subject: [PATCH 1/3] Added InitializingSignal and adopted the framework --- src/main/java/teetime/framework/Analysis.java | 7 +-- .../framework/RunnableProducerStage.java | 3 ++ .../framework/signal/InitializingSignal.java | 47 +++++++++++++++++++ src/site/markdown/wiki | 2 +- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/main/java/teetime/framework/signal/InitializingSignal.java diff --git a/src/main/java/teetime/framework/Analysis.java b/src/main/java/teetime/framework/Analysis.java index 20b9a4fe..a0c78ef9 100644 --- a/src/main/java/teetime/framework/Analysis.java +++ b/src/main/java/teetime/framework/Analysis.java @@ -154,9 +154,10 @@ private final void init() { throw new IllegalStateException("Unhandled termination strategy: " + terminationStrategy); } - final Set intraStages = traverseIntraStages(stage); - final AbstractExceptionListener newListener = factory.createInstance(); - initializeIntraStages(intraStages, thread, newListener); + // FIXME: remove, if this solves the #151 bug + // final Set intraStages = traverseIntraStages(stage); + // final AbstractExceptionListener newListener = factory.createInstance(); + // initializeIntraStages(intraStages, thread, newListener); } } diff --git a/src/main/java/teetime/framework/RunnableProducerStage.java b/src/main/java/teetime/framework/RunnableProducerStage.java index 2b7b556a..a9f2c9a4 100644 --- a/src/main/java/teetime/framework/RunnableProducerStage.java +++ b/src/main/java/teetime/framework/RunnableProducerStage.java @@ -15,6 +15,7 @@ */ package teetime.framework; +import teetime.framework.signal.InitializingSignal; import teetime.framework.signal.StartingSignal; import teetime.framework.signal.TerminatingSignal; @@ -26,6 +27,8 @@ public RunnableProducerStage(final Stage stage) { @Override protected void beforeStageExecution(final Stage stage) { + InitializingSignal initializingSignal = new InitializingSignal(); + stage.onSignal(initializingSignal, null); final StartingSignal startingSignal = new StartingSignal(); stage.onSignal(startingSignal, null); } diff --git a/src/main/java/teetime/framework/signal/InitializingSignal.java b/src/main/java/teetime/framework/signal/InitializingSignal.java new file mode 100644 index 00000000..bb631edc --- /dev/null +++ b/src/main/java/teetime/framework/signal/InitializingSignal.java @@ -0,0 +1,47 @@ +/** + * Copyright (C) 2015 Christian Wulf, Nelson Tavares de Sousa (http://teetime.sourceforge.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package teetime.framework.signal; + +import java.util.LinkedList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import teetime.framework.Stage; + +public final class InitializingSignal implements ISignal { + + private static final Logger LOGGER = LoggerFactory.getLogger(StartingSignal.class); + private final List catchedExceptions = new LinkedList(); + + public InitializingSignal() {} + + @Override + public void trigger(final Stage stage) { + try { + stage.onInitializing(); + } catch (Exception e) { // NOCS (Stages can throw any arbitrary Exception) + this.catchedExceptions.add(e); + LOGGER.error("Exception while sending the start signal", e); + } + } + + public List getCatchedExceptions() { + return this.catchedExceptions; + } + +} diff --git a/src/site/markdown/wiki b/src/site/markdown/wiki index 0e447457..bb53dfd7 160000 --- a/src/site/markdown/wiki +++ b/src/site/markdown/wiki @@ -1 +1 @@ -Subproject commit 0e4474577e1f49bc96e734c286b2d9e0363895e8 +Subproject commit bb53dfd7de974a433a7b96b0b65f4aacb8da3df3 From b5d2c03e1521c89266984551719a2e4a70c0853f Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa Date: Tue, 5 May 2015 16:52:43 +0200 Subject: [PATCH 2/3] moved initializing --- src/main/java/teetime/framework/Analysis.java | 18 ++++++++---------- .../framework/RunnableProducerStage.java | 3 --- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main/java/teetime/framework/Analysis.java b/src/main/java/teetime/framework/Analysis.java index a0c78ef9..f1ca4827 100644 --- a/src/main/java/teetime/framework/Analysis.java +++ b/src/main/java/teetime/framework/Analysis.java @@ -28,6 +28,7 @@ import teetime.framework.exceptionHandling.AbstractExceptionListener; import teetime.framework.exceptionHandling.IExceptionListenerFactory; import teetime.framework.exceptionHandling.IgnoringExceptionListenerFactory; +import teetime.framework.signal.InitializingSignal; import teetime.framework.signal.ValidatingSignal; import teetime.framework.validation.AnalysisNotValidException; import teetime.util.Pair; @@ -142,22 +143,24 @@ private final void init() { final RunnableProducerStage runnable = new RunnableProducerStage(stage); thread = createThread(runnable, stage.getId()); this.finiteProducerThreads.add(thread); + InitializingSignal initializingSignal = new InitializingSignal(); + stage.onSignal(initializingSignal, null); break; } case BY_INTERRUPT: { final RunnableProducerStage runnable = new RunnableProducerStage(stage); thread = createThread(runnable, stage.getId()); + InitializingSignal initializingSignal = new InitializingSignal(); + stage.onSignal(initializingSignal, null); this.infiniteProducerThreads.add(thread); break; } default: throw new IllegalStateException("Unhandled termination strategy: " + terminationStrategy); } - - // FIXME: remove, if this solves the #151 bug - // final Set intraStages = traverseIntraStages(stage); - // final AbstractExceptionListener newListener = factory.createInstance(); - // initializeIntraStages(intraStages, thread, newListener); + final Set intraStages = traverseIntraStages(stage); + final AbstractExceptionListener newListener = factory.createInstance(); + initializeIntraStages(intraStages, thread, newListener); } } @@ -173,11 +176,6 @@ private void initializeIntraStages(final Set intraStages, final Thread th for (Stage intraStage : intraStages) { intraStage.setOwningThread(thread); intraStage.setExceptionHandler(newListener); - try { - intraStage.onInitializing(); - } catch (Exception e) { // NOPMD(generic framework catch) - throw new IllegalStateException("The following exception occurs within initializing the analysis:", e); - } } } diff --git a/src/main/java/teetime/framework/RunnableProducerStage.java b/src/main/java/teetime/framework/RunnableProducerStage.java index a9f2c9a4..2b7b556a 100644 --- a/src/main/java/teetime/framework/RunnableProducerStage.java +++ b/src/main/java/teetime/framework/RunnableProducerStage.java @@ -15,7 +15,6 @@ */ package teetime.framework; -import teetime.framework.signal.InitializingSignal; import teetime.framework.signal.StartingSignal; import teetime.framework.signal.TerminatingSignal; @@ -27,8 +26,6 @@ public RunnableProducerStage(final Stage stage) { @Override protected void beforeStageExecution(final Stage stage) { - InitializingSignal initializingSignal = new InitializingSignal(); - stage.onSignal(initializingSignal, null); final StartingSignal startingSignal = new StartingSignal(); stage.onSignal(startingSignal, null); } From 9f21618da56e87c8152ca574c6c2e121c2be6ba9 Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa Date: Tue, 5 May 2015 17:27:10 +0200 Subject: [PATCH 3/3] Quick fix for #151 --- src/main/java/teetime/framework/AbstractStage.java | 2 +- src/main/java/teetime/framework/RunnableConsumerStage.java | 3 +++ src/main/java/teetime/stage/MultipleInstanceOfFilter.java | 1 - src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java | 3 +++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/teetime/framework/AbstractStage.java b/src/main/java/teetime/framework/AbstractStage.java index ab8d1dae..96b71bb4 100644 --- a/src/main/java/teetime/framework/AbstractStage.java +++ b/src/main/java/teetime/framework/AbstractStage.java @@ -58,10 +58,10 @@ public StageState getCurrentState() { public void onSignal(final ISignal signal, final InputPort inputPort) { if (!this.signalAlreadyReceived(signal, inputPort)) { signal.trigger(this); - for (OutputPort outputPort : outputPorts) { outputPort.sendSignal(signal); } + } } diff --git a/src/main/java/teetime/framework/RunnableConsumerStage.java b/src/main/java/teetime/framework/RunnableConsumerStage.java index 5a8e3964..ded489eb 100644 --- a/src/main/java/teetime/framework/RunnableConsumerStage.java +++ b/src/main/java/teetime/framework/RunnableConsumerStage.java @@ -47,6 +47,9 @@ protected void beforeStageExecution(final Stage stage) throws InterruptedExcepti for (InputPort inputPort : inputPorts) { inputPort.waitForStartSignal(); } + for (InputPort inputPort : inputPorts) { + inputPort.waitForStartSignal(); + } logger.trace("Starting..." + stage); } diff --git a/src/main/java/teetime/stage/MultipleInstanceOfFilter.java b/src/main/java/teetime/stage/MultipleInstanceOfFilter.java index 46a363af..42f0d22b 100644 --- a/src/main/java/teetime/stage/MultipleInstanceOfFilter.java +++ b/src/main/java/teetime/stage/MultipleInstanceOfFilter.java @@ -42,7 +42,6 @@ public OutputPort getOutputPortForType(final Class clazz) { @SuppressWarnings("unchecked") public void onStarting() throws Exception { super.onStarting(); - // We cache the map to avoid the creating of iterators during runtime cachedOutputPortsMap = (Entry, OutputPort>[]) outputPortsMap.entrySet().toArray(new Entry[outputPortsMap.size()]); } diff --git a/src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java b/src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java index 3654fedd..6221ba77 100644 --- a/src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java +++ b/src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java @@ -16,7 +16,9 @@ package teetime.stage; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; import java.util.ArrayList; @@ -42,6 +44,7 @@ public void filteringForSingleTypeShouldWork() { StageTester.test(filter).and().send(input).to(filter.getInputPort()).and().receive(result).from(filter.getOutputPortForType(String.class)).start(); + assertThat(result, is(not(empty()))); assertThat(result, contains("1", "2", "3")); }