progress for sure

This commit is contained in:
rusefillc 2022-11-14 21:17:52 -05:00
parent 9d8fdc97a1
commit 75b1305476
3 changed files with 137 additions and 4 deletions

View File

@ -0,0 +1,77 @@
package com.rusefi.can.analysis;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class CounterAggregator {
static List<CounterWithWidth> scan(LinkedHashMap<CounterScanner.BitStateKey, Integer> counters) {
ScanState state = new ScanState();
for (Map.Entry<CounterScanner.BitStateKey, Integer> e : counters.entrySet()) {
CounterScanner.BitStateKey currentState = e.getKey();
int counterSize = e.getValue();
// System.out.println("currentState " + currentState + " counterSize " + counterSize);
if (state.prev == null) {
if (counterSize == 1) {
state.prev = state.start = currentState;
state.totalNumberOfBits = 1;
}
} else {
if ((state.prev.getSid() != currentState.getSid() ||
state.prev.getByteIndex() != currentState.getByteIndex() ||
state.prev.getBitIndex() + 1 != currentState.getBitIndex()
)) {
state.wrap();
} else {
state.totalNumberOfBits++;
state.prev = currentState;
}
}
}
state.wrap();
return state.counters;
}
static class ScanState {
CounterScanner.BitStateKey prev = null;
List<CounterWithWidth> counters = new ArrayList<>();
StringBuffer sb = new StringBuffer();
CounterScanner.BitStateKey start;
int totalNumberOfBits;
void wrap() {
if (prev != null) {
counters.add(new CounterWithWidth(start, totalNumberOfBits));
prev = null;
}
}
}
static class CounterWithWidth {
public final CounterScanner.BitStateKey start;
private final int totalNumberOfBits;
public CounterWithWidth(CounterScanner.BitStateKey start, int totalNumberOfBits) {
this.start = start;
this.totalNumberOfBits = totalNumberOfBits;
}
@Override
public String toString() {
return "Counter{" +
"start at " + start +
", totalNumberOfBits " + totalNumberOfBits +
'}';
}
}
}

View File

@ -4,6 +4,8 @@ import com.rusefi.can.CANPacket;
import java.util.*;
import static com.rusefi.can.analysis.ByteRateOfChange.dualSid;
public class CounterScanner {
public static void scanForCounters(List<CANPacket> packets) {
@ -27,14 +29,26 @@ public class CounterScanner {
}
}
LinkedHashMap<BitStateKey, Integer> counters = new LinkedHashMap<>();
for (Map.Entry<BitStateKey, BitState> e : bitStates.entrySet()) {
BitState bitState = e.getValue();
if (bitState.couldBeCounter()) {
BitStateKey key = e.getKey();
System.out.println("Looks like counter " + key + " " + bitState.cycleLength);
counters.put(key, bitState.cycleLength);
}
}
List<CounterAggregator.CounterWithWidth> countersWithWidth = CounterAggregator.scan(counters);
for (CounterAggregator.CounterWithWidth counterWithWidth : countersWithWidth) {
System.out.println("Found " + counterWithWidth);
}
}
static class BitStateKey implements Comparable {
@ -75,10 +89,10 @@ public class CounterScanner {
@Override
public String toString() {
return "BitStateKey{" +
"sid=" + sid +
", byteIndex=" + byteIndex +
", bitIndex=" + bitIndex +
return "{" +
dualSid(sid) +
", byteIndex " + byteIndex +
", bitIndex " + bitIndex +
'}';
}

View File

@ -2,6 +2,9 @@ package com.rusefi.can.analysis;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.List;
import static org.junit.Assert.*;
public class CounterScannerTest {
@ -52,4 +55,43 @@ public class CounterScannerTest {
assertTrue(state.couldBeCounter());
assertEquals(2, state.cycleLength);
}
@Test
public void testAggregateCounterCandidatesPlain() {
LinkedHashMap<CounterScanner.BitStateKey, Integer> counters = new LinkedHashMap<>();
counters.put(new CounterScanner.BitStateKey(1, 3, 6), 4);
counters.put(new CounterScanner.BitStateKey(0, 7, 6), 1);
List<CounterAggregator.CounterWithWidth> countersWithWidth = CounterAggregator.scan(counters);
assertEquals(1, countersWithWidth.size());
assertEquals(7, countersWithWidth.get(0).start.getByteIndex());
System.out.println(countersWithWidth);
}
@Test
public void testAggregateCounterCandidates() {
LinkedHashMap<CounterScanner.BitStateKey, Integer> counters = new LinkedHashMap<>();
counters.put(new CounterScanner.BitStateKey(0, 7, 6), 1);
counters.put(new CounterScanner.BitStateKey(1, 3, 6), 4);
counters.put(new CounterScanner.BitStateKey(0, 3, 4), 1);
counters.put(new CounterScanner.BitStateKey(0, 3, 5), 2);
counters.put(new CounterScanner.BitStateKey(0, 3, 6), 4);
counters.put(new CounterScanner.BitStateKey(0, 5, 6), 4);
List<CounterAggregator.CounterWithWidth> countersWithWidth = CounterAggregator.scan(counters);
assertEquals(2, countersWithWidth.size());
System.out.println(countersWithWidth);
}
}