Search in sources :

Example 11 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class DeactivateObjectSkill method interpret.

@Override
public IntentInterpretation interpret(Intent intent, String language) {
    IntentInterpretation interpretation = new IntentInterpretation();
    Set<Item> matchedItems = findItems(intent);
    if (matchedItems == null || matchedItems.isEmpty()) {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("nothing_deactivated"));
        interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
    } else {
        interpretation.setMatchedItems(matchedItems);
        // filter out the items which can't receive an OFF command
        List<Item> filteredItems = matchedItems.stream().filter(i -> i.getAcceptedCommandTypes().contains(OnOffType.class)).collect(Collectors.toList());
        interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
        if (filteredItems.isEmpty()) {
            interpretation.setAnswer(answerFormatter.getRandomAnswer("nothing_deactivated"));
            interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
        } else if (filteredItems.size() == 1) {
            if (filteredItems.get(0).getState().equals(OnOffType.OFF)) {
                interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_already_off"));
            } else {
                eventPublisher.post(ItemEventFactory.createCommandEvent(filteredItems.get(0).getName(), OnOffType.OFF));
                interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_deactivated"));
            }
        } else {
            for (Item item : filteredItems) {
                eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), OnOffType.OFF));
            }
            interpretation.setAnswer(answerFormatter.getRandomAnswer("switches_deactivated", ImmutableMap.of("count", String.valueOf(filteredItems.size()))));
        }
    }
    return interpretation;
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) CardBuilder(org.openhab.ui.habot.card.CardBuilder) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) List(java.util.List) Skill(org.openhab.ui.habot.nlp.Skill) AbstractItemIntentInterpreter(org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) Item(org.eclipse.smarthome.core.items.Item) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation)

Example 12 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class HistoryLastChangesSkill method interpret.

