1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.ml;
import com.yahoo.path.Path;
import com.yahoo.searchlib.rankingexpression.rule.Arguments;
import com.yahoo.searchlib.rankingexpression.rule.ConstantNode;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import java.util.Optional;
/**
* Encapsulates the arguments of a specific model output
*
* @author bratseth
*/
public class FeatureArguments {
private final Path path;
/** Optional arguments */
private final Optional<String> signature, output;
public FeatureArguments(Arguments arguments) {
this(Path.fromString(argument(0, arguments)),
optionalArgument(1, arguments),
optionalArgument(2, arguments));
}
private FeatureArguments(Path path, Optional<String> signature, Optional<String> output) {
this.path = path;
this.signature = signature;
this.output = output;
}
public Path path() { return path; }
public Optional<String> signature() { return signature; }
public Optional<String> output() { return output; }
public String toName() {
return (signature.isPresent() ? signature.get() : "") +
(output.isPresent() ? "." + output.get() : "");
}
private static String argument(int argumentIndex, Arguments arguments) {
if (argumentIndex >= arguments.expressions().size())
throw new IllegalArgumentException("Requires at least " + argumentIndex +
" arguments, but got just " + arguments.size());
return asString(arguments.expressions().get(argumentIndex));
}
private static Optional<String> optionalArgument(int argumentIndex, Arguments arguments) {
if (argumentIndex >= arguments.expressions().size())
return Optional.empty();
return Optional.of(asString(arguments.expressions().get(argumentIndex)));
}
private static String asString(ExpressionNode node) {
if ( ! (node instanceof ConstantNode))
throw new IllegalArgumentException("Expected a constant string as argument, but got '" + node);
return stripQuotes(node.toString());
}
private static String stripQuotes(String s) {
if ( ! isQuoteSign(s.codePointAt(0))) return s;
if ( ! isQuoteSign(s.codePointAt(s.length() - 1 )))
throw new IllegalArgumentException("argument [" + s + "] is missing endquote");
return s.substring(1, s.length()-1);
}
private static boolean isQuoteSign(int c) {
return c == '\'' || c == '"';
}
}
|