コード例 #1
0
        bool IScope.TryGetVariable(string name, out IVariableDefinition value)
        {
            var result = TryGetVariable(name, out var v);

            value = v;
            return(result);
        }
コード例 #2
0
 public AnalysisVariable(IVariableDefinition variable, VariableType type, ILocationInfo location, int?version = null)
 {
     Variable = variable;
     Location = location;
     Type     = type;
     Version  = version;
 }
コード例 #3
0
ファイル: EvaluationScope.cs プロジェクト: igitur/CQL
 /// <summary>
 /// Lookups a variable in this and parent scopes. Returns TRUE and a variable if found, FALSE otherwise.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="variable"></param>
 /// <returns></returns>
 public bool TryGetVariable(string name, out IVariableDefinition variable)
 {
     name = ScopeExtensions.NormalizeVariableName(name);
     return(thisMembers.TryGetValue(name, out variable) ||
            variables.TryGetValue(name, out variable) ||
            (Parent != null && Parent.TryGetVariable(name, out variable)));
 }
コード例 #4
0
        public bool IsInScope(IVariableDefinition variable)
        {
            if (variable == null)
            {
                return(false);
            }

            return(VariablesScopesStack.Any(s => s.Variables.Contains(variable)));
        }
コード例 #5
0
        bool IScope.TryGetVariable(string name, out IVariableDefinition value)
        {
            if (TryGetVariable(name, out var variableDef))
            {
                value = variableDef;
                return(true);
            }

            value = null;
            return(false);
        }
コード例 #6
0
        public static IVariableDefinition CreateVariable(object instance, PropertyInfo property)
        {
            var variableType             = typeof(VariableDefinition <>).MakeGenericType(property.PropertyType);
            IVariableDefinition variable = null;

            try
            {
                if (property.IsStatic())
                {
                    variable = (IVariableDefinition)Activator.CreateInstance(variableType, null, property);
                }
                else
                {
                    variable = (IVariableDefinition)Activator.CreateInstance(variableType, instance, property);
                }
            }
            catch { }

            return(variable);
        }
コード例 #7
0
 public void AddVariableToCurrentScope(IVariableDefinition variable)
 {
     VariablesScopesStack.Peek().Variables.Add(variable);
 }
コード例 #8
0
 public VariableSetNode(IVariableDefinition <TValue> variableDefinition)
 {
     this.variableDefinition = variableDefinition;
 }
コード例 #9
0
 public static string GetFormattedValue(this IVariableDefinition def)
 {
     return(Utility.GetStringRepresentation(def.Value));
 }
