Skip to content

Commit 0c674f8

Browse files
author
muencseb
committedSep 7, 2018
Test exception/retrain
1 parent 788f18c commit 0c674f8

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed
 

‎src/main/java/recommendation/model/RecommendationMlModel.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class RecommendationMlModel {
3434
private final RDDHelper rddHelper = new RDDHelper(javaSparkContext);
3535
private MatrixFactorizationModel model;
3636

37-
37+
private volatile Integer modelNumber = 0;
3838
private volatile boolean modelIsReady = false;
3939

4040
/**
@@ -117,6 +117,7 @@ private void trainModel(Collection<InputRating> ratings) {
117117
mutex.writeLock().lock();
118118
model = als.setRank(10).setIterations(10).run(ratingRDD);
119119
mutex.writeLock().unlock();
120+
modelNumber++;
120121
modelIsReady = true;
121122

122123
}
@@ -128,4 +129,8 @@ private List<Rating> createSparkRating(Collection<InputRating> inputRatings) {
128129
.map(ir -> new Rating(ir.getUserId(), ir.getProductId(), ir.getRating()))
129130
.collect(Collectors.toList());
130131
}
132+
133+
public Integer getModelNumber() {
134+
return modelNumber;
135+
}
131136
}

‎src/main/java/recommendation/service/RecommendationService.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@ public void close() {
3131
recommendationMlModel.close();
3232
}
3333

34-
34+
/**
35+
* returns the current version of the model (+1 for each new model)
36+
*/
37+
public Integer getModelNumber() {
38+
return recommendationMlModel.getModelNumber();
39+
}
3540
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package service;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import recommendation.data.InputRating;
7+
import recommendation.exceptions.ModelNotReadyException;
8+
import recommendation.service.RecommendationService;
9+
10+
import java.util.ArrayList;
11+
12+
public class RecommendationServiceModelNotReadyTest {
13+
14+
15+
private static RecommendationService recommendationService;
16+
17+
@BeforeClass
18+
public static void initModel() {
19+
ArrayList<InputRating> inputRatings = new ArrayList<>();
20+
inputRatings.add(new InputRating(1, 1, 1));
21+
recommendationService = new RecommendationService(inputRatings);
22+
}
23+
24+
@AfterClass
25+
public static void close() {
26+
recommendationService.close();
27+
}
28+
29+
@Test(expected = ModelNotReadyException.class)
30+
public void shouldNotForgetKnownRating() throws ModelNotReadyException {
31+
recommendationService.getPrediction(1, 1);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package service;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import recommendation.data.InputRating;
7+
import recommendation.exceptions.ModelNotReadyException;
8+
import recommendation.service.RecommendationService;
9+
10+
import java.util.ArrayList;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.lessThan;
14+
import static org.junit.Assert.assertNotEquals;
15+
16+
public class RecommendationServiceRetrainTest {
17+
18+
19+
private static RecommendationService recommendationService;
20+
21+
static boolean firstCall = true;
22+
23+
private static ArrayList<InputRating> dataProvider() {
24+
ArrayList<InputRating> inputRatings = new ArrayList<>();
25+
inputRatings.add(new InputRating(1, 1, 1));
26+
inputRatings.add(new InputRating(1, 2, 0));
27+
inputRatings.add(new InputRating(1, 3, 2));
28+
inputRatings.add(new InputRating(2, 1, 1));
29+
if (firstCall) {
30+
inputRatings.add(new InputRating(1, 3, 0));
31+
firstCall = false;
32+
}
33+
return inputRatings;
34+
}
35+
36+
@BeforeClass
37+
public static void initModel() {
38+
recommendationService = new RecommendationService(() -> dataProvider(), 10, 0);
39+
while (!recommendationService.isModelReady()) {
40+
}
41+
}
42+
43+
@AfterClass
44+
public static void close() {
45+
recommendationService.close();
46+
}
47+
48+
@Test
49+
public void shouldDeliverANewPredictionAfterTraining() throws ModelNotReadyException {
50+
Double firstPrediction = recommendationService.getPrediction(2, 3);
51+
while (recommendationService.getModelNumber() == 1) {
52+
}
53+
Double secondPrediction = recommendationService.getPrediction(2, 3);
54+
assertNotEquals(firstPrediction, secondPrediction);
55+
assertThat(firstPrediction, lessThan(secondPrediction));
56+
}
57+
}

0 commit comments

Comments
 (0)
Failed to load comments.