EMMA Coverage Report (generated Tue May 01 18:46:53 CEST 2007)
[all classes][dk.deepthought.sidious.rules]

COVERAGE SUMMARY FOR SOURCE FILE [ConstrainingRule.java]

nameclass, %method, %block, %line, %
ConstrainingRule.java100% (1/1)100% (4/4)94%  (180/191)95%  (43,6/46)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ConstrainingRule100% (1/1)100% (4/4)94%  (180/191)95%  (43,6/46)
normalizedDistance (SensorInput, SensorInput): double 100% (1/1)93%  (82/88)92%  (15,6/17)
desire (State, State, Step): double 100% (1/1)95%  (87/92)96%  (24/25)
ConstrainingRule (SuperLinkID []): void 100% (1/1)100% (7/7)100% (3/3)
getGoals (): Collection 100% (1/1)100% (4/4)100% (1/1)

1package dk.deepthought.sidious.rules;
2 
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Collection;
6 
7import dk.deepthought.sidious.goalhandler.Goal;
8import dk.deepthought.sidious.greenhouse.ClimaticState;
9import dk.deepthought.sidious.greenhouse.SensorInput;
10import dk.deepthought.sidious.supportsystem.State;
11import dk.deepthought.sidious.supportsystem.Step;
12import dk.deepthought.sidious.supportsystem.SuperLinkID;
13 
14/**
15 * Rule to keep conventionally unconstrained sensors constrained. I.e. adds cost
16 * to otherwise "free" adjustments.
17 * 
18 * @author Deepthought
19 * 
20 */
21public class ConstrainingRule extends Rule {
22 
23        /**
24         * A list of sensor id's which will be constrained by this.
25         */
26        private Collection<SuperLinkID> idsToConstrain;
27 
28        /**
29         * Constructs a constraining rule that constrains sensors with input IDs.
30         * 
31         * @param sensorIDs
32         *            the ids of the sensors to constrain
33         */
34        public ConstrainingRule(SuperLinkID... sensorIDs) {
35                idsToConstrain = Arrays.asList(sensorIDs);
36        }
37 
38        /*
39         * (non-Javadoc)
40         * 
41         * @see dk.deepthought.sidious.rules.Rule#desire(dk.deepthought.sidious.supportsystem.State,
42         *      dk.deepthought.sidious.supportsystem.State, dk.deepthought.sidious.supportsystem.Step)
43         */
44        @Override
45        public double desire(State currentState, State newState, Step step) {
46                ClimaticState currentClima = null;
47                ClimaticState newClima = null;
48                if (currentState instanceof ClimaticState) {
49                        currentClima = (ClimaticState) currentState;
50                }
51                if (newState instanceof ClimaticState) {
52                        newClima = (ClimaticState) newState;
53                }
54                if (currentClima == null || newClima == null) {
55                        throw new IllegalArgumentException(
56                                        "Only climaticStates allowed as parameters!");
57                }
58                double returnValue = 0;
59                for (SuperLinkID id : idsToConstrain) {
60                        SensorInput currentSensor = null;
61                        SensorInput newSensor = null;
62                        for (SensorInput sensor : currentClima.getSensors()) {
63                                if (sensor.getID().equals(id)) {
64                                        currentSensor = sensor;
65                                        break;
66                                }
67                        }
68                        for (SensorInput sensor : newClima.getSensors()) {
69                                if (sensor.getID().equals(id)) {
70                                        newSensor = sensor;
71                                        break;
72                                }
73                        }
74                        returnValue += normalizedDistance(currentSensor, newSensor);
75                }
76                return returnValue;
77        }
78 
79        /*
80         * (non-Javadoc)
81         * 
82         * @see dk.deepthought.sidious.rules.Rule#getGoals()
83         */
84        @Override
85        public Collection<Goal> getGoals() {
86                return new ArrayList<Goal>();
87        }
88 
89        /**
90         * Calculates the normalized distance between two sensors.
91         * <p>
92         * It takes two sensors and normalizes the distance between them to a value
93         * between 0 and 1.
94         * 
95         * @param currentSensor
96         *            the first sensor
97         * @param newSensor
98         *            the second sensor
99         * @return the normalized value of the distance.
100         */
101        private double normalizedDistance(SensorInput currentSensor,
102                        SensorInput newSensor) {
103                double newValue = newSensor == null ? 0 : newSensor.getValue();
104                double currentValue = currentSensor == null ? 0 : currentSensor
105                                .getValue();
106                double normalizer;
107                double distance = Math.abs(newValue - currentValue);
108                // Normalizes to log 10 value
109                if (Math.abs(newValue) > 0 && Math.abs(currentValue) > 0
110                                && Math.abs(distance) > 0) {
111                        double flooredNorm = Math.floor(Math.min(Math.log10(distance), Math
112                                        .min(Math.log10(newValue), Math.log10(currentValue))));
113                        normalizer = flooredNorm > 0 ? flooredNorm : 0;
114                } else {
115                        normalizer = 0;
116                }
117                if (normalizer >= 0) {
118                        normalizer += 1;
119                        double div = Math.pow(10, normalizer);
120                        newValue /= div;
121                        currentValue /= div;
122                        distance = Math.abs(newValue - currentValue);
123                }
124                if (distance > 1) {
125                        distance = 1;
126                }
127                return distance;
128        }
129 
130}

[all classes][dk.deepthought.sidious.rules]
EMMA 2.0.5312 (C) Vladimir Roubtsov