diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen')
3 files changed, 112 insertions, 8 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java index ca0578d..4f194f3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -62,6 +62,10 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * The cases to add for this rule. */ public void makeRule(E rule, FunctionalList<FunctionalList<E>> cases) { + if (cases == null) { + throw new NullPointerException("Cases must not be null"); + } + super.addRule(rule); cases.forEach(currentCase -> super.addCase(rule, 1, currentCase)); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java index 28b61c5..cdfe056 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -57,21 +57,33 @@ public class WeightedGrammar<E> { public WeightedGrammar(Random source) { this(); + if (source == null) { + throw new NullPointerException( + "Source of randomness must be non-null"); + } + rng = source; } /** * Add a case to an already existing rule. * - * @param rule + * @param ruleName * The rule to add a case to. * @param probability * The probability for this rule to be chosen. * @param cse * The case being added. */ - public void addCase(E rule, int probability, FunctionalList<E> cse) { - rules.get(rule).addProbability(probability, cse); + public void addCase(E ruleName, int probability, + FunctionalList<E> cse) { + if (ruleName == null) { + throw new NullPointerException("Rule name must be not null"); + } else if (cse == null) { + throw new NullPointerException("Case body must not be null"); + } + + rules.get(ruleName).addProbability(probability, cse); } /** @@ -84,6 +96,14 @@ public class WeightedGrammar<E> { * @return Whether the alias was succesfully created */ public boolean addGrammarAlias(E name, E alias) { + if (name == null) { + throw new NullPointerException( + "Subgrammar name must not be null"); + } else if (alias == null) { + throw new NullPointerException( + "Subgrammar alias must not be null"); + } + if (subgrammars.containsKey(alias)) { return false; } else { @@ -107,6 +127,11 @@ public class WeightedGrammar<E> { if (rng == null) { rng = new Random(); } + + if (name == null) { + throw new NullPointerException("Rule name must not be null"); + } + return addRule(name, new WeightedRandom<>(rng)); } @@ -121,6 +146,12 @@ public class WeightedGrammar<E> { */ public boolean addRule(E name, WeightedRandom<FunctionalList<E>> cases) { + if (name == null) { + throw new NullPointerException("Name must not be null"); + } else if (cases == null) { + throw new NullPointerException("Cases must not be null"); + } + if (rules.containsKey(name)) { return false; } else { @@ -139,6 +170,13 @@ public class WeightedGrammar<E> { * @return Whether or not the subgrammar was succesfully added. */ public boolean addSubgrammar(E name, WeightedGrammar<E> subgrammar) { + if (name == null) { + throw new NullPointerException( + "Subgrammar name must not be null"); + } else if (subgrammar == null) { + throw new NullPointerException("Subgrammar must not be null"); + } + if (subgrammars.containsKey(name)) { return false; } else { @@ -157,6 +195,10 @@ public class WeightedGrammar<E> { */ public FunctionalList<FunctionalList<E>> generateDebugValues(E ruleName) { + if (ruleName == null) { + throw new NullPointerException("Rule name must not be null"); + } + FunctionalList<FunctionalList<E>> returnedList = new FunctionalList<>(); @@ -187,6 +229,15 @@ public class WeightedGrammar<E> { */ public <T> FunctionalList<T> generateGenericValues(E initRule, Function<E, T> tokenTransformer, T spacer) { + if (initRule == null) { + throw new NullPointerException( + "Initial rule must not be null"); + } else if (tokenTransformer == null) { + throw new NullPointerException("Transformer must not be null"); + } else if (spacer == null) { + throw new NullPointerException("Spacer must not be null"); + } + FunctionalList<T> returnedList = new FunctionalList<>(); if (subgrammars.containsKey(initRule)) { @@ -205,7 +256,14 @@ public class WeightedGrammar<E> { returnedList.add(spacer); })); } else { - returnedList.add(tokenTransformer.apply(initRule)); + T transformedToken = tokenTransformer.apply(initRule); + + if (transformedToken == null) { + throw new NullPointerException( + "Transformer created null token"); + } + + returnedList.add(transformedToken); returnedList.add(spacer); } @@ -244,6 +302,11 @@ public class WeightedGrammar<E> { * @return The subgrammar with the specified name. */ public WeightedGrammar<E> getSubgrammar(E name) { + if (name == null) { + throw new NullPointerException( + "Subgrammar name must not be null"); + } + return subgrammars.get(name); } @@ -270,6 +333,16 @@ public class WeightedGrammar<E> { */ public void multiPrefixRule(E ruleName, E prefixToken, int additionalProbability, int numberOfTimes) { + if (ruleName == null) { + throw new NullPointerException("Rule name must not be null"); + } else if (prefixToken == null) { + throw new NullPointerException( + "Prefix token must not be null"); + } else if (numberOfTimes < 1) { + throw new IllegalArgumentException( + "Number of times to prefix must be positive."); + } + WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = @@ -315,6 +388,13 @@ public class WeightedGrammar<E> { */ public void prefixRule(E ruleName, E prefixToken, int additionalProbability) { + if (ruleName == null) { + throw new NullPointerException("Rule name must not be null"); + } else if (prefixToken == null) { + throw new NullPointerException( + "Prefix token must not be null"); + } + WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = @@ -340,6 +420,10 @@ public class WeightedGrammar<E> { * The name of the rule to remove. */ public void deleteRule(E name) { + if (name == null) { + throw new NullPointerException("Rule name must not be null"); + } + rules.remove(name); } @@ -350,6 +434,10 @@ public class WeightedGrammar<E> { * The name of the subgrammar to remove. */ public void deleteSubgrammar(E name) { + if (name == null) { + throw new NullPointerException("Rule name must not be null"); + } + subgrammars.remove(name); } @@ -386,13 +474,20 @@ public class WeightedGrammar<E> { * * @param ruleName * The rule to suffix - * @param prefixToken + * @param suffixToken * The token to prefix to the rule * @param additionalProbability * Additional probability of the prefixed rule */ - public void suffixRule(E ruleName, E prefixToken, + public void suffixRule(E ruleName, E suffixToken, int additionalProbability) { + if (ruleName == null) { + throw new NullPointerException("Rule name must not be null"); + } else if (suffixToken == null) { + throw new NullPointerException( + "Prefix token must not be null"); + } + WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = @@ -401,7 +496,7 @@ public class WeightedGrammar<E> { rule.getValues().forEach((par) -> { FunctionalList<E> newCase = par.merge((left, right) -> right.clone()); - newCase.add(prefixToken); + newCase.add(suffixToken); newResults.add(new Pair<>(par.merge((left, right) -> left) + additionalProbability, newCase)); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index 5a8ef8f..5157ee2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -44,6 +44,11 @@ public class WeightedRandom<E> { probabilities = new FunctionalList<>(); results = new FunctionalList<>(); + if (src == null) { + throw new NullPointerException( + "Source of randomness must not be null"); + } + source = src; } @@ -109,4 +114,4 @@ public class WeightedRandom<E> { public FunctionalList<Pair<Integer, E>> getValues() { return probabilities.pairWith(results); } -} +}
\ No newline at end of file |
