Exemplo n.º 1
0
    public void ToStringTest(string varName)
    {
        var sut    = new VariableCondition(varName);
        var actual = sut.ToString();

        actual.Should().Be(varName);
    }
 public VariableConditionItem(string groupID, string key, VariableCondition cond, string value)
 {
     this.groupID = groupID;
     this.key     = key;
     this.type    = VariableType.String;
     this.cond    = cond;
     this.vString = value;
 }
        public VariableConditionViewModel(VariableCondition condition) : base(condition)
        {
            Option = new OptionViewModel(condition, nameof(condition.Value));
            Text   = new TextViewModel(condition);

            Attach(Option);
            Attach(Text);
        }
 public VariableConditionItem(string groupID, string key, VariableCondition cond, float value)
 {
     this.groupID = groupID;
     this.key     = key;
     this.type    = VariableType.Float;
     this.cond    = cond;
     this.vFloat  = value;
 }
 public VariableConditionItem(string groupID, string key, VariableCondition cond, bool value)
 {
     this.groupID = groupID;
     this.key     = key;
     this.type    = VariableType.Bool;
     this.cond    = cond;
     this.vBool   = value;
 }
Exemplo n.º 6
0
        public override void DrawCommandGUI()
        {
            serializedObject.Update();

            EditorGUILayout.PropertyField(serializedObject.FindProperty("anyOrAllConditions"));

            conditions.arraySize = EditorGUILayout.IntField("Size", conditions.arraySize);
            GUILayout.Label("Conditions", EditorStyles.boldLabel);

            VariableCondition t = target as VariableCondition;

            var flowchart = (Flowchart)t.GetFlowchart();

            if (flowchart == null)
            {
                return;
            }

            EditorGUI.indentLevel++;
            for (int i = 0; i < conditions.arraySize; i++)
            {
                var conditionAnyVar  = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("anyVar");
                var conditionCompare = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("compareOperator");

                EditorGUILayout.PropertyField(conditionAnyVar, new GUIContent("Variable"), true);

                // Get selected variable
                Variable selectedVariable = conditionAnyVar.FindPropertyRelative("variable").objectReferenceValue as Variable;

                if (selectedVariable == null)
                {
                    continue;
                }

                GUIContent[] operatorsList = emptyList;
                operatorsList = selectedVariable.IsComparisonSupported() ? compareListAll : compareListEqualOnly;

                // Get previously selected operator
                int selectedIndex = conditionCompare.enumValueIndex;
                if (selectedIndex < 0 || selectedIndex >= operatorsList.Length)
                {
                    // Default to first index if the operator is not found in the available operators list
                    // This can occur when changing between variable types
                    selectedIndex = 0;
                }

                selectedIndex = EditorGUILayout.Popup(
                    new GUIContent("Compare", "The comparison operator to use when comparing values"),
                    selectedIndex,
                    operatorsList);

                conditionCompare.enumValueIndex = selectedIndex;

                EditorGUILayout.Separator();
            }
            EditorGUI.indentLevel--;
            serializedObject.ApplyModifiedProperties();
        }
        public void SynchronizeDataFromJson(JsonDto jto, Simulator sim)
        {
            var checkedProperties = new List <string>();

            ValidateAndUpdateValueAsNeeded(nameof(Device),
                                           checkedProperties,
                                           _device?.GetJsonReference().Guid, jto.Device.Guid,
                                           () => _device = sim.GetAssignableDeviceByGuid(jto.Device?.Guid) ??
                                                           throw new LPGException("Could not find a device with for  " + jto.Device));
            ValidateAndUpdateValueAsNeeded(nameof(Location),
                                           checkedProperties, Location?.Guid, jto.Location.Guid,
                                           () =>
                                           _location = sim.Locations.FindByGuid(jto.Location?.Guid));
            ValidateAndUpdateValueAsNeeded(nameof(TimeLimit),
                                           checkedProperties, TimeLimit?.Guid, jto.TimeLimit.Guid,
                                           () =>
                                           _timeLimit = sim.TimeLimits.FindByGuid(jto.TimeLimit?.Guid));
            ValidateAndUpdateValueAsNeeded(nameof(TimeProfile),
                                           checkedProperties,
                                           TimeProfile?.Guid, jto.TimeProfile?.Guid,
                                           () => _timeprofile = sim.Timeprofiles.FindByJsonReference(jto.TimeProfile));
            if (TimeStandardDeviation != jto.StandardDeviation)
            {
                _timeStandardDeviation = jto.StandardDeviation;
                NeedsUpdate            = true;
            }
            if (Variable?.Guid != jto.Variable?.Guid)
            {
                _variable   = sim.Variables.FindByGuid(jto.Variable?.Guid);
                NeedsUpdate = true;
            }
            if (VariableCondition != jto.VariableCondition)
            {
                _variableCondition = jto.VariableCondition;
                NeedsUpdate        = true;
            }
            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (VariableValue != jto.VariableValue)
            {
                _variableValue = jto.VariableValue;
                NeedsUpdate    = true;
            }
            if (LoadType?.Guid != jto.LoadType?.Guid)
            {
                _vLoadType  = sim.LoadTypes.FindByGuid(jto.Variable?.Guid);
                NeedsUpdate = true;
            }
            ValidateAndUpdateValueAsNeeded(nameof(Guid),
                                           checkedProperties,
                                           Guid, jto.Guid,
                                           () => Guid = jto.Guid);
            CheckIfAllPropertiesWereCovered(checkedProperties, this);
            SaveToDB();
        }
