private void CheckGeneralAffordanceSettings([NotNull] Affordance affordance)
        {
            if (affordance.AffordanceDesires.Count == 0)
            {
                throw new DataIntegrityException(
                          "The affordance " + affordance.Name +
                          " has no desires and will therefore never be executed. Please either delete the affordance or add some desires.",
                          affordance);
            }
            if (affordance.AffordanceDevices.Count == 0)
            {
                throw new DataIntegrityException(
                          "The affordance " + affordance.Name +
                          " has no devices and will therefore not be executed. Please either delete the affordance or add some devices.",
                          affordance);
            }
            if (affordance.TimeLimit == null)
            {
                throw new DataIntegrityException("Affordance " + affordance.Name + " has no time limit!", affordance);
            }
            if (string.IsNullOrEmpty(affordance.AffCategory))
            {
                throw new DataIntegrityException(
                          "In the affordance " + affordance.Name +
                          " the affordance category is empty! This is needed for statistical reasons.", affordance);
            }

            if (affordance.Blue == 255 && affordance.Green == 255 && affordance.Red == 255 && PerformCleanupChecks)
            {
                throw new DataIntegrityException(
                          "The affordance " + affordance.Name +
                          " has white set as color. This is impossible to read, so please fix.", affordance);
            }
        }
Ejemplo n.º 2
0
        private int Comparison([NotNull] Affordance affordance, [NotNull] Affordance affordance1)
        {
            if (affordance == null)
            {
                throw new LPGException("affordance was null");
            }

            switch (SortBy)
            {
            case "By Name":
                return(string.Compare(affordance.Name, affordance1.Name, StringComparison.Ordinal));

            case "By Category":
                var result = string.Compare(affordance.AffCategory, affordance1.AffCategory,
                                            StringComparison.Ordinal);
                if (result == 0)
                {
                    result = affordance.CompareColor(affordance1);
                }

                return(result);

            default:
                throw new LPGException("Unknown Sort By");
            }
        }
        public void LoadFromDatabaseTest()
        {
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                var profiles    = db.LoadTimeBasedProfiles();
                var realDevices = db.LoadRealDevices(out var deviceCategories, out var loadTypes,
                                                     profiles);
                var timeBasedProfiles = db.LoadTimeBasedProfiles();
                var desires           = db.LoadDesires();

                var affordances       = new ObservableCollection <Affordance>();
                var subaffordances    = new ObservableCollection <SubAffordance>();
                var dateBasedProfiles = db.LoadDateBasedProfiles();

                var timeLimits         = db.LoadTimeLimits(dateBasedProfiles);
                var deviceActionGroups = db.LoadDeviceActionGroups();

                var deviceActions = db.LoadDeviceActions(timeBasedProfiles, realDevices,
                                                         loadTypes, deviceActionGroups);
                var locations = db.LoadLocations(realDevices, deviceCategories, loadTypes);
                var variables = db.LoadVariables();
                SubAffordance.LoadFromDatabase(subaffordances, db.ConnectionString, desires, false, locations, variables);
                Affordance.LoadFromDatabase(affordances, db.ConnectionString, timeBasedProfiles, deviceCategories,
                                            realDevices, desires, subaffordances, loadTypes, timeLimits, deviceActions, deviceActionGroups,
                                            locations, false, variables);

                db.Cleanup();
            }
        }
        private static void CheckValidLoadtypes([NotNull] Affordance affordance, [NotNull][ItemNotNull] ObservableCollection <RealDevice> devices)
        {
            foreach (var affordanceDevice in affordance.AffordanceDevices)
            {
                if (affordanceDevice.Device == null)
                {
                    continue;
                }
                switch (affordanceDevice.Device.AssignableDeviceType)
                {
                case AssignableDeviceType.Device:
                    CheckValidLoadtypesOnDevices(affordanceDevice, affordance);
                    break;

                case AssignableDeviceType.DeviceCategory:
                    CheckValidLoadTypesForDeviceCategories(devices, affordanceDevice, affordance);
                    break;

                case AssignableDeviceType.DeviceAction:     // dont need to check those
                    break;

                case AssignableDeviceType.DeviceActionGroup:
                    break;

                default:
                    throw new LPGException("Forgot an AssignableDeviceType");
                }
            }
        }
