Skip to content

Commit 4d770af

Browse files
author
muencseb
committedAug 15, 2019
Handle unknown user/product
1 parent e9fd0e6 commit 4d770af

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed
 

‎pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</execution>
3737
</executions>
3838
<configuration>
39-
<jvmTarget>1.8</jvmTarget>
39+
<jvmTarget>1.11</jvmTarget>
4040
</configuration>
4141
</plugin>
4242
<plugin>
@@ -59,8 +59,8 @@
5959
</execution>
6060
</executions>
6161
<configuration>
62-
<source>8</source>
63-
<target>8</target>
62+
<source>11</source>
63+
<target>11</target>
6464
</configuration>
6565
</plugin>
6666
</plugins>

‎src/main/java/ch/sebastianmue/javarank/recommendation/model/RecommendationMlModel.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.Collection;
1414
import java.util.List;
15+
import java.util.Optional;
1516
import java.util.concurrent.Callable;
1617
import java.util.concurrent.Executors;
1718
import java.util.concurrent.ScheduledExecutorService;
@@ -71,15 +72,22 @@ public boolean isModelReady() {
7172
*
7273
* @param userId
7374
* @param eventId
74-
* @return the prediction, which rating the used is likely to give
75+
* @return the prediction, which rating the used is likely to give. If either the user or the product is unknown, it will return empty
7576
* @throws ModelNotReadyException
7677
*/
77-
public Double getInterestPrediction(Integer userId, Integer eventId) throws ModelNotReadyException {
78+
public Optional<Double> getInterestPrediction(Integer userId, Integer eventId) throws ModelNotReadyException {
7879
if (!modelIsReady)
7980
throw new ModelNotReadyException();
8081
mutex.readLock().lock();
81-
Double prediction = model.predict(userId, eventId);
82-
mutex.readLock().unlock();
82+
Optional<Double> prediction;
83+
try {
84+
prediction = Optional.of(model.predict(userId, eventId));
85+
} catch (IllegalArgumentException e) {
86+
prediction = Optional.empty();
87+
}
88+
finally {
89+
mutex.readLock().unlock();
90+
}
8391
return prediction;
8492
}
8593

‎src/main/java/ch/sebastianmue/javarank/recommendation/service/RecommendationService.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package ch.sebastianmue.javarank.recommendation.service;
22

3+
import breeze.util.Opt;
34
import ch.sebastianmue.javarank.recommendation.data.InputRating;
45
import ch.sebastianmue.javarank.recommendation.exceptions.ModelNotReadyException;
56
import ch.sebastianmue.javarank.recommendation.model.RecommendationMlModel;
67

78
import java.util.Collection;
9+
import java.util.Optional;
810
import java.util.concurrent.Callable;
911

1012
public class RecommendationService {
@@ -23,7 +25,7 @@ public boolean isModelReady() {
2325
return recommendationMlModel.isModelReady();
2426
}
2527

26-
public Double getPrediction(Integer userId, Integer productId) throws ModelNotReadyException {
28+
public Optional<Double> getPrediction(Integer userId, Integer productId) throws ModelNotReadyException {
2729
return recommendationMlModel.getInterestPrediction(userId, productId);
2830
}
2931

‎src/test/java/ch/sebastianmue/javarank/service/RecommendationServiceRetrainTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ch.sebastianmue.javarank.recommendation.service.RecommendationService;
99

1010
import java.util.ArrayList;
11+
import java.util.Optional;
1112

1213
import static org.hamcrest.MatcherAssert.assertThat;
1314
import static org.hamcrest.Matchers.lessThan;
@@ -47,11 +48,11 @@ public static void close() {
4748

4849
@Test
4950
public void shouldDeliverANewPredictionAfterTraining() throws ModelNotReadyException {
50-
Double firstPrediction = recommendationService.getPrediction(2, 3);
51+
Optional<Double> firstPrediction = recommendationService.getPrediction(2, 3);
5152
while (recommendationService.getModelNumber() == 1) {
5253
}
53-
Double secondPrediction = recommendationService.getPrediction(2, 3);
54-
assertNotEquals(firstPrediction, secondPrediction);
55-
assertThat(firstPrediction, lessThan(secondPrediction));
54+
Optional<Double> secondPrediction = recommendationService.getPrediction(2, 3);
55+
assertNotEquals(firstPrediction.get(), secondPrediction.get());
56+
assertThat(firstPrediction.get(), lessThan(secondPrediction.get()));
5657
}
5758
}

‎src/test/java/ch/sebastianmue/javarank/service/RecommendationServiceTest.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import java.util.ArrayList;
1010

1111
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.is;
1213
import static org.hamcrest.Matchers.lessThan;
1314
import static org.junit.Assert.assertNotEquals;
15+
import static org.junit.Assert.assertTrue;
1416

1517
/**
16-
* The testing was keepen simple, as sparkml is already tested. The testing focus is on the new features.
18+
* The testing was keepn simple, as sparkml is already tested. The testing focus is on the new features.
1719
* This test just verifies, that sparkml was correctly called
1820
*/
1921
public class RecommendationServiceTest {
@@ -36,13 +38,22 @@ public static void initModel() {
3638

3739
@Test
3840
public void shouldNotForgetKnownRating() throws ModelNotReadyException {
39-
assertThat(recommendationService.getPrediction(1, 2), lessThan(recommendationService.getPrediction(1, 1)));
41+
assertThat(recommendationService.getPrediction(1, 2).get(), lessThan(recommendationService.getPrediction(1, 1).get()));
4042
assertNotEquals(0.0, recommendationService.getPrediction(1, 1));
4143
}
4244

4345
@Test
4446
public void shouldPredictFromOtherUser() throws ModelNotReadyException {
45-
assertThat(recommendationService.getPrediction(2, 2), lessThan(recommendationService.getPrediction(1, 3)));
47+
assertThat(recommendationService.getPrediction(2, 2).get(), lessThan(recommendationService.getPrediction(1, 3).get()));
48+
}
49+
@Test
50+
public void shouldHandleUnknownUser() throws ModelNotReadyException {
51+
assertTrue(recommendationService.getPrediction(3,2).isEmpty());
52+
}
53+
54+
@Test
55+
public void shouldHandleUnknownProduct() throws ModelNotReadyException {
56+
assertTrue(recommendationService.getPrediction(2,4).isEmpty());
4657
}
4758

4859
}

0 commit comments

Comments
 (0)
Failed to load comments.