Skip to content

Commit

Permalink
Clean up + fix build
Browse files Browse the repository at this point in the history
Previous builds failed because the TestTarget effectors used rtxSignal()
to raise a signal not defined in the test model and not checked in the
runtime.

Changes to fix this:
- dump log file if ribose run|compile fail
- dump compile and run logs at end of ci builds
- move IEffector.rtxSignal(int) to IOutput.signal(int)
- implement as Transductor.signal(int)
- update IEffector implementations to use IOutput.signal()
- fix up javadoc comments referring to IEffector.rtxSignal()
- clean up ValuesTest.inr

Changes from reverted commits (included in this commit):
- drop IField.decodeValue()
  - use IField.asString() (equivalent to IField.toString())
- drop Model.writeBytes(Bytes)
  - use Model.writeBytes(Bytes.bytes())
- Transductor loops on matchMode
- add .data to .gitignore

Signed-off-by: jrte <jrte.project@gmail.com>
  • Loading branch information
jrte committed Sep 22, 2023
1 parent f74748b commit 5cb880c
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java_pid*
*.dfa
*.pr
*.last
.data/
.tmp/
.settings/
.vscode/
Expand Down
7 changes: 6 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@
description="Clean build for java and patterns"/>

<target name="ci-test" depends="clean,ribose,package-test,compile-test-model,test,test-output-equivalence,env"
description="Full build with CI tests for java and patterns"/>
description="Full build with CI tests for java and patterns">
<echo message="Ribose compiler log ..."/>
<concat> <fileset dir="." includes="ribose-compile-0.log"/></concat>
<echo message="Ribose runtime log ..."/>
<concat> <fileset dir="." includes="ribose-runtime-0.log"/></concat>
</target>

<target name="check-compile-java" depends="init">
<depend srcdir="${java.source.dir}" destdir="${build.java.classes}" closure="yes">
Expand Down
20 changes: 6 additions & 14 deletions patterns/test/ValuesTest.inr
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
fail = nul;
pass = nil;
ValuesTest = (
(nil, clear)
(
(dash, paste)? (digit, paste)*
(
(digit, paste)+ (space, integer paste out clear)
| (dot, paste) (digit, paste)* (space, real paste out clear)
| ((black - {digit,dash,dot,query}), paste) (black, paste) (space, string paste out clear)
| (query, signal[`!pass`])
(digit, paste)+ (space|nl, integer paste out clear)
| (dot, paste) (digit, paste)* (space|nl, real paste out clear)
| ((black - {digit,dash,dot}), paste) (black, paste) (space|nl, string paste out clear)
)
(pass, out[` ` `!!pass`])?
((pass, out[`!!pass; `]) | (fail, out[`!!fail; `]))
)*
(dash, paste)? (digit, paste)*
(
(digit, paste)+ (nl|eos, integer out in[nl])
| (dot, paste) (digit, paste)* (nl|eos, real out in[nl])
| ((black - {digit,dash,dot,query}), paste) (black, paste) (nl|eos, string out in[nl])
| (query, signal[`!pass`])
)
(pass, out[` ` `!!pass`])?
(nl, out[nl])
):dfamin;

ValuesTest$(0,1):prsseq `build/automata/ValuesTest.pr`;
Expand Down
10 changes: 10 additions & 0 deletions ribose
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ execRun() {
shift
fi
$JAVA -ea -cp $path $vmargs com.characterforming.ribose.Ribose run $nil "$@"
rc=$?
if ((rc!=0)); then
echo "Run failed with exit code $rc"; cat ./ribose-runtime-0.log
fi
return $rc
}

