Restricted Scenario by script
In the scene AlternativeRestrictedScenario, we keep the same scenario than ListenerScenario. This scenario is mainly composed of RelationSensor, which is easy to understand and the base of scenarios. Therefore, to restrict a user we want to execute only realizations from those RelationSensor.
It works in two steps:
- Get all the realizations from the enabled transitions : QueryLauncher
- Execute realizations if possible : UserInteraction
QueryLauncher
QueryLauncher creates a list with every possible realizations at the current step of scenario, and that at each change of the scenario. When the scenario change, it ask directly to the scenario engine ScenarioEngineKernel all the possible realization present in the RelationSensor at the current enabled transitions.
// get sensitized transitions from scenario,
// convert transition to XURealizationQueryParameters,
// launch them.
LaunchQueries(
ScenarioEngineKernel.Instance.Engine.GetEnabledTransitions()
.Select(getQueryFromTransition)
.OfType<XURealizationQueryParameters>()
);
Then it fills a list of realizations ScenarioRealizations.
/// <summary>
/// The current possible realization computed with the current state of the scenario.
/// </summary>
public List<XURealization> ScenarioRealizations { get; private set; }
UserInteraction
This script is in charge of executing the runnable realizations. First, it generates a query with all the possible realizations in the scene so it can get the list of the realization from the query. It does this at each scenario's changes.
public void GeneratePossibleRealizations()
{
computingRealizations = true;
XURealizationQueryParameters query = XURealizationQueryParameters.GetQueryAllOptional(XareusManager.Instance.RelationEngine);
query.RelationsToUse.UnionWith(initialRelations);
query.MandatoryObjects.Add(referencedUser.User.XUObject);
RelationEngine.Instance.GetRealizations(query, l =>
{
possibleRealizations = l;
computingRealizations = false;
});
}
Then, if there is at least one possible realization, it gets the runnable realizations to compare them with the realizations from the list ScenarioRealization, created previously from QueryLauncher. Finally, it executes each runnable realization contained in the ScenarioRealization.
if (possibleRealizations.Count > 0)
{
working = true;
runnableRealizations.Clear();
foreach (XURealization real in possibleRealizations)
{
if (real.IsRunnable())
runnableRealizations.Add(real);
}
// Scenario enabled relations
foreach (XURealization real in runnableRealizations)
{
if (referencedUser.ScenarioRealizations.Contains(real))
{
XareusManager.Instance.ExecuteRealizationIfPossible(real, (r) =>
{
referencedUser.SignalExecutedRealization(r);
working = false;
});
return;
}
}
working = false;
}
With those two scripts, we restrict the user with only realization from the scenario. You can add any RelationSensor with the relation you need, with a scenario more or less complex, the user will have to follow the scenario.