Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: removed level of indirection for SubRuleContext. #173

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/software/amazon/event/ruler/ACFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static void tryStep(final ACTask task, final SubRuleContext.Generator su
}
}

private static void tryMustNotExistMatch(final Set<Double> candidateSubRuleIds, final NameState nameState,
private static void tryMustNotExistMatch(final Set<SubRuleContext> candidateSubRuleIds, final NameState nameState,
final ACTask task, int nextKeyIndex, final ArrayMembership arrayMembership,
final SubRuleContext.Generator subRuleContextGenerator) {
if (!nameState.hasKeyTransitions()) {
Expand All @@ -94,7 +94,7 @@ private static void tryMustNotExistMatch(final Set<Double> candidateSubRuleIds,
}

// Move from a state. Give all the remaining event fields a chance to transition from it.
private static void moveFrom(final Set<Double> candidateSubRuleIdsForNextStep, final NameState nameState,
private static void moveFrom(final Set<SubRuleContext> candidateSubRuleIdsForNextStep, final NameState nameState,
int fieldIndex, final ACTask task, final ArrayMembership arrayMembership,
final SubRuleContext.Generator subRuleContextGenerator) {
/*
Expand All @@ -120,12 +120,12 @@ private static void moveFrom(final Set<Double> candidateSubRuleIdsForNextStep, f
}
}

private static void moveFromWithPriorCandidates(final Set<Double> candidateSubRuleIds,
private static void moveFromWithPriorCandidates(final Set<SubRuleContext> candidateSubRuleIds,
final NameState fromState, final Patterns fromPattern,
final int fieldIndex, final ACTask task,
final ArrayMembership arrayMembership,
final SubRuleContext.Generator subRuleContextGenerator) {
Set<Double> candidateSubRuleIdsForNextStep = calculateCandidateSubRuleIdsForNextStep(candidateSubRuleIds,
Set<SubRuleContext> candidateSubRuleIdsForNextStep = calculateCandidateSubRuleIdsForNextStep(candidateSubRuleIds,
fromState, fromPattern);

// If there are no more candidate sub-rules, there is no need to proceed further.
Expand All @@ -145,12 +145,12 @@ private static void moveFromWithPriorCandidates(final Set<Double> candidateSubRu
* @return The set of candidate sub-rule IDs for the next step. Null means there are no candidates and thus, there
* is no point to evaluating subsequent steps.
*/
private static Set<Double> calculateCandidateSubRuleIdsForNextStep(final Set<Double> currentCandidateSubRuleIds,
private static Set<SubRuleContext> calculateCandidateSubRuleIdsForNextStep(final Set<SubRuleContext> currentCandidateSubRuleIds,
final NameState fromState,
final Patterns fromPattern) {
// These are all the sub-rules that use the matched pattern to transition to the next NameState. Note that they
// are not all candidates as they may have required different values for previously evaluated fields.
Set<Double> subRuleIds = fromState.getNonTerminalSubRuleIdsForPattern(fromPattern);
Set<SubRuleContext> subRuleIds = fromState.getNonTerminalSubRuleIdsForPattern(fromPattern);

// If no sub-rules used the matched pattern to transition to the next NameState, then there are no matches to be
// found by going further.
Expand All @@ -166,12 +166,12 @@ private static Set<Double> calculateCandidateSubRuleIdsForNextStep(final Set<Dou

// There are candidate sub-rules, so retain only those that used the matched pattern to transition to the next
// NameState.
Set<Double> candidateSubRuleIdsForNextStep = new HashSet<>();
Set<SubRuleContext> candidateSubRuleIdsForNextStep = new HashSet<>();
intersection(subRuleIds, currentCandidateSubRuleIds, candidateSubRuleIdsForNextStep);
return candidateSubRuleIdsForNextStep;
}

private static void addNameState(Set<Double> candidateSubRuleIds, NameState nameState, Patterns pattern,
private static void addNameState(Set<SubRuleContext> candidateSubRuleIds, NameState nameState, Patterns pattern,
ACTask task, int nextKeyIndex, final ArrayMembership arrayMembership,
final SubRuleContext.Generator subRuleContextGenerator) {
// one of the matches might imply a rule match
Expand Down
4 changes: 2 additions & 2 deletions src/main/software/amazon/event/ruler/ACStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class ACStep {
final int fieldIndex;
final NameState nameState;
final Set<Double> candidateSubRuleIds;
final Set<SubRuleContext> candidateSubRuleIds;
final ArrayMembership membershipSoFar;

ACStep(final int fieldIndex, final NameState nameState, final Set<Double> candidateSubRuleIds,
ACStep(final int fieldIndex, final NameState nameState, final Set<SubRuleContext> candidateSubRuleIds,
final ArrayMembership arrayMembership) {
this.fieldIndex = fieldIndex;
this.nameState = nameState;
Expand Down
12 changes: 6 additions & 6 deletions src/main/software/amazon/event/ruler/ACTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ACStep nextStep() {
/*
* Add a step to the queue for later consideration
*/
void addStep(final int fieldIndex, final NameState nameState, final Set<Double> candidateSubRuleIds,
void addStep(final int fieldIndex, final NameState nameState, final Set<SubRuleContext> candidateSubRuleIds,
final ArrayMembership membershipSoFar) {
stepQueue.add(new ACStep(fieldIndex, nameState, candidateSubRuleIds, membershipSoFar));
}
Expand All @@ -57,21 +57,21 @@ List<Object> getMatchedRules() {
return new ArrayList<>(matchingRules);
}

void collectRules(final Set<Double> candidateSubRuleIds, final NameState nameState, final Patterns pattern,
void collectRules(final Set<SubRuleContext> candidateSubRuleIds, final NameState nameState, final Patterns pattern,
final SubRuleContext.Generator subRuleContextGenerator) {
Set<Double> terminalSubRuleIds = nameState.getTerminalSubRuleIdsForPattern(pattern);
Set<SubRuleContext> terminalSubRuleIds = nameState.getTerminalSubRuleIdsForPattern(pattern);
if (terminalSubRuleIds == null) {
return;
}

// If no candidates, that means we're on the first step, so all sub-rules are candidates.
if (candidateSubRuleIds == null || candidateSubRuleIds.isEmpty()) {
for (Double terminalSubRuleId : terminalSubRuleIds) {
matchingRules.add(subRuleContextGenerator.getNameForGeneratedId(terminalSubRuleId));
for (SubRuleContext terminalSubRuleId : terminalSubRuleIds) {
matchingRules.add(terminalSubRuleId.getRuleName());
}
} else {
intersection(candidateSubRuleIds, terminalSubRuleIds, matchingRules,
id -> subRuleContextGenerator.getNameForGeneratedId(id));
SubRuleContext::getRuleName);
}
}
}
16 changes: 8 additions & 8 deletions src/main/software/amazon/event/ruler/Finder.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static List<Object> find(final Task task, final SubRuleContext.Generator
}

// Move from a state. Give all the remaining tokens a chance to transition from it
private static void moveFrom(final Set<Double> candidateSubRuleIdsForNextStep, final NameState nameState,
private static void moveFrom(final Set<SubRuleContext> candidateSubRuleIdsForNextStep, final NameState nameState,
final int tokenIndex, final Task task,
final SubRuleContext.Generator subRuleContextGenerator) {
/*
Expand Down Expand Up @@ -111,11 +111,11 @@ private static void moveFrom(final Set<Double> candidateSubRuleIdsForNextStep, f
}
}

private static void moveFromWithPriorCandidates(final Set<Double> candidateSubRuleIds,
private static void moveFromWithPriorCandidates(final Set<SubRuleContext> candidateSubRuleIds,
final NameState fromState, final Patterns fromPattern,
final int tokenIndex, final Task task,
final SubRuleContext.Generator subRuleContextGenerator) {
Set<Double> candidateSubRuleIdsForNextStep = calculateCandidateSubRuleIdsForNextStep(candidateSubRuleIds,
Set<SubRuleContext> candidateSubRuleIdsForNextStep = calculateCandidateSubRuleIdsForNextStep(candidateSubRuleIds,
fromState, fromPattern);

// If there are no more candidate sub-rules, there is no need to proceed further.
Expand All @@ -135,12 +135,12 @@ private static void moveFromWithPriorCandidates(final Set<Double> candidateSubRu
* @return The set of candidate sub-rule IDs for the next step. Null means there are no candidates and thus, there
* is no point to evaluating subsequent steps.
*/
private static Set<Double> calculateCandidateSubRuleIdsForNextStep(final Set<Double> currentCandidateSubRuleIds,
private static Set<SubRuleContext> calculateCandidateSubRuleIdsForNextStep(final Set<SubRuleContext> currentCandidateSubRuleIds,
final NameState fromState,
final Patterns fromPattern) {
// These are all the sub-rules that use the matched pattern to transition to the next NameState. Note that they
// are not all candidates as they may have required different values for previously evaluated fields.
Set<Double> subRuleIds = fromState.getNonTerminalSubRuleIdsForPattern(fromPattern);
Set<SubRuleContext> subRuleIds = fromState.getNonTerminalSubRuleIdsForPattern(fromPattern);

// If no sub-rules used the matched pattern to transition to the next NameState, then there are no matches to be
// found by going further.
Expand All @@ -156,7 +156,7 @@ private static Set<Double> calculateCandidateSubRuleIdsForNextStep(final Set<Dou

// There are candidate sub-rules, so retain only those that used the matched pattern to transition to the next
// NameState.
Set<Double> candidateSubRuleIdsForNextStep = new HashSet<>();
Set<SubRuleContext> candidateSubRuleIdsForNextStep = new HashSet<>();
intersection(subRuleIds, currentCandidateSubRuleIds, candidateSubRuleIdsForNextStep);
return candidateSubRuleIdsForNextStep;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ private static void tryValueMatching(final Task task, final Step step,
}
}

private static void tryNameMatching(final Set<Double> candidateSubRuleIds, final NameState nameState,
private static void tryNameMatching(final Set<SubRuleContext> candidateSubRuleIds, final NameState nameState,
final Task task, final int keyIndex,
final SubRuleContext.Generator subRuleContextGenerator) {
if (!nameState.hasKeyTransitions()) {
Expand All @@ -208,7 +208,7 @@ private static void tryNameMatching(final Set<Double> candidateSubRuleIds, final
}
}

private static void addNameState(Set<Double> candidateSubRuleIds, NameState nameState, Patterns pattern, Task task,
private static void addNameState(Set<SubRuleContext> candidateSubRuleIds, NameState nameState, Patterns pattern, Task task,
int nextKeyIndex, final SubRuleContext.Generator subRuleContextGenerator) {
// one of the matches might imply a rule match
task.collectRules(candidateSubRuleIds, nameState, pattern, subRuleContextGenerator);
Expand Down
36 changes: 18 additions & 18 deletions src/main/software/amazon/event/ruler/GenericMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void deletePatternRule(final T name, final Map<String, List<Patterns>> na
Collections.sort(keys);
synchronized(this) {
final List<String> deletedKeys = new ArrayList<>();
final Set<Double> candidateSubRuleIds = new HashSet<>();
final Set<SubRuleContext> candidateSubRuleIds = new HashSet<>();
deleteStep(getStartState(), keys, 0, namevals, name, deletedKeys, candidateSubRuleIds);
// check and delete the key from filedUsed ...
checkAndDeleteUsedFields(deletedKeys);
Expand All @@ -272,15 +272,15 @@ public void deleteRule(final T name, final Map<String, List<String>> namevals) {
deletePatternRule(name, patternMap);
}

private Set<Double> deleteStep(final NameState state,
private Set<SubRuleContext> deleteStep(final NameState state,
final List<String> keys,
final int keyIndex,
final Map<String, List<Patterns>> patterns,
final T ruleName,
final List<String> deletedKeys,
final Set<Double> candidateSubRuleIds) {
final Set<SubRuleContext> candidateSubRuleIds) {

final Set<Double> deletedSubRuleIds = new HashSet<>();
final Set<SubRuleContext> deletedSubRuleIds = new HashSet<>();
final String key = keys.get(keyIndex);
ByteMachine byteMachine = state.getTransitionOn(key);
NameMatcher<NameState> nameMatcher = state.getKeyTransitionOn(key);
Expand Down Expand Up @@ -309,7 +309,7 @@ private Set<Double> deleteStep(final NameState state,
boolean isTerminal = nextKeyIndex == keys.size();

// Trim the candidate sub-rule ID set to contain only the sub-rule IDs present in the next NameState.
Set<Double> nextNameStateSubRuleIds = isTerminal ?
Set<SubRuleContext> nextNameStateSubRuleIds = isTerminal ?
nextNameState.getTerminalSubRuleIdsForPattern(pattern) :
nextNameState.getNonTerminalSubRuleIdsForPattern(pattern);
// If no sub-rule IDs are found for next NameState, then we have no candidates, and will return below
Expand All @@ -319,9 +319,9 @@ private Set<Double> deleteStep(final NameState state,
// If candidate set is empty, we are at first NameState, so initialize to next NameState's sub-rule IDs.
// When initializing, ensure that sub-rule IDs match the provided rule name for deletion.
} else if (candidateSubRuleIds.isEmpty()) {
for (Double nextNameStateSubRuleId : nextNameStateSubRuleIds) {
for (SubRuleContext nextNameStateSubRuleId : nextNameStateSubRuleIds) {
if (Objects.equals(ruleName,
subRuleContextGenerator.getNameForGeneratedId(nextNameStateSubRuleId))) {
nextNameStateSubRuleId.getRuleName())) {
candidateSubRuleIds.add(nextNameStateSubRuleId);
}
}
Expand All @@ -331,9 +331,9 @@ private Set<Double> deleteStep(final NameState state,
}

if (isTerminal) {
for (Double candidateSubRuleId : candidateSubRuleIds) {
for (SubRuleContext candidateSubRuleId : candidateSubRuleIds) {
if (nextNameState.deleteSubRule(
subRuleContextGenerator.getNameForGeneratedId(candidateSubRuleId), candidateSubRuleId,
candidateSubRuleId.getRuleName(), candidateSubRuleId,
pattern, true)) {
deletedSubRuleIds.add(candidateSubRuleId);
// Only delete the pattern if the pattern does not transition to the next NameState.
Expand All @@ -351,8 +351,8 @@ private Set<Double> deleteStep(final NameState state,
deletedSubRuleIds.addAll(deleteStep(nextNameState, keys, nextKeyIndex, patterns, ruleName,
deletedKeys, new HashSet<>(candidateSubRuleIds)));

for (double deletedSubRuleId : deletedSubRuleIds) {
nextNameState.deleteSubRule(subRuleContextGenerator.getNameForGeneratedId(deletedSubRuleId),
for (SubRuleContext deletedSubRuleId : deletedSubRuleIds) {
nextNameState.deleteSubRule(deletedSubRuleId.getRuleName(),
deletedSubRuleId, pattern, false);
}

Expand Down Expand Up @@ -519,7 +519,7 @@ private void addStep(final List<String> keys,
boolean isTerminal = i + 1 == keys.size();
for (Patterns pattern : patterns.get(keys.get(i))) {
for (NameState nameState : nameStates[i]) {
nameState.addSubRule(ruleName, context.getId(), pattern, isTerminal);
nameState.addSubRule(ruleName, context, pattern, isTerminal);
}
}
}
Expand All @@ -541,7 +541,7 @@ private void addStep(final List<String> keys,
* keyIndex and greater. If keyIndex==0 and the returned set is empty, then the keys and patterns being
* added represent a new sub-rule.
*/
private Set<Double> addStep(final NameState state,
private Set<SubRuleContext> addStep(final NameState state,
final List<String> keys,
final int keyIndex,
final Map<String, List<Patterns>> patterns,
Expand Down Expand Up @@ -595,8 +595,8 @@ private Set<Double> addStep(final NameState state,
// rule+pattern for the given key are returned up the recursion stack as our "candidates". At each level of the
// stack, we remove any candidates if they did not have the same rule+pattern for that level's key. If our
// candidate set becomes empty, we know we are adding a new rule.
Set<Double> candidateSubRuleIds = null;
Set<Double> candidateSubRuleIdsForThisKey = null;
Set<SubRuleContext> candidateSubRuleIds = null;
Set<SubRuleContext> candidateSubRuleIdsForThisKey = null;
final int nextKeyIndex = keyIndex + 1;
boolean isTerminal = nextKeyIndex == keys.size();
for (NameState nameState : nameStates) {
Expand All @@ -606,8 +606,8 @@ private Set<Double> addStep(final NameState state,
if (isTerminal) {
candidateSubRuleIds = new HashSet<>();
for (Patterns pattern : patterns.get(key)) {
Set<Double> subRuleIdsForPattern = nameState.getTerminalSubRuleIdsForPattern(pattern);
Set<Double> subRuleIdsForName = subRuleContextGenerator.getIdsGeneratedForName(ruleName);
Set<SubRuleContext> subRuleIdsForPattern = nameState.getTerminalSubRuleIdsForPattern(pattern);
Set<SubRuleContext> subRuleIdsForName = subRuleContextGenerator.getIdsGeneratedForName(ruleName);
if (subRuleIdsForPattern != null && subRuleIdsForName != null) {
intersection(subRuleIdsForPattern, subRuleIdsForName, candidateSubRuleIds);
}
Expand All @@ -632,7 +632,7 @@ private Set<Double> addStep(final NameState state,

candidateSubRuleIdsForThisKey = new HashSet<>();
for (Patterns pattern : patternsForThisKey) {
Set<Double> nonTerminalSubRuleIds = nameState.getNonTerminalSubRuleIdsForPattern(pattern);
Set<SubRuleContext> nonTerminalSubRuleIds = nameState.getNonTerminalSubRuleIdsForPattern(pattern);
if (nonTerminalSubRuleIds != null) {
candidateSubRuleIdsForThisKey.addAll(nonTerminalSubRuleIds);
}
Expand Down
Loading