Exemplo n.º 8
0
    public void Evaluate(object value)
    {
        var variables = A.Fake <IVariableDictionary>(i => i.Strict());

        A.CallTo(() => variables["VarName"]).Returns(value);

        var sut    = new VariableCondition("VarName");
        var actual = sut.Evaluate(variables);

        actual.Should().Be(value);
    }
Exemplo n.º 9
0
 public VariableRequirementDto([NotNull] string name, double value, [NotNull] string location, StrGuid locationGuid,
                               VariableCondition variableCondition,
                               StrGuid variableGuid)
 {
     VariableGuid      = variableGuid;
     Name              = name;
     Value             = value;
     CalcLocationName  = location;
     LocationGuid      = locationGuid;
     VariableCondition = variableCondition;
 }
 public VariableRequirement([NotNull] string name, double value, [NotNull] string location, StrGuid locationGuid,
                            VariableCondition variableCondition, [NotNull] CalcVariableRepository repository,
                            StrGuid variableGuid)
 {
     _repository       = repository;
     _variableGuid     = variableGuid;
     Name              = name;
     Value             = value;
     CalcLocationName  = location;
     LocationGuid      = locationGuid;
     VariableCondition = variableCondition;
 }
 public JsonDto(StrGuid guid, JsonReference device, JsonReference location, JsonReference timeLimit, JsonReference timeProfile,
                decimal standardDeviation, JsonReference variable, VariableCondition variableCondition, double variableValue, JsonReference loadType)
 {
     Guid              = guid;
     Device            = device;
     Location          = location;
     TimeLimit         = timeLimit;
     TimeProfile       = timeProfile;
     StandardDeviation = standardDeviation;
     Variable          = variable;
     VariableCondition = variableCondition;
     VariableValue     = variableValue;
     LoadType          = loadType;
 }
