From 2e307dbd78ceb5579a36b1f48d30f7d112753f11 Mon Sep 17 00:00:00 2001 From: jrte Date: Tue, 19 Sep 2023 22:31:05 -0300 Subject: [PATCH] Clean up exception reporting + bug fixes Bug: ribose run output not directed to file specified on command line - fix up @throws for javadoc - remove exceptions declared but not thrown per method - bug fixes - delete jar files in clean-ribose target in build.xml - Model.java @ line 360 (fix array length growth factor) - ModelLoader.java @ line 164 (direct transductor output) - send InputStack log output to runtime log instead of compile log Signed-off-by: jrte --- build.xml | 10 ++++------ src/com/characterforming/jrte/engine/Base.java | 2 +- src/com/characterforming/jrte/engine/InputStack.java | 2 +- src/com/characterforming/jrte/engine/Model.java | 8 ++++---- .../characterforming/jrte/engine/ModelLoader.java | 3 ++- .../characterforming/jrte/engine/Transductor.java | 2 +- src/com/characterforming/jrte/test/FileRunner.java | 11 ++++------- src/com/characterforming/jrte/test/TestRunner.java | 12 ++++-------- src/com/characterforming/ribose/IEffector.java | 2 +- src/com/characterforming/ribose/IModel.java | 12 ++++++------ src/com/characterforming/ribose/IOutput.java | 4 ++-- .../ribose/IParameterizedEffector.java | 4 ++-- src/com/characterforming/ribose/ITarget.java | 2 +- src/com/characterforming/ribose/ITransductor.java | 6 +++--- src/com/characterforming/ribose/Ribose.java | 2 +- .../ribose/base/BaseParameterizedEffector.java | 1 - 16 files changed, 37 insertions(+), 46 deletions(-) diff --git a/build.xml b/build.xml index 9ef3e20..9e9c451 100755 --- a/build.xml +++ b/build.xml @@ -76,21 +76,19 @@ - + - - - + - + + - diff --git a/src/com/characterforming/jrte/engine/Base.java b/src/com/characterforming/jrte/engine/Base.java index c75da47..85bc6e0 100644 --- a/src/com/characterforming/jrte/engine/Base.java +++ b/src/com/characterforming/jrte/engine/Base.java @@ -144,7 +144,7 @@ public static int getOutBufferSize() { * @param bytes The UTF-8 byte array * @param length The number of bytes to decode, starting from 0 * @return the decoded integer - * @throws NumberFormatException on error + * @throws NumberFormatException if things don't work out */ public static int decodeInt(final byte[] bytes, int length) throws NumberFormatException { int value = 0; diff --git a/src/com/characterforming/jrte/engine/InputStack.java b/src/com/characterforming/jrte/engine/InputStack.java index 36f765c..97bc16c 100644 --- a/src/com/characterforming/jrte/engine/InputStack.java +++ b/src/com/characterforming/jrte/engine/InputStack.java @@ -54,7 +54,7 @@ final class FreeStack extends LinkedList { } InputStack(final int initialSize) { - this.logger = Base.getCompileLogger(); + this.logger = Base.getRuntimeLogger(); this.stack = Input.stack(initialSize); this.markList = Input.stack(initialSize); this.markState = InputStack.CLEAR; diff --git a/src/com/characterforming/jrte/engine/Model.java b/src/com/characterforming/jrte/engine/Model.java index 5b9df6c..9c2242d 100644 --- a/src/com/characterforming/jrte/engine/Model.java +++ b/src/com/characterforming/jrte/engine/Model.java @@ -47,6 +47,7 @@ import com.characterforming.ribose.base.Bytes; import com.characterforming.ribose.base.CompilationException; import com.characterforming.ribose.base.ModelException; +import com.characterforming.ribose.base.Signal; /** * @author Kim Briggs @@ -351,17 +352,16 @@ protected int addSignal(Bytes signalName) { protected int addTransducer(Bytes transducerName) { Integer ordinal = this.transducerOrdinalMap.get(transducerName); + assert ordinal == null || transducerName.equals(this.transducerNameIndex[ordinal]); if (ordinal == null) { ordinal = this.transducerOrdinalMap.size(); this.transducerOrdinalMap.put(transducerName, ordinal); if (ordinal >= this.transducerNameIndex.length) { - int length = ordinal + (ordinal << 1); + int length = ordinal + (Math.max(ordinal, 4) >> 1); this.transducerNameIndex = Arrays.copyOf(this.transducerNameIndex, length); this.transducerOffsetIndex = Arrays.copyOf(this.transducerOffsetIndex, length); } this.transducerNameIndex[ordinal] = transducerName; - } else { - assert this.transducerNameIndex[ordinal].equals(transducerName); } return ordinal; } @@ -510,7 +510,7 @@ protected long[] readTransitionMatrix() throws ModelException { final int nStates = this.io.readInt(); final int nInputs = this.io.readInt(); matrix = new long[nStates * nInputs]; - // preset all cells to invoke nul() effector on domain error + // preset all cells to signal NUL without changing state for (int state = 0; state < nStates; state++) { final int toState = state * nInputs; final long nul = Transducer.transition(toState, 0); diff --git a/src/com/characterforming/jrte/engine/ModelLoader.java b/src/com/characterforming/jrte/engine/ModelLoader.java index b7f0a4b..115ddc8 100644 --- a/src/com/characterforming/jrte/engine/ModelLoader.java +++ b/src/com/characterforming/jrte/engine/ModelLoader.java @@ -67,7 +67,7 @@ private ModelLoader(final File modelPath) * * @param modelFile the model file * @return the loaded model instance - * @throws ModelException on error + * @throws ModelException if things don't work out */ public static ModelLoader loadModel(File modelPath) throws ModelException { @@ -161,6 +161,7 @@ public boolean stream(Bytes transducer, ITarget target, Signal prologue, InputSt int position = read; if (read > 0) { ITransductor trex = transductor(target); + trex.output(out); if (prologue != null) trex.signal(prologue); if (trex.push(bytes, read).start(transducer).status().isRunnable()) { diff --git a/src/com/characterforming/jrte/engine/Transductor.java b/src/com/characterforming/jrte/engine/Transductor.java index 0df01a6..ff84d0f 100644 --- a/src/com/characterforming/jrte/engine/Transductor.java +++ b/src/com/characterforming/jrte/engine/Transductor.java @@ -147,7 +147,7 @@ public final class Transductor implements ITransductor, IOutput { * Constructor * * @param model The runtime model - * @throws ModelException on error + * @throws ModelException if things don't work out */ Transductor(final Model model) { super(); diff --git a/src/com/characterforming/jrte/test/FileRunner.java b/src/com/characterforming/jrte/test/FileRunner.java index 22c38ec..6a01042 100644 --- a/src/com/characterforming/jrte/test/FileRunner.java +++ b/src/com/characterforming/jrte/test/FileRunner.java @@ -24,7 +24,6 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharsetDecoder; @@ -40,18 +39,16 @@ import com.characterforming.ribose.ITransductor; import com.characterforming.ribose.ITransductor.Metrics; import com.characterforming.ribose.base.Bytes; -import com.characterforming.ribose.base.RiboseException; import com.characterforming.ribose.base.Signal; public class FileRunner { /** - * @param args - * @throws InterruptedException on error - * @throws RiboseException on error - * @throws IOException on error + * Shell interface. + * + * @param args arguments from the shell */ - public static void main(final String[] args) throws InterruptedException, RiboseException, IOException { + public static void main(final String[] args) { final boolean nil = args[0].compareTo("--nil") == 0; int arg = nil ? 1 : 0; if ((args.length - arg) < 3) { diff --git a/src/com/characterforming/jrte/test/TestRunner.java b/src/com/characterforming/jrte/test/TestRunner.java index 9b04b37..0b4da08 100644 --- a/src/com/characterforming/jrte/test/TestRunner.java +++ b/src/com/characterforming/jrte/test/TestRunner.java @@ -20,7 +20,6 @@ package com.characterforming.jrte.test; import java.io.File; -import java.io.IOException; import java.nio.CharBuffer; import java.nio.charset.CharsetEncoder; import java.util.logging.Level; @@ -33,19 +32,16 @@ import com.characterforming.ribose.ITransductor; import com.characterforming.ribose.ITransductor.Status; import com.characterforming.ribose.base.Bytes; -import com.characterforming.ribose.base.RiboseException; import com.characterforming.ribose.base.Signal; public class TestRunner { /** - * @param args - * @throws InterruptedException on error - * @throws RiboseException on error - * @throws IOException on error - * @throws SecurityException on error + * Shell interface. + * + * @param args arguments from the shell */ - public static void main(final String[] args) throws InterruptedException, RiboseException, SecurityException, IOException { + public static void main(final String[] args) { if (args.length == 0) { System.out.println(String.format("Usage: java -cp %1$s [wait-ms]", TestRunner.class.getName())); System.exit(1); diff --git a/src/com/characterforming/ribose/IEffector.java b/src/com/characterforming/ribose/IEffector.java index 293b7e6..6148765 100644 --- a/src/com/characterforming/ribose/IEffector.java +++ b/src/com/characterforming/ribose/IEffector.java @@ -80,7 +80,7 @@ public interface IEffector { * This method is invoked at runtime when triggered by an input transition. * * @return user-defined effectors should return {@code IEffector.RTX_NONE} - * @throws EffectorException on error + * @throws EffectorException if things don't work out */ int invoke() throws EffectorException; diff --git a/src/com/characterforming/ribose/IModel.java b/src/com/characterforming/ribose/IModel.java index dd94fee..ce0978e 100644 --- a/src/com/characterforming/ribose/IModel.java +++ b/src/com/characterforming/ribose/IModel.java @@ -63,7 +63,7 @@ public interface IModel extends AutoCloseable { * @param riboseModelFile path indicating where to create the model file * @return true if the model was created successfully * @throws ClassNotFoundException if {@code targetClassname} not found - * @throws ModelException if compilation + * @throws ModelException if compilation fails */ public static boolean compileRiboseModel(String targetClassname, File ginrAutomataDirectory, File riboseModelFile) throws ClassNotFoundException, ModelException { @@ -94,7 +94,7 @@ public static IModel loadRiboseModel(File riboseModelFile) * * @param target the target instance to bind to the transductor * @return a new transductor - * @throws ModelException on error + * @throws ModelException if things don't work out * @see ITransductor */ ITransductor transductor(ITarget target) @@ -123,7 +123,7 @@ ITransductor transductor(ITarget target) * @param in the input stream to transduce (eg, {@code System.in}) * @param out the output stream to render output to (eg, {@code System.out}) * @return true if transduction is complete, otherwise false - * @throws RiboseException on error + * @throws RiboseException if things don't work out */ boolean stream(Bytes transducer, Signal prologue, InputStream in, OutputStream out) throws RiboseException; @@ -139,7 +139,7 @@ boolean stream(Bytes transducer, Signal prologue, InputStream in, OutputStream o * @param in the input stream to transduce (eg, {@code System.in}) * @param out the output stream to render output to (eg, {@code System.out}) * @return true if target is an instance of the model target class and transduction is complete - * @throws RiboseException on error + * @throws RiboseException if things don't work out */ boolean stream(Bytes transducer, ITarget target, Signal prologue, InputStream in, OutputStream out) throws RiboseException; @@ -148,7 +148,7 @@ boolean stream(Bytes transducer, ITarget target, Signal prologue, InputStream in * Decompile a transducer to System.out * * @param transducerName the transducer name as aUnicode string - * @throws ModelException on error + * @throws ModelException if things don't work out */ void decompile(String transducerName) throws ModelException; @@ -163,7 +163,7 @@ void decompile(String transducerName) /** * Close the runtime model and file. * - * @throws ModelException on error + * @throws ModelException if things don't work out */ @Override void close() diff --git a/src/com/characterforming/ribose/IOutput.java b/src/com/characterforming/ribose/IOutput.java index 4999e9b..c6b1029 100755 --- a/src/com/characterforming/ribose/IOutput.java +++ b/src/com/characterforming/ribose/IOutput.java @@ -73,7 +73,7 @@ public interface IOutput { * * @param fieldName the name of the field (UTF-8 bytes, withouy `~` prefix in lead byte) * @return a field instance or null - * @throws TargetBindingException on error + * @throws TargetBindingException if things don't work out */ IField getField(Bytes fieldName) throws TargetBindingException; @@ -82,7 +82,7 @@ public interface IOutput { * * @param fieldName the name of the field (UTF-8 bytes, without `~` prefix in lead byte) * @return the ordinal number of the field - * @throws TargetBindingException on error + * @throws TargetBindingException if things don't work out */ int getFieldOrdinal(Bytes fieldName) throws TargetBindingException; diff --git a/src/com/characterforming/ribose/IParameterizedEffector.java b/src/com/characterforming/ribose/IParameterizedEffector.java index e5eca9c..98fcff2 100644 --- a/src/com/characterforming/ribose/IParameterizedEffector.java +++ b/src/com/characterforming/ribose/IParameterizedEffector.java @@ -105,7 +105,7 @@ public interface IParameterizedEffector extends IEffector< * * @param parameterIndex the index of the parameter object to be applied * @return user-defined effectors should return 0 (RTX_SI) - * @throws EffectorException on error + * @throws EffectorException if things don't work out */ int invoke(int parameterIndex) throws EffectorException; @@ -124,7 +124,7 @@ public interface IParameterizedEffector extends IEffector< * * @param parameterTokens an array of parameters, where each parameter is an array of bytes. * @return the compiled parameter value object - * @throws TargetBindingException on error + * @throws TargetBindingException if things don't work out */ P compileParameter(IToken[] parameterTokens) throws TargetBindingException; diff --git a/src/com/characterforming/ribose/ITarget.java b/src/com/characterforming/ribose/ITarget.java index c0d9910..e2c53fa 100644 --- a/src/com/characterforming/ribose/ITarget.java +++ b/src/com/characterforming/ribose/ITarget.java @@ -92,7 +92,7 @@ public interface ITarget { * the interaction is driven from an effector invokation. * * @return an array of IEffector instances bound to the target instance - * @throws TargetBindingException on error + * @throws TargetBindingException if things don't work out */ IEffector[] getEffectors() throws TargetBindingException; } diff --git a/src/com/characterforming/ribose/ITransductor.java b/src/com/characterforming/ribose/ITransductor.java index d1305ec..a579e4e 100644 --- a/src/com/characterforming/ribose/ITransductor.java +++ b/src/com/characterforming/ribose/ITransductor.java @@ -314,7 +314,7 @@ public void update(Metrics accumulator) { * * @param transducer the name of the transducer to push * @return this ITransductor - * @throws ModelException on error + * @throws ModelException if things don't work out */ ITransductor start(Bytes transducer) throws ModelException; @@ -339,8 +339,8 @@ public void update(Metrics accumulator) { * if the transduction involves backtracking with mark/reset. * * @return this ITransductor - * @throws EffectorException on error - * @throws DomainErrorException on error + * @throws EffectorException if an effector invocation fails + * @throws DomainErrorException if no transition defined for current input * @see #recycle(byte[]) * @see #status() */ diff --git a/src/com/characterforming/ribose/Ribose.java b/src/com/characterforming/ribose/Ribose.java index cffc6b9..87356b2 100755 --- a/src/com/characterforming/ribose/Ribose.java +++ b/src/com/characterforming/ribose/Ribose.java @@ -323,7 +323,7 @@ private static boolean execDecompile(final String[] args) { } catch (ModelException e) { final String format = "Failed to decompile %1$s"; rteLogger.log(Level.SEVERE, e, () -> String.format(format, - transducerName)); + transducerName)); } finally { Base.endLogging(); } diff --git a/src/com/characterforming/ribose/base/BaseParameterizedEffector.java b/src/com/characterforming/ribose/base/BaseParameterizedEffector.java index 1f27e33..afacf7a 100644 --- a/src/com/characterforming/ribose/base/BaseParameterizedEffector.java +++ b/src/com/characterforming/ribose/base/BaseParameterizedEffector.java @@ -101,7 +101,6 @@ public final void setParameters(IParameterizedEffector proxyEffector) { assert proxyEffector instanceof BaseParameterizedEffector; if (proxyEffector instanceof BaseParameterizedEffector proxy) { this.parameters = (P[])proxy.parameters; - this.tokens = proxy.tokens; } }