com.memority.toolkit.rule.api.context.StandardContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toolkit-rule-api Show documentation
Show all versions of toolkit-rule-api Show documentation
This artifact provides the API classes that are necessary to implement the contracts of Memority configuration Rules.
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Toolkit API , a Memority project.
*
* This file is released under the Memority Public Artifacts End-User License Agreement,
* see
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
package com.memority.toolkit.rule.api.context;
import java.util.HashMap;
import java.util.Map;
/**
* The standard parent context, an abstract class to used as a basis for application specific contexts.
*
* This context already holds the following root contexts:
*
* - {@link ActorsContext}: information the currently logged in Subject
* - {@link RuleContext}: information about the currently executed Rule
* - {@link VariablesContext}: a mutable context used to read and write custom variables
*
*
* @param the type of the subclassing context
*/
public class StandardContext implements Context {
protected ActorsContext actorsContext;
protected VariablesContext variablesContext = new VariablesContext();
protected String notificationEventType;
protected boolean simulation;
protected final Long randomSeed;
public StandardContext(Long randomSeed) {
this.randomSeed = randomSeed;
}
public StandardContext() {
this(null);
}
@Override
public Map getRootContexts() {
Map rootContexts = new HashMap<>();
rootContexts.put(KEY_CTXT, this);
rootContexts.put(KEY_VARS, getVariablesContext());
rootContexts.put(KEY_SUBJECT, getActorsContext() == null ? null : getActorsContext().getSubject());
rootContexts.put(KEY_REQUESTER, getActorsContext() == null ? null : getActorsContext().getRequester());
rootContexts.put(KEY_ACTORS, getActorsContext());
rootContexts.put(KEY_SIMULATION, isSimulation());
return rootContexts;
}
@Override
public ActorsContext getActorsContext() {
return this.actorsContext;
}
@Override
public VariablesContext getVariablesContext() {
return variablesContext;
}
@Override
public String getNotificationEventType() {
return notificationEventType;
}
@Override
public Long getRandomSeed() {
return randomSeed;
}
@Override
public boolean isSimulation() {
return simulation;
}
/**
* Set a variable value in the {@link VariablesContext}.
*
* @param varName the variable name
* @param varValue the variable value
* @return this context
*/
public C putVar(String varName, Object varValue) {
this.getVariablesContext().put(varName, varValue);
return self();
}
/**
* Returns the variable with the given name valueOf the {@link VariablesContext}.
*
* @param varName the variable name
* @return the variable value, maybe null
*/
public Object getVar(String varName) {
return this.getVariablesContext().get(varName);
}
/**
* Return this context. This is purely technical and used to allow DSL like methods
* that work with well known contexts ({@link #putVar(String, Object)}...
*
* @return this context actual class
*/
@SuppressWarnings("unchecked")
protected C self() {
return (C) this;
}
}