2 using System.Collections.Generic;
7 using System.Xml.Serialization;
16 private string _Condition;
24 public string Condition
26 get {
return _Condition; }
27 set { _Condition = value; }
31 IGenericExpression<bool> ConditionExpression = null;
33 private List<string> GetVariablesInternal()
35 List<string> Variables =
new List<string>();
37 if (C.IsNullOrWhiteSpace())
return Variables;
40 ExpressionContext Context =
new ExpressionContext();
41 Context.Options.ParseCulture =
System.Globalization.CultureInfo.InvariantCulture;
42 Context.Imports.AddType(typeof(Math));
44 VariableCollection VC = Context.Variables;
45 VC.ResolveVariableType +=
new EventHandler<ResolveVariableTypeEventArgs>(GetVariablesInternal_ResolveVariableType);
46 VC.ResolveVariableValue +=
new EventHandler<ResolveVariableValueEventArgs>(GetVariablesInternal_ResolveVariableValue);
48 IGenericExpression<bool> Exp = null;
51 Exp = Context.CompileGeneric<
bool>(Condition.Replace(((
char)
TableElementTypeEnum.NamedElement).ToString(),
"NamedElement_"));
54 Log.
Warning(
"Cant extract variables from condition {0}.".Build(Condition));
57 VC.ResolveVariableType -=
new EventHandler<ResolveVariableTypeEventArgs>(GetVariablesInternal_ResolveVariableType);
58 VC.ResolveVariableValue -=
new EventHandler<ResolveVariableValueEventArgs>(GetVariablesInternal_ResolveVariableValue);
61 if (Exp != null && Exp.Info != null)
63 Variables = Exp.Info.GetReferencedVariables().ToList();
69 void GetVariablesInternal_ResolveVariableValue(
object sender, ResolveVariableValueEventArgs e)
74 void GetVariablesInternal_ResolveVariableType(
object sender, ResolveVariableTypeEventArgs e)
76 e.VariableType = typeof(
double);
86 return GetVariablesInternal().Select(Va => (Va.StartsWith(
"NamedElement_") ? ((char)
TableElementTypeEnum.NamedElement).ToString() + Va.Substring(
"NamedElement_".Length) : Va)).ToList();
91 private void InitCondition()
93 string C = Condition.Replace(((
char)
TableElementTypeEnum.NamedElement).ToString(),
"NamedElement_");
94 ConditionExpression = null;
96 if (C.IsNullOrWhiteSpace())
98 Log.
Warning(
"No condition has been set for {0} named {1}.".Build(this.GetType().Name, Name));
102 List<string> Variables = GetVariablesInternal();
104 ExpressionContext Context =
new ExpressionContext();
105 Context.Options.ParseCulture =
System.Globalization.CultureInfo.InvariantCulture;
108 Context.Imports.AddType(typeof(Math));
110 foreach (
string V
in Variables)
113 while (P < C.Length && P >= 0)
115 P = C.IndexOf(V, P + 1, StringComparison.OrdinalIgnoreCase);
117 if (!C.Substring(P, V.Length + 1).Equals(V +
".", StringComparison.OrdinalIgnoreCase))
119 C = C.Substring(0, P) +
"{0}.Value".Build(V) + C.Substring(P + V.Length);
126 if (V.StartsWith(
"NamedElement_"))
139 Context.Variables[V] = TE;
144 Log.Exception(
"A exception has occured while setting up the variables for condition {0} of effect {1}.".Build(Condition, Name), E);
151 ConditionExpression = Context.CompileGeneric<
bool>(C);
156 Log.Exception(
"A exception has occured while compiling the condition {0} (internaly translated to {2}) of effect {1}.".Build(Condition, Name, C), E);
173 if (ConditionExpression != null)
177 if (ConditionExpression.Evaluate())
179 TableElementData.
Value = 255;
183 TableElementData.
Value = 0;
185 TriggerTargetEffect(TableElementData);
190 Log.
Exception(
"A exception occured when evaluating the expression {0} of effect {1}. Effect will be deactivated.".Build(ConditionExpression.Text, Name), E);
191 ConditionExpression = null;
219 ConditionExpression = null;
This effect evaluates the condition specified in the Condition property.
static void Warning(string Message)
Writes a warning message to the log.
override void Finish()
Finishes the Effect. Releases the references to the target effect, the table object and the expressi...
TableElementTypeEnum
Enum for the different TableElement types.
A simple logger used to record important events and exceptions.
int Value
The value of the table element.
List< string > GetVariables()
Gets a list of all variables in the condition.
TableElementList TableElements
Lists the TableElement objects for the Table. This list is automaticaly extend with new TableElement...
Data representing the state of a table emlement
Base class for effects targeting another effect.
void UpdateState(TableElementData Data)
Method to update the state and/or add a entry to the list
The Table namespace contains all table specific classes like the Table class itself, TableElement and effect assigment classes.
override void Trigger(TableElementData TableElementData)
Triggers the effect with the given TableElementData.
Holds all table specific information and handles all TableElements
Represents a element (e.g. Switch, Solenoid) of a pinball table
Table()
Initializes a new instance of the Table class.
override void Init(Table.Table Table)
Initializes the Effect. Resolves the name of the TargetEffect and initializes the condition context...
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.