<commit_before>/*******************************************************************************
 * Copyright (c) 2014 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.rules;

import com.whizzosoftware.hobson.api.event.EventTopics;
import com.whizzosoftware.hobson.api.event.HobsonEvent;
import com.whizzosoftware.hobson.api.event.PresenceUpdateEvent;
import com.whizzosoftware.hobson.api.event.VariableUpdateNotificationEvent;
import com.whizzosoftware.hobson.api.plugin.AbstractHobsonPlugin;
import com.whizzosoftware.hobson.api.plugin.PluginStatus;
import com.whizzosoftware.hobson.api.property.PropertyContainer;
import com.whizzosoftware.hobson.api.property.PropertyContainerClassContext;
import com.whizzosoftware.hobson.api.property.TypedProperty;
import com.whizzosoftware.hobson.api.task.TaskProvider;
import com.whizzosoftware.hobson.rules.jruleengine.JRETaskProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

/**
 * A plugin that provides event-based tasks using the JRuleEngine library.
 *
 * @author <NAME>
 */
public class RulesPlugin extends AbstractHobsonPlugin {
    private final static Logger logger = LoggerFactory.getLogger(RulesPlugin.class);

    public static final String CONDITION_CLASS_TURN_ON = "turnOn";
    public static final String CONDITION_CLASS_TURN_OFF = "turnOff";

    private JRETaskProvider taskProvider;

    public RulesPlugin(String pluginId) {
        super(pluginId);
    }

    @Override
    public String getName() {
        return "Rules Plugin";
    }

    @Override
    public void onStartup(PropertyContainer config) {
        taskProvider = new JRETaskProvider(getContext());
        taskProvider.setTaskManager(getTaskManager());
        taskProvider.setRulesFile(getDataFile("rules.json"));

        // publish condition classes
        publishConditionClass(PropertyContainerClassContext.create(getContext(), CONDITION_CLASS_TURN_ON), "A device or sensor turns on", Collections.singletonList(new TypedProperty("device", "Device", "The device to monitor", TypedProperty.Type.DEVICE)));
        publishConditionClass(PropertyContainerClassContext.create(getContext(), CONDITION_CLASS_TURN_OFF), "A device or sensor turns off", Collections.singletonList(new TypedProperty("device", "Device", "The device to monitor", TypedProperty.Type.DEVICE)));

        // set the plugin status to running
        setStatus(PluginStatus.running());
        logger.debug("Rules plugin has started");
    }

    @Override
    public String[] getEventTopics() {
        return new String[] {EventTopics.PRESENCE_TOPIC};
    }

    @Override
    public long getRefreshInterval() {
        return 0;
    }

    @Override
    public TaskProvider getTaskProvider() {
        return taskProvider;
    }

    @Override
    public void onRefresh() {}

    @Override
    protected TypedProperty[] createSupportedProperties() {
        return null;<commit_msg>Added constraints to condition classes that are triggered by a device variable change.<commit_after>/*******************************************************************************
 * Copyright (c) 2014 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.rules;

import com.whizzosoftware.hobson.api.event.EventTopics;
import com.whizzosoftware.hobson.api.event.HobsonEvent;
import com.whizzosoftware.hobson.api.event.PresenceUpdateEvent;
import com.whizzosoftware.hobson.api.event.VariableUpdateNotificationEvent;
import com.whizzosoftware.hobson.api.plugin.AbstractHobsonPlugin;
import com.whizzosoftware.hobson.api.plugin.PluginStatus;
import com.whizzosoftware.hobson.api.property.PropertyContainer;
import com.whizzosoftware.hobson.api.property.PropertyContainerClassContext;
import com.whizzosoftware.hobson.api.property.TypedProperty;
import com.whizzosoftware.hobson.api.property.TypedPropertyConstraint;
import com.whizzosoftware.hobson.api.task.TaskProvider;
import com.whizzosoftware.hobson.rules.jruleengine.JRETaskProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

/**
 * A plugin that provides event-based tasks using the JRuleEngine library.
 *
 * @author <NAME>
 */
public class RulesPlugin extends AbstractHobsonPlugin {
    private final static Logger logger = LoggerFactory.getLogger(RulesPlugin.class);

    public static final String CONDITION_CLASS_TURN_ON = "turnOn";
    public static final String CONDITION_CLASS_TURN_OFF = "turnOff";

    private JRETaskProvider taskProvider;

    public RulesPlugin(String pluginId) {
        super(pluginId);
    }

    @Override
    public String getName() {
        return "Rules Plugin";
    }

    @Override
    public void onStartup(PropertyContainer config) {
        taskProvider = new JRETaskProvider(getContext());
        taskProvider.setTaskManager(getTaskManager());
        taskProvider.setRulesFile(getDataFile("rules.json"));

        // publish condition classes
        publishConditionClass(PropertyContainerClassContext.create(getContext(), CONDITION_CLASS_TURN_ON), "A device or sensor turns on", Collections.singletonList(new TypedProperty("device", "Device", "The device to monitor", TypedProperty.Type.DEVICE, Collections.singletonMap(TypedPropertyConstraint.deviceVariable, "on"))));
        publishConditionClass(PropertyContainerClassContext.create(getContext(), CONDITION_CLASS_TURN_OFF), "A device or sensor turns off", Collections.singletonList(new TypedProperty("device", "Device", "The device to monitor", TypedProperty.Type.DEVICE, Collections.singletonMap(TypedPropertyConstraint.deviceVariable, "on"))));

        // set the plugin status to running
        setStatus(PluginStatus.running());
        logger.debug("Rules plugin has started");
    }

    @Override
    public String[] getEventTopics() {
        return new String[] {EventTopics.PRESENCE_TOPIC};
    }

    @Override
    public long getRefreshInterval() {
        return 0;
    }

    @Override
    public TaskProvider getTaskProvider() {
        return taskProvider;
    }

    @Override
    public void onRefresh() {}

    @Override
    protected TypedProperty[] createSupportedProperties() {
        return null;
{'fraction_non_alphanumeric': 0.13349941383352873, 'fraction_numerical': 0.003223915592028136, 'mean_word_length': 4.749789385004212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4152808', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1797, 'n_words': 421}