Exemplo n.º 12
0
 public AffVariableRequirement(double value, [CanBeNull] int?id, [CanBeNull] int?affordanceID, [NotNull] string connectionString,
                               VariableLocationMode variableLocationMode, [CanBeNull] Location location, VariableCondition condition,
                               [CanBeNull] Variable variable,
                               [NotNull] string description, [NotNull] string name, StrGuid guid) : base(name, TableName, connectionString, guid)
 {
     _value                = value;
     ID                    = id;
     _affordanceID         = affordanceID;
     TypeDescription       = "Affordance Variable Requirement";
     _variableLocationMode = variableLocationMode;
     _location             = location;
     _condition            = condition;
     _variable             = variable;
     _description          = description;
 }
        public static bool CheckCondition(double currentVariableValue, VariableCondition variableCondition,
                                          double setValue)
        {
            var execute = true;

            switch (variableCondition)
            {
            case VariableCondition.Equal:
                if (Math.Abs(currentVariableValue - setValue) > 0.000001)
                {
                    execute = false;
                }
                break;

            case VariableCondition.EqualOrGreater:
                if (currentVariableValue < setValue)     // this is correct because >= inverted is <
                {
                    execute = false;
                }
                break;

            case VariableCondition.EqualOrLess:
                if (currentVariableValue > setValue)
                {
                    execute = false;
                }
                break;

            case VariableCondition.Greater:
                if (currentVariableValue <= setValue)
                {
                    execute = false;
                }
                break;

            case VariableCondition.Less:
                if (currentVariableValue >= setValue)
                {
                    execute = false;
                }
                break;

            default:
                throw new LPGException("Forgotten VariableCondition");
            }

            return(execute);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Transforms the tree model to a symbolic regression model
        /// </summary>
        /// <returns>A new symbolic regression model which matches the tree model</returns>
        public SymbolicRegressionModel CreateSymbolicRegressionModel()
        {
            var rootSy    = new ProgramRootSymbol();
            var startSy   = new StartSymbol();
            var varCondSy = new VariableCondition()
            {
                IgnoreSlope = true
            };
            var constSy = new Constant();

            var startNode = startSy.CreateTreeNode();

            startNode.AddSubtree(CreateSymbolicRegressionTreeRecursive(tree, 0, varCondSy, constSy));
            var rootNode = rootSy.CreateTreeNode();

            rootNode.AddSubtree(startNode);
            return(new SymbolicRegressionModel(TargetVariable, new SymbolicExpressionTree(rootNode), new SymbolicDataAnalysisExpressionTreeLinearInterpreter()));
        }
 public HHTAutonomousDevice([CanBeNull] int?pID, [CanBeNull] IAssignableDevice device,
                            [CanBeNull] TimeBasedProfile timeprofile, int householdTraitID, decimal timeStandardDeviation,
                            [CanBeNull] VLoadType vLoadType, [CanBeNull] TimeLimit timeLimit, [NotNull] string connectionString, [NotNull] string name,
                            [CanBeNull] Location location, double variableValue, VariableCondition variableCondition,
                            [CanBeNull] Variable variable, StrGuid guid) : base(name, TableName, connectionString, guid)
 {
     TypeDescription = "Household Trait Autonomous Device";
     ID                     = pID;
     _device                = device;
     _timeprofile           = timeprofile;
     _householdTraitID      = householdTraitID;
     _timeStandardDeviation = timeStandardDeviation;
     _vLoadType             = vLoadType;
     _timeLimit             = timeLimit;
     _location              = location;
     _variableValue         = variableValue;
     _variableCondition     = variableCondition;
     _variable              = variable;
 }
Exemplo n.º 16
0
 public HouseTypeDevice([CanBeNull] int?pID, [CanBeNull] IAssignableDevice adev, [CanBeNull] TimeBasedProfile profile,
                        int houseID,
                        [CanBeNull] TimeLimit timeLimit, double timeStandardDeviation, [CanBeNull] VLoadType loadType,
                        [NotNull] string connectionString, [NotNull] string name,
                        [CanBeNull] Location loc, double variableValue, VariableCondition variableCondition,
                        [CanBeNull] Variable variable, StrGuid guid)
     : base(name, TableName, connectionString, guid)
 {
     ID        = pID;
     _location = loc;
     _device   = adev;
     _profile  = profile;
     HouseID   = houseID;
     TimeLimit = timeLimit;
     _timeStandardDeviation = timeStandardDeviation;
     _loadType          = loadType;
     _variableValue     = variableValue;
     TypeDescription    = "House Type Device";
     _variableCondition = variableCondition;
     _variable          = variable;
 }
Exemplo n.º 17
0
        public ISymbolicExpressionTree ExtractTree(int treeIdx)
        {
            var rf = RandomForest;
            // hoping that the internal representation of alglib is stable

            // TREE FORMAT
            // W[Offs]      -   size of sub-array (for the tree)
            //     node info:
            // W[K+0]       -   variable number        (-1 for leaf mode)
            // W[K+1]       -   threshold              (class/value for leaf node)
            // W[K+2]       -   ">=" branch index      (absent for leaf node)

            // skip irrelevant trees
            int offset = 0;

            for (int i = 0; i < treeIdx - 1; i++)
            {
                offset = offset + (int)Math.Round(rf.innerobj.trees[offset]);
            }

            var constSy   = new Constant();
            var varCondSy = new VariableCondition()
            {
                IgnoreSlope = true
            };

            var node = CreateRegressionTreeRec(rf.innerobj.trees, offset, offset + 1, constSy, varCondSy);

            var startNode = new StartSymbol().CreateTreeNode();

            startNode.AddSubtree(node);
            var root = new ProgramRootSymbol().CreateTreeNode();

            root.AddSubtree(startNode);
            return(new SymbolicExpressionTree(root));
        }
        public static string ConvertToVariableDescription(VariableCondition tc)
        {
            var entries = MakeAllEntries();

            return(entries.First(x => x.VariableCondition == tc).Description);
        }
Exemplo n.º 19
0
        private ISymbolicExpressionTreeNode CreateSymbolicRegressionTreeRecursive(TreeNode[] treeNodes, int nodeIdx, VariableCondition varCondSy, Constant constSy)
        {
            var curNode = treeNodes[nodeIdx];

            if (curNode.VarName == TreeNode.NO_VARIABLE)
            {
                var node = (ConstantTreeNode)constSy.CreateTreeNode();
                node.Value = curNode.Val;
                return(node);
            }
            else
            {
                var node = (VariableConditionTreeNode)varCondSy.CreateTreeNode();
                node.VariableName = curNode.VarName;
                node.Threshold    = curNode.Val;

                var left  = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.LeftIdx, varCondSy, constSy);
                var right = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.RightIdx, varCondSy, constSy);
                node.AddSubtree(left);
                node.AddSubtree(right);
                return(node);
            }
        }
