From 5e5e8101bf07e267aba53da8dbaf604f310d184a Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Fri, 3 May 2019 11:02:09 +0200 Subject: [PATCH 1/4] added ShoppingCart(wip) --- .gitignore | 3 ++- .../spm/baudas/analysis/ShoppingCart.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java diff --git a/.gitignore b/.gitignore index f85356d..21399b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.iml .idea -.project \ No newline at end of file +.project +target \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java new file mode 100644 index 0000000..f75c786 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java @@ -0,0 +1,26 @@ +package de.hsel.spm.baudas.analysis; + + +import org.jetbrains.annotations.Nullable; +import weka.core.Instances; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ShoppingCart implements Analysis>>{ + + + @Nullable + @Override + public Instances load(File file) { + return null; + } + + @Override + public Map> getResult() { + Map> result = new HashMap<>(); + return result; + } +} From 221c2aea920e8d2f0c976c680756aa47253376fa Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Sun, 5 May 2019 10:38:12 +0200 Subject: [PATCH 2/4] +Shopping cart analysis #SPM-23: add work 2h development --- .../spm/baudas/analysis/ShoppingCart.java | 83 ++++++++++++++++--- .../spm/baudas/analysis/ShoppingCartTest.java | 51 ++++++++++++ 2 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java index f75c786..7153b1e 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java @@ -1,26 +1,87 @@ package de.hsel.spm.baudas.analysis; - -import org.jetbrains.annotations.Nullable; +import weka.associations.Apriori; +import weka.associations.AssociationRule; +import weka.associations.Item; import weka.core.Instances; +import weka.filters.Filter; +import weka.filters.unsupervised.attribute.NumericCleaner; +import weka.filters.unsupervised.attribute.NumericToNominal; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -public class ShoppingCart implements Analysis>>{ +/** + * The shopping-cart Analysis. + * + * @author Julian Hinxlage + * @version 0.1 + */ +public class ShoppingCart implements Analysis, List>> { - - @Nullable - @Override - public Instances load(File file) { - return null; - } + private Instances instances; @Override - public Map> getResult() { - Map> result = new HashMap<>(); + public Map, List> getResult() { + Map, List> result = new HashMap<>(); + + Apriori model = new Apriori(); + + for (int i = 0; i < 11; i++) { + instances.deleteAttributeAt(0); + } + + //alle artikel anzahlen auf 0 oder 1 setzen + NumericCleaner nc = new NumericCleaner(); + nc.setMaxThreshold(1.0); + nc.setMaxDefault(1.0); + try { + nc.setInputFormat(instances); + instances = Filter.useFilter(instances, nc); + } catch (Exception e) { + e.printStackTrace(); + } + + //zahl werte zu nomial werten umwandeln + NumericToNominal num2nom = new NumericToNominal(); + num2nom.setAttributeIndices("first-last"); + try { + num2nom.setInputFormat(instances); + instances = Filter.useFilter(instances, num2nom); + } catch (Exception e) { + e.printStackTrace(); + } + + model.setTreatZeroAsMissing(true); + try { + model.buildAssociations(instances); + } catch (Exception e) { + e.printStackTrace(); + } + + for(int i = 0; i < model.getAssociationRules().getRules().size();i++){ + AssociationRule rule = model.getAssociationRules().getRules().get(i); + + List l1 = new ArrayList<>(); + for(Item item : rule.getPremise()){ + l1.add(item.getAttribute().name()); + } + List l2 = new ArrayList<>(); + for(Item item : rule.getConsequence()){ + l2.add(item.getAttribute().name()); + } + + result.put(l1,l2); + } + + return result; } + + public ShoppingCart(File file) { + instances = load(file); + } } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java new file mode 100644 index 0000000..e9e161d --- /dev/null +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java @@ -0,0 +1,51 @@ +package de.hsel.spm.baudas.analysis; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URL; +import java.util.List; +import java.util.Map; + +/** + * Test class for shopping-cart analysis. + * + * @author Julian Hinxlage + * @version 0.1 + */ +class ShoppingCartTest { + + @Test + void test100(){ + URL url = getClass().getClassLoader().getResource("kd100.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + Map, List> result = cart.getResult(); + + System.out.println(result); + } + + @Test + void test1000(){ + URL url = getClass().getClassLoader().getResource("kd1000.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + Map, List> result = cart.getResult(); + + System.out.println(result); + } + + @Test + void test10000(){ + URL url = getClass().getClassLoader().getResource("kd10000.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + Map, List> result = cart.getResult(); + + System.out.println(result); + } + +} From 20f13467c628f3529f7b278446f73804f65a636a Mon Sep 17 00:00:00 2001 From: joethei Date: Mon, 13 May 2019 16:49:17 +0200 Subject: [PATCH 3/4] + test cases #SPM-23: add work 20m testing --- .../spm/baudas/analysis/ShoppingCart.java | 7 +++++- .../spm/baudas/analysis/ShoppingCartTest.java | 23 +++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java index 7153b1e..3f01c76 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java @@ -23,10 +23,15 @@ import java.util.Map; public class ShoppingCart implements Analysis, List>> { private Instances instances; + private Map, List> result; @Override public Map, List> getResult() { - Map, List> result = new HashMap<>(); + if(result != null) { + return result; + } + + result = new HashMap<>(); Apriori model = new Apriori(); diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java index e9e161d..fa28aa9 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java @@ -4,13 +4,17 @@ import org.junit.jupiter.api.Test; import java.io.File; import java.net.URL; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * Test class for shopping-cart analysis. * - * @author Julian Hinxlage + * @author Johannes Theiner * @version 0.1 */ class ShoppingCartTest { @@ -23,7 +27,10 @@ class ShoppingCartTest { ShoppingCart cart = new ShoppingCart(new File(url.getFile())); Map, List> result = cart.getResult(); - System.out.println(result); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get(Collections.singletonList("Leuchten"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Schliesstechnik", "Renovierung"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); } @Test @@ -34,7 +41,11 @@ class ShoppingCartTest { ShoppingCart cart = new ShoppingCart(new File(url.getFile())); Map, List> result = cart.getResult(); - System.out.println(result); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get(Arrays.asList("Renovierung", "Leuchten"))); + assertEquals(Collections.singletonList("Sanitaermaterial"), result.get(Arrays.asList("Fliesen", "Armaturen"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Renovierung"))); + } @Test @@ -45,7 +56,11 @@ class ShoppingCartTest { ShoppingCart cart = new ShoppingCart(new File(url.getFile())); Map, List> result = cart.getResult(); - System.out.println(result); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); + assertEquals(Collections.singletonList("Sanitaermaterial"), result.get(Arrays.asList("Eisenwaren", "Fliesen", "Armaturen"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get(Arrays.asList("Renovierung", "Leuchten"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); + } } From dd2e5b768ccefe022ab156b87295cfbce26ba9a9 Mon Sep 17 00:00:00 2001 From: joethei Date: Mon, 13 May 2019 18:13:12 +0200 Subject: [PATCH 4/4] + timeouts for testcases #SPM-23: add work 30m testing --- .../spm/baudas/analysis/ShoppingCartTest.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java index fa28aa9..ea22ac1 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java @@ -4,12 +4,15 @@ import org.junit.jupiter.api.Test; import java.io.File; import java.net.URL; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeout; /** * Test class for shopping-cart analysis. @@ -25,12 +28,14 @@ class ShoppingCartTest { assert url != null; ShoppingCart cart = new ShoppingCart(new File(url.getFile())); - Map, List> result = cart.getResult(); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik"))); - assertEquals(Collections.singletonList("Elektromaterial"), result.get(Collections.singletonList("Leuchten"))); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Schliesstechnik", "Renovierung"))); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); + AtomicReference, List>> result = new AtomicReference<>(); + assertTimeout(Duration.ofSeconds(4), () -> result.set(cart.getResult())); + + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get().get(Collections.singletonList("Leuchten"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Schliesstechnik", "Renovierung"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); } @Test @@ -39,12 +44,14 @@ class ShoppingCartTest { assert url != null; ShoppingCart cart = new ShoppingCart(new File(url.getFile())); - Map, List> result = cart.getResult(); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); - assertEquals(Collections.singletonList("Elektromaterial"), result.get(Arrays.asList("Renovierung", "Leuchten"))); - assertEquals(Collections.singletonList("Sanitaermaterial"), result.get(Arrays.asList("Fliesen", "Armaturen"))); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Renovierung"))); + AtomicReference, List>> result = new AtomicReference<>(); + assertTimeout(Duration.ofSeconds(4), () -> result.set(cart.getResult())); + + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get().get(Arrays.asList("Renovierung", "Leuchten"))); + assertEquals(Collections.singletonList("Sanitaermaterial"), result.get().get(Arrays.asList("Fliesen", "Armaturen"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Renovierung"))); } @@ -54,12 +61,14 @@ class ShoppingCartTest { assert url != null; ShoppingCart cart = new ShoppingCart(new File(url.getFile())); - Map, List> result = cart.getResult(); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); - assertEquals(Collections.singletonList("Sanitaermaterial"), result.get(Arrays.asList("Eisenwaren", "Fliesen", "Armaturen"))); - assertEquals(Collections.singletonList("Elektromaterial"), result.get(Arrays.asList("Renovierung", "Leuchten"))); - assertEquals(Collections.singletonList("Eisenwaren"), result.get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); + AtomicReference, List>> result = new AtomicReference<>(); + assertTimeout(Duration.ofSeconds(5), () -> result.set(cart.getResult())); + + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); + assertEquals(Collections.singletonList("Sanitaermaterial"), result.get().get(Arrays.asList("Eisenwaren", "Fliesen", "Armaturen"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get().get(Arrays.asList("Renovierung", "Leuchten"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); }