コード例 #10
0
        //---------------------------------------------------------------------

        protected override IInputParameters Parse()
        {
            ReadLandisDataVar();

            InputParameters parameters = new InputParameters(SpeciesDataset.Count);

            InputVar <int> timestep = new InputVar <int>("Timestep");

            ReadVar(timestep);
            parameters.Timestep = timestep.Value;

            const string  LocalVariables          = "LocalVariables";
            const string  DerivedLocalVariables   = "DerivedLocalVariables";
            const string  NeighborhoodVariables   = "NeighborhoodVariables";
            const string  ClimateVariables        = "ClimateVariables";
            const string  DistanceVariables       = "DistanceVariables";
            const string  SpeciesModels           = "SpeciesModels";
            const string  LocalVarMapFileNames    = "LocalVarMapFileNames";
            const string  NeighborVarMapFileNames = "NeighborVarMapFileNames";
            const string  ClimateVarMapFileNames  = "ClimateVarMapFileNames";
            const string  DistanceVarMapFileNames = "DistanceVarMapFileNames";
            const string  SpeciesMapFileName      = "SpeciesMapFileNames";
            const string  LogFile             = "LogFile";
            const string  SpeciesLogFileNames = "SpeciesLogFileNames";
            List <string> keywordList         = new List <string> {
                LocalVariables, DerivedLocalVariables, NeighborhoodVariables, ClimateVariables, DistanceVariables, SpeciesModels, LocalVarMapFileNames, NeighborVarMapFileNames, ClimateVarMapFileNames, DistanceVarMapFileNames, SpeciesMapFileName, LogFile, SpeciesLogFileNames
            };

            if (ReadOptionalName(LocalVariables))
            {
                InputVar <string> speciesName = new InputVar <string>("Species");

                Dictionary <string, int> lineNumbers = new Dictionary <string, int>();

                InputVar <string> mapName    = new InputVar <string>("Map Name");
                InputVar <string> forestType = new InputVar <string>("Forest Type");
                InputVar <string> ageKeyword = new InputVar <string>("Age Keyword");
                InputVar <int>    minAge     = new InputVar <int>("Min Age");
                InputVar <int>    maxAge     = new InputVar <int>("Max Age");

                lineNumbers.Clear();
                Dictionary <string, int> forestTypeLineNumbers = new Dictionary <string, int>();


                const string nameDelimiter = "->";  // delimiter that separates map name and forest type

                IMapDefinition mapDefn = null;

                while (!AtEndOfInput && !keywordList.Contains(CurrentName))
                {
                    StringReader currentLine = new StringReader(CurrentLine);

                    //  If the current line has the delimiter, then read the map
                    //  name.
                    if (CurrentLine.Contains(nameDelimiter))
                    {
                        ReadValue(mapName, currentLine);
                        CheckForRepeatedName(mapName.Value, "map name", lineNumbers);

                        mapDefn      = new MapDefinition();
                        mapDefn.Name = mapName.Value;
                        parameters.ReclassMaps.Add(mapDefn);

                        TextReader.SkipWhitespace(currentLine);
                        string word = TextReader.ReadWord(currentLine);
                        if (word != nameDelimiter)
                        {
                            throw NewParseException("Expected \"{0}\" after the map name {1}.",
                                                    nameDelimiter, mapName.Value.String);
                        }

                        forestTypeLineNumbers.Clear();
                    }
                    else
                    {
                        //  If there is no name delimiter and we don't have the
                        //  name for the first map yet, then it's an error.
                        if (mapDefn == null)
                        {
                            throw NewParseException("Expected a line with map name followed by \"{0}\"", nameDelimiter);
                        }
                    }

                    ReadValue(forestType, currentLine);
                    CheckForRepeatedName(forestType.Value, "forest type",
                                         forestTypeLineNumbers);

                    IForestType currentForestType = new ForestType(SpeciesDataset.Count);
                    currentForestType.Name = forestType.Value;
                    mapDefn.ForestTypes.Add(currentForestType);

                    // Read the age ranges for the species:
                    ReadValue(ageKeyword, currentLine);
                    if (ageKeyword.Value == "All")
                    {
                        currentForestType.MinAge = 0;
                        int maxAgeAllSpecies = 0;
                        foreach (ISpecies species in PlugIn.ModelCore.Species)
                        {
                            if (species.Longevity > maxAgeAllSpecies)
                            {
                                maxAgeAllSpecies = species.Longevity;
                            }
                        }
                        currentForestType.MaxAge = maxAgeAllSpecies;
                    }
                    else
                    {
                        //ReadValue(minAge, currentLine);
                        currentForestType.MinAge = Convert.ToInt32(ageKeyword.Value);

                        TextReader.SkipWhitespace(currentLine);
                        string currentWord = TextReader.ReadWord(currentLine);
                        if (currentWord != "to")
                        {
                            StringBuilder message = new StringBuilder();
                            message.AppendFormat("Expected \"to\" after the minimum age ({0})",
                                                 minAge.Value.String);
                            if (currentWord.Length > 0)
                            {
                                message.AppendFormat(", but found \"{0}\" instead", currentWord);
                            }
                            throw NewParseException(message.ToString());
                        }

                        ReadValue(maxAge, currentLine);
                        currentForestType.MaxAge = maxAge.Value;
                    }
                    //  Read species for forest types

                    List <string> speciesNames = new List <string>();

                    TextReader.SkipWhitespace(currentLine);
                    while (currentLine.Peek() != -1)
                    {
                        ReadValue(speciesName, currentLine);
                        string name = speciesName.Value.Actual;
                        bool   negativeMultiplier = name.StartsWith("-");
                        if (negativeMultiplier)
                        {
                            name = name.Substring(1);
                            if (name.Length == 0)
                            {
                                throw new InputValueException(speciesName.Value.String,
                                                              "No species name after \"-\"");
                            }
                        }
                        if (name == "All")
                        {
                            foreach (ISpecies species in PlugIn.ModelCore.Species)
                            {
                                speciesNames.Add(species.Name);
                                currentForestType[species.Index] = 1;
                            }
                        }
                        else if (name == "None")
                        {
                            foreach (ISpecies species in PlugIn.ModelCore.Species)
                            {
                                speciesNames.Add(species.Name);
                                currentForestType[species.Index] = 0;
                            }
                        }
                        else
                        {
                            ISpecies species = GetSpecies(new InputValue <string>(name, speciesName.Value.String));
                            if (speciesNames.Contains(species.Name))
                            {
                                throw NewParseException("The species {0} appears more than once.", species.Name);
                            }
                            speciesNames.Add(species.Name);
                            currentForestType[species.Index] = negativeMultiplier ? -1 : 1;
                        }

                        TextReader.SkipWhitespace(currentLine);
                    }
                    if (speciesNames.Count == 0)
                    {
                        throw NewParseException("At least one species is required.");
                    }

                    GetNextLine();
                }
            }
            if (ReadOptionalName(DerivedLocalVariables))
            {
                Dictionary <string, int> lineNumbers = new Dictionary <string, int>();
                lineNumbers.Clear();
                InputVar <string> derVarName    = new InputVar <string>("Derived Variable Name");
                InputVar <string> derVarFormula = new InputVar <string>("Derived Variable Formula");
                const string      nameDelimiter = "->"; // delimiter that separates map name and forest type
                List <string>     formulaSymbol = new List <string>(new string[] { "+", "-", "*" });

                IVariableDefinition varDefn = null;
                while (!AtEndOfInput && !keywordList.Contains(CurrentName))
                {
                    StringReader currentLine = new StringReader(CurrentLine);

                    ReadValue(derVarName, currentLine);
                    CheckForRepeatedName(derVarName.Value, "var name", lineNumbers);

                    varDefn      = new VariableDefinition();
                    varDefn.Name = derVarName.Value;

                    TextReader.SkipWhitespace(currentLine);
                    string word = TextReader.ReadWord(currentLine);
                    if (word != nameDelimiter)
                    {
                        throw NewParseException("Expected \"{0}\" after the map name {1}.",
                                                nameDelimiter, derVarName.Value.String);
                    }

                    TextReader.SkipWhitespace(currentLine);
                    string variable = TextReader.ReadWord(currentLine);
                    varDefn.Variables.Add(variable);
                    while (currentLine.Peek() != -1)
                    {
                        TextReader.SkipWhitespace(currentLine);
                        string op = TextReader.ReadWord(currentLine);
                        if (op == "")
                        {
                            break;
                        }
                        varDefn.Operators.Add(op);

                        TextReader.SkipWhitespace(currentLine);
                        variable = TextReader.ReadWord(currentLine);
                        varDefn.Variables.Add(variable);
                    }
                    parameters.DerivedVars.Add(varDefn);

                    GetNextLine();
                }
            }
            if (ReadOptionalName(NeighborhoodVariables))
            {
                // Read Neighborhood Variables
                Dictionary <string, int> lineNumbers = new Dictionary <string, int>();
                lineNumbers.Clear();
                InputVar <string> neighborVarName = new InputVar <string>("Neighbor Variable Name");
                InputVar <string> localVarName    = new InputVar <string>("Local Variable Name");
                InputVar <int>    neighborRadius  = new InputVar <int>("Neighbor Radius");
                InputVar <string> transform       = new InputVar <string>("Transform");

                INeighborVariableDefinition neighborVarDefn = null;
                while (!AtEndOfInput && !keywordList.Contains(CurrentName))
                {
                    StringReader currentLine = new StringReader(CurrentLine);

                    ReadValue(neighborVarName, currentLine);
                    CheckForRepeatedName(neighborVarName.Value, "var name", lineNumbers);

                    neighborVarDefn      = new NeighborVariableDefinition();
                    neighborVarDefn.Name = neighborVarName.Value;

                    ReadValue(localVarName, currentLine);
                    neighborVarDefn.LocalVariable = localVarName.Value;

                    ReadValue(neighborRadius, currentLine);
                    neighborVarDefn.NeighborRadius = neighborRadius.Value;

                    ReadValue(transform, currentLine);
                    neighborVarDefn.Transform = transform.Value;

                    parameters.NeighborVars.Add(neighborVarDefn);
                    GetNextLine();
                }
            }
            if (ReadOptionalName(ClimateVariables))
            {
                // Read Climate Variables
                Dictionary <string, int> lineNumbers = new Dictionary <string, int>();
                lineNumbers.Clear();
                InputVar <string> climateVarName        = new InputVar <string>("Climate Variable Name");
                InputVar <string> climateLibraryVarName = new InputVar <string>("Climate Library Variable Name");
                InputVar <string> sourceName            = new InputVar <string>("Source Name");
                InputVar <string> varYear   = new InputVar <string>("Variable Year");
                InputVar <int>    minMonth  = new InputVar <int>("Min Month");
                InputVar <int>    maxMonth  = new InputVar <int>("Max Month");
                InputVar <string> transform = new InputVar <string>("Tranformation");

                IClimateVariableDefinition climateVarDefn = null;
                while (!AtEndOfInput && !keywordList.Contains(CurrentName))
                {
                    StringReader currentLine = new StringReader(CurrentLine);
                    ReadValue(climateVarName, currentLine);
                    CheckForRepeatedName(climateVarName.Value, "var name", lineNumbers);

                    climateVarDefn      = new ClimateVariableDefinition();
                    climateVarDefn.Name = climateVarName.Value;

                    ReadValue(varYear, currentLine);
                    climateVarDefn.Year = varYear.Value;

                    ReadValue(minMonth, currentLine);
                    climateVarDefn.MinMonth = minMonth.Value;

                    TextReader.SkipWhitespace(currentLine);
                    string currentWord = TextReader.ReadWord(currentLine);
                    if (currentWord != "to")
                    {
                        StringBuilder message = new StringBuilder();
                        message.AppendFormat("Expected \"to\" after the minimum month ({0})",
                                             minMonth.Value.String);
                        if (currentWord.Length > 0)
                        {
                            message.AppendFormat(", but found \"{0}\" instead", currentWord);
                        }
                        throw NewParseException(message.ToString());
                    }

                    ReadValue(maxMonth, currentLine);
                    climateVarDefn.MaxMonth = maxMonth.Value;

                    ReadValue(sourceName, currentLine);
                    climateVarDefn.SourceName = sourceName.Value;

                    ReadValue(climateLibraryVarName, currentLine);
                    climateVarDefn.ClimateLibVariable = climateLibraryVarName.Value;

                    ReadValue(transform, currentLine);
                    climateVarDefn.Transform = transform.Value;

                    parameters.ClimateVars.Add(climateVarDefn);
                    GetNextLine();
                }
            }
            if (ReadOptionalName(DistanceVariables))
            {
                // Read Distance Variables
                Dictionary <string, int> lineNumbers = new Dictionary <string, int>();
                lineNumbers.Clear();
                InputVar <string> distanceVarName = new InputVar <string>("Distance Variable Name");
                InputVar <string> localVarName    = new InputVar <string>("Local Variable Name");
                InputVar <string> transform       = new InputVar <string>("Transform");

                IDistanceVariableDefinition distanceVarDefn = null;
                while (!AtEndOfInput && !keywordList.Contains(CurrentName))
                {
                    StringReader currentLine = new StringReader(CurrentLine);

                    ReadValue(distanceVarName, currentLine);
                    CheckForRepeatedName(distanceVarName.Value, "var name", lineNumbers);

                    distanceVarDefn      = new DistanceVariableDefinition();
                    distanceVarDefn.Name = distanceVarName.Value;

                    ReadValue(localVarName, currentLine);
                    distanceVarDefn.LocalVariable = localVarName.Value;

                    ReadValue(transform, currentLine);
                    distanceVarDefn.Transform = transform.Value;

                    parameters.DistanceVars.Add(distanceVarDefn);
                    GetNextLine();
                }
            }
            // Read species models
            ReadName(SpeciesModels);

            InputVar <string> birdName   = new InputVar <string>("Bird Name");
            InputVar <string> parameter  = new InputVar <string>("Parameter");
            InputVar <string> paramType  = new InputVar <string>("Parameter Type");
            InputVar <double> paramValue = new InputVar <double>("Parameter Value");


            Dictionary <string, int> speciesLineNumbers = new Dictionary <string, int>();

            speciesLineNumbers.Clear();

            const string speciesNameDelimiter = "->";  // delimiter that separates map name and forest type

            IModelDefinition modelDefn = null;

            while (!AtEndOfInput && !keywordList.Contains(CurrentName))
            {
                StringReader currentLine = new StringReader(CurrentLine);

                //  If the current line has the delimiter, then read the map
                //  name.
                if (CurrentLine.Contains(speciesNameDelimiter))
                {
                    ReadValue(birdName, currentLine);
                    CheckForRepeatedName(birdName.Value, "bird name", speciesLineNumbers);

                    modelDefn      = new ModelDefinition();
                    modelDefn.Name = birdName.Value;
                    parameters.Models.Add(modelDefn);

                    TextReader.SkipWhitespace(currentLine);
                    string word = TextReader.ReadWord(currentLine);
                    if (word != speciesNameDelimiter)
                    {
                        throw NewParseException("Expected \"{0}\" after the map name {1}.",
                                                speciesNameDelimiter, birdName.Value.String);
                    }

                    speciesLineNumbers.Clear();
                }
                else
                {
                    //  If there is no name delimiter and we don't have the
                    //  name for the first map yet, then it's an error.
                    if (modelDefn == null)
                    {
                        throw NewParseException("Expected a line with map name followed by \"{0}\"", speciesNameDelimiter);
                    }
                }

                // Read the parameter name:
                ReadValue(parameter, currentLine);
                modelDefn.Parameters.Add(parameter.Value);

                // Read the parameter types:
                ReadValue(paramType, currentLine);
                modelDefn.ParamTypes.Add(paramType.Value);

                // Read the parameter value:
                ReadValue(paramValue, currentLine);
                modelDefn.Values.Add(paramValue.Value);

                GetNextLine();
            }

            // Template for filenames of maps
            InputVar <string> localVarMapFileNames = new InputVar <string>(LocalVarMapFileNames);
            bool readLocalMaps = false;

            if (ReadOptionalVar(localVarMapFileNames))
            {
                parameters.LocalVarMapFileNames = localVarMapFileNames.Value;
                readLocalMaps = true;
            }

            InputVar <string> neighborMapFileNames = new InputVar <string>(NeighborVarMapFileNames);
            bool readNeighborMaps = false;

            if (ReadOptionalVar(neighborMapFileNames))
            {
                parameters.NeighborMapFileNames = neighborMapFileNames.Value;
                readNeighborMaps = true;
            }

            InputVar <string> climateMapFileNames = new InputVar <string>(ClimateVarMapFileNames);
            bool readClimateMaps = false;

            if (ReadOptionalVar(climateMapFileNames))
            {
                parameters.ClimateMapFileNames = climateMapFileNames.Value;
                readClimateMaps = true;
            }

            InputVar <string> distanceMapFileNames = new InputVar <string>(DistanceVarMapFileNames);
            bool readDistanceMaps = false;

            if (ReadOptionalVar(distanceMapFileNames))
            {
                parameters.DistanceMapFileNames = distanceMapFileNames.Value;
                readDistanceMaps = true;
            }

            InputVar <string> speciesMapFileNames = new InputVar <string>(SpeciesMapFileName);
            bool readSpeciesMaps = false;

            if (ReadOptionalVar(speciesMapFileNames))
            {
                parameters.SpeciesMapFileNames = speciesMapFileNames.Value;
                readSpeciesMaps = true;
            }
            InputVar <string> speciesLogFileNames = new InputVar <string>(SpeciesLogFileNames);
            bool readSpeciesLogs = false;

            if (ReadOptionalVar(speciesLogFileNames))
            {
                parameters.SpeciesLogFileNames = speciesLogFileNames.Value;
                readSpeciesLogs = true;
            }
            InputVar <string> logFile = new InputVar <string>(LogFile);
            bool readLogFile          = false;

            if (ReadOptionalVar(logFile))
            {
                parameters.LogFileName = logFile.Value;
                readLogFile            = true;
            }

            if (readLogFile)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", LogFile));
            }
            else if (readSpeciesLogs)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", SpeciesLogFileNames));
            }
            else if (readSpeciesMaps)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", SpeciesMapFileName));
            }
            else if (readDistanceMaps)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", DistanceVarMapFileNames));
            }
            else if (readClimateMaps)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", ClimateVarMapFileNames));
            }
            else if (readNeighborMaps)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", NeighborVarMapFileNames));
            }
            else if (readLocalMaps)
            {
                CheckNoDataAfter(string.Format("the {0} parameter", LocalVarMapFileNames));
            }
            else
            {
                CheckNoDataAfter(string.Format("the {0} parameter", SpeciesModels));
            }

            return(parameters); //.GetComplete();
        }
