Skip to content

Commit 6755154

Browse files
committed
Improve logical expression abstraction
1 parent 243a794 commit 6755154

6 files changed

Lines changed: 155 additions & 154 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: "Copilot Setup Steps"
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
paths:
7+
- .github/workflows/copilot-setup-steps.yml
8+
pull_request:
9+
paths:
10+
- .github/workflows/copilot-setup-steps.yml
11+
12+
jobs:
13+
# The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
14+
copilot-setup-steps:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
19+
# Steps that will run before the agent starts
20+
steps:
21+
- name: 'Checkout'
22+
uses: actions/checkout@v2
23+
with:
24+
submodules: true
25+
- name: 'Cache'
26+
uses: actions/cache@v4
27+
with:
28+
path: |
29+
~/.m2
30+
~/.gradle
31+
key: ${{ runner.os }}-gradle-${{ hashFiles('build.gradle') }}
32+
- name: 'Setup Java'
33+
uses: actions/setup-java@v1
34+
with:
35+
java-version: 17
36+
java-package: jdk
37+
- name: 'Build'
38+
run: ./gradlew build --max-workers 1
39+
env:
40+
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
41+
MAVEN_KEY: ${{ secrets.MAVEN_KEY }}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.cyclops.cyclopscore.nbt.path.parse;
2+
3+
import net.minecraft.nbt.ByteTag;
4+
import net.minecraft.nbt.Tag;
5+
import org.cyclops.cyclopscore.nbt.path.INbtPathExpression;
6+
import org.cyclops.cyclopscore.nbt.path.NbtParseException;
7+
import org.cyclops.cyclopscore.nbt.path.NbtPath;
8+
import org.cyclops.cyclopscore.nbt.path.NbtPathExpressionMatches;
9+
10+
import javax.annotation.Nullable;
11+
import java.util.function.Supplier;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
14+
import java.util.stream.Stream;
15+
16+
/**
17+
* @author rubensworks
18+
*/
19+
public abstract class NbtPathExpressionParseHandlerBooleanLogicalAdapter implements INbtPathExpressionParseHandler {
20+
21+
private final Pattern regex;
22+
23+
protected NbtPathExpressionParseHandlerBooleanLogicalAdapter(String relation) {
24+
this.regex = Pattern.compile("^ *" + relation + " *");
25+
}
26+
27+
protected abstract boolean getLogicalValue(boolean left, Supplier<Boolean> right);
28+
29+
@Nullable
30+
@Override
31+
public HandleResult handlePrefixOf(String nbtPathExpression, int pos) {
32+
Matcher matcher = this.regex
33+
.matcher(nbtPathExpression)
34+
.region(pos, nbtPathExpression.length());
35+
if (!matcher.find()) {
36+
return HandleResult.INVALID;
37+
}
38+
39+
// Parse the right-hand side expression
40+
int rightPos = pos + matcher.group().length();
41+
if (rightPos >= nbtPathExpression.length()) {
42+
return HandleResult.INVALID;
43+
}
44+
45+
// Find the end of the right expression
46+
int endPos = NbtPathExpressionHelpers.findExpressionEnd(nbtPathExpression, rightPos);
47+
if (endPos == rightPos) {
48+
return HandleResult.INVALID;
49+
}
50+
51+
String rightExpressionString = nbtPathExpression.substring(rightPos, endPos);
52+
try {
53+
INbtPathExpression rightExpression = NbtPath.parse(rightExpressionString.trim());
54+
return new HandleResult(new Expression(rightExpression, this),
55+
matcher.group().length() + rightExpressionString.length());
56+
} catch (NbtParseException e) {
57+
return HandleResult.INVALID;
58+
}
59+
}
60+
61+
public static class Expression implements INbtPathExpression {
62+
63+
protected final INbtPathExpression expression;
64+
protected final NbtPathExpressionParseHandlerBooleanLogicalAdapter handler;
65+
66+
public Expression(INbtPathExpression expression, NbtPathExpressionParseHandlerBooleanLogicalAdapter handler) {
67+
this.expression = expression;
68+
this.handler = handler;
69+
}
70+
71+
@Override
72+
public NbtPathExpressionMatches matchContexts(Stream<NbtPathExpressionExecutionContext> executionContexts) {
73+
return new NbtPathExpressionMatches(executionContexts
74+
.map(executionContext -> {
75+
Tag currentTag = executionContext.getCurrentTag();
76+
77+
// The left side is the current tag (should be a boolean result from previous expression)
78+
boolean leftValue = NbtPathExpressionHelpers.isTruthy(currentTag);
79+
80+
// Evaluate the right expression against the root context
81+
// This ensures both sides of the expression are evaluated against the same base context
82+
Tag rootTag = executionContext.getRootContext().getCurrentTag();
83+
84+
// AND operation
85+
boolean result = handler.getLogicalValue(leftValue, () -> expression.test(rootTag));
86+
87+
return new NbtPathExpressionExecutionContext(ByteTag.valueOf(result), executionContext);
88+
})
89+
);
90+
}
91+
92+
}
93+
94+
}
Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,18 @@
11
package org.cyclops.cyclopscore.nbt.path.parse;
22

