Class RulesService

java.lang.Object
org.apache.camel.builder.BuilderSupport
org.apache.camel.builder.RouteBuilder
org.openremote.manager.rules.RulesService
All Implemented Interfaces:
org.apache.camel.builder.ModelRoutesBuilder, org.apache.camel.CamelContextAware, org.apache.camel.Ordered, org.apache.camel.RoutesBuilder, org.apache.camel.spi.HasCamelContext, org.apache.camel.spi.ResourceAware, org.openremote.model.ContainerService

public class RulesService extends org.apache.camel.builder.RouteBuilder implements org.openremote.model.ContainerService
Manages RulesEngines for stored Rulesets and processes asset attribute updates.

If an updated attribute doesn't have meta MetaItemType.RULE_STATE is false and the attribute has an AgentLink meta, this implementation of AttributeEventInterceptor converts the update message to an AttributeInfo fact. This service keeps the facts and thus the state of rule facts are in sync with the asset state changes that occur. If an asset attribute value changes, the AttributeInfo in the rules engines will be updated to reflect the change.

Each asset attribute update is processed in the following order:

  1. Global Rulesets
  2. Realm Rulesets
  3. Asset Rulesets (in hierarchical order from oldest ancestor down)
Processing order of rulesets with the same scope or same parent is not guaranteed.
  • Field Details

    • PRIORITY

      public static final int PRIORITY
      See Also:
    • OR_RULE_EVENT_EXPIRES

      public static final String OR_RULE_EVENT_EXPIRES
      See Also:
    • OR_RULE_EVENT_EXPIRES_DEFAULT

      public static final String OR_RULE_EVENT_EXPIRES_DEFAULT
      See Also:
    • OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS

      public static final String OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS
      This value defines the periodic firing of the rules engines, and therefore has an impact on system load. If a temporary fact has a shorter expiration time, it's not guaranteed to be removed within that time. Any time-based operation, such as matching temporary facts in a sliding time window, must be designed with this margin in mind.
      See Also:
    • OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS_DEFAULT

      public static final int OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS_DEFAULT
      See Also:
    • OR_RULES_QUICK_FIRE_MILLIS

      public static final String OR_RULES_QUICK_FIRE_MILLIS
      See Also:
    • OR_RULES_QUICK_FIRE_MILLIS_DEFAULT

      public static final int OR_RULES_QUICK_FIRE_MILLIS_DEFAULT
      See Also:
    • globalEngine

      protected final AtomicReference<RulesEngine<org.openremote.model.rules.GlobalRuleset>> globalEngine
    • realmEngines

      protected final Map<String,RulesEngine<org.openremote.model.rules.RealmRuleset>> realmEngines
    • assetEngines

      protected final Map<String,RulesEngine<org.openremote.model.rules.AssetRuleset>> assetEngines
    • ENGINE_LOCK

      protected static final Object ENGINE_LOCK
    • geofenceAssetAdapters

      protected List<GeofenceAssetAdapter> geofenceAssetAdapters
    • timerService

      protected org.openremote.container.timer.TimerService timerService
    • executorService

      protected ExecutorService executorService
    • scheduledExecutorService

      protected ScheduledExecutorService scheduledExecutorService
    • persistenceService

      protected org.openremote.container.persistence.PersistenceService persistenceService
    • rulesetStorageService

      protected RulesetStorageService rulesetStorageService
    • identityService

      protected ManagerIdentityService identityService
    • assetStorageService

      protected AssetStorageService assetStorageService
    • notificationService

      protected NotificationService notificationService
    • webhookService

      protected WebhookService webhookService
    • alarmService

      protected AlarmService alarmService
    • assetProcessingService

      protected AssetProcessingService assetProcessingService
    • assetDatapointService

      protected AssetDatapointService assetDatapointService
    • assetPredictedDatapointService

      protected AssetPredictedDatapointService assetPredictedDatapointService
    • clientEventService

      protected ClientEventService clientEventService
    • gatewayService

      protected GatewayService gatewayService
    • realms

      protected org.openremote.model.security.Realm[] realms
    • locationPredicateRulesConsumer

      protected AssetLocationPredicateProcessor locationPredicateRulesConsumer
    • engineAssetLocationPredicateMap

      protected final Map<RulesEngine<?>,List<RulesEngine.AssetLocationPredicates>> engineAssetLocationPredicateMap
    • assetsWithModifiedLocationPredicates

      protected final Set<String> assetsWithModifiedLocationPredicates
    • attributeEvents

      protected final Set<org.openremote.model.attribute.AttributeEvent> attributeEvents
    • preInitAttributeEvents

      protected final Set<org.openremote.model.attribute.AttributeEvent> preInitAttributeEvents
    • defaultEventExpiresMillis

      protected long defaultEventExpiresMillis
    • tempFactExpirationMillis

      protected long tempFactExpirationMillis
    • quickFireMillis

      protected long quickFireMillis
    • initDone

      protected boolean initDone
    • startDone

      protected boolean startDone
    • rulesFiringTimer

      protected io.micrometer.core.instrument.Timer rulesFiringTimer
  • Constructor Details

    • RulesService

      public RulesService()
  • Method Details

    • getPriority

      public int getPriority()
      Specified by:
      getPriority in interface org.openremote.model.ContainerService
    • init

      public void init(org.openremote.model.Container container) throws Exception
      Specified by:
      init in interface org.openremote.model.ContainerService
      Throws:
      Exception
    • configure

      public void configure() throws Exception
      Specified by:
      configure in class org.apache.camel.builder.RouteBuilder
      Throws:
      Exception
    • start

      public void start(org.openremote.model.Container container) throws Exception
      Specified by:
      start in interface org.openremote.model.ContainerService
      Throws:
      Exception
    • stop

      public void stop(org.openremote.model.Container container) throws Exception
      Specified by:
      stop in interface org.openremote.model.ContainerService
      Throws:
      Exception
    • isRuleState

      protected static boolean isRuleState(org.openremote.model.value.MetaHolder metaHolder)
    • onAttributeEvent

      public void onAttributeEvent(org.openremote.model.attribute.AttributeEvent event) throws AssetProcessingException
      React to events that have been committed to the DB and inject them into the appropriate RulesEngines.
      Throws:
      AssetProcessingException
    • doProcessAttributeUpdate

      protected void doProcessAttributeUpdate(org.openremote.model.attribute.AttributeEvent attributeEvent)
    • isRulesetKnown

      public boolean isRulesetKnown(org.openremote.model.rules.Ruleset ruleset)
    • getAssetGeofences

      public org.openremote.model.rules.geofence.GeofenceDefinition[] getAssetGeofences(String assetId)
    • processRealmChange

      protected void processRealmChange(org.openremote.model.security.Realm realm, org.openremote.model.PersistenceEvent.Cause cause)
    • processAssetChange

      protected void processAssetChange(org.openremote.model.asset.Asset<?> asset, org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> persistenceEvent)
    • processRulesetChange

      protected void processRulesetChange(org.openremote.model.rules.Ruleset ruleset, org.openremote.model.PersistenceEvent.Cause cause)
    • deployGlobalRuleset

      protected RulesEngine<org.openremote.model.rules.GlobalRuleset> deployGlobalRuleset(org.openremote.model.rules.GlobalRuleset ruleset)
      Deploy the ruleset into the global engine creating the engine if necessary.
    • undeployGlobalRuleset

      protected void undeployGlobalRuleset(org.openremote.model.rules.GlobalRuleset ruleset)
    • deployRealmRuleset

      protected RulesEngine<org.openremote.model.rules.RealmRuleset> deployRealmRuleset(org.openremote.model.rules.RealmRuleset ruleset)
    • undeployRealmRuleset

      protected void undeployRealmRuleset(org.openremote.model.rules.RealmRuleset ruleset)
    • deployAssetRulesets

      protected Stream<RulesEngine<org.openremote.model.rules.AssetRuleset>> deployAssetRulesets(List<org.openremote.model.rules.AssetRuleset> rulesets)
    • deployAssetRuleset

      protected RulesEngine<org.openremote.model.rules.AssetRuleset> deployAssetRuleset(org.openremote.model.rules.AssetRuleset ruleset)
    • undeployAssetRuleset

      protected void undeployAssetRuleset(org.openremote.model.rules.AssetRuleset ruleset)
    • insertOrUpdateAttributeInfo

      protected void insertOrUpdateAttributeInfo(org.openremote.model.attribute.AttributeEvent attributeEvent)
    • retractAttributeInfo

      protected void retractAttributeInfo(org.openremote.model.attribute.AttributeEvent attributeEvent)
    • getAssetStatesInScope

      protected List<org.openremote.model.attribute.AttributeInfo> getAssetStatesInScope(String assetId)
    • getEnginesInScope

      protected List<RulesEngine<?>> getEnginesInScope(String realm, String[] assetPath)
    • findRuleStateAttributes

      protected Stream<org.openremote.model.util.Pair<org.openremote.model.asset.Asset<?>,Stream<org.openremote.model.attribute.Attribute<?>>>> findRuleStateAttributes()
    • onEngineLocationRulesChanged

      protected void onEngineLocationRulesChanged(RulesEngine<?> rulesEngine, List<RulesEngine.AssetLocationPredicates> newEngineAssetStateLocationPredicates)
      Called when an engine's rules change identifying assets with location attributes that also have LocationAttributePredicate in the rules. The job here is to identify the asset's (via AttributeInfo) that have modified LocationAttributePredicates and to notify the GeofenceAssetAdapters.
    • processModifiedGeofences

      protected void processModifiedGeofences()
    • getRulesetDeployment

      protected Optional<RulesetDeployment> getRulesetDeployment(Long rulesetId)
    • fireDeploymentsWithPredictedDataForAsset

      public void fireDeploymentsWithPredictedDataForAsset(String assetId)
      Trigger rules engines which have the MetaItemDescriptor Ruleset.TRIGGER_ON_PREDICTED_DATA and contain AttributeInfo of the specified asset id. Use this when PredictedDatapoints has changed for this asset.
      Parameters:
      assetId - of the asset which has new predicated data points.
    • toString

      public String toString()
      Overrides:
      toString in class org.apache.camel.builder.RouteBuilder