@Override
public IntentInterpretation interpret(Intent intent, String language) {
    IntentInterpretation interpretation = new IntentInterpretation();
    Set<Item> matchedItems = findItems(intent);
    if (matchedItems == null || matchedItems.isEmpty()) {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
        interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
        return interpretation;
    }
    Set<String> tags = intent.getEntities().entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
    Card card = new Card("HbCard");
    card.addTags(tags);
    card.updateTimestamp();
    card.setEphemeral(true);
    card.setAddToDeckDenied(true);
    Component timeline = new Component("HbTimeline");
    if (matchedItems.size() == 1) {
        Item item = matchedItems.stream().findFirst().get();
        // TODO figure out a solution
        HistoricItem historicItem = PersistenceExtensions.previousState(item, false);
        if (historicItem == null) {
            interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
            interpretation.setHint(answerFormatter.getRandomAnswer("no_historical_data"));
            return interpretation;
        }
        card.setTitle(item.getLabel());
        card.setSubtitle(item.getName());
        DateFormat dateFormat = new SimpleDateFormat();
        Component pastTimelineEntry = new Component("HbTimelineEntry");
        pastTimelineEntry.addConfig("title", formatState(item, historicItem.getState()));
        pastTimelineEntry.addConfig("subtitle", dateFormat.format(historicItem.getTimestamp()));
        timeline.addComponent("main", pastTimelineEntry);
        Component nowTimelineEntry = new Component("HbTimelineEntry");
        nowTimelineEntry.addConfig("title", formatState(item, historicItem.getState()));
        nowTimelineEntry.addConfig("subtitle", dateFormat.format(new Date()));
        timeline.addComponent("main", nowTimelineEntry);
    } else {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("multiple_items_error"));
        return interpretation;
    }
    card.addComponent("main", timeline);
    this.cardRegistry.add(card);
    interpretation.setAnswer(answerFormatter.getRandomAnswer("info_found_simple"));
    interpretation.setCard(card);
    return interpretation;
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) CardRegistry(org.openhab.ui.habot.card.internal.CardRegistry) TransformationHelper(org.eclipse.smarthome.core.transform.TransformationHelper) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) Set(java.util.Set) PersistenceExtensions(org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Skill(org.openhab.ui.habot.nlp.Skill) Card(org.openhab.ui.habot.card.Card) AbstractItemIntentInterpreter(org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Component(org.openhab.ui.habot.card.Component) State(org.eclipse.smarthome.core.types.State) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) FrameworkUtil(org.osgi.framework.FrameworkUtil) DateFormat(java.text.DateFormat) Item(org.eclipse.smarthome.core.items.Item) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Component(org.openhab.ui.habot.card.Component) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Card(org.openhab.ui.habot.card.Card)

Example 13 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class CardBuilder method buildCard.

/**
 * Retrieves or build a card for the specified intent and matched items
 *
 * @param intent the intent including entities
 * @param matchedItems the matched items
 * @return the card (either retrieved or built)
 */
public Card buildCard(Intent intent, Collection<Item> matchedItems) {
    Set<String> tags = intent.getEntities().entrySet().stream().filter(e -> e.getKey().equals("object") || e.getKey().equals("location")).map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
    Collection<Card> cardsInRegistry = this.cardRegistry.getCardByTags(tags).stream().filter(c -> !c.isNotReuseableInChat() && !c.isEphemeral()).collect(Collectors.toList());
    if (cardsInRegistry.size() > 0) {
        // don't handle multiple cards, just return the first one
        Card existingCard = cardsInRegistry.iterator().next();
        existingCard.updateTimestamp();
        cardRegistry.update(existingCard);
        return existingCard;
    }
    Card card = new Card("HbCard");
    card.addTags(tags);
    card.setEphemeral(true);
    card.addConfig("bigger", true);
    card.updateTimestamp();
    if (matchedItems.size() == 1) {
        Item item = matchedItems.stream().findFirst().get();
        card.setTitle(item.getLabel());
        card.setSubtitle(item.getName());
        switch(item.getType()) {
            case CoreItemFactory.SWITCH:
                Component switchComponent = new Component("HbSwitch");
                switchComponent.addConfig("item", item.getName());
                card.addComponent("right", switchComponent);
                break;
            case CoreItemFactory.DIMMER:
                if (item.hasTag("habot:switchable")) {
                    Component dimmerSwitchComponent = new Component("HbSwitch");
                    dimmerSwitchComponent.addConfig("item", item.getName());
                    card.addComponent("right", dimmerSwitchComponent);
                } else if (!item.hasTag("habot:control:knob")) {
                    Component dimmerValueComponent = new Component("HbSingleItemValue");
                    dimmerValueComponent.addConfig("item", item.getName());
                    card.addComponent("right", dimmerValueComponent);
                }
                Component dimmerContainerComponent = new Component("HbContainer");
                dimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                if (item.hasTag("habot:control:knob")) {
                    Component knobComponent = new Component("HbKnob");
                    knobComponent.addConfig("item", item.getName());
                    knobComponent.addConfig("size", "200px");
                    knobComponent.addConfig("textSize", "2rem");
                    knobComponent.addConfig("color", "primary");
                    dimmerContainerComponent.addComponent("main", knobComponent);
                } else {
                    Component sliderComponent = new Component("HbSlider");
                    sliderComponent.addConfig("item", item.getName());
                    dimmerContainerComponent.addComponent("main", sliderComponent);
                }
                card.addComponent("main", dimmerContainerComponent);
                break;
            case CoreItemFactory.ROLLERSHUTTER:
                Component shutterValueComponent = new Component("HbSingleItemValue");
                shutterValueComponent.addConfig("item", item.getName());
                card.addComponent("right", shutterValueComponent);
                Component shutterContainerComponent = new Component("HbContainer");
                shutterContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                Component shutterControlComponent = new Component("HbShutterControl");
                shutterControlComponent.addConfig("item", item.getName());
                shutterControlComponent.addConfig("size", "lg");
                shutterControlComponent.addConfig("rounded", true);
                shutterControlComponent.addConfig("glossy", true);
                shutterControlComponent.addConfig("push", true);
                shutterControlComponent.addConfig("stopIcon", "close");
                shutterContainerComponent.addComponent("main", shutterControlComponent);
                card.addComponent("main", shutterContainerComponent);
                break;
            case CoreItemFactory.PLAYER:
                Component playerContainerComponent = new Component("HbContainer");
                playerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                Component playerComponent = new Component("HbPlayer");
                playerComponent.addConfig("item", item.getName());
                playerComponent.addConfig("size", "lg");
                playerContainerComponent.addComponent("main", playerComponent);
                card.addComponent("main", playerContainerComponent);
                break;
            case CoreItemFactory.COLOR:
                Component colorPickerComponent = new Component("HbColorPicker");
                colorPickerComponent.addConfig("item", item.getName());
                card.addComponent("right", colorPickerComponent);
                Component brightnessDimmerComponent = new Component("HbSwitch");
                brightnessDimmerComponent.addConfig("item", item.getName());
                card.addComponent("right", brightnessDimmerComponent);
                Component brightnessDimmerContainerComponent = new Component("HbContainer");
                brightnessDimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                Component brightnessSliderComponent = new Component("HbSlider");
                brightnessSliderComponent.addConfig("item", item.getName());
                brightnessDimmerContainerComponent.addComponent("main", brightnessSliderComponent);
                card.addComponent("main", brightnessDimmerContainerComponent);
                break;
            default:
                if (item.getType() == CoreItemFactory.IMAGE || item.getTags().stream().anyMatch(t -> t.startsWith("habot:image:sitemap:"))) {
                    /*
                         * If the item is an image (or a String with a tag indicating it's an image), build a
                         * HbImage component in the "media" slot
                         */
                    Component singleImageComponent = new Component("HbImage");
                    singleImageComponent.addConfig("item", item.getName());
                    card.addComponent("media", singleImageComponent);
                } else {
                    /*
                         * Try to get a formatted state to determine whether it's small enough to display
                         * in the "right" slot - otherwise add it to the "main" slot
                         */
                    String formattedState = formatState(item, item.getState());
                    Component singleItemComponent = new Component("HbSingleItemValue");
                    singleItemComponent.addConfig("item", item.getName());
                    if (formattedState.length() < 10) {
                        card.addComponent("right", singleItemComponent);
                    } else {
                        card.addComponent("main", singleItemComponent);
                    }
                }
                break;
        }
    } else {
        card.setTitle(getCardTitleFromGroupLabels(tags));
        // TODO: i18n
        card.setSubtitle(matchedItems.size() + " items");
        // TODO: detect images and build a HbCarousel with them - for webcams etc.
        Component list = new Component("HbList");
        for (Item item : matchedItems) {
            Component listItem = new Component("HbListItem");
            listItem.addConfig("item", item.getName());
            listItem.addConfig("label", item.getLabel());
            list.addComponent("items", listItem);
        }
        card.addComponent("list", list);
    }
    // Adds the (ephemeral) card to the registry anyways so it appears in the "recent cards" page
    cardRegistry.add(card);
    return card;
}
Also used : CardRegistry(org.openhab.ui.habot.card.internal.CardRegistry) TransformationHelper(org.eclipse.smarthome.core.transform.TransformationHelper) Collection(java.util.Collection) Set(java.util.Set) HistoryLastChangesSkill(org.openhab.ui.habot.nlp.internal.skill.HistoryLastChangesSkill) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StateDescription(org.eclipse.smarthome.core.types.StateDescription) State(org.eclipse.smarthome.core.types.State) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) FrameworkUtil(org.osgi.framework.FrameworkUtil) Item(org.eclipse.smarthome.core.items.Item)