Ejemplo n.º 5
0
        static Dictionary <Condition, List <System.Type> > getRelations(Affordance action, List <Affordance> actions)
        {
            Dictionary <Condition, List <System.Type> > relations = new Dictionary <Condition, List <System.Type> > ();

            foreach (Condition preCond in action.getPreconditions())
            {
                foreach (Affordance act in actions)
                {
                    foreach (Condition effect in act.getEffects())
                    {
                        if ((effect.condition.Equals(preCond.condition)) && (effect.status == preCond.status))
                        {
                            if (relations.ContainsKey(preCond))
                            {
                                if (!relations[preCond].Contains(act.GetType()))
                                {
                                    relations[preCond].Add(act.GetType());
                                }
                            }
                            else
                            {
                                List <System.Type> listActions = new List <System.Type> ();
                                listActions.Add(act.GetType());
                                relations.Add(preCond, listActions);
                            }

                            //addToDictionary(preCond, act.GetType (), out relations);
                        }
                    }
                }
            }

            return(relations);
        }
        public void IsAffordanceAvailableTestRealDeviceIsThere()
        {
            // Location: realdevice
            // affordance: same realdevice
            Config.IsInUnitTesting = true;
            var col           = new ColorRGB(255, 0, 0);
            var deviceActions = new ObservableCollection <DeviceAction>();
            var aff           = new Affordance("bla", null, null, false, PermittedGender.All, 1, col, string.Empty, null,
                                               string.Empty, string.Empty,
                                               true, true, 0, 100, false, ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
            var rd1         = new RealDevice("rd1", 1, string.Empty, null, string.Empty, false, false, string.Empty, Guid.NewGuid().ToStrGuid());
            var allDevices1 = new List <IAssignableDevice>
            {
                // check if only the device is there
                rd1
            };
            var connectionString = string.Empty;
            var lt = new VLoadType("lt", string.Empty, "bla", "blub", 1, 1, new TimeSpan(0, 1, 0), 1, connectionString,
                                   LoadTypePriority.Mandatory, true, Guid.NewGuid().ToStrGuid());
            var tbp = new TimeBasedProfile("name", 1, connectionString, TimeProfileType.Absolute, "data source", Guid.NewGuid().ToStrGuid());

            aff.AffordanceDevices.Add(new AffordanceDevice(rd1, tbp, null, 0, null,
                                                           new ObservableCollection <RealDevice>(),
                                                           new ObservableCollection <DeviceCategory>(), "name", lt, string.Empty, 1, Guid.NewGuid().ToStrGuid()));
            (aff.IsAffordanceAvailable(allDevices1, deviceActions)).Should().BeTrue();
        }
Ejemplo n.º 7
0
        private static int CalcDiff([NotNull] Affordance a1, [NotNull] Affordance a2)
        {
            var count = Math.Abs(a1.Red - a2.Red);

            count += Math.Abs(a1.Blue - a2.Blue);
            count += Math.Abs(a1.Green - a2.Green);
            return(count);
        }
 public JsonDto GetJson()
 {
     return(new JsonDto(Affordance?.GetJsonReference(),
                        TimeLimit?.GetJsonReference(), Weight,
                        StartMinusMinutes,
                        StartPlusMinutes,
                        EndMinusMinutes, EndPlusMinutes, Guid));
 }
        private void CheckOverlappingDeviceProfiles([NotNull] Affordance affordance,
                                                    [NotNull][ItemNotNull] ObservableCollection <DeviceAction> allDeviceActions, Random rnd)
        {
            //find overlapping device profiles
            var tpes = new List <TimeProfileEntry>();

            foreach (var affdev in affordance.AffordanceDevices)
            {
                if (affdev.Device == null)
                {
                    continue;
                }
                switch (affdev.Device.AssignableDeviceType)
                {
                case AssignableDeviceType.Device:
                case AssignableDeviceType.DeviceCategory:
                    tpes.Add(MakeTimeProfileEntryFromDevice(affdev, affordance, rnd));
                    break;

                case AssignableDeviceType.DeviceAction:
                    var da = (DeviceAction)affdev.Device;
                    tpes.AddRange(MakeTimeProfileEntryFromDeviceAction(da, affordance, affdev, rnd));
                    break;

                case AssignableDeviceType.DeviceActionGroup:
                    var dag = (DeviceActionGroup)affdev.Device;
                    var das = dag.GetDeviceActions(allDeviceActions);
                    tpes.AddRange(MakeTimeProfileEntryFromDeviceAction(das[0], affordance, affdev, rnd));
                    break;

                default:
                    throw new LPGException("Forgotten AssignableDeviceType");
                }
            }
            var maxTime = 0;

            foreach (var timeProfileEntry in tpes)
            {
                var duration = timeProfileEntry.OffSet + timeProfileEntry.NewLength;
                if (duration > maxTime)
                {
                    maxTime = duration;
                }
            }
            var keys        = tpes.Select(x => x.Key).Distinct().ToList();
            var busySignals = new Dictionary <string, BitArray>();

            foreach (var key in keys)
            {
                busySignals.Add(key, new BitArray(maxTime));
            }
            foreach (var tpe in tpes)
            {
                SetBusyBits(tpe, busySignals);
            }
        }
 public AffordanceTaggingEntry([NotNull] string name, int taggingSetID, [CanBeNull] AffordanceTag tag,
                               [CanBeNull] Affordance affordance, [NotNull] string connectionString, [CanBeNull] int?pID, StrGuid guid)
     : base(name, pID, TableName,
            connectionString, guid)
 {
     _taggingSetID   = taggingSetID;
     _tag            = tag;
     _affordance     = affordance;
     TypeDescription = "Affordance Tagging Entry";
 }
 public TimeProfileEntry([NotNull] AffordanceDevice affDev, int newLength, [NotNull] VLoadType loadType, int offSet,
                         [NotNull] Affordance affordance, double timeFactor, [NotNull] string deviceName)
 {
     AffDev     = affDev;
     NewLength  = newLength;
     LoadType   = loadType;
     OffSet     = offSet;
     Affordance = affordance;
     TimeFactor = timeFactor;
     DeviceName = deviceName;
 }
        private static void CheckAffordancesForStandbyDevices([NotNull] Affordance aff,
                                                              [NotNull][ItemNotNull] ObservableCollection <DeviceAction> actions)
        {
            var standbyDevices = new List <RealDevice>();

            foreach (var standby in aff.AffordanceStandbys)
            {
                if (standby.Device == null)
                {
                    throw new DataIntegrityException("Standby device was null");
                }
                standbyDevices.AddRange(standby.Device.GetRealDevices(actions));
            }
            var totalMessage = string.Empty;

            foreach (var device in aff.AffordanceDevices)
            {
                if (device.Device == null)
                {
                    throw new DataIntegrityException("Device was null");
                }
                if (device.Device.IsOrContainsStandbyDevice(actions))
                {
                    var foundAll       = true;
                    var affdevs        = device.Device.GetRealDevices(actions);
                    var missingDevices = string.Empty;
                    foreach (var realDevs in affdevs)
                    {
                        if (!standbyDevices.Contains(realDevs))
                        {
                            foundAll        = false;
                            missingDevices += realDevs.Name + ", ";
                        }
                    }
                    if (!foundAll)
                    {
                        missingDevices = missingDevices.Substring(0, missingDevices.Length - 2);
                        Logger.Warning(device.Device.IsOrContainsStandbyDevice(actions).ToString());
                        totalMessage += "The affordance " + aff.Name + " has the device " + device.Device.Name +
                                        ", which is marked as having a standby power use." +
                                        " But the affordance doesn't have that device as a required standby device. Please fix." + Environment.NewLine +
                                        "If there is already standby set for this, then maybe it's missing some devices. The missing devices are:" + Environment.NewLine +
                                        missingDevices + Environment.NewLine;
                    }
                }
            }

            if (totalMessage.Length > 0)
            {
                throw new DataIntegrityException(totalMessage, aff);
            }
        }
        public void CalculateAverageEnergyUseTestDeviceAction()
        {
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                Config.IsInUnitTesting = true;
                var col     = new ColorRGB(255, 0, 0);
                var devices = new ObservableCollection <RealDevice>();
                var rd2     = new RealDevice("rd2", 1, string.Empty, null, string.Empty, false, false, db.ConnectionString, Guid.NewGuid().ToStrGuid());
                var lt      = new VLoadType("lt", string.Empty, "bla", "blub", 1, 1, new TimeSpan(0, 1, 0), 1,
                                            db.ConnectionString,
                                            LoadTypePriority.Mandatory, true, Guid.NewGuid().ToStrGuid());
                lt.SaveToDB();
                rd2.SaveToDB();
                rd2.AddLoad(lt, 666, 0, 0);

                devices.Add(rd2);

                var deviceCategories = new ObservableCollection <DeviceCategory>();
                var deviceActions    = new ObservableCollection <DeviceAction>();
                var aff = new Affordance("bla", null, null, false, PermittedGender.All, 1, col, string.Empty, null,
                                         string.Empty,
                                         db.ConnectionString, true, true, 0,
                                         100, false, ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(),
                                         BodilyActivityLevel.Low);
                aff.SaveToDB();

                var tp = new TimeBasedProfile("tp", null, db.ConnectionString, TimeProfileType.Relative, "fake", Guid.NewGuid().ToStrGuid());
                tp.SaveToDB();
                tp.AddNewTimepoint(new TimeSpan(0, 0, 0), 100, false);
                tp.AddNewTimepoint(new TimeSpan(0, 2, 0), 0, false);
                var dag = new DeviceActionGroup("dag", db.ConnectionString, string.Empty, Guid.NewGuid().ToStrGuid());
                dag.SaveToDB();
                var da = new DeviceAction("da", null, string.Empty, db.ConnectionString, dag, rd2, Guid.NewGuid().ToStrGuid());
                da.SaveToDB();
                da.AddDeviceProfile(tp, 0, lt, 1);
                deviceActions.Add(da);
                var tbp = new TimeBasedProfile("name", 1, db.ConnectionString, TimeProfileType.Absolute, "data source", Guid.NewGuid().ToStrGuid());
                aff.AddDeviceProfile(dag, tbp, 0, devices, deviceCategories, lt, 1);
                var res = aff.CalculateAverageEnergyUse(deviceActions);
                foreach (var keyValuePair in res)
                {
                    Logger.Info(keyValuePair.Key + ": " + keyValuePair.Value);
                }
                res.Count.Should().Be(1);
                var first = res.First();
                first.Value.Should().Be(666 * 2);
                db.Cleanup();
            }
        }
 private static void CheckRequirements([NotNull] Affordance aff)
 {
     foreach (var requirement in aff.RequiredVariables)
     {
         var list =
             aff.RequiredVariables.Where(
                 x =>
                 x.Variable == requirement.Variable && x.LocationMode == requirement.LocationMode &&
                 x.Location == requirement.Location).ToList();
         if (list.Count > 1)
         {
             throw new DataIntegrityException("The affordance " + aff.Name + " has more than one requirement ");
         }
     }
 }
        public void IsAffordanceAvailableTestCheckDeviceActionGroupInDeviceActionGroup()
        {
            // Location: device action group
            // affordance: device Action Group
            Config.IsInUnitTesting = true;
            var col           = new ColorRGB(255, 0, 0);
            var deviceActions = new ObservableCollection <DeviceAction>();
            var aff           = new Affordance("bla", null, null, false, PermittedGender.All, 1, col, string.Empty, null,
                                               string.Empty, string.Empty,
                                               true, true, 0, 100, false, ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
            var rd1 = new RealDevice("rd1", 1, string.Empty, null, string.Empty, false, false, string.Empty, Guid.NewGuid().ToStrGuid());

            var dg = new DeviceActionGroup("group", string.Empty, string.Empty, Guid.NewGuid().ToStrGuid());
            var da = new DeviceAction("device action 1", null, "blub", string.Empty, dg, rd1, Guid.NewGuid().ToStrGuid());

            deviceActions.Add(da);
            var devices = new ObservableCollection <RealDevice>
            {
                rd1
            };
            // check if it works with a device category that has the device
            var dc1 = new DeviceCategory("dc1", 0, string.Empty, false, devices, Guid.NewGuid().ToStrGuid(), null, true);

            rd1.DeviceCategory = dc1;
            dc1.RefreshSubDevices();
            (dc1.SubDevices.Count).Should().Be(1);
            var connectionString = string.Empty;
            var lt = new VLoadType("lt", string.Empty, "bla", "blub", 1, 1, new TimeSpan(0, 1, 0), 1, connectionString,
                                   LoadTypePriority.Mandatory, true, Guid.NewGuid().ToStrGuid());
            var tbp = new TimeBasedProfile("name", 1, connectionString, TimeProfileType.Absolute, "data source", Guid.NewGuid().ToStrGuid());

            aff.AffordanceDevices.Add(new AffordanceDevice(dg, tbp, null, 0, null,
                                                           new ObservableCollection <RealDevice>(),
                                                           new ObservableCollection <DeviceCategory>(), "name", lt, string.Empty, 1, Guid.NewGuid().ToStrGuid()));
            var allDevices3 = new List <IAssignableDevice>
            {
                dg
            };

            if (da.DeviceActionGroup == null)
            {
                throw new LPGException("device action group was null");
            }
            var relevantDeviceActionGroup = da.DeviceActionGroup.GetDeviceActions(deviceActions);

            (relevantDeviceActionGroup.Count).Should().Be(1);
            (aff.IsAffordanceAvailable(allDevices3, deviceActions)).Should().BeTrue();
        }
 public void AffordanceStandbyTests1()
 {
     using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
     {
         Config.IsInUnitTesting = true;
         var col = new ColorRGB(255, 0, 0);
         var aff = new Affordance("bla", null, null, true, PermittedGender.Female, 0.1m, col,
                                  "AffordanceCategory", null, "desc", db.ConnectionString, true, true, 0, 99, false,
                                  ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
         aff.SaveToDB();
         aff.AffordanceStandbys.Count.Should().Be(0);
         var rd2 = new RealDevice("rd2", 1, string.Empty, null, string.Empty, false, false, db.ConnectionString, Guid.NewGuid().ToStrGuid());
         rd2.SaveToDB();
         aff.AddStandby(rd2);
         aff.AffordanceStandbys.Count.Should().Be(1);
         db.Cleanup();
     }
 }
 private static void CheckAges([NotNull] Affordance aff)
 {
     if (aff.MaximumAge < aff.MinimumAge)
     {
         throw new DataIntegrityException(
                   "The affordance " + aff.Name + " has a lower maximum age than the minimum age. Please fix.", aff);
     }
     if (aff.MaximumAge == 0)
     {
         throw new DataIntegrityException("The affordance " + aff.Name + " has a maximum age of 0. Please fix.",
                                          aff);
     }
     if (aff.MinimumAge < 0)
     {
         throw new DataIntegrityException("The affordance " + aff.Name + " has a minimum age of 0. Please fix.",
                                          aff);
     }
 }
 public HHTAffordance([CanBeNull] int?pID, [CanBeNull] Affordance affv, [CanBeNull] HHTLocation hhtLocation,
                      int householdTraitID,
                      [NotNull]    string connectionString, [NotNull] string hhaffName, [CanBeNull] TimeLimit timeLimit, int weight
                      , int startMinusMinutes, int startPlusMinutes, int endMinusMinutes, int endPlusMinutes,
                      StrGuid guid) : base(hhaffName, TableName, connectionString, guid)
 {
     _timeLimit         = timeLimit;
     _weight            = weight;
     _startMinusMinutes = startMinusMinutes;
     _startPlusMinutes  = startPlusMinutes;
     _endMinusMinutes   = endMinusMinutes;
     _endPlusMinutes    = endPlusMinutes;
     ID                = pID;
     _affordance       = affv;
     _hhtLocation      = hhtLocation;
     _householdTraitID = householdTraitID;
     TypeDescription   = "Household Trait Affordance Location";
 }
Ejemplo n.º 19
0
 public AffordancePresenter([NotNull] ApplicationPresenter applicationPresenter, [NotNull] AffordanceView view,
                            [NotNull] Affordance aff)
     : base(view, "ThisAffordance.HeaderString", aff, applicationPresenter)
 {
     _aff = aff;
     SelectedAddCategory = Sim.CategoryOrDevice[0];
     Sim.Affordances.RefreshAllAffordanceCategory();
     _usedIns = new ObservableCollection <UsedIn>();
     RefreshUsedIn();
     LocationModes.Add(VariableLocationMode.CurrentLocation);
     LocationModes.Add(VariableLocationMode.OtherLocation);
     _allActions.Add(VariableAction.SetTo);
     _allActions.Add(VariableAction.Add);
     _allActions.Add(VariableAction.Subtract);
     _allConditions.SynchronizeWithList(VariableConditionHelper.CollectAllStrings());
     _executionTimes = new ObservableCollection <string>();
     _executionTimes.SynchronizeWithList(VariableExecutionTimeHelper.CollectAllStrings());
     ActionsAfterInterruptions.SynchronizeWithList(ActionAfterInterruptionHelper.CollectAllStrings());
 }
        public void CalculateMaximumInternalTimeResolutionTestForDeviceAction()
        {
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                Config.IsInUnitTesting = true;
                var col     = new ColorRGB(255, 0, 0);
                var devices = new ObservableCollection <RealDevice>();
                var rd2     = new RealDevice("rd2", 1, string.Empty, null, string.Empty, false, false, db.ConnectionString, Guid.NewGuid().ToStrGuid());
                rd2.SaveToDB();
                devices.Add(rd2);

                var deviceCategories = new ObservableCollection <DeviceCategory>();
                var aff = new Affordance("bla", null, null, false, PermittedGender.All, 1, col, string.Empty, null,
                                         string.Empty,
                                         db.ConnectionString, true, true, 0, 100,
                                         false, ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
                aff.SaveToDB();

                var tp = new TimeBasedProfile("tp", null, db.ConnectionString, TimeProfileType.Relative, "fake", Guid.NewGuid().ToStrGuid());
                tp.SaveToDB();
                tp.AddNewTimepoint(new TimeSpan(0, 0, 0), 1, false);
                tp.AddNewTimepoint(new TimeSpan(0, 1, 0), 1, false);
                tp.AddNewTimepoint(new TimeSpan(0, 10, 0), 1, false);
                var lt = new VLoadType("lt", string.Empty, "bla", "blub", 1, 1, new TimeSpan(0, 1, 0), 1,
                                       db.ConnectionString,
                                       LoadTypePriority.Mandatory, true, Guid.NewGuid().ToStrGuid());
                lt.SaveToDB();
                var dag = new DeviceActionGroup("dag", db.ConnectionString, string.Empty, Guid.NewGuid().ToStrGuid());
                dag.SaveToDB();
                var da = new DeviceAction("da", null, string.Empty, db.ConnectionString, dag, rd2, Guid.NewGuid().ToStrGuid());
                da.SaveToDB();
                da.AddDeviceProfile(tp, 0, lt, 1);
                new ObservableCollection <DeviceAction>().Add(da);
                var tbp = new TimeBasedProfile("name", 1, db.ConnectionString, TimeProfileType.Absolute, "data source", Guid.NewGuid().ToStrGuid());
                aff.AddDeviceProfile(da, tbp, 0, devices, deviceCategories, lt, 1);

                var ts = aff.CalculateMaximumInternalTimeResolution();
                (ts.TotalSeconds).Should().Be(60);
                Logger.Info(ts.ToString());
                db.Cleanup();
            }
        }
Ejemplo n.º 21
0
    // Use this for initialization
    void Start()
    {
        Affordance start = new Affordance();

        start.setStart();
        start.addEffects(new Condition("Bear1", "InScene", false));
        start.addEffects(new Condition("Bear2", "InScene", false));
        Apologize goal = new Apologize("Bear1", "Bear2");

        goal.setGoal();
        Planner popPlanner = new Planner();

        popPlanner.computePlan(start, goal);

        popPlanner.showActions();
        //popPlanner.showConstraints ();
        popPlanner.showOrderingConstraints();

        //Utils.populateActionRelations ();
    }
        public void AffordanceRequirementVariableTest()
        {
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                Config.IsInUnitTesting = true;
                var col = new ColorRGB(255, 0, 0);

                var aff = new Affordance("bla", null, null, true, PermittedGender.Female, 0.1m, col,
                                         "affordanceCategory", null, "desc", db.ConnectionString, true, true, 0, 99, false,
                                         ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
                aff.SaveToDB();
                aff.ExecutedVariables.Count.Should().Be(0);
                var va = new Variable("var1", "desc", "unit", db.ConnectionString, Guid.NewGuid().ToStrGuid());
                va.SaveToDB();
                aff.AddVariableRequirement(1, VariableLocationMode.CurrentLocation, null, VariableCondition.Equal, va,
                                           string.Empty);
                aff.RequiredVariables.Count.Should().Be(1);
                db.Cleanup();
            }
        }
        private static void CheckPersonTimeProfiles([NotNull] Affordance affordance)
        {
            if (affordance.PersonProfile == null)
            {
                throw new DataIntegrityException("Affordance " + affordance.Name + " has no Person time profile!",
                                                 affordance);
            }
            var dps = affordance.PersonProfile.ObservableDatapoints;

            for (var i = 0; i < dps.Count - 1; i++)
            {
                if (Math.Abs(dps[i].Value) < Constants.Ebsilon)
                {
                    throw new DataIntegrityException(
                              "The Person profile called \"" + affordance.PersonProfile.Name + "\" for the affordance " +
                              affordance.Name +
                              " drops to zero. But a Person profile has to keep the Person occupied. There are no gaps allowed in a Person profile.",
                              affordance);
                }
            }
        }
 private static void CheckAffordanceDesires([NotNull] Affordance aff)
 {
     foreach (var des1 in aff.AffordanceDesires)
     {
         foreach (var des2 in aff.AffordanceDesires)
         {
             if (des1 != des2 && des1.Desire == des2.Desire)
             {
                 throw new DataIntegrityException(
                           "In the affordance " + aff.Name + " the desire " + des1.Desire +
                           " exists twice. This is not right.", aff);
             }
         }
         if (des1.SatisfactionValue <= 0 || des1.SatisfactionValue > 1)
         {
             throw new DataIntegrityException(
                       "The satisfaction value for the desire " + des1.Desire.Name +
                       " is over 100% or below 0%. Please fix.", aff);
         }
     }
 }
        public void IsAffordanceAvailableTestAffordanceHasNoDevice()
        {
            // Location: realdevice
            // affordance: nothing
            Config.IsInUnitTesting = true;
            var col           = new ColorRGB(255, 0, 0);
            var deviceActions = new ObservableCollection <DeviceAction>();
            var aff           = new Affordance("bla", null, null, false, PermittedGender.All, 1, col, string.Empty, null,
                                               string.Empty, string.Empty,
                                               true, true, 0, 100, false, ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(),
                                               BodilyActivityLevel.Low);
            var rd2 = new RealDevice("rd2", 1, string.Empty, null, string.Empty, false, false, string.Empty, Guid.NewGuid().ToStrGuid());
            // check if the device is not there
            var allDevices2 = new List <IAssignableDevice>
            {
                rd2
            };
            var result = aff.IsAffordanceAvailable(allDevices2, deviceActions);

            (result).Should().BeFalse();
        }
 public void LoadFromDatabaseTest()
 {
     using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
     {
         var affdev             = new ObservableCollection <AffordanceDevice>();
         var deviceCategories   = new ObservableCollection <DeviceCategory>();
         var realDevices        = new ObservableCollection <RealDevice>();
         var timeBasedProfiles  = new ObservableCollection <TimeBasedProfile>();
         var affordances        = new ObservableCollection <Affordance>();
         var dateBasedProfiles  = db.LoadDateBasedProfiles();
         var timeLimits         = db.LoadTimeLimits(dateBasedProfiles);
         var loadtypes          = db.LoadLoadTypes();
         var deviceActionGroups = db.LoadDeviceActionGroups();
         var deviceActions      = new ObservableCollection <DeviceAction>();
         db.ClearTable(AffordanceDevice.TableName);
         AffordanceDevice.LoadFromDatabase(affdev, db.ConnectionString, deviceCategories, realDevices,
                                           timeBasedProfiles, affordances, loadtypes, deviceActions, deviceActionGroups, false);
         affdev.Count.Should().Be(0);
         var rd = new RealDevice("blub", 1, "1", null, "name", true, false, db.ConnectionString, Guid.NewGuid().ToStrGuid());
         rd.SaveToDB();
         realDevices.Add(rd);
         var tp = new TimeBasedProfile("blub", null, db.ConnectionString, TimeProfileType.Relative,
                                       "fake", Guid.NewGuid().ToStrGuid());
         tp.SaveToDB();
         timeBasedProfiles.Add(tp);
         var aff = new Affordance("blub", tp, null, true, PermittedGender.All, 0, new ColorRGB(255, 0, 0),
                                  "bla", timeLimits[0], string.Empty, db.ConnectionString, false, false, 0, 99, false,
                                  ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
         aff.SaveToDB();
         affordances.Add(aff);
         var newaffdev = new AffordanceDevice(rd, tp, null, 0, aff.ID, realDevices, deviceCategories,
                                              "blub", loadtypes[0], db.ConnectionString, 1, Guid.NewGuid().ToStrGuid());
         newaffdev.SaveToDB();
         AffordanceDevice.LoadFromDatabase(affdev, db.ConnectionString, deviceCategories, realDevices,
                                           timeBasedProfiles, affordances, loadtypes, deviceActions, deviceActionGroups, false);
         affdev.Count.Should().Be(1);
         affdev[0].LoadType.Should().Be(loadtypes[0]);
         db.Cleanup();
     }
 }
        private static void CheckAffordanceDevices([NotNull] Affordance affordance)
        {
            var hashSet =
                new HashSet <Tuple <IAssignableDevice, decimal, TimeBasedProfile, VLoadType> >();

            foreach (var devtup in affordance.AffordanceDevices)
            {
                if (Math.Abs(devtup.Probability) < 0.000001)
                {
                    throw new DataIntegrityException("The probability for the device " + devtup.Device?.Name +
                                                     " is 0. This is not very useful. Please fix.");
                }
                var key =
                    new Tuple <IAssignableDevice, decimal, TimeBasedProfile, VLoadType>(devtup.Device, devtup.TimeOffset,
                                                                                        devtup.TimeProfile, devtup.LoadType);
                if (hashSet.Contains(key))
                {
                    throw new DataIntegrityException(
                              "The device " + devtup.Device + " exists twice in the affordance " + affordance +
                              " with the exact same time, load type and profile. Please fix", affordance);
                }
                hashSet.Add(key);

                if (devtup.Device == null)
                {
                    throw new DataIntegrityException(
                              "While creating the affordance " + affordance.Name + ", it had no device!", affordance);
                }
                if (devtup.LoadType == null && (devtup.Device.AssignableDeviceType == AssignableDeviceType.Device ||
                                                devtup.Device.AssignableDeviceType ==
                                                AssignableDeviceType.DeviceCategory))
                {
                    throw new DataIntegrityException(
                              "In the affordance " + affordance.Name + ", there is a real device with no load type!",
                              affordance);
                }
            }
        }
 public void AffordanceVariableTests2()
 {
     using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
     {
         db.ClearTable(Affordance.TableName);
         db.ClearTable(AffordanceStandby.TableName);
         db.ClearTable(AffordanceDevice.TableName);
         db.ClearTable(AffordanceDesire.TableName);
         db.ClearTable(AffordanceSubAffordance.TableName);
         db.ClearTable(AffordanceTaggingEntry.TableName);
         db.ClearTable(HHTAffordance.TableName);
         db.ClearTable(AffVariableOperation.TableName);
         var v = new Variable("var1", "desc", "unit", db.ConnectionString, Guid.NewGuid().ToStrGuid());
         v.SaveToDB();
         Config.IsInUnitTesting = true;
         var col = new ColorRGB(255, 0, 0);
         var aff = new Affordance("bla", null, null, true, PermittedGender.Female, 0.1m, col,
                                  "affordanceCategory", null, "desc", db.ConnectionString, true, true, 0, 99, false,
                                  ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
         aff.SaveToDB();
         aff.ExecutedVariables.Count.Should().Be(0);
         aff.AddVariableOperation(0, VariableLocationMode.CurrentLocation, null, VariableAction.SetTo, v,
                                  string.Empty,
                                  VariableExecutionTime.Beginning);
         aff.SaveToDB();
         aff.ExecutedVariables.Count.Should().Be(1);
         var sim2             = new Simulator(db.ConnectionString);
         var affordanceLoaded = sim2.Affordances.It[0];
         affordanceLoaded.ExecutedVariables.Count.Should().Be(1);
         affordanceLoaded.ExecutedVariables[0].Name.Should().Be("var1");
         affordanceLoaded.DeleteVariableOperation(affordanceLoaded.ExecutedVariables[0]);
         affordanceLoaded.ExecutedVariables.Count.Should().Be(0);
         var sim3 = new Simulator(db.ConnectionString);
         var affordanceLoaded2 = sim3.Affordances.It[0];
         affordanceLoaded2.ExecutedVariables.Count.Should().Be(0);
         db.Cleanup();
     }
 }
        public void AffordanceStandbyTests2()
        {
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                db.ClearTable(Affordance.TableName);
                db.ClearTable(AffordanceStandby.TableName);
                db.ClearTable(AffordanceDevice.TableName);
                db.ClearTable(AffordanceDesire.TableName);
                db.ClearTable(AffordanceSubAffordance.TableName);
                db.ClearTable(AffordanceTaggingEntry.TableName);
                db.ClearTable(HHTAffordance.TableName);
                db.ClearTable(AffVariableRequirement.TableName);

                Config.IsInUnitTesting = true;
                var col = new ColorRGB(255, 0, 0);
                var aff = new Affordance("bla", null, null, true, PermittedGender.Female, 0.1m, col,
                                         "AffordanceCategory", null, "desc", db.ConnectionString, true, true, 0, 99, false,
                                         ActionAfterInterruption.GoBackToOld, false, Guid.NewGuid().ToStrGuid(), BodilyActivityLevel.Low);
                aff.SaveToDB();
                aff.AffordanceStandbys.Count.Should().Be(0);
                var rd2 = new RealDevice("rd2", 1, string.Empty, null, string.Empty, false, false, db.ConnectionString, Guid.NewGuid().ToStrGuid());
                rd2.SaveToDB();
                aff.AddStandby(rd2);
                aff.SaveToDB();
                aff.AffordanceStandbys.Count.Should().Be(1);
                var sim2             = new Simulator(db.ConnectionString);
                var loadedAffordance = sim2.Affordances.It[0];
                loadedAffordance.AffordanceStandbys.Count.Should().Be(1);
                loadedAffordance.AffordanceStandbys[0].Device?.Name.Should().Be("rd2");
                loadedAffordance.DeleteStandby(loadedAffordance.AffordanceStandbys[0]);
                loadedAffordance.AffordanceStandbys.Count.Should().Be(0);
                var sim3 = new Simulator(db.ConnectionString);
                var affordanceLoaded2 = sim3.Affordances.It[0];
                affordanceLoaded2.AffordanceStandbys.Count.Should().Be(0);
                db.Cleanup();
            }
        }
Ejemplo n.º 30
0
    private void AddAffordance(
        MethodInfo method,
        AffordanceAttribute attr)
    {
        Type requiredType = method.GetParameters()[0].ParameterType;
        bool compatibleType = 
            typeof(SmartObject).IsAssignableFrom(requiredType);

        if (method == null
            || method.ReturnType != typeof(Node)
            || method.GetParameters().Length != 1
            || compatibleType == false)
            throw new ApplicationException(
                this.gameObject.name 
                + ": Wrong function signature for affordance");
        string name = method.Name;

        Affordance affordance = new Affordance(
            name,
            attr.Reflexive,
            requiredType,
            method);

        this.affordances.Add(name, affordance);
    }
        private List <TimeProfileEntry> MakeTimeProfileEntryFromDeviceAction([NotNull] DeviceAction da, [NotNull] Affordance affordance,
                                                                             [NotNull] AffordanceDevice affdev, Random rnd)
        {
            var tpes = new List <TimeProfileEntry>();

            foreach (var actionProfile in da.Profiles)
            {
                var lt = actionProfile.VLoadType;
                if (lt == null)
                {
                    throw new LPGException("load type was null");
                }
                var tp = actionProfile.Timeprofile;
                if (tp == null)
                {
                    throw new LPGException("Time profile was null");
                }
                var cp        = CalcDeviceFactory.GetCalcProfile(tp, new TimeSpan(0, 1, 0));
                var factor    = GetMaxExpansionFactor((double)affordance.TimeStandardDeviation, rnd);
                var newlength = cp.GetNewLengthAfterCompressExpand(factor);
                if (da.Device == null)
                {
                    throw new LPGException("Device was null");
                }
                var tpe = new TimeProfileEntry(affdev, newlength, lt,
                                               (int)(affdev.TimeOffset + actionProfile.TimeOffset), affordance, factor, da.Device.Name);
                tpes.Add(tpe);
            }
            return(tpes);
        }