Skip to content

Commit 81b0500

Browse files
committed
include builtin QueryFunctions in plugins response.
1 parent 2f64625 commit 81b0500

8 files changed

Lines changed: 161 additions & 28 deletions

File tree

engine/src/main/java/nl/inl/blacklab/plugins/Plugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void configure(Map<String, Object> config, File pluginDir) {
3737
this.pluginDir = pluginDir;
3838
}
3939

40-
private PluginDescriptor descriptor = new PluginDescriptor();
40+
private final PluginDescriptor descriptor = new PluginDescriptor();
4141

4242
public PluginParam addParam(PluginParam spec) {
4343
return descriptor.addParam(spec);

engine/src/main/java/nl/inl/blacklab/plugins/PluginManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static synchronized <T extends Plugin> void getUnloaded(String scriptName) {
316316
throw new PluginException("Groovy plugin overrides getId(): script file is " + unloaded.scriptFile +
317317
", getId() returns " + plugin.getId());
318318
}
319-
register(plugin, unloaded.pluginConfig, scriptName);
319+
register(plugin, unloaded.pluginConfig, scriptName, true);
320320
} else {
321321
logger.warn("Groovy script " + unloaded.scriptFile + " does not evaluate to a Plugin instance; ignoring.");
322322
}
@@ -325,11 +325,11 @@ static synchronized <T extends Plugin> void getUnloaded(String scriptName) {
325325
}
326326
}
327327