コード例 #11
0
 public VariableDefTestInfo(IVariableDefinition variableDef, string name, IScope scope)
 {
     _variableDef = variableDef;
     Name         = name;
     _scope       = scope;
 }
コード例 #12
0
 /// <summary>
 /// Converts a evaluation into a validation variable.
 /// </summary>
 /// <param name="this"></param>
 /// <returns></returns>
 public static IVariableDeclaration ToValidationVariable(this IVariableDefinition @this)
 {
     return(new VariableDeclaration(@this.Name, @this.Value.GetType()));
 }
コード例 #13
0
ファイル: ScopeExtensions.cs プロジェクト: Lotes/CQL
 /// <summary>
 /// Lookup THIS
 /// </summary>
 /// <param name="this"></param>
 /// <param name="variable"></param>
 /// <returns></returns>
 public static bool TryGetThis(this IEvaluationScope @this, out IVariableDefinition variable)
 {
     return(@this.TryGetVariable(ThisName, out variable));
 }
コード例 #14
0
 public string GetFormCheckBoxName(IVariableDefinition v, DataSourceDefinition ds)
 {
     return(String.Concat("variable_", v.Name, "_", ds.ID.ToString()));
 }
コード例 #15
0
        public VariableGetNode(IVariableDefinition <TValue> variableDefinition)
        {
            this.variableDefinition = variableDefinition;

            outlet = new PullValueOutlet <TValue>(variableDefinition.Getter);
        }