AssociationRule.java

  1. package org.thegalactic.rule;

  2. /*
  3.  * AssociationRule.java
  4.  *
  5.  * Copyright: 2010-2015 Karell Bertet, France
  6.  * Copyright: 2015-2016 The Galactic Organization, France
  7.  *
  8.  * License: http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html CeCILL-B license
  9.  *
  10.  * This file is part of java-lattices.
  11.  * You can redistribute it and/or modify it under the terms of the CeCILL-B license.
  12.  */
  13. import java.util.SortedSet;

  14. /**
  15.  * This class gives a representation for an association rule.
  16.  *
  17.  * A rule is composed of a premise and a conclusion that are comparable sets,
  18.  * i.e. sets of elements that can be sorted by the lectic order defined by class
  19.  * `ComparableSet`. An association rule extends regular rule with the notion of
  20.  * support and confidence. The confidence represents the number of observations
  21.  * for which the rule is true. The support represents the minimum number of
  22.  * observations for the rule to be relevant.
  23.  *
  24.  * @todo do we use composition over inheritance?
  25.  */
  26. public class AssociationRule extends Rule {

  27.     /* ------------- FIELDS ------------------ */
  28.     /**
  29.      * The confidence of the rule.
  30.      */
  31.     private double confidence;

  32.     /**
  33.      * The support of the rule.
  34.      */
  35.     private double support;

  36.     /* ------------- CONSTRUCTORS ------------------ */
  37.     /**
  38.      * Constructs a new empty Rule with a empty premise and an empty conclusion.
  39.      */
  40.     public AssociationRule() {
  41.         super();
  42.     }

  43.     /**
  44.      * Constructs a new Rule with the premise and the conclusion given in
  45.      * parameters.
  46.      *
  47.      * @param premise    a set of indexed elements
  48.      * @param conclusion a set of indexed elements
  49.      */
  50.     public AssociationRule(final SortedSet<Comparable> premise, final SortedSet<Comparable> conclusion) {
  51.         super(premise, conclusion);
  52.     }

  53.     /**
  54.      * Constructs a new Rule with the premise, the conclusion, the confidence
  55.      * and the support given in parameters.
  56.      *
  57.      * @param premise    a set of indexed elements
  58.      * @param conclusion a set of indexed elements
  59.      * @param support    a support value
  60.      * @param confidence a confidence value
  61.      */
  62.     public AssociationRule(
  63.             final SortedSet<Comparable> premise,
  64.             final SortedSet<Comparable> conclusion,
  65.             final double support,
  66.             final double confidence
  67.     ) {
  68.         super(premise, conclusion);
  69.         this.support = support;
  70.         this.confidence = confidence;
  71.     }

  72.     /* ------------- ACCESSORS METHODS ------------------ */
  73.     /**
  74.      * Returns the confidence value of the rule.
  75.      *
  76.      * @return confidence value
  77.      */
  78.     public final double getConfidence() {
  79.         return this.confidence;
  80.     }

  81.     /**
  82.      * Returns the support value of the rule.
  83.      *
  84.      * @return support value
  85.      */
  86.     public final double getSupport() {
  87.         return this.support;
  88.     }

  89.     /* ------------- MODIFICATION METHODS ------------------ */
  90.     /**
  91.      * Set the confidence value of the rule.
  92.      *
  93.      * @param confidence the confidence value
  94.      */
  95.     public final void setConfidence(final double confidence) {
  96.         this.confidence = confidence;
  97.     }

  98.     /**
  99.      * Set the support value of the rule.
  100.      *
  101.      * @param support the support value
  102.      */
  103.     public final void setSupport(final double support) {
  104.         this.support = support;
  105.     }

  106.     /* ------------- OVERRIDEN METHODS ------------------ */
  107.     /**
  108.      * Returns a String representation of this component.
  109.      *
  110.      * The following format is used:
  111.      *
  112.      * ~~~
  113.      * [premise] -> [conclusion] : s:support/c:confidence
  114.      * ~~~
  115.      *
  116.      * @return a string made of premises followed by -> and the conclusions.
  117.      */
  118.     @Override
  119.     public String toString() {
  120.         final StringBuilder builder = new StringBuilder();
  121.         builder.append(super.toString()).append(" : s:").append(this.support).append("/c:").append(this.confidence);
  122.         return builder.toString();
  123.     }
  124. }