2 using System.Collections.Generic;
5 using DirectOutput.Table;
7 using System.Xml.Serialization;
9 namespace DirectOutput.FX.ConditionFX
16 private string _Condition;
24 public string Condition
26 get {
return _Condition; }
27 set { _Condition = value; }
31 IGenericExpression<bool> ConditionExpression = null;
34 public List<string> GetVariables()
36 List<string> Variables =
new List<string>();
39 if (C.IsNullOrWhiteSpace())
return Variables;
44 int VariableStart = -1;
45 for (
int i = 0; i < C.Length - 1; i++)
50 if (VariableStart >= 0)
61 else if (VariableStart >= 0)
63 if (C.Substring(i, 1).IsInteger())
67 else if (C.Substring(i, 1) ==
"-" && (i - VariableStart) == 1)
71 else if ((i - VariableStart) > 1 && C.Substring(VariableStart + 1, i - VariableStart - 1).IsInteger())
74 if (!Variables.Contains(C.Substring(VariableStart, i - VariableStart).ToUpper()))
76 Variables.Add(C.Substring(VariableStart, i - VariableStart).ToUpper());
97 Log.
Exception(
"A exception occured while trying to extract variable names from the condition {0}.".Build(Condition), E);
104 private void InitCondition()
106 List<string> Variables = GetVariables();
107 string C = Condition;
108 ConditionExpression = null;
110 if (C.IsNullOrWhiteSpace())
112 Log.
Warning(
"No condition has been set for {0} named {1}.".Build(this.GetType().Name, Name));
117 ExpressionContext Context =
new ExpressionContext();
118 Context.Options.ParseCulture = System.Globalization.CultureInfo.InvariantCulture;
121 Context.Imports.AddType(typeof(Math));
123 foreach (
string V
in Variables)
126 while (P<C.Length && P>=0)
128 P = C.IndexOf(V, P + 1, StringComparison.OrdinalIgnoreCase);
130 if (!C.Substring(P, V.Length + 1).Equals(V +
".", StringComparison.OrdinalIgnoreCase))
132 C = C.Substring(0, P) +
"{0}.Value".Build(V) + C.Substring(P + V.Length);
146 Context.Variables[V] = TE;
151 Log.Exception(
"A exception has occured while setting up the variables for condition {0} of effect {1}.".Build(Condition, Name), E);
158 ConditionExpression = Context.CompileGeneric<
bool>(C);
163 Log.Exception(
"A exception has occured while compiling the condition {0} (internaly translated to {2}) of effect {1}.".Build(Condition, Name, C), E);
180 if (ConditionExpression != null)
184 if (ConditionExpression.Evaluate())
186 TableElementData.
Value = 255;
190 TableElementData.
Value = 0;
192 TriggerTargetEffect(TableElementData);
197 Log.
Exception(
"A exception occured when evaluating the expression {0} of effect {1}. Effect will be deactivated.".Build(ConditionExpression.Text, Name), E);
198 ConditionExpression = null;
224 public override void Finish()
226 ConditionExpression = null;