Example 14 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class AbstractTrainerTest method interpret.

protected Intent interpret(String query) {
    System.out.println("----");
    System.out.println("\"" + query + "\"");
    System.out.println(new TreeMap<>(this.trainer.getScoreMap(query)).descendingMap().toString());
    Intent intent = this.trainer.interpret(query);
    System.out.println(intent.toString());
    return intent;
}
Also used : Intent(org.openhab.ui.habot.nlp.Intent)

Example 15 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class TrainerDeTest method testGetStatus.

@Test
public void testGetStatus() throws Exception {
    Intent actual;
    this.trainer = new IntentTrainer("de", skills);
    actual = interpret("Heizung in der Küche");
    assertEquals(Skills.GET_STATUS, actual.getName());
    assertEquals(2, actual.getEntities().size());
    assertEquals("heizung", actual.getEntities().get("object"));
    assertEquals("küche", actual.getEntities().get("location"));
    actual = interpret("wie hoch ist die Temperatur im Wohnzimmer");
    assertEquals(Skills.GET_STATUS, actual.getName());
    assertEquals(2, actual.getEntities().size());
    assertEquals("temperatur", actual.getEntities().get("object"));
    assertEquals("wohnzimmer", actual.getEntities().get("location"));
    actual = interpret("wie hoch ist die Temperatur im Keller");
    assertEquals(Skills.GET_STATUS, actual.getName());
    assertEquals(2, actual.getEntities().size());
    assertEquals("temperatur", actual.getEntities().get("object"));
    assertEquals("keller", actual.getEntities().get("location"));
}
Also used : IntentTrainer(org.openhab.ui.habot.nlp.internal.IntentTrainer) Intent(org.openhab.ui.habot.nlp.Intent) Test(org.junit.Test)

Aggregations

Intent (org.openhab.ui.habot.nlp.Intent)21 Test (org.junit.Test)11 IntentTrainer (org.openhab.ui.habot.nlp.internal.IntentTrainer)11 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Item (org.eclipse.smarthome.core.items.Item)7 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)7 Reference (org.osgi.service.component.annotations.Reference)7 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)5 Skill (org.openhab.ui.habot.nlp.Skill)5 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)4 AbstractItemIntentInterpreter (org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Collection (java.util.Collection)3 List (java.util.List)3 ItemEventFactory (org.eclipse.smarthome.core.items.events.ItemEventFactory)3 TransformationHelper (org.eclipse.smarthome.core.transform.TransformationHelper)3 State (org.eclipse.smarthome.core.types.State)3 StateDescription (org.eclipse.smarthome.core.types.StateDescription)3 CardBuilder (org.openhab.ui.habot.card.CardBuilder)3