/
RulesEngine.java
816 lines (699 loc) · 32.2 KB
/
RulesEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
* Copyright 2017, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.manager.server.rules;
import org.drools.core.common.InternalWorkingMemory;
import org.drools.core.time.InternalSchedulerService;
import org.drools.core.time.JobContext;
import org.drools.core.time.JobHandle;
import org.drools.core.time.impl.*;
import org.drools.template.ObjectDataCompiler;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.conf.EqualityBehaviorOption;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.event.rule.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.conf.ClockTypeOption;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.api.time.SessionClock;
import org.openremote.container.timer.TimerService;
import org.openremote.container.util.Util;
import org.openremote.manager.server.asset.AssetProcessingService;
import org.openremote.manager.server.asset.AssetStorageService;
import org.openremote.manager.server.asset.ServerAsset;
import org.openremote.manager.server.concurrent.ManagerExecutorService;
import org.openremote.manager.server.notification.NotificationService;
import org.openremote.manager.server.security.ManagerIdentityService;
import org.openremote.model.asset.*;
import org.openremote.model.attribute.AttributeType;
import org.openremote.model.rules.AssetRuleset;
import org.openremote.model.rules.GlobalRuleset;
import org.openremote.model.rules.Ruleset;
import org.openremote.model.rules.TenantRuleset;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.notification.AlertNotification;
import org.openremote.model.rules.Assets;
import org.openremote.model.rules.Users;
import org.openremote.model.rules.template.TemplateFilter;
import org.openremote.model.user.UserQuery;
import org.openremote.model.util.Pair;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import static org.openremote.model.attribute.Attribute.isAttributeTypeEqualTo;
public class RulesEngine<T extends Ruleset> {
public static final Logger LOG = Logger.getLogger(RulesEngine.class.getName());
private static final int AUTO_START_DELAY_SECONDS = 2;
private static Long counter = 1L;
static final protected Util UTIL = new Util();
final protected TimerService timerService;
final protected ManagerExecutorService executorService;
final protected NotificationService notificationService;
final protected AssetStorageService assetStorageService;
final protected AssetProcessingService assetProcessingService;
final protected ManagerIdentityService identityService;
final protected Class<T> rulesetType;
final protected String id;
final protected Function<RulesEngine, AgendaEventListener> rulesEngineListeners;
protected final Map<Long, T> rulesets = new LinkedHashMap<>();
protected String rulesetsDebug;
protected KieSession knowledgeSession;
protected KieServices kieServices;
protected KieFileSystem kfs;
// We need to be able to reference the KieModule dynamically generated for this engine
// from the singleton KieRepository to do this we need a pom.xml file with a release ID - crazy drools!!
protected ReleaseId releaseId;
protected Future runningFuture;
protected Throwable error;
final protected Map<AssetState, FactHandle> assetStates = new HashMap<>();
// This consumer is useful in testing, as we can't have a reliable event fact
// count from Drools session (events are expired automatically))
protected Consumer<AssetEvent> assetEventsConsumer;
protected ScheduledFuture startTimer;
public RulesEngine(TimerService timerService,
ManagerExecutorService executorService,
AssetStorageService assetStorageService,
NotificationService notificationService,
AssetProcessingService assetProcessingService,
ManagerIdentityService identityService,
Class<T> rulesetType,
String id,
Function<RulesEngine, AgendaEventListener> rulesEngineListeners) {
this.timerService = timerService;
this.executorService = executorService;
this.assetStorageService = assetStorageService;
this.notificationService = notificationService; // shouldBeUser service or Identity Service ?
this.assetProcessingService = assetProcessingService;
this.identityService = identityService;
this.rulesetType = rulesetType;
this.id = id;
this.rulesEngineListeners = rulesEngineListeners;
}
protected synchronized static Long getNextCounter() {
return counter++;
}
@SuppressWarnings("unchecked")
public synchronized T[] getAllRulesets() {
T[] arr = Util.createArray(rulesets.size(), rulesetType);
return rulesets.values().toArray(arr);
}
public String getId() {
return id;
}
public boolean isRunning() {
return runningFuture != null;
}
public boolean isError() {
return error != null;
}
public Throwable getError() {
return error;
}
public KieSession getKnowledgeSession() {
return knowledgeSession;
}
public SessionClock getSessionClock() {
KieSession session = knowledgeSession;
if (session != null) {
return session.getSessionClock();
}
return null;
}
public synchronized boolean isEmpty() {
return rulesets.isEmpty();
}
public void setAssetEventsConsumer(Consumer<AssetEvent> assetEventsConsumer) {
this.assetEventsConsumer = assetEventsConsumer;
}
protected void setGlobal(String identifier, Object object) {
try {
knowledgeSession.setGlobal(identifier, object);
} catch (Throwable t) {
// Ignore, Drools complains if the DRL doesn't declare the global, but it works
}
}
/**
* Adds the ruleset to the engine by first stopping the engine and
* then deploying new rules and then restarting the engine (after
* {@link #AUTO_START_DELAY_SECONDS}) to prevent excessive engine stop/start.
* <p>
* If engine is in an error state (one of the rulesets failed to deploy)
* then the engine will not restart.
*
* @return Whether or not the ruleset deployed successfully
*/
public synchronized boolean addRuleset(T ruleset) {
if (ruleset == null || ruleset.getRules() == null || ruleset.getRules().isEmpty()) {
// Assume it's a success if deploying an empty ruleset
LOG.finest("Ruleset is empty so no rules to deploy");
return true;
}
if (kfs == null) {
initialiseEngine();
}
T existingRuleset = rulesets.get(ruleset.getId());
if (existingRuleset != null && existingRuleset.getVersion() == ruleset.getVersion()) {
LOG.fine("Ruleset version already deployed so ignoring");
return true;
}
if (isRunning()) {
stop();
}
// Stop any running start timer
if (startTimer != null) {
startTimer.cancel(false);
}
// Check if ruleset is already deployed (maybe an older version)
if (existingRuleset != null) {
// Remove this old rules file
kfs.delete("src/main/resources/" + ruleset.getId());
//noinspection SuspiciousMethodCalls
rulesets.remove(existingRuleset);
updateRulesetsDebug();
}
LOG.info("Adding ruleset: " + ruleset);
boolean addSuccessful = false;
try {
// If the ruleset references a template asset, compile it as a template
String drl = ruleset.getTemplateAssetId() != null
? compileTemplate(ruleset.getTemplateAssetId(), ruleset.getRules())
: ruleset.getRules();
// ID will be unique within the scope of a rules engine as ruleset will all be of same type
kfs.write("src/main/resources/" + ruleset.getId() + ".drl", drl);
// Unload the rules string from the ruleset we don't need it anymore and don't want it using memory
ruleset.setRules(null);
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
Collection<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
LOG.severe("Error in ruleset: " + ruleset);
for (Message error : errors) {
LOG.severe(error.getText());
}
LOG.fine(drl);
// If compilation failed, remove rules from FileSystem so it won't fail on next pass here if any
kfs.delete("src/main/resources/" + ruleset.getId());
} else {
LOG.info("Added ruleset: " + ruleset);
addSuccessful = true;
}
} catch (Exception e) {
LOG.log(Level.SEVERE, "Error in ruleset: " + ruleset, e);
error = e;
// If compilation failed, remove rules from FileSystem so it won't fail on next pass here if any
kfs.delete("src/main/resources/" + ruleset.getId());
}
if (!addSuccessful) {
error = new RuntimeException("Ruleset contains an error: " + ruleset);
// Update status of each ruleset
rulesets.forEach((id, rd) -> {
if (rd.getDeploymentStatus() == Ruleset.DeploymentStatus.DEPLOYED) {
rd.setDeploymentStatus(Ruleset.DeploymentStatus.READY);
}
});
} else {
startTimer = executorService.schedule(this::start, AUTO_START_DELAY_SECONDS * 1000);
}
// Add new ruleset
ruleset.setDeploymentStatus(addSuccessful ? Ruleset.DeploymentStatus.DEPLOYED : Ruleset.DeploymentStatus.FAILED);
rulesets.put(ruleset.getId(), ruleset);
updateRulesetsDebug();
return addSuccessful;
}
protected synchronized void removeRuleset(Ruleset ruleset) {
if (kfs == null) {
return;
}
T matchedRuleset = rulesets.get(ruleset.getId());
if (matchedRuleset == null) {
LOG.finer("Ruleset cannot be retracted as it was never deployed: " + ruleset);
return;
}
if (isRunning()) {
stop();
}
// Stop any running start timer
if (startTimer != null) {
startTimer.cancel(false);
}
// Remove this old rules file
kfs.delete("src/main/resources/" + ruleset.getId());
rulesets.remove(ruleset.getId());
updateRulesetsDebug();
// Update status of each ruleset
boolean anyFailed = rulesets
.values()
.stream()
.anyMatch(rd -> rd.getDeploymentStatus() == Ruleset.DeploymentStatus.FAILED);
if (!anyFailed) {
error = null;
rulesets.forEach((id, rd) -> {
if (rd.getDeploymentStatus() == Ruleset.DeploymentStatus.READY) {
rd.setDeploymentStatus(Ruleset.DeploymentStatus.DEPLOYED);
}
});
}
if (!isError() && !isEmpty()) {
// Queue engine start
startTimer = executorService.schedule(this::start, AUTO_START_DELAY_SECONDS * 1000);
}
}
protected void initialiseEngine() {
// Initialise
kieServices = KieServices.Factory.get();
KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
String versionId = getNextCounter().toString();
releaseId = kieServices.newReleaseId("org.openremote", "openremote-kiemodule", versionId);
KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("OpenRemoteKModule");
kieBaseModel
.setDefault(true)
.setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
.setEventProcessingMode(EventProcessingOption.STREAM)
.newKieSessionModel("ksession1")
.setDefault(true)
.setType(KieSessionModel.KieSessionType.STATEFUL);
kfs = kieServices.newKieFileSystem();
kfs.generateAndWritePomXML(releaseId);
kfs.writeKModuleXML(kieModuleModel.toXML());
LOG.info("Initialised rules for deployment '" + getId() + "':" + kieBaseModel.toString());
}
protected synchronized void start() {
if (isRunning()) {
return;
}
if (isError()) {
LOG.fine("Cannot start rules engine as an error occurred during initialisation");
return;
}
if (isEmpty()) {
LOG.finest("No rulesets loaded so nothing to start");
return;
}
LOG.info("Starting: " + this);
// Note each rule engine has its' own KieModule which are stored in a singleton register by drools
// we need to ensure we get the right module here otherwise we could be using the wrong rules
KieContainer kieContainer = kieServices.newKieContainer(releaseId);
KieSessionConfiguration kieSessionConfiguration = kieServices.newKieSessionConfiguration();
// Which clock to use ("pseudo" for testing, "realtime" otherwise)
switch (timerService.getClock()) {
case PSEUDO:
kieSessionConfiguration.setOption(ClockTypeOption.get("pseudo"));
break;
default:
kieSessionConfiguration.setOption(ClockTypeOption.get("realtime"));
}
try {
knowledgeSession = kieContainer.newKieSession(kieSessionConfiguration);
// If the pseudo clock is enabled (we run a test environment?) then set current
// time on startup of session, as real time is used in offset calculations for
// automatic event expiration in Drools (probably a design mistake)
if (timerService.getClock() == TimerService.Clock.PSEUDO) {
((PseudoClockScheduler) knowledgeSession.getSessionClock())
.setStartupTime(timerService.getCurrentTimeMillis());
}
setGlobal("assets", createAssetsFacade());
setGlobal("users", createUsersFacade());
setGlobal("LOG", LOG);
// TODO Still need this UTIL?
setGlobal("util", UTIL);
knowledgeSession.addEventListener(new RuleExecutionLogger(this::toString));
AgendaEventListener eventListener = rulesEngineListeners != null ? rulesEngineListeners.apply(this) : null;
if (eventListener != null) {
knowledgeSession.addEventListener(eventListener);
}
knowledgeSession.addEventListener(new RuleRuntimeEventListener() {
@Override
public void objectInserted(ObjectInsertedEvent event) {
LOG.fine("+++ On " + RulesEngine.this + ", object inserted: " + event.getObject());
}
@Override
public void objectUpdated(ObjectUpdatedEvent event) {
LOG.fine("^^^ On " + RulesEngine.this + ", object updated: " + event.getObject());
}
@Override
public void objectDeleted(ObjectDeletedEvent event) {
LOG.fine("--- On " + RulesEngine.this + ", object deleted: " + event.getOldObject());
}
});
// Start engine in active mode
fireUntilHalt();
// Insert initial asset states
try {
Set<AssetState> initialState = assetStates.keySet();
LOG.info("On " + this + ", inserting initial asset states: " + initialState.size());
for (AssetState assetState : initialState) {
insertAssetState(assetState);
}
} catch (Exception ex) {
// This is called in a background timer thread, we must log here or the exception is swallowed
LOG.log(Level.SEVERE, "On " + this + ", inserting initial asset states failed", ex);
// TODO Should we stop the engine here?
error = ex;
stop();
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "On " + this + ", creating the knowledge session failed", ex);
error = ex;
stop();
}
}
protected synchronized void fireUntilHalt() {
// Block a background thread
runningFuture = executorService.getRulesExecutor().submit(() -> {
boolean stoppedOnError = false;
try {
knowledgeSession.fireUntilHalt();
} catch (Exception ex) {
// Errors in rule RHS
LOG.log(Level.SEVERE, "On " + RulesEngine.this + ", error firing rules", ex);
stoppedOnError = true;
} finally {
if (stoppedOnError) {
// Keep running if stopped firing because of a RHS error
runningFuture.cancel(true);
runningFuture = null;
fireUntilHalt();
}
}
});
}
protected synchronized void stop() {
if (!isRunning()) {
return;
}
LOG.info("Stopping: " + this);
if (knowledgeSession != null) {
try {
knowledgeSession.halt();
knowledgeSession.dispose();
LOG.fine("On " + this + ", knowledge session disposed");
} finally {
runningFuture.cancel(true);
runningFuture = null;
}
}
}
protected synchronized void insertAssetState(AssetState newAssetState) {
FactHandle factHandle = insertIntoSession(newAssetState);
assetStates.put(newAssetState, factHandle);
}
protected synchronized void updateAssetState(AssetState assetState) {
// Check if fact already exists using equals()
if (!assetStates.containsKey(assetState)) {
// Delete any existing fact for this attribute ref
// Put the fact into working memory and store the handle
retractAssetState(assetState);
if (isRunning()) {
insertAssetState(assetState);
} else {
assetStates.put(assetState, null);
}
}
}
protected synchronized void retractAssetState(AssetState assetState) {
// If there already is a fact in working memory for this attribute then delete it
AssetState update = assetStates.keySet()
.stream()
.filter(au -> au.attributeRefsEqual(assetState))
.findFirst()
.orElse(null);
// Always remove from asset states
FactHandle factHandle = update != null ? assetStates.remove(update) : null;
if (factHandle != null) {
if (isRunning()) {
try {
// ... retract it from working memory ...
knowledgeSession.delete(factHandle);
} catch (Exception e) {
LOG.warning("On " + this + ", failed to retract fact: " + update);
}
}
}
}
protected synchronized void insertAssetEvent(long expirationOffset, AssetEvent assetEvent) {
FactHandle factHandle = insertIntoSession(assetEvent);
if (factHandle != null) {
scheduleExpiration(assetEvent, factHandle, expirationOffset);
}
if (assetEventsConsumer != null) {
assetEventsConsumer.accept(assetEvent);
}
}
protected synchronized FactHandle insertIntoSession(AbstractAssetUpdate update) {
if (!isRunning()) {
LOG.fine("On " + this + ", engine is in error state or not running, ignoring: " + update);
return null;
}
FactHandle fh = knowledgeSession.insert(update);
LOG.fine("On " + this + ", fact count after insert: " + knowledgeSession.getFactCount());
return fh;
}
protected Assets createAssetsFacade() {
return new Assets() {
@Override
public RestrictedQuery query() {
RestrictedQuery query = new RestrictedQuery() {
@Override
public RestrictedQuery select(Select select) {
throw new IllegalArgumentException("Overriding query projection is not allowed in this rules scope");
}
@Override
public RestrictedQuery id(String id) {
throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
}
@Override
public RestrictedQuery tenant(TenantPredicate tenantPredicate) {
if (GlobalRuleset.class.isAssignableFrom(rulesetType))
return super.tenant(tenantPredicate);
throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
}
@Override
public RestrictedQuery userId(String userId) {
throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
}
@Override
public RestrictedQuery orderBy(OrderBy orderBy) {
throw new IllegalArgumentException("Overriding query result order is not allowed in this rules scope");
}
@Override
public String getResult() {
ServerAsset asset = assetStorageService.find(this);
return asset != null ? asset.getId() : null;
}
@Override
public List<String> getResults() {
return assetStorageService.findAllIds(this);
}
@Override
public void applyResult(Consumer<String> assetIdConsumer) {
assetIdConsumer.accept(getResult());
}
@Override
public void applyResults(Consumer<List<String>> assetIdListConsumer) {
assetIdListConsumer.accept(getResults());
}
};
if (TenantRuleset.class.isAssignableFrom(rulesetType)) {
query.tenantPredicate = new AssetQuery.TenantPredicate(id);
}
if (AssetRuleset.class.isAssignableFrom(rulesetType)) {
ServerAsset restrictedAsset = assetStorageService.find(id, true);
if (restrictedAsset == null) {
throw new IllegalStateException("Asset is no longer available for this deployment: " + id);
}
query.pathPredicate = new AssetQuery.PathPredicate(restrictedAsset.getPath());
}
return query;
}
@Override
public void dispatch(AttributeEvent... events) {
if (events == null)
return;
for (AttributeEvent event : events) {
// Set event source time to current container time
event.timestamp = timerService.getCurrentTimeMillis();
// Check if the asset ID of the event can be found in the original query
AssetQuery checkQuery = query();
checkQuery.id = event.getEntityId();
if (assetStorageService.find(checkQuery) == null) {
throw new IllegalArgumentException(
"Access to asset not allowed for this rule engine scope: " + event
);
}
LOG.fine("Dispatching on " + RulesEngine.this + ": " + event);
assetProcessingService.sendAttributeEvent(event, AttributeEvent.Source.INTERNAL);
}
}
};
}
protected Users createUsersFacade() {
return new Users() {
@Override
public Users.RestrictedQuery query() {
RestrictedQuery query = new RestrictedQuery() {
@Override
public Users.RestrictedQuery tenant(UserQuery.TenantPredicate tenantPredicate) {
if (GlobalRuleset.class.isAssignableFrom(rulesetType))
return super.tenant(tenantPredicate);
throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
}
@Override
public Users.RestrictedQuery asset(UserQuery.AssetPredicate assetPredicate) {
if (GlobalRuleset.class.isAssignableFrom(rulesetType))
return super.asset(assetPredicate);
if (TenantRuleset.class.isAssignableFrom(rulesetType)) {
return super.asset(assetPredicate);
// TODO: should only be allowed if asset belongs to tenant
}
if (AssetRuleset.class.isAssignableFrom(rulesetType)) {
return super.asset(assetPredicate);
// TODO: should only be allowed if restricted asset is descendant of scope's asset
}
throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
}
@Override
public List<String> getResults() {
return notificationService.findAllUsersWithToken(this);
}
@Override
public void applyResults(Consumer<List<String>> usersIdListConsumer) {
usersIdListConsumer.accept(getResults());
}
};
if (TenantRuleset.class.isAssignableFrom(rulesetType)) {
query.tenantPredicate = new UserQuery.TenantPredicate(id);
}
if (AssetRuleset.class.isAssignableFrom(rulesetType)) {
ServerAsset restrictedAsset = assetStorageService.find(id, true);
if (restrictedAsset == null) {
throw new IllegalStateException("Asset is no longer available for this deployment: " + id);
}
query.assetPredicate = new UserQuery.AssetPredicate(id);
}
return query;
}
@Override
public void storeAndNotify(String userId, AlertNotification alert) {
if (TenantRuleset.class.isAssignableFrom(rulesetType)) {
boolean userIsInTenant = identityService.isUserInTenant(userId, id);
if (!userIsInTenant) {
throw new IllegalArgumentException("User not in tenant: " + id);
}
}
if (AssetRuleset.class.isAssignableFrom(rulesetType)) {
boolean userIsLinkedToAsset = assetStorageService.isUserAsset(userId, id);
if (!userIsLinkedToAsset) {
throw new IllegalArgumentException("User not linked to asset: " + id);
}
}
notificationService.storeAndNotify(userId, alert);
}
};
}
/**
* Use the internal scheduling of Drools to expire events, so we can coordinate with the internal clock.
* Yes, this is a hack.
*/
protected void scheduleExpiration(AssetEvent assetEvent, FactHandle factHandle, long expirationOffset) {
if (!isRunning())
return;
InternalSchedulerService sessionScheduler = knowledgeSession.getSessionClock();
JobHandle jobHandle = new JDKTimerService.JDKJobHandle(assetEvent.getId().hashCode());
class AssetEventExpireJobContext implements JobContext {
public JobHandle handle;
@Override
public void setJobHandle(JobHandle jobHandle) {
this.handle = jobHandle;
}
@Override
public JobHandle getJobHandle() {
return handle;
}
@Override
public InternalWorkingMemory getWorkingMemory() {
return null;
}
}
TimerJobInstance timerJobInstance = new DefaultTimerJobInstance(
ctx -> {
LOG.fine("On " + RulesEngine.this + ", fact expired, deleting: " + assetEvent);
synchronized (RulesEngine.this) {
knowledgeSession.delete(factHandle);
}
},
new AssetEventExpireJobContext(),
new PointInTimeTrigger(knowledgeSession.getSessionClock().getCurrentTime() + expirationOffset, null, null),
jobHandle,
sessionScheduler
);
sessionScheduler.internalSchedule(timerJobInstance);
}
protected String compileTemplate(String templateAssetId, String rules) {
ServerAsset templateAsset = assetStorageService.find(templateAssetId, true);
if (templateAsset == null)
throw new IllegalStateException("Template asset not found: " + templateAssetId);
List<TemplateFilter> filters = templateAsset.getAttributesStream()
.filter(isAttributeTypeEqualTo(AttributeType.RULES_TEMPLATE_FILTER))
.map(attribute -> new Pair<>(attribute.getName(), attribute.getValue()))
.filter(pair -> pair.key.isPresent() && pair.value.isPresent())
.map(pair -> new Pair<>(pair.key.get(), pair.value.get()))
.map(pair -> TemplateFilter.fromModelValue(pair.key, pair.value))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
LOG.fine("Rendering rules template with filters: " + filters);
ObjectDataCompiler converter = new ObjectDataCompiler();
InputStream is = new ByteArrayInputStream(rules.getBytes(StandardCharsets.UTF_8));
return converter.compile(filters, is);
}
protected synchronized void updateRulesetsDebug() {
rulesetsDebug = Arrays.toString(rulesets.values().stream().map(rd ->
rd.getClass().getSimpleName()
+ " - "
+ rd.getName()
+ ": "
+ rd.getDeploymentStatus()).toArray(String[]::new)
);
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"id='" + id + '\'' +
", running='" + isRunning() + '\'' +
", error='" + error + '\'' +
", rulesets='" + rulesetsDebug + '\'' +
'}';
}
}