sclass ProbabilisticMachine { transient TreeSetWithDuplicates doneStates = new(byProbability()); transient TreeSetWithDuplicates states = new(byProbability()); transient TreeSetWithDuplicates steppableStates = new(byProbability()); transient TreeSetWithDuplicates droppedStates = new(byProbability()); transient int stateCount; double cutoffPercentage = 50; Comparator byProbability() { ret (a, b) -> cmp(b.probability, a.probability); } class State { int number = ++stateCount; State prev; double probability = 100; BasicLogicRule remainingRule; toString { ret toStringWithFields(this, "number", "probability") + stringIf(done(), " (done)"; } bool done() { ret remainingRule == null; } O action() { ret remainingRule == null ? null : remainingRule.lhs; } abstract void runAction(O action); void step { if (!done()) runAction(action()); } State prepareClone() { new State s; copyFields(this, s, 'probability, 'matches); s.prev = this; s.remainingRule = optCast BasicLogicRule(remainingRule.rhs); ret s; } } void addState(State s) { if (s.probability < cutoffPercentage) ret with droppedStates.add(s); addToCollections(s, states, steppableStates); if (s.done()) doneStates.add(s); } bool stepFirstUnstepped() { State s = popFirst(steppableStates), ret false if null; ret true with s.step(); } void reset { clearAll(doneStates, states, steppableStates, droppedStates); stateCount = 0; } void think { while ping (stepFirstUnstepped()) {} } L bestStates(int n) { ret takeFirst(n, doneStates); } }