15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.test.lib.jittester; 25 26 import java.util.Iterator; 27 import java.util.LinkedList; 28 import java.util.TreeSet; 29 import jdk.test.lib.jittester.factories.Factory; 30 import jdk.test.lib.jittester.utils.PseudoRandom; 31 32 /** 33 * The Rule. A helper to perform production. 34 */ 35 public class Rule extends Factory implements Comparable<Rule> { 36 private String name; 37 private Integer limit = -1; 38 39 @Override 40 public int compareTo(Rule rule) { 41 return name.compareTo(rule.name); 42 } 43 44 private TreeSet<RuleEntry> variants; 45 46 public Rule(String name) { 47 this.name = name; 48 variants = new TreeSet<>(); 49 } 50 51 public void add(String ruleName, Factory factory) { 52 add(ruleName, factory, 1.0); 53 } 54 55 public void add(String ruleName, Factory factory, double weight) { 56 variants.add(new RuleEntry(ruleName, factory, weight)); 57 } 58 59 public int size() { 60 return variants.size(); 61 } 62 63 @Override 64 public IRNode produce() throws ProductionFailedException { 65 if (!variants.isEmpty()) { 66 // Begin production. 67 LinkedList<RuleEntry> rulesList = new LinkedList<>(variants); 68 PseudoRandom.shuffle(rulesList); 69 70 while (!rulesList.isEmpty() && (limit == -1 || limit > 0)) { 71 double sum = rulesList.stream() 72 .mapToDouble(r -> r.weight) 73 .sum(); 74 double rnd = PseudoRandom.random() * sum; 75 Iterator<RuleEntry> iterator = rulesList.iterator(); 76 RuleEntry ruleEntry; 77 double weightAccumulator = 0; 78 do { 79 ruleEntry = iterator.next(); 80 weightAccumulator += ruleEntry.weight; 81 if (weightAccumulator >= rnd) { 82 break; 83 } 84 } while (iterator.hasNext()); 85 try { 86 return ruleEntry.produce(); 87 } catch (ProductionFailedException e) { 88 } 89 iterator.remove(); 90 if (limit != -1) { 91 limit--; 92 } 93 } 94 //throw new ProductionFailedException(); 95 } 96 // should probably throw exception here.. 97 //return getChildren().size() > 0 ? getChild(0).produce() : null; 98 throw new ProductionFailedException(); 99 } 100 101 private class RuleEntry extends Factory implements Comparable<RuleEntry> { 102 private final double weight; 103 private final Factory factory; 104 private final String name; 105 106 private RuleEntry(String name, Factory factory, double weight) { 107 this.name = name; 108 this.weight = weight; 109 this.factory = factory; 110 } 111 112 @Override 113 public IRNode produce() throws ProductionFailedException { 114 return factory.produce(); 115 } 116 117 @Override 118 public int compareTo(RuleEntry entry) { 119 return name.compareTo(entry.name); 120 } 121 } 122 } | 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.test.lib.jittester; 25 26 import java.util.Iterator; 27 import java.util.LinkedList; 28 import java.util.TreeSet; 29 import jdk.test.lib.jittester.factories.Factory; 30 import jdk.test.lib.jittester.utils.PseudoRandom; 31 32 /** 33 * The Rule. A helper to perform production. 34 */ 35 public class Rule<T extends IRNode> extends Factory<T> implements Comparable<Rule<T>> { 36 private final String name; 37 private final TreeSet<RuleEntry> variants; 38 private Integer limit = -1; 39 40 @Override 41 public int compareTo(Rule<T> rule) { 42 return name.compareTo(rule.name); 43 } 44 45 public Rule(String name) { 46 this.name = name; 47 variants = new TreeSet<>(); 48 } 49 50 public void add(String ruleName, Factory<? extends T> factory) { 51 add(ruleName, factory, 1.0); 52 } 53 54 public void add(String ruleName, Factory<? extends T> factory, double weight) { 55 variants.add(new RuleEntry(ruleName, factory, weight)); 56 } 57 58 public int size() { 59 return variants.size(); 60 } 61 62 @Override 63 public T produce() throws ProductionFailedException { 64 if (!variants.isEmpty()) { 65 // Begin production. 66 LinkedList<RuleEntry> rulesList = new LinkedList<>(variants); 67 PseudoRandom.shuffle(rulesList); 68 69 while (!rulesList.isEmpty() && (limit == -1 || limit > 0)) { 70 double sum = rulesList.stream() 71 .mapToDouble(r -> r.weight) 72 .sum(); 73 double rnd = PseudoRandom.random() * sum; 74 Iterator<RuleEntry> iterator = rulesList.iterator(); 75 RuleEntry ruleEntry; 76 double weightAccumulator = 0; 77 do { 78 ruleEntry = iterator.next(); 79 weightAccumulator += ruleEntry.weight; 80 if (weightAccumulator >= rnd) { 81 break; 82 } 83 } while (iterator.hasNext()); 84 try { 85 return ruleEntry.produce(); 86 } catch (ProductionFailedException e) { 87 } 88 iterator.remove(); 89 if (limit != -1) { 90 limit--; 91 } 92 } 93 //throw new ProductionFailedException(); 94 } 95 // should probably throw exception here.. 96 //return getChildren().size() > 0 ? getChild(0).produce() : null; 97 throw new ProductionFailedException(); 98 } 99 100 private class RuleEntry extends Factory<T> implements Comparable<RuleEntry> { 101 private final double weight; 102 private final Factory<? extends T> factory; 103 private final String name; 104 105 private RuleEntry(String name, Factory<? extends T> factory, double weight) { 106 this.name = name; 107 this.weight = weight; 108 this.factory = factory; 109 } 110 111 @Override 112 public T produce() throws ProductionFailedException { 113 return factory.produce(); 114 } 115 116 @Override 117 public int compareTo(RuleEntry entry) { 118 return name.compareTo(entry.name); 119 } 120 } 121 } |