Skip to content

Commit

Permalink
Format code.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-rapp committed Aug 30, 2018
1 parent 9a22ac5 commit 19bca33
Show file tree
Hide file tree
Showing 32 changed files with 197 additions and 113 deletions.
7 changes: 4 additions & 3 deletions src/main/java/de/mrapp/apriori/AssociationRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import java.io.Serializable
* @author Michael Rapp
* @since 1.0.0
*/
data class AssociationRule<ItemType : Item>(val body: ItemSet<ItemType>,
val head: ItemSet<ItemType>,
val support: Double) : Comparable<AssociationRule<*>>, Serializable {
data class AssociationRule<ItemType : Item>(
val body: ItemSet<ItemType>,
val head: ItemSet<ItemType>,
val support: Double) : Comparable<AssociationRule<*>>, Serializable {

init {
ensureAtLeast(support, 0.0, "The support must be at least 0")
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/de/mrapp/apriori/Filter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ interface Filter<T> : Predicate<T> {
companion object {

/**
* Returns a filter, which applies to item sets. By default, no item sets are filtered at all.
* Returns a filter, which applies to item sets. By default, no item sets are filtered at
* all.
*/
fun forItemSets() = ItemSetFilter(Predicate { _ -> true }, null)

/**
* Returns a filter, which applies to association rules. By default, no association rules are
* filtered at all.
* Returns a filter, which applies to association rules. By default, no association rules
* are filtered at all.
*/
fun forAssociationRules() = AssociationRuleFilter(Predicate { _ -> true }, null)

Expand All @@ -50,8 +51,9 @@ interface Filter<T> : Predicate<T> {
* @property parent The parent of the filter. When applied to an item, all parents of the
* filter are tested
*/
abstract class AbstractFilter<T, FilterType : Filter<T>>(protected val predicate: Predicate<T>,
protected val parent: FilterType? = null) : Filter<T>
abstract class AbstractFilter<T, FilterType : Filter<T>>(
protected val predicate: Predicate<T>,
protected val parent: FilterType? = null) : Filter<T>

/**
* A filter, which applies to item sets.
Expand Down Expand Up @@ -170,7 +172,6 @@ interface Filter<T> : Predicate<T> {
* minimum size must be at least 0
* @param maxSize The maximum size, which should be set, as an [Integer] value. The
* maximum size must be at least the minimum size
* @return The filter, this method has been called upon, as an instance of the class [ ]. The filter may not be null
*/
@JvmOverloads
fun byHeadSize(minSize: Int, maxSize: Int = Integer.MAX_VALUE): AssociationRuleFilter {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/mrapp/apriori/FrequentItemSets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class FrequentItemSets<ItemType : Item> : SortedArraySet<ItemSet<ItemType>>,
* which have been found by the algorithm.
*
* @param T The type of the items that are contained by the item sets
* @param frequentItemSets A collection that contains the frequent item sets or null, if
* no frequent item sets have been found by the algorithm
* @param frequentItemSets A collection that contains the frequent item sets or null, if no
* frequent item sets have been found by the algorithm
*/
fun <T> formatFrequentItemSets(frequentItemSets: Collection<ItemSet<T>>): String {
val stringBuilder = StringBuilder()
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/mrapp/apriori/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import java.io.Serializable
/**
* Defines the interface, an item, which is part of a [Transaction], must implement. The Apriori
* algorithm relies on items to correctly implement the [Object.hashCode] and [Object.equals]
* methods to be able to compare items to each other. Furthermore, the interface [Comparable]
* must be implemented. This allows to sort items (e.g. by their names) in order to generate
* candidates in an efficient way, when searching for frequent item sets.
* methods to be able to compare items to each other. Furthermore, the interface [Comparable] must
* be implemented. This allows to sort items (e.g. by their names) in order to generate candidates
* in an efficient way, when searching for frequent item sets.
*
* @author Michael Rapp
* @since 1.0.0
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/mrapp/apriori/Metric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package de.mrapp.apriori

/**
* Defines the interface, a class, which allows to measure the "interestingly" of association
* rules according to a certain metric, must implement.
* Defines the interface, a class, which allows to measure the "interestingly" of association rules
* according to a certain metric, must implement.
*
* @author Michael Rapp
* @since 1.0.0
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/mrapp/apriori/Output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import java.io.Serializable
/**
* An output of the Apriori algorithm.
*
* @param ItemType The type of the items, which have been processed by the Apriori algorithm
* @param ItemType The type of the items, which have been processed by the Apriori
* algorithm
* @property configuration The configuration of the Apriori algorithm
* @property startTime The time, the Apriori algorithm was started
* @property endTime The time, the Apriori algorithm terminated
* @property frequentItemSets The frequent item sets that have been found by the Apriori algorithm
* @property ruleSet The rule set that contains the association rules, which have been
* generated by the Apriori algorithm or null, if the algorithm has not been
* configured to generate any rules
* generated by the Apriori algorithm or null, if the algorithm has not
* been configured to generate any rules
* @author Michael Rapp
* @since 1.0.0
*/
Expand All @@ -38,7 +39,7 @@ class Output<ItemType : Item>(val configuration: Configuration,
val ruleSet: RuleSet<ItemType>?) : Serializable {

/**
* Returns the runtime of the Apriori algorithm in milliseconds.
* The runtime of the Apriori algorithm in milliseconds.
*/
val runtime: Long
get() = endTime - startTime
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/mrapp/apriori/TieBreaker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ interface TieBreaker<T> : Comparator<T> {
*
* @param T The type of the items, the tie-breaking strategy applies to
* @param TieBreakerType The type of the tie-breaking strategy
* @property parent The tie-breaking strategy, which is used for tie-breaking
* before applying this tie-breaking strategy
* @property parent The tie-breaking strategy, which is used for tie-breaking before
* applying this tie-breaking strategy
* @property comparator The comparator, which specifies which one of two item sets or
* association rules should be sorted before the other one when
* performing tie-breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import de.mrapp.apriori.Transaction
import java.util.*

/**
* An extension of the class [ItemSet], which allows to store the transactions, the item
* set occurs in.
* An extension of the class [ItemSet], which allows to store the transactions, the item set occurs
* in.
*
* @param ItemType The type of the items, which are contained by the item set
* @author Michael Rapp
Expand All @@ -29,8 +29,8 @@ import java.util.*
class TransactionalItemSet<ItemType : Item> : ItemSet<ItemType> {

/**
* A map that contains the transactions, the item set occurs in. The transactions are
* mapped to unique ids.
* A map that contains the transactions, the item set occurs in. The transactions are mapped to
* unique ids.
*/
var transactions: MutableMap<Int, Transaction<ItemType>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ class AssociationRuleGeneratorModule<ItemType : Item> : AssociationRuleGenerator

/**
* Generates association rules from a specific [itemSet] by moving items from a rule's [body]
* to its [head]. This method is executed recursively until the resulting rule does not reach the
* minimum confidence anymore.
* to its [head]. This method is executed recursively until the resulting rule does not reach
* the minimum confidence anymore.
*/
private fun generateRules(itemSet: ItemSet<ItemType>,
frequentItemSets: Map<Int, ItemSet<ItemType>>,
ruleSet: RuleSet<ItemType>,
body: ItemSet<ItemType>,
head: ItemSet<ItemType>?, minConfidence: Double) {
for (item in body) {
val headItemSet = head?.let{ ItemSet(it) } ?: ItemSet()
val headItemSet = head?.let { ItemSet(it) } ?: ItemSet()
headItemSet.add(item)
val bodyItemSet = ItemSet(body)
bodyItemSet.remove(item)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/mrapp/apriori/tasks/AbstractTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ package de.mrapp.apriori.tasks
import de.mrapp.apriori.Apriori

/**
* An abstract base class for all tasks, which execute a module multiple times in order to
* obtain the results, which are requested according to a specific configuration.
* An abstract base class for all tasks, which execute a module multiple times in order to obtain
* the results, which are requested according to a specific configuration.
*
* @property configuration The configuration that is used by the task
* @author Michael Rapp
Expand Down
53 changes: 48 additions & 5 deletions src/test/java/de/mrapp/apriori/AbstractDataTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,62 @@ abstract class AbstractDataTest {

const val INPUT_FILE_4 = "data4.txt"

val FREQUENT_ITEM_SETS_1 = arrayOf(arrayOf("milk", "sugar"), arrayOf("coffee"), arrayOf("coffee", "milk", "sugar"), arrayOf("coffee", "sugar"), arrayOf("milk"), arrayOf("coffee", "milk"), arrayOf("bread"), arrayOf("bread", "sugar"), arrayOf("sugar"))
val FREQUENT_ITEM_SETS_1 = arrayOf(
arrayOf("milk", "sugar"),
arrayOf("coffee"),
arrayOf("coffee", "milk", "sugar"),
arrayOf("coffee", "sugar"),
arrayOf("milk"),
arrayOf("coffee", "milk"),
arrayOf("bread"),
arrayOf("bread", "sugar"),
arrayOf("sugar")
)

val SUPPORTS_1 = doubleArrayOf(0.5, 0.75, 0.5, 0.5, 0.75, 0.75, 0.5, 0.5, 0.75)

val FREQUENT_ITEM_SETS_2 = arrayOf(arrayOf("beer"), arrayOf("wine"), arrayOf("beer", "wine"), arrayOf("chips", "pizza"), arrayOf("beer", "chips", "wine"), arrayOf("chips"), arrayOf("beer", "chips"), arrayOf("chips", "wine"), arrayOf("pizza"), arrayOf("pizza", "wine"))
val FREQUENT_ITEM_SETS_2 = arrayOf(
arrayOf("beer"),
arrayOf("wine"),
arrayOf("beer", "wine"),
arrayOf("chips", "pizza"),
arrayOf("beer", "chips", "wine"),
arrayOf("chips"),
arrayOf("beer", "chips"),
arrayOf("chips", "wine"),
arrayOf("pizza"),
arrayOf("pizza", "wine")
)

val SUPPORTS_2 = doubleArrayOf(0.5, 0.5, 0.25, 0.25, 0.25, 0.75, 0.5, 0.25, 0.5, 0.25)

val FREQUENT_ITEM_SETS_3 = arrayOf(arrayOf("0", "3"), arrayOf("0", "1", "3"), arrayOf("3"), arrayOf("0", "4"), arrayOf("0", "1", "4"), arrayOf("4"), arrayOf("1", "4"), arrayOf("0"), arrayOf("0", "1"), arrayOf("1"))
val FREQUENT_ITEM_SETS_3 = arrayOf(
arrayOf("0", "3"),
arrayOf("0", "1", "3"),
arrayOf("3"),
arrayOf("0", "4"),
arrayOf("0", "1", "4"),
arrayOf("4"),
arrayOf("1", "4"),
arrayOf("0"),
arrayOf("0", "1"),
arrayOf("1")
)

val SUPPORTS_3 = doubleArrayOf(0.5, 0.5, 0.5, 0.75, 0.75, 0.75, 0.75, 1.0, 1.0, 1.0)

val FREQUENT_ITEM_SETS_4 = arrayOf(arrayOf("0", "3"), arrayOf("0", "1", "3"), arrayOf("3"), arrayOf("0", "4"), arrayOf("0", "1", "4"), arrayOf("4"), arrayOf("1", "4"), arrayOf("0"), arrayOf("0", "1"), arrayOf("1"))
val FREQUENT_ITEM_SETS_4 = arrayOf(
arrayOf("0", "3"),
arrayOf("0", "1", "3"),
arrayOf("3"),
arrayOf("0", "4"),
arrayOf("0", "1", "4"),
arrayOf("4"),
arrayOf("1", "4"),
arrayOf("0"),
arrayOf("0", "1"),
arrayOf("1")
)

val SUPPORTS_4 = doubleArrayOf(0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 1.0, 1.0, 1.0)

Expand All @@ -73,4 +116,4 @@ abstract class AbstractDataTest {

}

}
}
43 changes: 25 additions & 18 deletions src/test/java/de/mrapp/apriori/AprioriTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class AprioriTest : AbstractDataTest() {
val maxSupport = 0.8
val supportDelta = 0.2
val frequentItemSetCount = 0
val apriori = Apriori.Builder<NamedItem>(minSupport).maxSupport(maxSupport).supportDelta(supportDelta)
.frequentItemSetCount(frequentItemSetCount).create()
val apriori = Apriori.Builder<NamedItem>(minSupport).maxSupport(maxSupport)
.supportDelta(supportDelta).frequentItemSetCount(frequentItemSetCount).create()
val configuration = apriori.configuration
assertEquals(minSupport, configuration.minSupport)
assertEquals(maxSupport, configuration.maxSupport)
Expand All @@ -68,8 +68,8 @@ class AprioriTest : AbstractDataTest() {
val maxSupport = 0.8
val supportDelta = 0.2
val frequentItemSetCount = 2
val apriori = Apriori.Builder<NamedItem>(frequentItemSetCount).minSupport(minSupport).maxSupport(maxSupport)
.supportDelta(supportDelta).create()
val apriori = Apriori.Builder<NamedItem>(frequentItemSetCount).minSupport(minSupport)
.maxSupport(maxSupport).supportDelta(supportDelta).create()
val configuration = apriori.configuration
assertEquals(minSupport, configuration.minSupport)
assertEquals(maxSupport, configuration.maxSupport)
Expand All @@ -88,9 +88,10 @@ class AprioriTest : AbstractDataTest() {
val maxConfidence = 0.8
val confidenceDelta = 0.2
val ruleCount = 0
val apriori = Apriori.Builder<NamedItem>(frequentItemSetCount).generateRules(minConfidence).minSupport(minSupport)
.maxSupport(maxSupport).supportDelta(supportDelta).frequentItemSetCount(frequentItemSetCount)
.maxConfidence(maxConfidence).confidenceDelta(confidenceDelta).ruleCount(ruleCount).create()
val apriori = Apriori.Builder<NamedItem>(frequentItemSetCount).generateRules(minConfidence)
.minSupport(minSupport).maxSupport(maxSupport).supportDelta(supportDelta)
.frequentItemSetCount(frequentItemSetCount).maxConfidence(maxConfidence)
.confidenceDelta(confidenceDelta).ruleCount(ruleCount).create()
val configuration = apriori.configuration
assertEquals(minSupport, configuration.minSupport)
assertEquals(maxSupport, configuration.maxSupport)
Expand All @@ -113,10 +114,10 @@ class AprioriTest : AbstractDataTest() {
val maxConfidence = 0.8
val confidenceDelta = 0.2
val ruleCount = 2
val apriori = Apriori.Builder<NamedItem>(frequentItemSetCount).generateRules(ruleCount).minSupport(minSupport)
.maxSupport(maxSupport).supportDelta(supportDelta).frequentItemSetCount(frequentItemSetCount)
.minConfidence(minConfidence).maxConfidence(maxConfidence).confidenceDelta(confidenceDelta)
.create()
val apriori = Apriori.Builder<NamedItem>(frequentItemSetCount).generateRules(ruleCount)
.minSupport(minSupport).maxSupport(maxSupport).supportDelta(supportDelta)
.frequentItemSetCount(frequentItemSetCount).minConfidence(minConfidence)
.maxConfidence(maxConfidence).confidenceDelta(confidenceDelta).create()
val configuration = apriori.configuration
assertEquals(minSupport, configuration.minSupport)
assertEquals(maxSupport, configuration.maxSupport)
Expand Down Expand Up @@ -160,13 +161,16 @@ class AprioriTest : AbstractDataTest() {
map[itemSet1.hashCode()] = itemSet1
map[itemSet2.hashCode()] = itemSet2
val frequentItemSetMiner = object : FrequentItemSetMiner<NamedItem> {
override fun findFrequentItemSets(iterable: Iterable<Transaction<NamedItem>>, minSupport: Double) = map
override fun findFrequentItemSets(iterable: Iterable<Transaction<NamedItem>>,
minSupport: Double) = map
}
val associationRuleGenerator = object : AssociationRuleGenerator<NamedItem> {
override fun generateAssociationRules(frequentItemSets: Map<Int, ItemSet<NamedItem>>, minConfidence: Double) = throw RuntimeException()
override fun generateAssociationRules(frequentItemSets: Map<Int, ItemSet<NamedItem>>,
minConfidence: Double) = throw RuntimeException()
}
val frequentItemSetMinerTask = FrequentItemSetMinerTask(configuration, frequentItemSetMiner)
val associationRuleGeneratorTask = AssociationRuleGeneratorTask(configuration, associationRuleGenerator)
val associationRuleGeneratorTask = AssociationRuleGeneratorTask(configuration,
associationRuleGenerator)
val file = getInputFile(AbstractDataTest.INPUT_FILE_1)
val apriori = Apriori(configuration, frequentItemSetMinerTask, associationRuleGeneratorTask)
val output = apriori.execute(Iterable { DataIterator(file) })
Expand Down Expand Up @@ -201,13 +205,16 @@ class AprioriTest : AbstractDataTest() {
val associationRule = AssociationRule(ItemSet(), ItemSet<NamedItem>(), 0.5)
ruleSet.add(associationRule)
val frequentItemSetMiner = object : FrequentItemSetMiner<NamedItem> {
override fun findFrequentItemSets(iterable: Iterable<Transaction<NamedItem>>, minSupport: Double) = map
override fun findFrequentItemSets(iterable: Iterable<Transaction<NamedItem>>,
minSupport: Double) = map
}
val associationRuleGenerator = object : AssociationRuleGenerator<NamedItem> {
override fun generateAssociationRules(frequentItemSets: Map<Int, ItemSet<NamedItem>>, minConfidence: Double) = ruleSet
override fun generateAssociationRules(frequentItemSets: Map<Int, ItemSet<NamedItem>>,
minConfidence: Double) = ruleSet
}
val frequentItemSetMinerTask = FrequentItemSetMinerTask(configuration, frequentItemSetMiner)
val associationRuleGeneratorTask = AssociationRuleGeneratorTask(configuration, associationRuleGenerator)
val associationRuleGeneratorTask = AssociationRuleGeneratorTask(configuration,
associationRuleGenerator)
val file = getInputFile(AbstractDataTest.INPUT_FILE_1)
val apriori = Apriori(configuration, frequentItemSetMinerTask, associationRuleGeneratorTask)
val output = apriori.execute(Iterable { DataIterator(file) })
Expand All @@ -223,4 +230,4 @@ class AprioriTest : AbstractDataTest() {
assertEquals(item2, set.last().first())
}

}
}
5 changes: 3 additions & 2 deletions src/test/java/de/mrapp/apriori/AssociationRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class AssociationRuleTest {

@Test
fun testCoversWithArrayParameter() {
val items = arrayOf(NamedItem("a"), NamedItem("c"), NamedItem("d"), NamedItem("e"), NamedItem("f"))
val items = arrayOf(NamedItem("a"), NamedItem("c"), NamedItem("d"), NamedItem("e"),
NamedItem("f"))
val body = ItemSet<NamedItem>()
body.add(NamedItem("a"))
body.add(NamedItem("b"))
Expand Down Expand Up @@ -138,4 +139,4 @@ class AssociationRuleTest {
assertEquals("[a, b] -> [c, d]", associationRule.toString())
}

}
}
Loading

0 comments on commit 19bca33

Please sign in to comment.