Skip to content

Commit 7c47a99

Browse files
authored
Merge pull request #2486 from github/starcke/candidate-selection
Tweak candidate selection
2 parents 805d712 + 15c2a86 commit 7c47a99

File tree

2 files changed

+132
-14
lines changed

2 files changed

+132
-14
lines changed

extensions/ql-vscode/src/data-extensions-editor/auto-model.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
} from "./auto-model-api";
99
import type { UsageSnippetsBySignature } from "./auto-model-usages-query";
1010

11+
// Soft limit on the number of candidates to send to the model.
12+
// Note that the model may return fewer than this number of candidates.
13+
const candidateLimit = 20;
14+
// Soft limit on the number of samples to send to the model.
15+
const sampleLimit = 100;
16+
1117
export function createAutoModelRequest(
1218
language: string,
1319
externalApiUsages: ExternalApiUsage[],
@@ -40,11 +46,15 @@ export function createAutoModelRequest(
4046
? 0
4147
: externalApiUsage.methodParameters.split(",").length;
4248

49+
const candidates: Method[] = [];
50+
const samples: Method[] = [];
4351
for (
44-
let argumentIndex = 0;
52+
let argumentIndex = -1; // Start at -1 which means `this` as in `this.method()`
4553
argumentIndex < numberOfArguments;
4654
argumentIndex++
4755
) {
56+
const argumentInput: string =
57+
argumentIndex === -1 ? "Argument[this]" : `Argument[${argumentIndex}]`;
4858
const method: Method = {
4959
package: externalApiUsage.packageName,
5060
type: externalApiUsage.typeName,
@@ -54,21 +64,35 @@ export function createAutoModelRequest(
5464
modeledMethod.type === "none"
5565
? undefined
5666
: toMethodClassification(modeledMethod),
57-
usages: usagesForMethod.slice(0, 10),
58-
input: `Argument[${argumentIndex}]`,
67+
usages: usagesForMethod.slice(0, 6), // At most 6 usages per argument
68+
input: argumentInput,
5969
};
6070

71+
// A method that is supported is modeled outside of the model file, so it is not a candidate.
72+
// We also do not want it as a sample because we do not know the classification.
73+
if (modeledMethod.type === "none" && externalApiUsage.supported) {
74+
continue;
75+
}
76+
77+
// Candidates are methods that are not currently modeled
6178
if (modeledMethod.type === "none") {
62-
request.candidates.push(method);
79+
candidates.push(method);
6380
} else {
64-
request.samples.push(method);
81+
samples.push(method);
6582
}
6683
}
84+
// If there is room for at least one candidate, add all candidates.
85+
// This ensures that we send all arguments for a method together.
86+
// NOTE: this might go above the candidate limit, but that's okay.
87+
if (request.candidates.length < candidateLimit) {
88+
request.candidates.push(...candidates);
89+
}
90+
// Same for samples
91+
if (request.samples.length < sampleLimit) {
92+
request.samples.push(...samples);
93+
}
6794
}
6895

69-
request.candidates = request.candidates.slice(0, 20);
70-
request.samples = request.samples.slice(0, 100);
71-
7296
return request;
7397
}
7498

extensions/ql-vscode/test/unit-tests/data-extensions-editor/auto-model.test.ts

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("createAutoModelRequest", () => {
3939
typeName: "Connection",
4040
methodName: "createQuery",
4141
methodParameters: "(String)",
42-
supported: true,
42+
supported: false,
4343
usages: [
4444
{
4545
label: "createQuery(...)",
@@ -69,7 +69,7 @@ describe("createAutoModelRequest", () => {
6969
typeName: "Query",
7070
methodName: "executeScalar",
7171
methodParameters: "(Class)",
72-
supported: true,
72+
supported: false,
7373
usages: [
7474
{
7575
label: "executeScalar(...)",
@@ -99,7 +99,7 @@ describe("createAutoModelRequest", () => {
9999
typeName: "Sql2o",
100100
methodName: "open",
101101
methodParameters: "()",
102-
supported: true,
102+
supported: false,
103103
usages: [
104104
{
105105
label: "open(...)",
@@ -129,7 +129,7 @@ describe("createAutoModelRequest", () => {
129129
typeName: "PrintStream",
130130
methodName: "println",
131131
methodParameters: "(String)",
132-
supported: true,
132+
supported: false,
133133
usages: [
134134
{
135135
label: "println(...)",
@@ -149,7 +149,7 @@ describe("createAutoModelRequest", () => {
149149
typeName: "Sql2o",
150150
methodName: "Sql2o",
151151
methodParameters: "(String,String,String)",
152-
supported: true,
152+
supported: false,
153153
usages: [
154154
{
155155
label: "new Sql2o(...)",
@@ -169,7 +169,7 @@ describe("createAutoModelRequest", () => {
169169
typeName: "Sql2o",
170170
methodName: "Sql2o",
171171
methodParameters: "(String)",
172-
supported: true,
172+
supported: false,
173173
usages: [
174174
{
175175
label: "new Sql2o(...)",
@@ -183,6 +183,26 @@ describe("createAutoModelRequest", () => {
183183
},
184184
],
185185
},
186+
{
187+
signature: "org.test.MyClass#test()",
188+
packageName: "org.test",
189+
typeName: "MyClass",
190+
methodName: "test",
191+
methodParameters: "()",
192+
supported: true,
193+
usages: [
194+
{
195+
label: "abc.test(...)",
196+
url: {
197+
uri: "file:/home/runner/work/test/Test.java",
198+
startLine: 23,
199+
startColumn: 23,
200+
endLine: 23,
201+
endColumn: 36,
202+
},
203+
},
204+
],
205+
},
186206
];
187207

188208
const modeledMethods: Record<string, ModeledMethod> = {
@@ -235,6 +255,32 @@ describe("createAutoModelRequest", () => {
235255
).toEqual({
236256
language: "java",
237257
samples: [
258+
{
259+
package: "org.sql2o",
260+
type: "Sql2o",
261+
name: "open",
262+
signature: "()",
263+
classification: {
264+
type: "CLASSIFICATION_TYPE_NEUTRAL",
265+
kind: "",
266+
explanation: "",
267+
},
268+
usages: usages["org.sql2o.Sql2o#open()"],
269+
input: "Argument[this]",
270+
},
271+
{
272+
package: "org.sql2o",
273+
type: "Sql2o",
274+
name: "Sql2o",
275+
signature: "(String)",
276+
classification: {
277+
type: "CLASSIFICATION_TYPE_SINK",
278+
kind: "jndi-injection",
279+
explanation: "",
280+
},
281+
usages: usages["org.sql2o.Sql2o#Sql2o(String)"],
282+
input: "Argument[this]",
283+
},
238284
{
239285
package: "org.sql2o",
240286
type: "Sql2o",
@@ -250,6 +296,15 @@ describe("createAutoModelRequest", () => {
250296
},
251297
],
252298
candidates: [
299+
{
300+
package: "org.sql2o",
301+
type: "Connection",
302+
name: "createQuery",
303+
signature: "(String)",
304+
usages: usages["org.sql2o.Connection#createQuery(String)"],
305+
input: "Argument[this]",
306+
classification: undefined,
307+
},
253308
{
254309
package: "org.sql2o",
255310
type: "Connection",
@@ -259,6 +314,15 @@ describe("createAutoModelRequest", () => {
259314
input: "Argument[0]",
260315
classification: undefined,
261316
},
317+
{
318+
package: "org.sql2o",
319+
type: "Query",
320+
name: "executeScalar",
321+
signature: "(Class)",
322+
usages: usages["org.sql2o.Query#executeScalar(Class)"],
323+
input: "Argument[this]",
324+
classification: undefined,
325+
},
262326
{
263327
package: "org.sql2o",
264328
type: "Query",
@@ -268,6 +332,18 @@ describe("createAutoModelRequest", () => {
268332
input: "Argument[0]",
269333
classification: undefined,
270334
},
335+
{
336+
package: "org.springframework.boot",
337+
type: "SpringApplication",
338+
name: "run",
339+
signature: "(Class,String[])",
340+
usages:
341+
usages[
342+
"org.springframework.boot.SpringApplication#run(Class,String[])"
343+
],
344+
input: "Argument[this]",
345+
classification: undefined,
346+
},
271347
{
272348
package: "org.springframework.boot",
273349
type: "SpringApplication",
@@ -292,6 +368,15 @@ describe("createAutoModelRequest", () => {
292368
input: "Argument[1]",
293369
classification: undefined,
294370
},
371+
{
372+
package: "java.io",
373+
type: "PrintStream",
374+
name: "println",
375+
signature: "(String)",
376+
usages: usages["java.io.PrintStream#println(String)"],
377+
input: "Argument[this]",
378+
classification: undefined,
379+
},
295380
{
296381
package: "java.io",
297382
type: "PrintStream",
@@ -301,6 +386,15 @@ describe("createAutoModelRequest", () => {
301386
input: "Argument[0]",
302387
classification: undefined,
303388
},
389+
{
390+
package: "org.sql2o",
391+
type: "Sql2o",
392+
name: "Sql2o",
393+
signature: "(String,String,String)",
394+
usages: usages["org.sql2o.Sql2o#Sql2o(String,String,String)"],
395+
input: "Argument[this]",
396+
classification: undefined,
397+
},
304398
{
305399
package: "org.sql2o",
306400
type: "Sql2o",

0 commit comments

Comments
 (0)