001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.datamining; 018 019import java.io.Serializable; 020import java.util.Comparator; 021import java.util.Set; 022 023import org.conqat.lib.commons.collections.CollectionUtils; 024 025/** 026 * An association rule is of the form I -> j, where I is a set of items and j is 027 * an item. The rule means that if all items of I appear in a shopping basket, 028 * then j is "likely" to appear in that basket as well. Likely is defined by the 029 * confidence which is computed as the ratio between the number of baskets 030 * containing both I <b>and</b> j and the number of baskets that contain I. 031 */ 032public class AssociationRule<T> implements Serializable { 033 034 /** Version used for serialization. */ 035 private static final long serialVersionUID = 1; 036 037 /** The item set (i.e. the I in I->j) */ 038 private Set<T> itemSet; 039 040 /** The associated item (i.e. the j in I->j) */ 041 private T associatedItem; 042 043 /** The confidence level [0..1] */ 044 private double confidence; 045 046 public AssociationRule(Set<T> itemSet, T associatedItem, double confidence) { 047 this.itemSet = itemSet; 048 this.associatedItem = associatedItem; 049 this.confidence = confidence; 050 } 051 052 /** Returns the item set */ 053 public Set<T> getItemSet() { 054 return itemSet; 055 } 056 057 /** Returns the associated item */ 058 public T getAssociatedItem() { 059 return associatedItem; 060 } 061 062 /** Returns the confidence. */ 063 public double getConfidence() { 064 return confidence; 065 } 066 067 /** {@inheritDoc} */ 068 @Override 069 public String toString() { 070 return CollectionUtils.sort(itemSet, new Comparator<T>() { 071 @Override 072 public int compare(T t1, T t2) { 073 return t1.toString().compareTo(t2.toString()); 074 } 075 }).toString() + " -> " + associatedItem + " (" + confidence + ")"; 076 } 077}