blob: 3969054c1d1c0760744304e98bb5c8274549d64d (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.search;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.io.IOUtils;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.prelude.semantics.RuleBase;
import com.yahoo.prelude.semantics.RuleImporter;
import com.yahoo.prelude.semantics.parser.ParseException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
/**
* Reads the semantic rules from the application package by delegating to RuleConfigDeriver.
*
* @author bratseth
*/
// TODO: Move into SemanticRules
public class SemanticRuleBuilder {
/** Build the set of semantic rules for an application package */
public SemanticRules build(ApplicationPackage applicationPackage) {
List<NamedReader> ruleBaseFiles = null;
try {
ruleBaseFiles = applicationPackage.getFiles(ApplicationPackage.RULES_DIR, "sr");
return new SemanticRules(ruleBaseFiles.stream().map(this::toRuleBaseConfigView).collect(Collectors.toList()));
}
finally {
NamedReader.closeAll(ruleBaseFiles);
}
}
private SemanticRules.RuleBase toRuleBaseConfigView(NamedReader reader) {
try {
String ruleBaseString = IOUtils.readAll(reader.getReader());
boolean isDefault = ruleBaseString.contains("@default");
return new SemanticRules.RuleBase(toName(reader.getName()), isDefault, ruleBaseString);
}
catch (IOException e) {
throw new IllegalArgumentException("Could not load rules bases", e);
}
}
private String toName(String fileName) {
String shortName = new File(fileName).getName();
return shortName.substring(0, shortName.length()-".sr".length());
}
}
|