@@ -13,7 +13,6 @@ import org.evomaster.core.problem.rest.builder.RestActionBuilderV3
13
13
import org.evomaster.core.problem.rest.schema.RestSchema
14
14
import org.evomaster.core.EMConfig
15
15
import org.evomaster.core.problem.rest.classifier.GaussianOnlineClassifier
16
- import org.evomaster.core.problem.rest.data.RestPath
17
16
import org.evomaster.core.problem.rest.schema.OpenApiAccess
18
17
import org.evomaster.core.problem.rest.service.sampler.AbstractRestSampler
19
18
import org.evomaster.core.search.action.Action
@@ -71,7 +70,6 @@ class AIGaussianCheck : IntegrationTestRestBase() {
71
70
return result
72
71
}
73
72
74
-
75
73
fun runClassifierExample () {
76
74
val schema = OpenApiAccess .getOpenAPIFromLocation(" $baseUrlOfSut /v3/api-docs" )
77
75
val restSchema = RestSchema (schema)
@@ -84,88 +82,96 @@ class AIGaussianCheck : IntegrationTestRestBase() {
84
82
probRestExamples = 0.0
85
83
}
86
84
87
-
88
85
val options = RestActionBuilderV3 .Options (config)
89
86
val actionCluster = mutableMapOf<String , Action >()
90
87
RestActionBuilderV3 .addActionsFromSwagger(restSchema, actionCluster, options = options)
91
88
92
89
val actionList = actionCluster.values.filterIsInstance<RestCallAction >()
93
90
94
- val pathToDimension = mutableMapOf<RestPath , Int >()
91
+ val endpointToDimension = mutableMapOf<String , Int ? >()
95
92
for (action in actionList) {
96
- val path = action.path
97
- if (pathToDimension.containsKey(path)) continue
93
+ val name = action.getName()
98
94
99
- val dimension = action.parameters.count { p ->
95
+ val hasUnsupportedGene = action.parameters.any { p ->
100
96
val g = p.gene
101
- g is IntegerGene || g is DoubleGene || g is BooleanGene || g is EnumGene <* >
97
+ g !is IntegerGene && g !is DoubleGene && g !is BooleanGene && g !is EnumGene <* >
98
+ }
99
+
100
+ val dimension = if (hasUnsupportedGene) {
101
+ null
102
+ } else {
103
+ action.parameters.count { p ->
104
+ val g = p.gene
105
+ g is IntegerGene || g is DoubleGene || g is BooleanGene || g is EnumGene <* >
106
+ }
102
107
}
103
- pathToDimension[path] = dimension
104
- }
105
108
106
- val pathToClassifier = mutableMapOf<RestPath , GaussianOnlineClassifier >()
107
- for ((path, dimension) in pathToDimension) {
108
- val model = GaussianOnlineClassifier ()
109
- model.setDimension(dimension)
110
- pathToClassifier[path] = model
109
+ println (" Endpoint: $name , dimension: $dimension " )
110
+ endpointToDimension[name] = dimension
111
111
}
112
112
113
- println (" Classifiers initialized with their dimensions:" )
114
- for ((path, expected) in pathToDimension) {
115
- val classifier = pathToClassifier[path]!!
116
- println (" $path -> expected: $expected , actualDim: ${classifier.getDimension()} " )
113
+ /* *
114
+ * Initialize a classifier for each endpoint
115
+ * For an endpoint containing unsupported genes, the associated classifier is null
116
+ */
117
+ val endpointToClassifier = mutableMapOf<String , GaussianOnlineClassifier ?>()
118
+ for ((name, dimension) in endpointToDimension) {
119
+ if (dimension== null ){
120
+ endpointToClassifier[name] = null
121
+ }else {
122
+ val model = GaussianOnlineClassifier ()
123
+ model.setDimension(dimension)
124
+ endpointToClassifier[name] = model
125
+ }
117
126
}
118
127
119
- // val random = Randomness()
120
- // val sampler = injector.getInstance(AbstractRestSampler::class.java)
121
- // val template = random.choose(actionList)
122
- // val sampledAction = template.copy() as RestCallAction
123
- // sampledAction.doInitialize(random)
124
- //
125
- // // createIndividual doesn't work!
126
- // // val individual = createIndividual(listOf(sampledAction), SampleType.RANDOM)
127
- //
128
- // val individual = sampler.createIndividual(SampleType.RANDOM, listOf(sampledAction).toMutableList())
129
- // val mainAction = individual.seeMainExecutableActions()[0]
130
- // val response = executeRestCallAction(mainAction,"$baseUrlOfSut")
131
- // println("Response:\n${response.getStatusCode()}")
128
+ for ((name, expectedDimension) in endpointToDimension) {
129
+ println (" Expected dimension for $name : $expectedDimension " )
130
+ }
132
131
132
+ // Execute the procedure for a period of time
133
133
val random = Randomness ()
134
134
val sampler = injector.getInstance(AbstractRestSampler ::class .java)
135
135
var time = 1
136
- val timeLimit = 20
136
+ val timeLimit = 200
137
137
while (time <= timeLimit) {
138
138
val template = random.choose(actionList)
139
139
val sampledAction = template.copy() as RestCallAction
140
140
sampledAction.doInitialize(random)
141
141
142
- val path = sampledAction.path
143
- val dimension = pathToDimension[path] ? : error( " No dimension for path: $path " )
144
- val classifier = pathToClassifier[path] ? : error( " Expected classifier for path: $path " )
142
+ val name = sampledAction.getName()
143
+ val classifier = endpointToClassifier[name]
144
+ val dimension = endpointToDimension[name]
145
145
val geneValues = sampledAction.parameters.map { it.gene.getValueAsRawString() }
146
146
147
147
println (" *************************************************" )
148
148
println (" Time : $time " )
149
- println (" Path : $path " )
149
+ println (" Path : $name " )
150
+ println (" Classifier : ${if (classifier == null ) " null" else " GAUSSIAN" } " )
151
+ println (" Dimension : $dimension " )
150
152
println (" Input Genes : ${geneValues.joinToString(" , " )} " )
151
- println (" Input dim : ${classifier.getDimension()} " )
152
- println (" Expected Dim : $dimension " )
153
153
println (" Actual Genes : ${geneValues.size} " )
154
154
155
- // // executeRestCallAction is replaced with createIndividual to avoid override error
155
+ // executeRestCallAction is replaced with createIndividual to avoid override error
156
156
// val individual = createIndividual(listOf(sampledAction), SampleType.RANDOM)
157
157
val individual = sampler.createIndividual(SampleType .RANDOM , listOf (sampledAction).toMutableList())
158
158
val action = individual.seeMainExecutableActions()[0 ]
159
159
val result = executeRestCallAction(action," $baseUrlOfSut " )
160
- println (" Response:\n ${result.getStatusCode()} " )
160
+ println (" Response : ${result.getStatusCode()} " )
161
+
162
+ // Skip classification for the endpoints with unsupported genes
163
+ if (classifier== null ){
164
+ println (" No classification as the classifier is null, i.e., the endpoint contains unsupported genes" )
165
+ continue
166
+ }
161
167
162
168
// Update and classify
163
169
classifier.updateModel(action, result)
164
170
val classification = classifier.classify(action)
165
171
166
- println (" Probabilities : ${classification.probabilities } " )
167
- require(classification.probabilities .values.all { it in 0.0 .. 1. 0 }) {
168
- " All probabilities must be in [0,1] "
172
+ println (" Score : ${classification.scores } " )
173
+ require(classification.scores .values.all { it >= 0 }) {
174
+ " In Gaussian scores must be positive as they are likelihoods. "
169
175
}
170
176
171
177
val d200 = classifier.getDensity200()
@@ -180,7 +186,6 @@ class AIGaussianCheck : IntegrationTestRestBase() {
180
186
formatStats(" Density200" , d200.mean, d200.variance, d200.n)
181
187
formatStats(" Density400" , d400.mean, d400.variance, d400.n)
182
188
183
- println (" ----------------------------------" )
184
189
time++
185
190
}
186
191
}
0 commit comments