1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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 }