Exemplo n.º 20
0
        public ConditionalAction(VariableCondition variableCondition)
        {
            VariableCondition = variableCondition;

            Name = nameof(ConditionalAction);
        }
Exemplo n.º 21
0
 public static VariableCondition[] Remove(int index, VariableCondition[] list)
 {
     ArrayList tmp = new ArrayList();
     foreach(VariableCondition str in list) tmp.Add(str);
     tmp.RemoveAt(index);
     return tmp.ToArray(typeof(VariableCondition)) as VariableCondition[];
 }
Exemplo n.º 22
0
    public static VariableCondition VariableConditionSettings(VariableCondition vars)
    {
        vars.needed = (AIConditionNeeded)EditorGUILayout.EnumPopup("Needed",
                vars.needed, GUILayout.Width(mWidth));

        if(GUILayout.Button("Add Variable", GUILayout.Width(mWidth)))
        {
            vars.AddVariable();
        }
        for(int i=0; i<vars.variableKey.Length; i++)
        {
            EditorGUILayout.BeginHorizontal();
            if(GUILayout.Button("Remove", GUILayout.Width(mWidth*0.5f)))
            {
                vars.RemoveVariable(i);
                break;
            }
            vars.checkType[i] = EditorGUILayout.Toggle(
                    vars.checkType[i], GUILayout.Width(20));
            vars.variableKey[i] = EditorGUILayout.TextField(
                    vars.variableKey[i], GUILayout.Width(mWidth*0.5f));

            if(vars.checkType[i]) GUILayout.Label("== ");
            else GUILayout.Label(" != ");

            vars.variableValue[i] = EditorGUILayout.TextField(
                    vars.variableValue[i], GUILayout.Width(mWidth*0.5f));

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.Separator();

        if(GUILayout.Button("Add Number Variable", GUILayout.Width(mWidth)))
        {
            vars.AddNumberVariable();
        }
        for(int i=0; i<vars.numberVarKey.Length; i++)
        {
            EditorGUILayout.BeginHorizontal();
            if(GUILayout.Button("Remove", GUILayout.Width(mWidth*0.5f)))
            {
                vars.RemoveNumberVariable(i);
                break;
            }

            vars.numberCheckType[i] = EditorGUILayout.Toggle(
                    vars.numberCheckType[i], GUILayout.Width(20));
            vars.numberVarKey[i] = EditorGUILayout.TextField(
                    vars.numberVarKey[i], GUILayout.Width(mWidth*0.5f));
            if(!vars.numberCheckType[i]) GUILayout.Label("not");
            vars.numberValueCheck[i] = (ValueCheck)EditorGUILayout.EnumPopup(
                    vars.numberValueCheck[i], GUILayout.Width(mWidth*0.4f));
            vars.numberVarValue[i] = EditorGUILayout.FloatField(
                    vars.numberVarValue[i], GUILayout.Width(mWidth*0.5f));

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.Separator();
        return vars;
    }
Exemplo n.º 23
0
        public Models.VisualScript.Action GetAction()
        {
            var variableCondition = new VariableCondition(SelectedVariableName, SelectedObject.Name, ConditionType, ValueToCompare);

            return(new ConditionalAction(variableCondition));
        }
 public TiggerConditionEntry(VariableCondition variableCondition, [NotNull] string description)
 {
     VariableCondition = variableCondition;
     Description       = description;
 }
Exemplo n.º 25
0
        private ISymbolicExpressionTreeNode CreateRegressionTreeRec(double[] trees, int offset, int k, Constant constSy, VariableCondition varCondSy)
        {
            // alglib source for evaluation of one tree (dfprocessinternal)
            // offs = 0
            //
            // Set pointer to the root
            //
            // k = offs + 1;
            //
            // //
            // // Navigate through the tree
            // //
            // while (true) {
            //   if ((double)(df.trees[k]) == (double)(-1)) {
            //     if (df.nclasses == 1) {
            //       y[0] = y[0] + df.trees[k + 1];
            //     } else {
            //       idx = (int)Math.Round(df.trees[k + 1]);
            //       y[idx] = y[idx] + 1;
            //     }
            //     break;
            //   }
            //   if ((double)(x[(int)Math.Round(df.trees[k])]) < (double)(df.trees[k + 1])) {
            //     k = k + innernodewidth;
            //   } else {
            //     k = offs + (int)Math.Round(df.trees[k + 2]);
            //   }
            // }

            if ((double)(trees[k]) == (double)(-1))
            {
                var constNode = (ConstantTreeNode)constSy.CreateTreeNode();
                constNode.Value = trees[k + 1];
                return(constNode);
            }
            else
            {
                var condNode = (VariableConditionTreeNode)varCondSy.CreateTreeNode();
                condNode.VariableName = AllowedInputVariables[(int)Math.Round(trees[k])];
                condNode.Threshold    = trees[k + 1];
                condNode.Slope        = double.PositiveInfinity;

                var left  = CreateRegressionTreeRec(trees, offset, k + 3, constSy, varCondSy);
                var right = CreateRegressionTreeRec(trees, offset, offset + (int)Math.Round(trees[k + 2]), constSy, varCondSy);

                condNode.AddSubtree(left); // not 100% correct because interpreter uses: if(x <= thres) left() else right() and RF uses if(x < thres) left() else right() (see above)
                condNode.AddSubtree(right);
                return(condNode);
            }
        }
Exemplo n.º 26
0
        public override void DrawCommandGUI()
        {
            serializedObject.Update();

            VariableCondition t = target as VariableCondition;

            var flowchart = (Flowchart)t.GetFlowchart();

            if (flowchart == null)
            {
                return;
            }

            // Select Variable
            EditorGUILayout.PropertyField(variableProp);

            if (variableProp.objectReferenceValue == null)
            {
                serializedObject.ApplyModifiedProperties();
                return;
            }

            // Get selected variable
            Variable selectedVariable = variableProp.objectReferenceValue as Variable;

            System.Type variableType = selectedVariable.GetType();

            // Get operators for the variable
            CompareOperator[] compareOperators = VariableCondition.operatorsByVariableType[variableType];

            // Create operator list
            List <GUIContent> operatorsList = new List <GUIContent>();

            foreach (var compareOperator in compareOperators)
            {
                switch (compareOperator)
                {
                case CompareOperator.Equals:
                    operatorsList.Add(new GUIContent("=="));
                    break;

                case CompareOperator.NotEquals:
                    operatorsList.Add(new GUIContent("!="));
                    break;

                case CompareOperator.LessThan:
                    operatorsList.Add(new GUIContent("<"));
                    break;

                case CompareOperator.GreaterThan:
                    operatorsList.Add(new GUIContent(">"));
                    break;

                case CompareOperator.LessThanOrEquals:
                    operatorsList.Add(new GUIContent("<="));
                    break;

                case CompareOperator.GreaterThanOrEquals:
                    operatorsList.Add(new GUIContent(">="));
                    break;

                default:
                    Debug.LogError("The " + compareOperator.ToString() + " operator has no matching GUIContent.");
                    break;
                }
            }

            // Get previously selected operator
            int selectedIndex = System.Array.IndexOf(compareOperators, t._CompareOperator);

            if (selectedIndex < 0)
            {
                // Default to first index if the operator is not found in the available operators list
                // This can occur when changing between variable types
                selectedIndex = 0;
            }

            selectedIndex = EditorGUILayout.Popup(
                new GUIContent("Compare", "The comparison operator to use when comparing values"),
                selectedIndex,
                operatorsList.ToArray());

            compareOperatorProp.enumValueIndex = (int)compareOperators[selectedIndex];

            EditorGUILayout.PropertyField(propByVariableType[variableType]);

            serializedObject.ApplyModifiedProperties();
        }
Exemplo n.º 27
0
        public override void DrawCommandGUI()
        {
            serializedObject.Update();

            VariableCondition t = target as VariableCondition;

            var flowchart = (Flowchart)t.GetFlowchart();

            if (flowchart == null)
            {
                return;
            }

            EditorGUILayout.PropertyField(variableProp);

            if (variableProp.objectReferenceValue == null)
            {
                serializedObject.ApplyModifiedProperties();
                return;
            }

            Variable selectedVariable = variableProp.objectReferenceValue as Variable;

            System.Type variableType = selectedVariable.GetType();

            List <GUIContent> operatorList = new List <GUIContent>();

            operatorList.Add(new GUIContent("=="));
            operatorList.Add(new GUIContent("!="));
            if (variableType == typeof(IntegerVariable) ||
                variableType == typeof(FloatVariable))
            {
                operatorList.Add(new GUIContent("<"));
                operatorList.Add(new GUIContent(">"));
                operatorList.Add(new GUIContent("<="));
                operatorList.Add(new GUIContent(">="));
            }

            compareOperatorProp.enumValueIndex = EditorGUILayout.Popup(new GUIContent("Compare", "The comparison operator to use when comparing values"),
                                                                       compareOperatorProp.enumValueIndex,
                                                                       operatorList.ToArray());

            if (variableType == typeof(BooleanVariable))
            {
                EditorGUILayout.PropertyField(booleanDataProp);
            }
            else if (variableType == typeof(IntegerVariable))
            {
                EditorGUILayout.PropertyField(integerDataProp);
            }
            else if (variableType == typeof(FloatVariable))
            {
                EditorGUILayout.PropertyField(floatDataProp);
            }
            else if (variableType == typeof(StringVariable))
            {
                EditorGUILayout.PropertyField(stringDataProp);
            }

            serializedObject.ApplyModifiedProperties();
        }
Exemplo n.º 28
0
 public static VariableCondition[] Add(VariableCondition n, VariableCondition[] list)
 {
     ArrayList tmp = new ArrayList();
     foreach(VariableCondition str in list) tmp.Add(str);
     tmp.Add(n);
     return tmp.ToArray(typeof(VariableCondition)) as VariableCondition[];
 }