Beispiel #1
0
  private static List<String> getItemValues(HasRuleValues hasRuleValues, String id) {
    Map<String, Item> items = hasRuleValues.getItems();
    Map<String, Itemset> itemsets = hasRuleValues.getItemsets();

    Itemset itemset = itemsets.get(id);

    List<ItemRef> itemRefs = itemset.getItemRefs();

    List<String> result = new ArrayList<>(itemRefs.size());

    for (int i = 0, max = itemRefs.size(); i < max; i++) {
      ItemRef itemRef = itemRefs.get(i);

      Item item = items.get(itemRef.getItemRef());

      result.add(item.getValue());
    }

    return result;
  }
Beispiel #2
0
  private static List<AssociationRule> getRuleValues(
      HasRuleValues hasRuleValues, final OutputField outputField) {
    List<AssociationRule> associationRules =
        hasRuleValues.getRuleValues(outputField.getAlgorithm());

    Comparator<AssociationRule> comparator =
        new Comparator<AssociationRule>() {

          private OutputField.RankBasis rankBasis = outputField.getRankBasis();

          private OutputField.RankOrder rankOrder = outputField.getRankOrder();

          @Override
          public int compare(AssociationRule left, AssociationRule right) {
            int order;

            switch (this.rankBasis) {
              case CONFIDENCE:
                order = (getConfidence(left)).compareTo(getConfidence(right));
                break;
              case SUPPORT:
                order = (getSupport(left)).compareTo(getSupport(right));
                break;
              case LIFT:
                order = (getLift(left)).compareTo(getLift(right));
                break;
              case LEVERAGE:
                order = (getLeverage(left)).compareTo(getLeverage(right));
                break;
              case AFFINITY:
                order = (getAffinity(left)).compareTo(getAffinity(right));
                break;
              default:
                throw new UnsupportedFeatureException(outputField, this.rankBasis);
            } // End switch

            switch (this.rankOrder) {
              case ASCENDING:
                return order;
              case DESCENDING:
                return -order;
              default:
                throw new UnsupportedFeatureException(outputField, this.rankOrder);
            }
          }

          private Double getConfidence(AssociationRule rule) {
            return checkRuleFeature(rule, rule.getConfidence());
          }

          private Double getSupport(AssociationRule rule) {
            return checkRuleFeature(rule, rule.getSupport());
          }

          private Double getLift(AssociationRule rule) {
            return checkRuleFeature(rule, rule.getLift());
          }

          private Double getLeverage(AssociationRule rule) {
            return checkRuleFeature(rule, rule.getLeverage());
          }

          private Double getAffinity(AssociationRule rule) {
            return checkRuleFeature(rule, rule.getAffinity());
          }

          private <V> V checkRuleFeature(AssociationRule rule, V value) {

            if (value == null) {
              throw new InvalidFeatureException(rule);
            }

            return value;
          }
        };

    Ordering<AssociationRule> ordering = Ordering.from(comparator);

    return ordering.sortedCopy(associationRules);
  }