execCompile() {
Expand All @@ -20,6 +25,11 @@ execCompile() {
else
$JAVA -ea -cp $path $vmargs com.characterforming.ribose.Ribose compile --target com.characterforming.jrte.engine.SimpleTarget "$@"
fi
rc=$?
if ((rc!=0)); then
echo "Compile failed with exit code $rc"; cat ./ribose-compile-0.log
fi
return $rc
}

execDecompile() {
Expand Down
39 changes: 17 additions & 22 deletions src/com/characterforming/jrte/engine/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package com.characterforming.jrte.engine;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.util.Arrays;

import com.characterforming.ribose.IField;
Expand Down Expand Up @@ -81,23 +80,19 @@ public byte[] copyValue() {
return Arrays.copyOf(this.data, this.length);
}

@Override // @see com.characterforming.ribose.IField#decodeValue()
public char[] decodeValue() {
char[] chars = null;
ByteBuffer in = ByteBuffer.wrap(this.data, 0, this.getLength());
CharBuffer out = CharBuffer.allocate(this.getLength());
CoderResult result = this.decoder.reset().decode(in, out, true);
assert !result.isOverflow() && !result.isError();
if (!result.isOverflow() && !result.isError()) {
chars = new char[out.flip().length()];
out.get(chars);
}
return chars;
}

@Override // @see com.characterforming.ribose.IField#asString()
public String asString() {
return new String(this.decodeValue());
try {
return this.decoder.reset().decode(
ByteBuffer.wrap(this.data, 0, this.length)
).toString();
} catch (CharacterCodingException e) {
char[] chars = new char[this.length];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char)(0xff & this.data[i]);
}
return new String(chars);
}
}

@Override // @see com.characterforming.ribose.IField#asInteger()
Expand Down Expand Up @@ -137,6 +132,11 @@ public double asReal() {
return fraction * value;
}

@Override // @see java.lang.Object#toString()
public String toString() {
return this.asString();
}

void clear() {
this.length = 0;
}
Expand Down Expand Up @@ -173,9 +173,4 @@ private void growValue(int size) {
this.data = v;
}
}

@Override // @see java.lang.Object#toString()
public String toString() {
return Bytes.decode(this.decoder, this.data, this.length).toString();
}
}
13 changes: 3 additions & 10 deletions src/com/characterforming/jrte/engine/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ protected int addSignal(Bytes signalName) {
}