3-
import net.minecraft.nbt.ByteTag;
4-
import net.minecraft.nbt.Tag;
5-
import org.cyclops.cyclopscore.nbt.path.INbtPathExpression;
6-
import org.cyclops.cyclopscore.nbt.path.NbtParseException;
7-
import org.cyclops.cyclopscore.nbt.path.NbtPath;
8-
import org.cyclops.cyclopscore.nbt.path.NbtPathExpressionMatches;
9-
10-
import javax.annotation.Nullable;
11-
import java.util.regex.Matcher;
12-
import java.util.regex.Pattern;
13-
import java.util.stream.Stream;
3+
import java.util.function.Supplier;
144

155
/**
166
* A handler that handles boolean AND expressions in the form of "expression1 {@literal &&} expression2".
177
*/
18-
public class NbtPathExpressionParseHandlerBooleanLogicalAnd implements INbtPathExpressionParseHandler {
19-
20-
private static final Pattern REGEX_EXPRESSION = Pattern.compile("^ *&& *");
21-
22-
@Nullable
23-
@Override
24-
public HandleResult handlePrefixOf(String nbtPathExpression, int pos) {
25-
Matcher matcher = REGEX_EXPRESSION
26-
.matcher(nbtPathExpression)
27-
.region(pos, nbtPathExpression.length());
28-
if (!matcher.find()) {
29-
return HandleResult.INVALID;
30-
}
8+
public class NbtPathExpressionParseHandlerBooleanLogicalAnd extends NbtPathExpressionParseHandlerBooleanLogicalAdapter {
319

32-
// Parse the right-hand side expression
33-
int rightPos = pos + matcher.group().length();
34-
if (rightPos >= nbtPathExpression.length()) {
35-
return HandleResult.INVALID;
36-
}
37-
38-
// Find the end of the right expression
39-
int endPos = NbtPathExpressionHelpers.findExpressionEnd(nbtPathExpression, rightPos);
40-
if (endPos == rightPos) {
41-
return HandleResult.INVALID;
42-
}
43-
44-
String rightExpressionString = nbtPathExpression.substring(rightPos, endPos);
45-
try {
46-
INbtPathExpression rightExpression = NbtPath.parse(rightExpressionString);
47-
return new HandleResult(new Expression(rightExpression),
48-
matcher.group().length() + rightExpressionString.length());
49-
} catch (NbtParseException e) {
50-
return HandleResult.INVALID;
51-
}
10+
public NbtPathExpressionParseHandlerBooleanLogicalAnd() {
11+
super("&&");
5212
}
5313

54-
public static class Expression implements INbtPathExpression {
55-
56-
protected final INbtPathExpression expression;
57-
58-
public Expression(INbtPathExpression expression) {
59-
this.expression = expression;
60-
}
61-
62-
@Override
63-
public NbtPathExpressionMatches matchContexts(Stream<NbtPathExpressionExecutionContext> executionContexts) {
64-
return new NbtPathExpressionMatches(executionContexts
65-
.map(executionContext -> {
66-
Tag currentTag = executionContext.getCurrentTag();
67-
68-
// The left side is the current tag (should be a boolean result from previous expression)
69-
boolean leftValue = NbtPathExpressionHelpers.isTruthy(currentTag);
70-
71-
// Evaluate the right expression against the root context
72-
// This ensures both sides of the expression are evaluated against the same base context
73-
Tag rootTag = executionContext.getRootContext().getCurrentTag();
74-
boolean rightValue = expression.test(rootTag);
75-
76-
// AND operation
77-
boolean result = leftValue && rightValue;
78-
79-
return new NbtPathExpressionExecutionContext(
80-
ByteTag.valueOf(result ? (byte) 1 : (byte) 0), executionContext);
81-
})
82-
);
83-
}
14+
@Override
15+
protected boolean getLogicalValue(boolean left, Supplier<Boolean> right) {
16+
return left && right.get();
8417
}
8518
}

src/main/java/org/cyclops/cyclopscore/nbt/path/parse/NbtPathExpressionParseHandlerBooleanLogicalNot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public HandleResult handlePrefixOf(String nbtPathExpression, int pos) {
4343

4444
String expressionString = nbtPathExpression.substring(exprPos, endPos);
4545
try {
46-
INbtPathExpression expression = NbtPath.parse(expressionString);
46+
INbtPathExpression expression = NbtPath.parse(expressionString.trim());
4747
return new HandleResult(new Expression(expression),
4848
matcher.group().length() + expressionString.length());
4949
} catch (NbtParseException e) {
Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,18 @@
11
package org.cyclops.cyclopscore.nbt.path.parse;
22

3-
import net.minecraft.nbt.ByteTag;
4-
import net.minecraft.nbt.Tag;
5-
import org.cyclops.cyclopscore.nbt.path.INbtPathExpression;
6-
import org.cyclops.cyclopscore.nbt.path.NbtParseException;
7-
import org.cyclops.cyclopscore.nbt.path.NbtPath;
8-
import org.cyclops.cyclopscore.nbt.path.NbtPathExpressionMatches;
9-
10-
import javax.annotation.Nullable;
11-
import java.util.regex.Matcher;
12-
import java.util.regex.Pattern;
13-
import java.util.stream.Stream;
3+
import java.util.function.Supplier;
144

155
/**
166
* A handler that handles boolean OR expressions in the form of "expression1 {@literal ||} expression2".
177
*/
18-
public class NbtPathExpressionParseHandlerBooleanLogicalOr implements INbtPathExpressionParseHandler {
19-
20-
private static final Pattern REGEX_EXPRESSION = Pattern.compile("^ *\\|\\| *");
21-
22-
@Nullable
23-
@Override
24-
public HandleResult handlePrefixOf(String nbtPathExpression, int pos) {
25-
Matcher matcher = REGEX_EXPRESSION
26-
.matcher(nbtPathExpression)
27-
.region(pos, nbtPathExpression.length());
28-
if (!matcher.find()) {
29-
return HandleResult.INVALID;
30-
}
8+
public class NbtPathExpressionParseHandlerBooleanLogicalOr extends NbtPathExpressionParseHandlerBooleanLogicalAdapter {
319

32-
// Parse the right-hand side expression
33-
int rightPos = pos + matcher.group().length();
34-
if (rightPos >= nbtPathExpression.length()) {
35-
return HandleResult.INVALID;
36-
}
37-
38-
// Find the end of the right expression
39-
int endPos = NbtPathExpressionHelpers.findExpressionEnd(nbtPathExpression, rightPos);
40-
if (endPos == rightPos) {
41-
return HandleResult.INVALID;
42-
}
43-
44-
String rightExpressionString = nbtPathExpression.substring(rightPos, endPos);
45-
try {
46-
INbtPathExpression rightExpression = NbtPath.parse(rightExpressionString);
47-
return new HandleResult(new Expression(rightExpression),
48-
matcher.group().length() + rightExpressionString.length());
49-
} catch (NbtParseException e) {
50-
return HandleResult.INVALID;
51-
}
10+
public NbtPathExpressionParseHandlerBooleanLogicalOr() {
11+
super("\\|\\|");
5212
}
5313

54-
public static class Expression implements INbtPathExpression {
55-
56-
protected final INbtPathExpression expression;
57-
58-
public Expression(INbtPathExpression expression) {
59-
this.expression = expression;
60-
}
61-
62-
@Override
63-
public NbtPathExpressionMatches matchContexts(Stream<NbtPathExpressionExecutionContext> executionContexts) {
64-
return new NbtPathExpressionMatches(executionContexts
65-
.map(executionContext -> {
66-
Tag currentTag = executionContext.getCurrentTag();
67-
68-
// The left side is the current tag (should be a boolean result from previous expression)
69-
boolean leftValue = NbtPathExpressionHelpers.isTruthy(currentTag);
70-
71-
// Evaluate the right expression against the root context
72-
// This ensures both sides of the expression are evaluated against the same base context
73-
Tag rootTag = executionContext.getRootContext().getCurrentTag();
74-
boolean rightValue = expression.test(rootTag);
75-
76-
// OR operation
77-
boolean result = leftValue || rightValue;
78-
79-
return new NbtPathExpressionExecutionContext(
80-
ByteTag.valueOf(result ? (byte) 1 : (byte) 0), executionContext);
81-
})
82-
);
83-
}
14+
@Override
15+
protected boolean getLogicalValue(boolean left, Supplier<Boolean> right) {
16+
return left || right.get();
8417
}
8518
}

src/test/java/org/cyclops/cyclopscore/nbt/path/TestNbtPathLogicalOperators.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void testParseLogicalWithFilterExpression() throws NbtParseException {
141141
expectedFiltered.add(item1);
142142
expectedFiltered.add(item3);
143143

144-
List<Tag> expected = Lists.newArrayList(expectedFiltered);
144+
List<Tag> expected = List.of(expectedFiltered);
145145
assertThat(expression.match(Stream.of(root)).getMatches().collect(Collectors.toList()), equalTo(expected));
146146
assertThat(expression.test(root), is(true));
147147
}
@@ -171,7 +171,7 @@ public void testParseLogicalWithFilterExpressionAnd() throws NbtParseException {
171171
ListTag expectedFiltered = new ListTag();
172172
expectedFiltered.add(item1);
173173

174-
List<Tag> expected = Lists.newArrayList(expectedFiltered);
174+
List<Tag> expected = List.of(expectedFiltered);
175175
assertThat(expression.match(Stream.of(root)).getMatches().collect(Collectors.toList()), equalTo(expected));
176176
assertThat(expression.test(root), is(true));
177177
}
@@ -203,7 +203,7 @@ public void testParseLogicalNotWithFilterExpression() throws NbtParseException {
203203
expectedFiltered.add(item1);
204204
expectedFiltered.add(item3);
205205

206-
List<Tag> expected = Lists.newArrayList(expectedFiltered);
206+
List<Tag> expected = List.of(expectedFiltered);
207207
assertThat(expression.match(Stream.of(root)).getMatches().collect(Collectors.toList()), equalTo(expected));
208208
assertThat(expression.test(root), is(true));
209209
}
@@ -262,8 +262,8 @@ public void testParseNotEqualWithFilterExpression() throws NbtParseException {
262262
expectedFiltered.add(item1);
263263
expectedFiltered.add(item3);
264264

265-
List<Tag> expected = Lists.newArrayList(expectedFiltered);
266-
assertThat(expression.match(Stream.of(root)).getMatches().collect(Collectors.toList()), equalTo(expected));
265+
List<Tag> expected = List.of(expectedFiltered);
266+
assertThat(expression.match(Stream.of(root)).getMatches().toList(), equalTo(expected));
267267
assertThat(expression.test(root), is(true));
268268
}
269269

0 commit comments

Comments
 (0)