Skip to content

Commit

Permalink
[voice] Support custom rules on item metadata (#3897)
Browse files Browse the repository at this point in the history
* [voice] Support custom rules on item metadata
* fix isTemplate functionality and test
* fix filter location based for non labeled rules

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
  • Loading branch information
GiviMAD committed Dec 6, 2023
1 parent 76b10ac commit 075bcea
Show file tree
Hide file tree
Showing 4 changed files with 613 additions and 171 deletions.
Expand Up @@ -64,9 +64,9 @@
@NonNullByDefault
@Component(service = HumanLanguageInterpreter.class)
public class StandardInterpreter extends AbstractRuleBasedInterpreter {
public static final String VOICE_SYSTEM_NAMESPACE = "voice-system";
private Logger logger = LoggerFactory.getLogger(StandardInterpreter.class);
private final ItemRegistry itemRegistry;
private final String metadataNamespace = "voice-system";
private final MetadataRegistry metadataRegistry;

@Activate
Expand Down Expand Up @@ -164,7 +164,7 @@ public void createRules(@Nullable Locale locale) {
);
/* Item description commands */
addRules(Locale.ENGLISH, createItemDescriptionRules( //
(allowedItemNames, labeledCmd) -> restrictedItemRule(allowedItemNames, //
(allowedItems, labeledCmd) -> restrictedItemRule(allowedItems, //
seq(alt("set", "change"), opt(the)), /* item */ seq(to, labeledCmd)//
), //
Locale.ENGLISH).toArray(Rule[]::new));
Expand Down Expand Up @@ -233,7 +233,7 @@ public void createRules(@Nullable Locale locale) {
/* Item description commands */

addRules(Locale.GERMAN, createItemDescriptionRules( //
(allowedItemNames, labeledCmd) -> restrictedItemRule(allowedItemNames, //
(allowedItems, labeledCmd) -> restrictedItemRule(allowedItems, //
seq(schalte, denDieDas), /* item */ seq(opt("auf"), labeledCmd)//
), //
Locale.GERMAN).toArray(Rule[]::new));
Expand Down Expand Up @@ -301,7 +301,7 @@ public void createRules(@Nullable Locale locale) {
/* Item description commands */

addRules(Locale.FRENCH, createItemDescriptionRules( //
(allowedItemNames, labeledCmd) -> restrictedItemRule(allowedItemNames, //
(allowedItems, labeledCmd) -> restrictedItemRule(allowedItems, //
seq("mets", lela), /* item */ seq(poursurdude, lela, labeledCmd)//
), //
Locale.FRENCH).toArray(Rule[]::new));
Expand Down Expand Up @@ -351,11 +351,18 @@ public void createRules(@Nullable Locale locale) {

/* NextPreviousType */

itemRule(cambiar,
itemRule(seq(cambiar, opt(articulo)),
/* item */ seq(opt("a"),
alt(cmd("siguiente", NextPreviousType.NEXT),
cmd("anterior", NextPreviousType.PREVIOUS)))),

itemRule(
seq(opt(poner),
alt(cmd("siguiente", NextPreviousType.NEXT),
cmd("anterior", NextPreviousType.PREVIOUS)),
"en"),
opt(articulo) /* item */ ),

/* PlayPauseType */

itemRule(seq(cmd(alt("continuar", "continúa", "reanudar", "reanuda", "play"), PlayPauseType.PLAY),
Expand Down Expand Up @@ -392,7 +399,7 @@ public void createRules(@Nullable Locale locale) {
/* Item description commands */

addRules(localeES, createItemDescriptionRules( //
(allowedItemNames, labeledCmd) -> restrictedItemRule(allowedItemNames, //
(allowedItems, labeledCmd) -> restrictedItemRule(allowedItems, //
seq(alt(cambiar, poner), opt(articulo)), /* item */ seq(preposicion, labeledCmd)//
), //
localeES).toArray(Rule[]::new));
Expand All @@ -409,12 +416,12 @@ public String getLabel(@Nullable Locale locale) {
return "Built-in Interpreter";
}

private List<Rule> createItemDescriptionRules(CreateItemDescriptionRule creator, @Nullable Locale locale) {
private List<Rule> createItemDescriptionRules(CreateItemDescriptionRule creator, Locale locale) {
// Map different item state/command labels with theirs values by item
HashMap<String, HashMap<Item, String>> options = new HashMap<>();
List<Rule> customRules = new ArrayList<>();
for (var item : itemRegistry.getItems()) {
customRules.addAll(createItemCustomRules(item));
customRules.addAll(createItemMetadataRules(locale, item));
var stateDesc = item.getStateDescription(locale);
if (stateDesc != null) {
stateDesc.getOptions().forEach(op -> {
Expand Down Expand Up @@ -445,34 +452,28 @@ private List<Rule> createItemDescriptionRules(CreateItemDescriptionRule creator,
.map(entry -> {
String label = entry.getKey();
Map<Item, String> commandByItem = entry.getValue();
List<String> itemNames = commandByItem.keySet().stream().map(Item::getName).toList();
String[] labelParts = Arrays.stream(label.split("\\s")).filter(p -> !p.isBlank())
.toArray(String[]::new);
Expression labeledCmd = cmd(seq((Object[]) labelParts),
new ItemStateCommandSupplier(label, commandByItem));
return creator.itemDescriptionRule(itemNames, labeledCmd);
return creator.itemDescriptionRule(commandByItem.keySet(), labeledCmd);
})) //
.collect(Collectors.toList());
}

private List<Rule> createItemCustomRules(Item item) {
var interpreterMetadata = metadataRegistry.get(new MetadataKey(metadataNamespace, item.getName()));
private List<Rule> createItemMetadataRules(Locale locale, Item item) {
var interpreterMetadata = metadataRegistry.get(new MetadataKey(VOICE_SYSTEM_NAMESPACE, item.getName()));
if (interpreterMetadata == null) {
return List.of();
}
List<Rule> list = new ArrayList<>();
for (String s : interpreterMetadata.getValue().split("\n")) {
String line = s.trim();
Rule rule = this.parseItemCustomRule(item, line);
if (rule != null) {
list.add(rule);
}
}
return list;
return Arrays.stream(interpreterMetadata.getValue().split("\n")) //
.map(line -> this.parseItemCustomRules(locale, item, line.trim(), interpreterMetadata)) //
.flatMap(List::stream) //
.collect(Collectors.toList());
}

private interface CreateItemDescriptionRule {
Rule itemDescriptionRule(List<String> allowedItemNames, Expression labeledCmd);
Rule itemDescriptionRule(Set<Item> allowedItemNames, Expression labeledCmd);
}

private record ItemStateCommandSupplier(String label,
Expand Down

0 comments on commit 075bcea

Please sign in to comment.