-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite variable parsing to use element names
- Loading branch information
1 parent
a931d3c
commit 767d709
Showing
13 changed files
with
170 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 15 additions & 27 deletions
42
core/src/main/java/tc/oc/pgm/variables/VariableDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,41 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import java.util.function.Function; | ||
import tc.oc.pgm.api.match.Match; | ||
import tc.oc.pgm.features.SelfIdentifyingFeatureDefinition; | ||
import tc.oc.pgm.filters.Filterable; | ||
|
||
public class VariableDefinition<T extends Filterable<?>> extends SelfIdentifyingFeatureDefinition { | ||
|
||
private final Class<T> scope; | ||
private final double def; | ||
private final VariableType variableType; | ||
|
||
public VariableDefinition(String id, Class<T> scope, double def, VariableType type) { | ||
private final boolean isDynamic; | ||
private final Function<VariableDefinition<T>, Variable<T>> builder; | ||
|
||
public VariableDefinition( | ||
String id, | ||
Class<T> scope, | ||
boolean isDynamic, | ||
Function<VariableDefinition<T>, Variable<T>> builder) { | ||
super(id); | ||
this.scope = scope; | ||
this.def = def; | ||
this.variableType = type; | ||
this.isDynamic = isDynamic; | ||
this.builder = builder; | ||
} | ||
|
||
public Class<T> getScope() { | ||
return scope; | ||
} | ||
|
||
public double getDefault() { | ||
return def; | ||
} | ||
|
||
public Variable<?> buildInstance() { | ||
return getVariableType().buildInstance(this); | ||
public boolean isDynamic() { | ||
return isDynamic; | ||
} | ||
|
||
public VariableType getVariableType() { | ||
return variableType; | ||
public Variable<T> buildInstance() { | ||
return builder.apply(this); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Variable<T> getVariable(Match match) { | ||
return (Variable<T>) match.getFeatureContext().get(this.getId()); | ||
} | ||
|
||
public static class Context<T extends Filterable<?>, C> extends VariableDefinition<T> { | ||
private final C context; | ||
|
||
public Context(String id, Class<T> scope, double def, VariableType type, C context) { | ||
super(id, scope, def, type); | ||
this.context = context; | ||
} | ||
|
||
public C getContext() { | ||
return context; | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
core/src/main/java/tc/oc/pgm/variables/VariableParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
import org.jdom2.Element; | ||
import tc.oc.pgm.api.feature.FeatureReference; | ||
import tc.oc.pgm.api.filter.Filterables; | ||
import tc.oc.pgm.api.map.factory.MapFactory; | ||
import tc.oc.pgm.api.match.Match; | ||
import tc.oc.pgm.api.party.Party; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.filters.Filterable; | ||
import tc.oc.pgm.teams.TeamFactory; | ||
import tc.oc.pgm.util.MethodParser; | ||
import tc.oc.pgm.util.MethodParsers; | ||
import tc.oc.pgm.util.xml.InvalidXMLException; | ||
import tc.oc.pgm.util.xml.Node; | ||
import tc.oc.pgm.util.xml.XMLUtils; | ||
import tc.oc.pgm.variables.types.BlitzVariable; | ||
import tc.oc.pgm.variables.types.DummyVariable; | ||
import tc.oc.pgm.variables.types.ScoreVariable; | ||
import tc.oc.pgm.variables.types.TeamVariableAdapter; | ||
|
||
public class VariableParser { | ||
// The limitation is due to them being used in exp4j formulas for. | ||
public static final Pattern VARIABLE_ID = Pattern.compile("[A-Za-z_]\\w*"); | ||
|
||
private final MapFactory factory; | ||
private final Map<String, Method> methodParsers; | ||
|
||
public VariableParser(MapFactory factory) { | ||
this.factory = factory; | ||
this.methodParsers = MethodParsers.getMethodParsersForClass(getClass()); | ||
} | ||
|
||
public VariableDefinition<?> parse(Element el) throws InvalidXMLException { | ||
String id = Node.fromRequiredAttr(el, "id").getValue(); | ||
if (!VARIABLE_ID.matcher(id).matches()) | ||
throw new InvalidXMLException( | ||
"Variable IDs must start with a letter or underscore and can only include letters, digits or underscores.", | ||
el); | ||
|
||
Method parser = methodParsers.get(el.getName().toLowerCase()); | ||
if (parser != null) { | ||
try { | ||
return (VariableDefinition<?>) parser.invoke(this, el, id); | ||
} catch (Exception e) { | ||
throw InvalidXMLException.coerce(e, new Node(el)); | ||
} | ||
} else { | ||
throw new InvalidXMLException("Unknown variable type: " + el.getName(), el); | ||
} | ||
} | ||
|
||
@MethodParser("variable") | ||
public VariableDefinition<?> parseDummy(Element el, String id) throws InvalidXMLException { | ||
Class<? extends Filterable<?>> scope = Filterables.parse(Node.fromRequiredAttr(el, "scope")); | ||
double def = XMLUtils.parseNumber(Node.fromAttr(el, "default"), Double.class, 0d); | ||
return new VariableDefinition<>(id, scope, true, vd -> new DummyVariable<>(vd, def)); | ||
} | ||
|
||
@MethodParser("lives") | ||
public VariableDefinition<MatchPlayer> parseBlitzLives(Element el, String id) | ||
throws InvalidXMLException { | ||
return new VariableDefinition<>(id, MatchPlayer.class, false, BlitzVariable::new); | ||
} | ||
|
||
@MethodParser("score") | ||
public VariableDefinition<Party> parseScore(Element el, String id) throws InvalidXMLException { | ||
return new VariableDefinition<>(id, Party.class, false, ScoreVariable::new); | ||
} | ||
|
||
@MethodParser("with-team") | ||
public VariableDefinition<Match> parseTeamAdapter(Element el, String id) | ||
throws InvalidXMLException { | ||
@SuppressWarnings("unchecked") | ||
VariableDefinition<Party> var = | ||
factory.getFeatures().resolve(Node.fromRequiredAttr(el, "var"), VariableDefinition.class); | ||
if (var.getScope() != Party.class) { | ||
throw new InvalidXMLException( | ||
"Team scope is required for with-team variable, got " + var.getScope().getSimpleName(), | ||
el); | ||
} | ||
|
||
FeatureReference<TeamFactory> team = | ||
factory.getFeatures().createReference(Node.fromRequiredAttr(el, "team"), TeamFactory.class); | ||
|
||
return new VariableDefinition<>( | ||
id, Match.class, var.isDynamic(), vd -> new TeamVariableAdapter(vd, var, team)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.