328-
private static void register(Plugin plugin, BLConfigPlugins pluginConfig, String scriptFileName) {
328+
private static void register(Plugin plugin, BLConfigPlugins pluginConfig, String scriptFileName, boolean registerClassName) {
329329
ensureInitialized();
330330
for (Class<? extends Plugin> pluginClass: pluginTypes) {
331331
if (pluginClass.isInstance(plugin)) {
332-
type(pluginClass).register(pluginClass.cast(plugin), pluginConfig, scriptFileName);
332+
type(pluginClass).register(pluginClass.cast(plugin), pluginConfig, scriptFileName, registerClassName);
333333
}
334334
}
335335
}

engine/src/main/java/nl/inl/blacklab/plugins/PluginsOfType.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void loadClasses(BLConfigPlugins pluginConfig, ClassLoader cl) {
6464
plugin = it.next();
6565
if (plugin.getId() == null)
6666
plugin.setId(plugin.getClass().getSimpleName());
67-
register(plugin, pluginConfig, null);
67+
register(plugin, pluginConfig, null, true);
6868
} catch (ServiceConfigurationError e) {
6969
logger.error("Plugin failed to load: " + e.getMessage(), e);
7070
} catch (Exception e) {
@@ -75,7 +75,7 @@ private void loadClasses(BLConfigPlugins pluginConfig, ClassLoader cl) {
7575
}
7676
}
7777

78-
void register(Plugin plugin, BLConfigPlugins configs, String alternateId) {
78+
void register(Plugin plugin, BLConfigPlugins configs, String alternateId, boolean registerClassName) {
7979
String id = plugin.getId();
8080
if (id != null && !PLUGIN_ID_PATTERN.matcher(id).matches()) {
8181
logger.warn("Plugin id " + id + " (class " + plugin.getClass().getName() +
@@ -97,14 +97,19 @@ void register(Plugin plugin, BLConfigPlugins configs, String alternateId) {
9797
add(alternateId, data); // e.g. groovy script name without extension
9898
if (!plugin.localId().equals(id)) // localId is e.g. function name, so "abs" for QueryFunctionAbs
9999
add(plugin.localId(), data);
100-
if (!plugin.getClass().isAnonymousClass()) {
100+
if (registerClassName && !plugin.getClass().isAnonymousClass()) {
101101
if (!plugin.getClass().getName().contains("$")) // skip e.g. "Script1$1"
102102
add(plugin.getClass().getName(), data);
103103
if (!plugin.getClass().getSimpleName().matches("\\d+")) // skip e.g. "1"
104104
add(plugin.getClass().getSimpleName(), data);
105105
}
106106
}
107107

108+
public void add(String id, Plugin plugin) {
109+
PluginData<T> data = new PluginData<>((T) plugin, null, null);
110+
add(id, data);
111+
}
112+
108113
private void add(String id, PluginData<T> data) {
109114
if (BlackLab.isPluginAllowed(data.getPlugin())) {
110115
synchronized (this) {

engine/src/main/java/nl/inl/blacklab/plugins/QueryFunction.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import nl.inl.blacklab.plugins.param.PString;
1616
import nl.inl.blacklab.plugins.param.PluginParam;
1717
import nl.inl.blacklab.search.QueryExecutionContext;
18+
import nl.inl.blacklab.search.extensions.QueryFunctionLambda;
1819
import nl.inl.blacklab.search.indexmetadata.RelationUtil;
1920
import nl.inl.blacklab.search.lucene.SpanQueryAnyToken;
2021
import nl.inl.blacklab.search.lucene.SpanQueryDefaultValue;
@@ -30,7 +31,10 @@ public abstract class QueryFunction extends Plugin implements TextPattern.EvalRe
3031
/** Default value for a query parameter that means "any span" (<code><'.*' //></code>) */
3132
public static final String VALUE_ANY_SPAN = "_ANY_SPAN_";
3233

33-
/** Function name */
34+
/** Function name
35+
* <p>
36+
* (this is the plugin's localId; id must be globally unique so cannot just be e.g. "abs"
37+
* or name collision would be likely) */
3438
private final String name;
3539

3640
/** Parameter types */
@@ -59,6 +63,12 @@ public QueryFunction(String name, List<PluginParam> argTypes,
5963
this.relationsFunction = relationsFunction;
6064
}
6165

66+
@Override
67+
public String getId() {
68+
String id = super.getId();
69+
return id == null ? (this instanceof QueryFunctionLambda ? name : getClass().getSimpleName()) : id;
70+
}
71+
6272
@Override
6373
public String localId() {
6474
return name;

engine/src/main/java/nl/inl/blacklab/plugins/param/PluginDescriptor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@
1111
*/
1212
public final class PluginDescriptor {
1313

14-
public static final PluginDescriptor NO_PARAMETERS = new PluginDescriptor(Map.of());
15-
16-
public static PluginDescriptor of(PluginParam... params) {
17-
Map<String, PluginParam> paramsMap = new HashMap<>();
18-
for (PluginParam param: params) {
19-
paramsMap.put(param.name(), param);
20-
}
21-
return new PluginDescriptor(paramsMap);
22-
}
23-
2414
private final Map<String, PluginParam> params;
2515

2616
private boolean frozen;

engine/src/main/java/nl/inl/blacklab/search/extensions/QueryExtensions.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package nl.inl.blacklab.search.extensions;
22

33
import java.util.Collections;
4-
import java.util.HashMap;
54
import java.util.List;
6-
import java.util.Map;
75

86
import nl.inl.blacklab.plugins.PluginManager;
97
import nl.inl.blacklab.plugins.QueryFunction;
@@ -17,9 +15,6 @@ public class QueryExtensions {
1715
private QueryExtensions() {
1816
}
1917

20-
/** Registry of extension functions by name */
21-
private static final Map<String, QueryFunction> functions = new HashMap<>();
22-
2318
static {
2419
register(XFDebug.class); // Debug functions such as _ident(), _FI1(), _FI2()
2520
register(XFRelations.class); // Functions for working with relations
@@ -62,7 +57,7 @@ private static void register(String name, List<PluginParam> argTypes, List<Objec
6257
}
6358

6459
public static void register(QueryFunction func) {
65-
functions.put(func.getName(), func);
60+
PluginManager.type(QueryFunction.class).add(func.getName(), func);
6661
}
6762

6863
public static void registerRelationsFunction(String name, List<PluginParam> argTypes, List<Object> defaultValues,
@@ -92,10 +87,7 @@ public static boolean exists(String name) {
9287
}
9388

9489
private static QueryFunction getInternal(String name) {
95-
QueryFunction queryFunction = functions.get(name);
96-
return queryFunction == null ?
97-
PluginManager.type(QueryFunction.class).getIfExists(name).orElse(null) :
98-
queryFunction;
90+
return PluginManager.type(QueryFunction.class).getIfExists(name).orElse(null);
9991
}
10092

10193
}

test/data/saved-responses/info/Server info page.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,74 @@
253253
{
254254
"id": "QueryFunctionUnion",
255255
"localId": "union"
256+
},
257+
{
258+
"id": "_adjust",
259+
"localId": "_adjust"
260+
},
261+
{
262+
"id": "_edge",
263+
"localId": "_edge"
264+
},
265+
{
266+
"id": "_fimatch",
267+
"localId": "_fimatch"
268+
},
269+
{
270+
"id": "_FI1",
271+
"localId": "_FI1"
272+
},
273+
{
274+
"id": "_FI2",
275+
"localId": "_FI2"
276+
},
277+
{
278+
"id": "_ident",
279+
"localId": "_ident"
280+
},
281+
{
282+
"id": "_indoc",
283+
"localId": "_indoc"
284+
},
285+
{
286+
"id": "_lenfilter",
287+
"localId": "_lenfilter"
288+
},
289+
{
290+
"id": "_posfilter",
291+
"localId": "_posfilter"
292+
},
293+
{
294+
"id": "rel",
295+
"localId": "rel"
296+
},
297+
{
298+
"id": "rspan",
299+
"localId": "rspan"
300+
},
301+
{
302+
"id": "cspan",
303+
"localId": "cspan"
304+
},
305+
{
306+
"id": "rfield",
307+
"localId": "rfield"
308+
},
309+
{
310+
"id": "rcapture",
311+
"localId": "rcapture"
312+
},
313+
{
314+
"id": "punctBefore",
315+
"localId": "punctBefore"
316+
},
317+
{
318+
"id": "punctAfter",
319+
"localId": "punctAfter"
320+
},
321+
{
322+
"id": "with-spans",
323+
"localId": "with-spans"
256324
}
257325
],
258326
"QueryParserProvider": [

test/data/saved-responses/info/server.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,74 @@
253253
{
254254
"id": "QueryFunctionUnion",
255255
"localId": "union"
256+
},
257+
{
258+
"id": "_adjust",
259+
"localId": "_adjust"
260+
},
261+
{
262+
"id": "_edge",
263+
"localId": "_edge"
264+
},
265+
{
266+
"id": "_fimatch",
267+
"localId": "_fimatch"
268+
},
269+
{
270+
"id": "_FI1",
271+
"localId": "_FI1"
272+
},
273+
{
274+
"id": "_FI2",
275+
"localId": "_FI2"
276+
},
277+
{
278+
"id": "_ident",
279+
"localId": "_ident"
280+
},
281+
{
282+
"id": "_indoc",
283+
"localId": "_indoc"
284+
},
285+
{
286+
"id": "_lenfilter",
287+
"localId": "_lenfilter"
288+
},
289+
{
290+
"id": "_posfilter",
291+
"localId": "_posfilter"
292+
},
293+
{
294+
"id": "rel",
295+
"localId": "rel"
296+
},
297+
{
298+
"id": "rspan",
299+
"localId": "rspan"
300+
},
301+
{
302+
"id": "cspan",
303+
"localId": "cspan"
304+
},
305+
{
306+
"id": "rfield",
307+
"localId": "rfield"
308+
},
309+
{
310+
"id": "rcapture",
311+
"localId": "rcapture"
312+
},
313+
{
314+
"id": "punctBefore",
315+
"localId": "punctBefore"
316+
},
317+
{
318+
"id": "punctAfter",
319+
"localId": "punctAfter"
320+
},
321+
{
322+
"id": "with-spans",
323+
"localId": "with-spans"
256324
}
257325
],
258326
"QueryParserProvider": [

0 commit comments

Comments
 (0)