public void TestParseVariable_Parameter() { StringReader tr = new StringReader("<parameter name='N' type='index' comment='number of processes in the system' />"); XmlTextReader reader = new XmlTextReader(tr); reader.Read(); // read into block Variable actual = ParseHyXML.ParseVariableParameter(reader); Variable expected = new VariableParameter("N", Variable.VarType.index, Variable.VarUpdateType.constant, ""); Assert.AreEqual(expected, actual); tr = new StringReader("<parameter name='abcd' type='real' comment='' />"); reader = new XmlTextReader(tr); reader.Read(); // read into block actual = ParseHyXML.ParseVariableParameter(reader); expected = new VariableParameter("abcd", Variable.VarType.real, Variable.VarUpdateType.constant, ""); Assert.AreEqual(expected, actual); tr = new StringReader("<parameter name='r1234' type='integer' comment='' />"); reader = new XmlTextReader(tr); reader.Read(); // read into block actual = ParseHyXML.ParseVariableParameter(reader); expected = new VariableParameter("r1234", Variable.VarType.integer, Variable.VarUpdateType.constant, ""); Assert.AreEqual(expected, actual); }
public void ApplyParameters() { // Set the return type to the Vector class if the value returns a vector. if (ElementValueType == ValueType.Vector) { CodeType = VectorType.Instance; } // Get the parameters. Parameters = new Parse.CodeParameter[WorkshopParameters.Length]; for (int i = 0; i < Parameters.Length; i++) { string name = WorkshopParameters[i].Name.Replace(" ", ""); string description = Wiki?.GetWikiParameter(WorkshopParameters[i].Name)?.Description; if (WorkshopParameters[i] is VarRefParameter) { Parameters[i] = new VariableParameter( name, description, ((VarRefParameter)WorkshopParameters[i]).IsGlobal ? VariableType.Global : VariableType.Player, new VariableResolveOptions() { CanBeIndexed = false, FullVariable = true } ); } else { CodeType codeType = null; // If the parameter is an enum, get the enum CodeType. if (WorkshopParameters[i] is EnumParameter) { codeType = ValueGroupType.GetEnumType(((EnumParameter)WorkshopParameters[i]).EnumData); } var defaultValue = WorkshopParameters[i].GetDefault(); Parameters[i] = new CodeParameter( name, description, codeType, defaultValue == null ? null : new ExpressionOrWorkshopValue(defaultValue) ); // If the default parameter value is an Element and the Element is restricted, if (defaultValue is Element parameterElement && parameterElement.ElementList.Restricted != null) { // ...then add the restricted call type to the parameter's list of restricted call types. Parameters[i].RestrictedCalls.Add((RestrictedCallType)parameterElement.ElementList.Restricted); } } } }
/// <summary> /// Parse a parameter (variable that is constant) /// </summary> /// <param name="reader"></param> /// <returns></returns> public static Variable ParseVariableParameter(XmlTextReader reader) { String name = reader.GetAttribute(ParameterAttributes.name.ToString()); String type = reader.GetAttribute(ParameterAttributes.type.ToString()); String comment = reader.GetAttribute(ParameterAttributes.comment.ToString()); String assumption = reader.GetAttribute(ParameterAttributes.assumption.ToString()); Variable p = new VariableParameter(name, (Variable.VarType)Enum.Parse(typeof(Variable.VarType), type, true), Variable.VarUpdateType.constant, assumption); return(p); }
public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer) { var dto = value switch { ConstantParameter constantParameter => new Dto(ParameterType.Constant, constantParameter.Value.Map <JToken>() ?? JValue.CreateNull()), VariableParameter variableParameter => new Dto(ParameterType.Variable, variableParameter.Path), ExpressionParameter expressionParameter => new Dto(ParameterType.Expression, expressionParameter.Code), _ => throw new NotImplementedException(), }; serializer.Serialize(writer, dto); }
public async Task ResolveReturnsValueFromContextAtPath() { // Arrange var path = "testing.test"; var parameter = new VariableParameter <int>(path); var flowContext = Mock.Of <IFlowContext>(o => o.GetData(path).Result == (object)69420); // Act var result = await parameter.Resolve(flowContext); // Assert result.Should().Be(69420); }
public static ReadOnlyParameter Interpolate(this VariableParameter givenVariable, HierarchicalVariableScope scope) { var builder = new StringBuilder(givenVariable.Value); List <string> variables; do { variables = VariablesInString(builder.ToString()).ToList(); foreach (string variable in variables) { var variableName = variable.Replace("${", "").Replace("}", ""); string replacement; if (variableName != givenVariable.Name) { var matchedVariable = scope.GetVariable(variableName); replacement = matchedVariable.Interpolate(scope).Value; } else if (scope.Parent != null) { var parentScope = scope.Parent; var matchedVariable = parentScope.GetVariable(variableName); replacement = matchedVariable.Interpolate(parentScope).Value; } else { replacement = null; } if (replacement == null) { //TODO: make this an explicit exception type throw new VariableException("Variable \"" + variable + "\" not defined"); } builder.Replace(variable, replacement);//TODO } } while (variables.Count != 0); return(new ReadOnlyParameter(givenVariable.Name, builder.ToString())); }
public double this[VariableParameter param] { get { switch (param) { case LensRayTransferFunction.VariableParameter.PositionTheta: return(PositionTheta); case LensRayTransferFunction.VariableParameter.PositionPhi: return(PositionPhi); case LensRayTransferFunction.VariableParameter.DirectionTheta: return(DirectionTheta); case LensRayTransferFunction.VariableParameter.DirectionPhi: return(DirectionPhi); default: throw new InvalidOperationException(); } } }
public bool Deserialize(IParser reader, Type expectedType, Func <IParser, Type, object?> nestedObjectDeserializer, out object?value) { value = default; if (expectedType != typeof(VariableParameter) && expectedType != typeof(ExpressionParameter)) { return(false); } var argument = reader.Consume <Scalar>(); if (expectedType == typeof(VariableParameter)) { value = new VariableParameter(argument.Value); } else if (expectedType == typeof(ExpressionParameter)) { value = new ExpressionParameter(argument.Value); } else { return(false); } return(true); }
public double this[VariableParameter param] { get { switch (param) { case LensRayTransferFunction.VariableParameter.PositionTheta: return PositionTheta; case LensRayTransferFunction.VariableParameter.PositionPhi: return PositionPhi; case LensRayTransferFunction.VariableParameter.DirectionTheta: return DirectionTheta; case LensRayTransferFunction.VariableParameter.DirectionPhi: return DirectionPhi; default: throw new InvalidOperationException(); } } }
/// <summary> /// Sample the LRTF with fixed position and direction phi parameters, /// ie. vary the respective theta parameters. /// </summary> /// <param name="sampleCount">Number of sample in each of the two /// variable dimensions.</param> /// <returns>Table of rays in parametrized representation.</returns> public Parameters[] SampleLrtf1D(LensRayTransferFunction.Parameters parameters, VariableParameter variableParam, int sampleCount) { Parameters[] table = new Parameters[sampleCount]; switch (variableParam) { case VariableParameter.PositionTheta: parameters.PositionTheta = 0; break; case VariableParameter.PositionPhi: parameters.PositionPhi = 0; break; case VariableParameter.DirectionTheta: parameters.DirectionTheta = 0; break; case VariableParameter.DirectionPhi: parameters.DirectionPhi = 0; break; default: break; } double param = 0.0; double step = 1 / (double)(sampleCount - 1); for (int i = 0; i < sampleCount; i++) { switch (variableParam) { case VariableParameter.PositionTheta: parameters.PositionTheta = param; break; case VariableParameter.PositionPhi: parameters.PositionPhi = param; break; case VariableParameter.DirectionTheta: parameters.DirectionTheta = param; break; case VariableParameter.DirectionPhi: parameters.DirectionPhi = param; break; default: break; } var outputParams = ComputeLrtf(parameters); if (!outputParams.IsDefined) { outputParams = null; } table[i] = outputParams; param += step; } return table; }
public override void Execute() { Interpreter.SetVariable(VariableParameter.GetResult(), default(bool)); }
public override void Execute() { Interpreter.SetVariable(VariableParameter.GetResult(), null); }
/// <summary> /// Sample the LRTF with fixed position and direction phi parameters, /// ie. vary the respective theta parameters. /// </summary> /// <param name="sampleCount">Number of sample in each of the two /// variable dimensions.</param> /// <returns>Table of rays in parametrized representation.</returns> public Parameters[] SampleLrtf1D(LensRayTransferFunction.Parameters parameters, VariableParameter variableParam, int sampleCount) { Parameters[] table = new Parameters[sampleCount]; switch (variableParam) { case VariableParameter.PositionTheta: parameters.PositionTheta = 0; break; case VariableParameter.PositionPhi: parameters.PositionPhi = 0; break; case VariableParameter.DirectionTheta: parameters.DirectionTheta = 0; break; case VariableParameter.DirectionPhi: parameters.DirectionPhi = 0; break; default: break; } double param = 0.0; double step = 1 / (double)(sampleCount - 1); for (int i = 0; i < sampleCount; i++) { switch (variableParam) { case VariableParameter.PositionTheta: parameters.PositionTheta = param; break; case VariableParameter.PositionPhi: parameters.PositionPhi = param; break; case VariableParameter.DirectionTheta: parameters.DirectionTheta = param; break; case VariableParameter.DirectionPhi: parameters.DirectionPhi = param; break; default: break; } var outputParams = ComputeLrtf(parameters); if (!outputParams.IsDefined) { outputParams = null; } table[i] = outputParams; param += step; } return(table); }
public long this[VariableParameter parameter] { get => State.TryGetValue(parameter, out var value) ? value : 0; set => State[parameter] = value;
public void Set(VariableParameter Value) { SetInterval(Value.Start, Value.End); }
ElementList(ElementBaseJson function) { _function = function; Name = function.CodeName(); Documentation = function.Documentation; Attributes.GetRestrictedCallTypes = this; if (function.Restricted != null) { _restricted = GetRestrictedCallTypeFromString(function.Restricted); } // Get the parameters. if (function.Parameters == null) { Parameters = new CodeParameter[0]; } else { Parameters = new CodeParameter[function.Parameters.Length]; } for (int i = 0; i < Parameters.Length; i++) { // Get the name and documentation. string name = function.Parameters[i].Name.Replace(" ", ""); string documentation = function.Parameters[i].Documentation; // If 'VariableReferenceIsGlobal' is not null, the parameter is a variable reference. if (function.Parameters[i].VariableReferenceIsGlobal != null) { // Set the parameter as a variable reference parameter. Parameters[i] = new VariableParameter( name, documentation, function.Parameters[i].VariableReferenceIsGlobal.Value ? VariableType.Global : VariableType.Player, new CodeTypeFromStringSolver("Any"), new VariableResolveOptions() { CanBeIndexed = false, FullVariable = true } ); } else // Not a variable reference parameter. { // The type of the parameter. string type = function.Parameters[i].Type ?? "Any"; // Get the default value. IWorkshopTree defaultValueWorkshop = null; ExpressionOrWorkshopValue defaultValue = null; if (function.Parameters[i].HasDefaultValue) { defaultValueWorkshop = function.Parameters[i].GetDefaultValue(); defaultValue = new ExpressionOrWorkshopValue(defaultValueWorkshop); } // Set the parameter. Parameters[i] = new CodeParameter(name, documentation, new CodeTypeFromStringSolver(type), defaultValue); // If the default parameter value is an Element and the Element is restricted, if (defaultValueWorkshop is Element parameterElement && parameterElement.Function.Restricted != null) { // ...then add the restricted call type to the parameter's list of restricted call types. Parameters[i].RestrictedCalls.Add(GetRestrictedCallTypeFromString(parameterElement.Function.Restricted)); } } } }