Package org.openremote.manager.rules
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:
- Global Rulesets
- Realm Rulesets
- Asset Rulesets (in hierarchical order from oldest ancestor down)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AlarmServiceprotected AssetDatapointServiceprotected final Map<String, RulesEngine<org.openremote.model.rules.AssetRuleset>> protected AssetPredictedDatapointServiceprotected AssetProcessingServiceprotected AssetStorageServiceprotected final Set<org.openremote.model.attribute.AttributeEvent> protected ClientEventServiceprotected longprotected static final Objectprotected final Map<RulesEngine<?>, List<RulesEngine.AssetLocationPredicates>> protected ExecutorServiceprotected GatewayServiceprotected List<GeofenceAssetAdapter> protected final AtomicReference<RulesEngine<org.openremote.model.rules.GlobalRuleset>> protected ManagerIdentityServiceprotected booleanprotected AssetLocationPredicateProcessorprotected NotificationServicestatic final Stringstatic final Stringstatic final StringThis value defines the periodic firing of the rules engines, and therefore has an impact on system load.static final intstatic final Stringstatic final intprotected org.openremote.container.persistence.PersistenceServiceprotected final Set<org.openremote.model.attribute.AttributeEvent> static final intprotected longprotected final Map<String, RulesEngine<org.openremote.model.rules.RealmRuleset>> protected org.openremote.model.security.Realm[]protected RulesetStorageServiceprotected io.micrometer.core.instrument.Timerprotected ScheduledExecutorServiceprotected booleanprotected longprotected org.openremote.container.timer.TimerServiceprotected WebhookServiceFields inherited from class org.apache.camel.builder.RouteBuilder
logFields inherited from interface org.openremote.model.ContainerService
DEFAULT_PRIORITY, HIGH_PRIORITY, LOW_PRIORITY, MED_PRIORITYFields inherited from interface org.apache.camel.Ordered
HIGHEST, LOWEST -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected RulesEngine<org.openremote.model.rules.AssetRuleset> deployAssetRuleset(org.openremote.model.rules.AssetRuleset ruleset) protected Stream<RulesEngine<org.openremote.model.rules.AssetRuleset>> deployAssetRulesets(List<org.openremote.model.rules.AssetRuleset> rulesets) 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.protected RulesEngine<org.openremote.model.rules.RealmRuleset> deployRealmRuleset(org.openremote.model.rules.RealmRuleset ruleset) protected voiddoProcessAttributeUpdate(org.openremote.model.attribute.AttributeEvent attributeEvent) protected Stream<org.openremote.model.util.Pair<org.openremote.model.asset.Asset<?>, Stream<org.openremote.model.attribute.Attribute<?>>>> voidTrigger rules engines which have theMetaItemDescriptorRuleset.TRIGGER_ON_PREDICTED_DATAand containAttributeInfoof the specified asset id.org.openremote.model.rules.geofence.GeofenceDefinition[]getAssetGeofences(String assetId) protected List<org.openremote.model.attribute.AttributeInfo> getAssetStatesInScope(String assetId) protected List<RulesEngine<?>> getEnginesInScope(String realm, String[] assetPath) intprotected Optional<RulesetDeployment> getRulesetDeployment(Long rulesetId) voidinit(org.openremote.model.Container container) protected voidinsertOrUpdateAttributeInfo(org.openremote.model.attribute.AttributeEvent attributeEvent) booleanisRulesetKnown(org.openremote.model.rules.Ruleset ruleset) protected static booleanisRuleState(org.openremote.model.value.MetaHolder metaHolder) voidonAttributeEvent(org.openremote.model.attribute.AttributeEvent event) React to events that have been committed to the DB and inject them into the appropriateRulesEngines.protected voidonEngineLocationRulesChanged(RulesEngine<?> rulesEngine, List<RulesEngine.AssetLocationPredicates> newEngineAssetStateLocationPredicates) Called when an engine's rules change identifying assets with location attributes that also haveLocationAttributePredicatein the rules.protected voidprocessAssetChange(org.openremote.model.asset.Asset<?> asset, org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> persistenceEvent) protected voidprotected voidprocessRealmChange(org.openremote.model.security.Realm realm, org.openremote.model.PersistenceEvent.Cause cause) protected voidprocessRulesetChange(org.openremote.model.rules.Ruleset ruleset, org.openremote.model.PersistenceEvent.Cause cause) protected voidretractAttributeInfo(org.openremote.model.attribute.AttributeEvent attributeEvent) voidstart(org.openremote.model.Container container) voidstop(org.openremote.model.Container container) toString()protected voidundeployAssetRuleset(org.openremote.model.rules.AssetRuleset ruleset) protected voidundeployGlobalRuleset(org.openremote.model.rules.GlobalRuleset ruleset) protected voidundeployRealmRuleset(org.openremote.model.rules.RealmRuleset ruleset) Methods inherited from class org.apache.camel.builder.RouteBuilder
addLifecycleInterceptor, addRoutes, addRoutesToCamelContext, addTemplatedRoutesToCamelContext, bindToRegistry, bindToRegistry, checkInitialized, configuration, configureRest, configureRests, configureRoute, configureRouteConfiguration, configureRoutes, configureRouteTemplate, configureTemplatedRoute, customize, customize, dataFormat, errorHandler, errorHandler, expression, from, from, from, fromF, fromV, fromV, getBeans, getOrder, getResource, getRestCollection, getRestConfiguration, getRests, getRouteCollection, getRoutes, getRouteTemplateCollection, getTemplatedRouteCollection, initializeCamelContext, intercept, interceptFrom, interceptFrom, interceptSendToEndpoint, loadRoutesBuilder, loadRoutesBuilder, onCompletion, onException, onException, populateBeans, populateOrUpdateRoutes, populateRests, populateRoutes, populateRouteTemplates, populateTemplatedRoutes, populateTransformers, populateValidators, prepareModel, property, propertyInject, removeLifecycleInterceptor, rest, rest, restConfiguration, routeTemplate, setErrorHandlerFactory, setResource, setRestCollection, setRouteTemplateCollection, setTemplatedRouteCollection, templatedRoute, tokenizer, transformer, updateRoutesToCamelContext, validatorMethods inherited from class org.apache.camel.builder.BuilderSupport
body, bodyAs, constant, constant, constant, createErrorHandlerBuilder, csimple, csimple, datasonnet, datasonnet, datasonnet, deadLetterChannel, deadLetterChannel, defaultErrorHandler, endpoint, endpoint, exceptionMessage, exchangeProperty, expression, getCamelContext, getContext, getErrorHandlerFactory, hasErrorHandlerFactory, header, java, java, joor, joor, jq, jq, jsonpath, jsonpath, jtaTransactionErrorHandler, jtaTransactionErrorHandler, jtaTransactionErrorHandler, method, method, method, method, noErrorHandler, regexReplaceAll, regexReplaceAll, setCamelContext, simple, simple, simpleF, simpleF, springTransactionErrorHandler, springTransactionErrorHandler, springTransactionErrorHandler, systemProperty, systemProperty, variable, xpath, xpath, xpath, xpath
-
Field Details
-
PRIORITY
public static final int PRIORITY- See Also:
-
OR_RULE_EVENT_EXPIRES
- See Also:
-
OR_RULE_EVENT_EXPIRES_DEFAULT
- See Also:
-
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
- See Also:
-
OR_RULES_QUICK_FIRE_MILLIS_DEFAULT
public static final int OR_RULES_QUICK_FIRE_MILLIS_DEFAULT- See Also:
-
globalEngine
-
realmEngines
-
assetEngines
-
ENGINE_LOCK
-
geofenceAssetAdapters
-
timerService
protected org.openremote.container.timer.TimerService timerService -
executorService
-
scheduledExecutorService
-
persistenceService
protected org.openremote.container.persistence.PersistenceService persistenceService -
rulesetStorageService
-
identityService
-
assetStorageService
-
notificationService
-
webhookService
-
alarmService
-
assetProcessingService
-
assetDatapointService
-
assetPredictedDatapointService
-
clientEventService
-
gatewayService
-
realms
protected org.openremote.model.security.Realm[] realms -
locationPredicateRulesConsumer
-
engineAssetLocationPredicateMap
protected final Map<RulesEngine<?>,List<RulesEngine.AssetLocationPredicates>> engineAssetLocationPredicateMap -
assetsWithModifiedLocationPredicates
-
attributeEvents
-
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:
getPriorityin interfaceorg.openremote.model.ContainerService
-
init
- Specified by:
initin interfaceorg.openremote.model.ContainerService- Throws:
Exception
-
configure
- Specified by:
configurein classorg.apache.camel.builder.RouteBuilder- Throws:
Exception
-
start
- Specified by:
startin interfaceorg.openremote.model.ContainerService- Throws:
Exception
-
stop
- Specified by:
stopin interfaceorg.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 appropriateRulesEngines.- Throws:
AssetProcessingException
-
doProcessAttributeUpdate
protected void doProcessAttributeUpdate(org.openremote.model.attribute.AttributeEvent attributeEvent) -
isRulesetKnown
public boolean isRulesetKnown(org.openremote.model.rules.Ruleset ruleset) -
getAssetGeofences
-
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
-
getEnginesInScope
-
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 haveLocationAttributePredicatein the rules. The job here is to identify the asset's (viaAttributeInfo) that have modifiedLocationAttributePredicates and to notify theGeofenceAssetAdapters. -
processModifiedGeofences
protected void processModifiedGeofences() -
getRulesetDeployment
-
fireDeploymentsWithPredictedDataForAsset
Trigger rules engines which have theMetaItemDescriptorRuleset.TRIGGER_ON_PREDICTED_DATAand containAttributeInfoof the specified asset id. Use this whenPredictedDatapointshas changed for this asset.- Parameters:
assetId- of the asset which has new predicated data points.
-
toString
- Overrides:
toStringin classorg.apache.camel.builder.RouteBuilder
-