protected int addTransducer(Bytes transducerName) {
Integer ordinal = this.transducerOrdinalMap.get(transducerName);
Integer ordinal = this.transducerOrdinalMap.computeIfAbsent(transducerName, absent -> null);
assert ordinal == null || transducerName.equals(this.transducerNameIndex[ordinal]);
if (ordinal == null) {
ordinal = this.transducerOrdinalMap.size();
Expand Down Expand Up @@ -561,16 +561,9 @@ protected void writeBytes(final byte[] bytes) throws ModelException {
}
}

protected void writeBytes(final Bytes bytes) throws ModelException {
if (bytes != null) {
this.writeBytes(bytes.bytes());
}
}

protected void writeBytes(final ByteBuffer byteBuffer) throws ModelException {
byte[] bytes = null;
if (byteBuffer != null) {
bytes = new byte[byteBuffer.limit() - byteBuffer.position()];
byte[] bytes = new byte[byteBuffer.limit() - byteBuffer.position()];
byteBuffer.get(bytes, byteBuffer.position(), byteBuffer.limit());
this.writeBytes(bytes);
}
Expand Down Expand Up @@ -657,7 +650,7 @@ protected void writeIntArray(final int[] ints) throws ModelException {
}

protected void writeString(final String s) throws ModelException {
this.writeBytes(Bytes.encode(this.getEncoder(), s));
this.writeBytes(Bytes.encode(this.getEncoder(), s).bytes());
}

protected void writeTransitionMatrix(final int[][][] matrix) throws ModelException {
Expand Down
2 changes: 1 addition & 1 deletion src/com/characterforming/jrte/engine/ModelCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private boolean save(byte[][][][] compiledParameters) {
this.writeOrdinalMap(transducerOrdinalMap, 0);
for (int index = 0; index < transducerCount; index++) {
if (this.transducerOffsetIndex[index] > 0) {
this.writeBytes(this.transducerNameIndex[index]);
this.writeBytes(this.transducerNameIndex[index].bytes());
this.writeLong(this.transducerOffsetIndex[index]);
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/com/characterforming/jrte/engine/TransducerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
package com.characterforming.jrte.engine;

final class TransducerState {
final int[] countdown;
int countdown;
int signal;
int state;
int[] inputFilter;
long[] transitionMatrix;
Expand All @@ -30,28 +31,28 @@ final class TransducerState {
String name;

TransducerState() {
this.countdown = new int[] {0, 0};
this.reset();
this.name = null;
this.inputFilter = null;
this.transitionMatrix = null;
this.effectorVector = null;
this.inputEquivalents = 0;
}

TransducerState transducer(Transducer transducer) {
this.state = 0;
this.reset();
this.name = transducer.getName();
this.inputFilter = transducer.getInputFilter();
this.transitionMatrix = transducer.getTransitionMatrix();
this.effectorVector = transducer.getEffectorVector();
this.inputEquivalents = transducer.getInputEquivalentsCount();
this.countdown[0] = this.countdown[1] = 0;
return this;
}

void reset() {
this.state = 0;
this.name = null;
this.inputFilter = null;
this.transitionMatrix = null;
this.effectorVector = null;
this.inputEquivalents = 0;
this.countdown[0] = this.countdown[1] = 0;
this.countdown = 0;
this.signal = -1;
}

@Override
Expand Down
37 changes: 24 additions & 13 deletions src/com/characterforming/jrte/engine/Transductor.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public ITransductor run() throws EffectorException, DomainErrorException {
}

// absorb self-referencing (msum,mscan) or sequential (mproduct) transitions with nil effect
if (this.matchMode != MATCH_NONE && token < SIGNUL) {
while (this.matchMode != MATCH_NONE && token < SIGNUL) {
switch (this.matchMode) {
case MATCH_SUM:
token = sumTrap(input, token);
Expand Down Expand Up @@ -389,7 +389,7 @@ public ITransductor run() throws EffectorException, DomainErrorException {
}
if (0 != (aftereffects & IEffector.RTX_SIGNAL)) {
signal = aftereffects >>> 16;
if ((signal < SIGNUL) || (signal >= this.signalLimit)) {
if ((signal < SIGNUL) || (signal >= this.transducer.inputFilter.length)) {
assert false : String.format("Signal %1$d out of range [256..%2$d)", signal, this.signalLimit);
signal = SIGNUL;
}
Expand Down Expand Up @@ -476,6 +476,15 @@ public IField getField(final Bytes fieldName) {
return this.getField(this.getFieldOrdinal(fieldName));
}

@Override // @see com.characterforming.ribose.IOutput#signal(int)
public int signal(int signalOrdinal) throws EffectorException {
if (256 > signalOrdinal || signalOrdinal >= this.model.getSignalLimit()) {
throw new EffectorException(String.format("Signal ordinal %1$d is out of range [256,%2$d)",
signalOrdinal, this.transducerStack.peek().inputFilter.length));
}
return (signalOrdinal << 16) | IEffector.RTX_SIGNAL;
}

void setEffectors(IEffector<?>[] effectors) {
this.effectors = effectors;
}
Expand Down Expand Up @@ -530,7 +539,7 @@ private int effect(int action, int token, int[] effectorVector)
if ((token != SIGNUL && token != SIGEOS)) {
++this.metrics.errors;
this.errorInput = token;
aftereffects |= IEffector.rtxSignal(SIGNUL);
aftereffects |= this.signal(Signal.NUL.signal());
} else {
aftereffects |= IEffector.RTX_STOPPED;
}
Expand All @@ -555,9 +564,9 @@ private int effect(int action, int token, int[] effectorVector)
this.selected.clear();
break;
case COUNT:
if (--this.transducer.countdown[0] <= 0) {
this.transducer.countdown[0] = 0;
aftereffects |= IEffector.rtxSignal(this.transducer.countdown[1]);
if (--this.transducer.countdown <= 0) {
this.transducer.countdown = 0;
aftereffects |= this.signal(this.transducer.signal);
}
break;
case IN:
Expand Down Expand Up @@ -852,12 +861,12 @@ private SignalEffector(final Transductor transductor) {

@Override
public int invoke() throws EffectorException {
return IEffector.rtxSignal(Signal.NIL.signal());
return signal(Signal.NIL.signal());
}

@Override
public int invoke(final int parameterIndex) throws EffectorException {
return IEffector.rtxSignal(super.getParameter(parameterIndex));
return signal(super.getParameter(parameterIndex));
}

@Override // @see com.characterforming.ribose.IParameterizedEffector#allocateParameters(int)
Expand Down Expand Up @@ -970,12 +979,14 @@ public int[][] allocateParameters(int parameterCount) {
@Override
public int invoke(final int parameterIndex) throws EffectorException {
assert (transducer == transducerStack.peek()) || (transducer == transducerStack.get(transducerStack.tos()-1));
System.arraycopy(super.getParameter(parameterIndex), 0, transducer.countdown, 0, 2);
if (transducer.countdown[0] < 0) {
IField field = Transductor.this.getField(-1 - transducer.countdown[0]);
transducer.countdown[0] = (field != null) ? (int)field.asInteger() : -1;
int[] parameter = super.getParameter(parameterIndex);
transducer.countdown = parameter[0];
transducer.signal = parameter[1];
if (transducer.countdown < 0) {
IField field = Transductor.this.getField(-1 - transducer.countdown);
transducer.countdown = (field != null) ? (int)field.asInteger() : -1;
}
return transducer.countdown[0] <= 0 ? IEffector.rtxSignal(transducer.countdown[1]) : IEffector.RTX_NONE;
return transducer.countdown <= 0 ? signal(transducer.signal) : IEffector.RTX_NONE;
}

@Override
Expand Down
15 changes: 9 additions & 6 deletions src/com/characterforming/jrte/test/TestTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.characterforming.ribose.ITarget;
import com.characterforming.ribose.base.BaseEffector;
import com.characterforming.ribose.base.EffectorException;
import com.characterforming.ribose.base.Signal;
import com.characterforming.ribose.base.TargetBindingException;

public class TestTarget implements ITarget {
private static final int fail = 266;
private static final int fail = Signal.NUL.signal();
private static final int pass = Signal.NIL.signal();

/**
* Constructor
*/
Expand Down Expand Up @@ -43,9 +46,9 @@ public int invoke() throws EffectorException {
try {
integer = Long.parseLong(string);
} catch (NumberFormatException e) {
return IEffector.rtxSignal(fail);
return super.output.signal(fail);
}
return integer == field.asInteger() ? IEffector.RTX_NONE : IEffector.rtxSignal(fail);
return super.output.signal(integer == field.asInteger() ? pass : fail);
}
}

Expand All @@ -62,9 +65,9 @@ public int invoke() throws EffectorException {
try {
real = Double.parseDouble(string);
} catch (NumberFormatException e) {
return IEffector.rtxSignal(fail);
return super.output.signal(fail);
}
return real == field.asReal() ? IEffector.RTX_NONE : IEffector.rtxSignal(fail);
return super.output.signal(real == field.asReal() ? pass : fail);
}
}

Expand All @@ -76,7 +79,7 @@ private class StringValueEffector extends BaseEffector<TestTarget> {
@Override
public int invoke() throws EffectorException {
IField field = super.output.getField();
return field.toString().equals(field.asString()) ? IEffector.RTX_NONE : IEffector.rtxSignal(fail);
return super.output.signal(field.toString().equals(field.asString()) ? pass : fail);
}
}
}
Loading

0 comments on commit 5cb880c

Please sign in to comment.