diff --git a/greenhouse_core/src/env/actuators/SampleBasedActuatorArtifact.java b/greenhouse_core/src/env/actuators/SampleBasedActuatorArtifact.java index 5da3672..1b133f9 100644 --- a/greenhouse_core/src/env/actuators/SampleBasedActuatorArtifact.java +++ b/greenhouse_core/src/env/actuators/SampleBasedActuatorArtifact.java @@ -9,6 +9,7 @@ import city.sane.wot.thing.ConsumedThing; import utility.ThingDescriptorUtility; import utility.component.Component; +import utility.component.ComponentUtility; import utility.sample.Sample; import utility.setting.RangeSetting; @@ -95,7 +96,7 @@ void actuate(final Sample outOfRangeSample, final RangeSetting setting) { * @param turnOffAction the action that needs to be turned off. */ private void switchActuatorAction(final String category, final String turnOnAction, final String turnOffAction) { - List components = ThingDescriptorUtility.getActuatorsByCategory(this.components, category); + List components = ComponentUtility.getActuatorsByCategory(this.components, category); for (Component component : components) { //search for the specific action in the td list for a specific edge Optional td = ThingDescriptorUtility.getThingDescriptor(this.thingDescriptors, component.getEdgeIp()); diff --git a/greenhouse_core/src/env/actuators/TimeBasedActuatorArtifact.java b/greenhouse_core/src/env/actuators/TimeBasedActuatorArtifact.java index 85e11e6..a0cb152 100644 --- a/greenhouse_core/src/env/actuators/TimeBasedActuatorArtifact.java +++ b/greenhouse_core/src/env/actuators/TimeBasedActuatorArtifact.java @@ -10,6 +10,7 @@ import city.sane.wot.thing.ConsumedThing; import utility.ThingDescriptorUtility; import utility.component.Component; +import utility.component.ComponentUtility; import utility.setting.HourSetting; import utility.setting.Settings; @@ -73,7 +74,7 @@ void checkSettings(final String category) { if (!this.components.isEmpty() && this.settings.isPresent() && this.settings.get().getSetting(category).isPresent()) { HourSetting setting = (HourSetting) this.settings.get().getSetting(category).get(); - List actuators = ThingDescriptorUtility.getActuatorsByCategory(this.components, category); + List actuators = ComponentUtility.getActuatorsByCategory(this.components, category); checkTime(setting, actuators); } } @@ -116,4 +117,5 @@ private void performActuatorAction(final List actuators, final String } } } + } diff --git a/greenhouse_core/src/env/communication/DiscoverComponentsArtifact.java b/greenhouse_core/src/env/communication/DiscoverComponentsArtifact.java index e89f283..f123ff1 100644 --- a/greenhouse_core/src/env/communication/DiscoverComponentsArtifact.java +++ b/greenhouse_core/src/env/communication/DiscoverComponentsArtifact.java @@ -36,6 +36,8 @@ */ public class DiscoverComponentsArtifact extends Artifact { + private final static long TICK_TIME = 100; + private OkHttpClient client; private List components; private List thingDescriptors; @@ -67,21 +69,30 @@ private void configWot() { */ @OPERATION void discoverComponents() { - // initialization due to avoid to try to communicate with disconnected components + //initialization due to avoid to try to communicate with disconnected components this.components = new ArrayList<>(); this.thingDescriptors = new ArrayList<>(); - // create cycle for broadcast requests! - // for test purposes - /* - * for (int i = 1; i >= 255; i++){ - * System.out.println("http://192.168.189." + i); - * } - */ - getThingDescriptor("http://192.168.246.1"); // this is in the cicle - - // at the end of the cicle, update components and thing descriptors - defineObsProperty("components", this.components); - defineObsProperty("thingDescriptors", this.thingDescriptors); + //calling asynchronous operation + execInternalOp("broadcastRequest"); + } + + /** + * This internal operation is used to run an asynchronous operation, + * which it is usually for a long term operation. + * This internal operation is used to cicle on all the ip adresses + * in order to find all the edges connected to the greenhouse, and + * it requires some time. + * + * Even though it runs asynchronously, it is necessary to call + * await time, which stops the executing operation and makes + * it possible to commit the observable states. + */ + @INTERNAL_OPERATION + void broadcastRequest() { + for (int i = 1; i <= 255; i++) { + getThingDescriptor("http://192.168.246." + i); + await_time(TICK_TIME); + } } /** @@ -94,11 +105,15 @@ private void getThingDescriptor(final String url) { Request request = new Request.Builder().url(url).build(); try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { + System.out.println("Successful edge response at url " + url); JsonObject thingDescriptor = JsonParser.parseString(response.body().string()).getAsJsonObject(); - updateComponents(thingDescriptor); // if found, update components + updateComponents(thingDescriptor); //if found, update components + //update components and thing descriptors + defineObsProperty("components", this.components); + defineObsProperty("thingDescriptors", this.thingDescriptors); } } catch (IOException e) { - e.printStackTrace(); + System.err.println("Impossible to contact edge at url " + url); } } diff --git a/greenhouse_core/src/env/sampling/SamplingCoordinatorArtifact.java b/greenhouse_core/src/env/sampling/SamplingCoordinatorArtifact.java index f51584a..dccd0b4 100644 --- a/greenhouse_core/src/env/sampling/SamplingCoordinatorArtifact.java +++ b/greenhouse_core/src/env/sampling/SamplingCoordinatorArtifact.java @@ -5,14 +5,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import cartago.*; import utility.component.Component; +import utility.component.ComponentUtility; import utility.sample.Sample; import utility.sample.SamplesUtility; import utility.setting.RangeSetting; -import utility.setting.Setting; import utility.setting.Settings; /** @@ -60,7 +59,7 @@ void setupComponents(final List components) { * the settings only if they exist. */ @OPERATION - void setupSettings( final Optional settings) { + void setupSettings(final Optional settings) { this.settings = settings; } @@ -76,7 +75,7 @@ void setupSettings( final Optional settings) { */ @OPERATION void sampleOperation(final String category) { - List filteredComponents = getComponentsByCategory(category); + List filteredComponents = ComponentUtility.getComponentsByCategory(this.components, category); if (!filteredComponents.isEmpty()) { defineObsProperty("sampling", filteredComponents); } @@ -93,47 +92,46 @@ void sampleOperation(final String category) { @OPERATION void updateOperation(final List currentSamples) { if (!currentSamples.isEmpty()) { - Sample avarageSample = SamplesUtility.getAvarageOfSamples(currentSamples); - String category = avarageSample.getCategory(); + Sample averageSample = SamplesUtility.getAverageOfSamples(currentSamples); + String category = averageSample.getCategory(); Optional lastSample = SamplesUtility.getSampleByCategory(lastSamples, category); - System.out.println("Avarage current Sample: " + avarageSample + "\nLast avarage sample: " + lastSample); + System.out.println("Average current Sample: " + averageSample + "\nLast average sample: " + lastSample); - uploadToPersistence(avarageSample, lastSample); - startActuator(avarageSample, category); + uploadToPersistence(averageSample, lastSample); + startActuator(averageSample, category); - this.lastSamples.add(avarageSample); + this.lastSamples.add(averageSample); } } /** - * Utility method used to check the avarage of the current samples and the last + * Utility method used to check the average of the current samples and the last * sample retrieved. If their difference is bigger that the specified DELTA, - * then the new avarage sample is going to be uploaded in the persistence + * then the new average sample is going to be uploaded in the persistence * service. * - * @param avarageSample the avarage of the current samples retrieved. + * @param averageSample the average of the current samples retrieved. * @param lastSample the last sample saved. */ - private void uploadToPersistence(final Sample avarageSample, final Optional lastSample){ + private void uploadToPersistence(final Sample averageSample, final Optional lastSample) { if (lastSample.isPresent()) { - if (isSampleDifferenceBiggerThanDelta(avarageSample, lastSample)){ - defineObsProperty("uploadPersistence", avarageSample); + if (isSampleDifferenceBiggerThanDelta(averageSample, lastSample)) { + defineObsProperty("uploadPersistence", averageSample); lastSamples.remove(lastSample.get()); - } - + } } } /** - * Utility method used to check if the difference of the current avarage sample + * Utility method used to check if the difference of the current average sample * and the last sample is bigger than the DELTA. * - * @param avarageSample the avarage of the current samples retrieved. + * @param averageSample the average of the current samples retrieved. * @param lastSample the last sample saved. * @return true if the difference of the samples is bigger thant the DELTA, false otherwise. */ - private boolean isSampleDifferenceBiggerThanDelta(final Sample avarageSample, final Optional lastSample){ - return Math.abs(avarageSample.getValue() - lastSample.get().getValue()) > DELTA; + private boolean isSampleDifferenceBiggerThanDelta(final Sample averageSample, final Optional lastSample) { + return Math.abs(averageSample.getValue() - lastSample.get().getValue()) > DELTA; } /** @@ -141,56 +139,46 @@ private boolean isSampleDifferenceBiggerThanDelta(final Sample avarageSample, fi * out of range sample and that it is necessary to perform an action * from an actuator. * - * @param avarageSample the avarage of the current samples retrieved. - * @param category the category of the current avarage sample. + * @param averageSample the average of the current samples retrieved. + * @param category the category of the current average sample. */ - private void startActuator(final Sample avarageSample, final String category){ + private void startActuator(final Sample averageSample, final String category) { if (this.settings.isPresent()) { - // handle only range settings - if (getSettingByCategory(category).isPresent() - && getSettingByCategory(category).get() instanceof RangeSetting) { - RangeSetting rangeSetting = (RangeSetting) getSettingByCategory(category).get(); - if (isSampleOutOfRange(avarageSample, rangeSetting)) { + if (isRangeSettingPresent(category)) { + RangeSetting rangeSetting = (RangeSetting) this.settings.get().getSetting(category).get(); + if (isSampleOutOfRange(averageSample, rangeSetting)) { System.out.println("Create out of range behavior"); - defineObsProperty("actuate", category, avarageSample, rangeSetting); + defineObsProperty("actuate", category, averageSample, rangeSetting); } } } } /** - * Utility method used check if the current avarage sample is out of the minimum and + * Utility method used check if the current average sample is out of the minimum and * maximum define by the [[utility.setting.RangeSetting]] of its category. * - * @param avarageSample the avaragae of the current samples retrieved. + * @param averageSample the average of the current samples retrieved. * @param rangeSetting the range setting that define the minimum and the maximum * value of the sample of a specific category. - * @return true if the value of the avarage sample is out of range, false otherwise. - */ - private boolean isSampleOutOfRange(final Sample avarageSample, final RangeSetting rangeSetting){ - return avarageSample.getValue() < rangeSetting.getMin() || avarageSample.getValue() > rangeSetting.getMax(); - } - - /** - * Utility method used to retrieve from a list of components only the components of a specific - * category. - * - * @param category the cateogry of the components that are wanted to be retrieved. - * @return the list of components that have a specific category. + * @return true if the value of the average sample is out of range, false otherwise. */ - private List getComponentsByCategory(final String category) { - return this.components.stream().filter(c -> c.getCategory().equals(category)).filter(c -> !c.getProperties().isEmpty()).collect(Collectors.toList()); + private boolean isSampleOutOfRange(final Sample averageSample, final RangeSetting rangeSetting) { + return averageSample.getValue() < rangeSetting.getMin() || averageSample.getValue() > rangeSetting.getMax(); } /** - * Utility method used to retrieve a setting with a specific category - * from an optional list of settings. + * Utility method used to verify if it exists a RangeSetting + * in the current settings list of the specified category. * - * @param category the category of the settings wanted. - * @return an optional which contains the setting of that category if found. + * @param category the category of the RangeSetting searched. + * @return true if a RangeSetting of the category specified exists, + * false otherwise. */ - private Optional getSettingByCategory(final String category) { - return this.settings.get().getSetting(category); + private boolean isRangeSettingPresent(final String category) { + Settings currentSettings = this.settings.get(); + return (currentSettings.getSetting(category).isPresent() + && currentSettings.getSetting(category).get() instanceof RangeSetting); } } diff --git a/greenhouse_core/src/env/utility/Pair.java b/greenhouse_core/src/env/utility/Pair.java index d46ae74..5ab36c6 100644 --- a/greenhouse_core/src/env/utility/Pair.java +++ b/greenhouse_core/src/env/utility/Pair.java @@ -1,19 +1,39 @@ package utility; +/** + * Pair is an generic utility class used to contain in + * a single object two values logically connected in some way. + */ public class Pair { private final X x; private final Y y; + /** + * Constructor of Pair. + * + * @param x the first generic value of the Pair. + * @param y the second generic value of the Pair. + */ public Pair(final X x, final Y y) { this.x = x; this.y = y; } + /** + * A Pair has a generic first element. + * + * @return the first generic value of the Pair. + */ public X getX() { return x; } + /** + * A Pair has a generic second element. + * + * @return the second generic value of the Pair. + */ public Y getY() { return y; } diff --git a/greenhouse_core/src/env/utility/ThingDescriptorUtility.java b/greenhouse_core/src/env/utility/ThingDescriptorUtility.java index b7159e2..3fab7f2 100644 --- a/greenhouse_core/src/env/utility/ThingDescriptorUtility.java +++ b/greenhouse_core/src/env/utility/ThingDescriptorUtility.java @@ -12,19 +12,36 @@ import city.sane.wot.thing.ConsumedThing; -import utility.component.Component; - +/** + * ThingDescriptorUtility is an utility class used to provide + * methods in order to handle easily the thing descriptor. + */ public class ThingDescriptorUtility { - - public static List getActuatorsByCategory(final List components, final String category) { - return components.stream().filter(c -> c.getCategory().equals(category)) - .filter(c -> !c.getActions().isEmpty()).collect(Collectors.toList()); - } + /** + * Utility method used to find in a list of thing descriptors a specific + * thing descriptor by its identifier. + * + * @param thingDescriptors the list of thing descriptors. + * @param id the id of the thing descriptor wanted. + * @return an optional of the thing descriptor with the specified id if found, + * and optional of empty otherwise. + */ public static Optional getThingDescriptor(final List thingDescriptors, final String id) { return thingDescriptors.stream().filter(t -> t.getId().equals(id)).findFirst(); } + /** + * Utility method used to get a list of actions and/or properties names from + * the thing descriptor of a specific component, of a specific category. + * + * @param td the thing descriptor that contains the actions or the properties. + * @param category the category of the action or properties wanted. + * @param componentId the id of the component of which the names of properties or + * actions are wanted. + * @return a list of names of the action or the properties of a specific component, based + * on its category. + */ public static List getNamesByCategory(final JsonObject td, final String category, final String componentId) { List names = new ArrayList<>(); Set> entries = td.entrySet(); @@ -36,4 +53,5 @@ public static List getNamesByCategory(final JsonObject td, final String } return names.stream().filter(n -> n.substring(n.lastIndexOf("-") + 1).equals(componentId)).collect(Collectors.toList()); } + } diff --git a/greenhouse_core/src/env/utility/component/Component.java b/greenhouse_core/src/env/utility/component/Component.java index fc8009c..ce7b795 100644 --- a/greenhouse_core/src/env/utility/component/Component.java +++ b/greenhouse_core/src/env/utility/component/Component.java @@ -9,6 +9,7 @@ * action names that have been retrieved from the thing descriptor. */ public class Component { + private final String edgeIp; private final String id; private final String category; @@ -16,14 +17,14 @@ public class Component { private final List actions; /** - * Component constructor, used to give all the information that + * Component constructor, used to give all the information that * a Component needs. * - * @param edgeIp the ip of the edge where the component is connected. - * @param id the id of the component. + * @param edgeIp the ip of the edge where the component is connected. + * @param id the id of the component. * @param category the category of the component. * @param properties the list of the properties' names that the component have. - * @param actions the list of the actions' names tha the component can perform. + * @param actions the list of the actions' names tha the component can perform. */ protected Component(final String edgeIp, final String id, final String category, final List properties, final List actions) { @@ -34,41 +35,79 @@ protected Component(final String edgeIp, final String id, final String category, this.actions = actions; } + /** + * A component is always connected to an edge, which has an ip. + * This method returns the edge's ip where the component is connected to. + * + * @return the edge's ip that is resposible of the component. + */ public String getEdgeIp() { return edgeIp; } + /** + * A component has an identificator that is unique for the edge + * it is connected to. + * + * @return the unique id of the component. + */ public String getId() { return id; } + /** + * A component has a category it falls into, which defines the kind + * of sensor of actuator it is. + * + * @return the category of the component. + */ public String getCategory() { return category; } + /** + * A component has a list of names of the properties it can access, + * which are extracted from the thing descriptor of the edge it + * is connected to. + * + * @return the list of names of the properties that the component can + * access. + */ public List getProperties() { return properties; } /** + * Method used to extract a property using a known + * part of the name of the property wanted. * - * - * @param subString - * @return + * @param subString the part of the property name known. + * @return the complete name of the property found + * starting from the substring provided. */ public Optional getPropertyBySubString(final String subString) { return this.properties.stream().filter(s -> s.contains(subString)).findFirst(); } + /** + * A component has a list of names of the action it can perform, + * which are extracted from the thing descriptor of the edge it + * is connected to. + * + * @return the list of names of the actions that the component can + * perform. + */ public List getActions() { return actions; } /** + * Method used to extract an action using a known + * part of the name of the action wanted. * - * - * @param subString - * @return + * @param subString the part of the action name known. + * @return the complete name of the action found + * starting from the substring provided. */ public Optional getActionBySubString(final String subString) { return this.actions.stream().filter(s -> s.contains(subString)).findFirst(); diff --git a/greenhouse_core/src/env/utility/component/ComponentBuilder.java b/greenhouse_core/src/env/utility/component/ComponentBuilder.java index 4ef8a9e..08cca52 100644 --- a/greenhouse_core/src/env/utility/component/ComponentBuilder.java +++ b/greenhouse_core/src/env/utility/component/ComponentBuilder.java @@ -3,6 +3,10 @@ import java.util.ArrayList; import java.util.List; +/** + * ComponentBuilder applies the Builder pattern in order to + * create a [[utility.component.Component]] in an easier way. + */ public class ComponentBuilder { private final String edgeIp; private final String id; @@ -10,6 +14,14 @@ public class ComponentBuilder { private List properties; private List actions; + /** + * Private ComponentBuilder constructor, used to create a ComponentBuilder internally, + * which contains part of the information used to create a Component. + * + * @param edgeIp the ip of the edge where the component is connected. + * @param id the id of the component. + * @param category the category of the component. + */ private ComponentBuilder(final String edgeIp, final String id, final String category) { this.edgeIp = edgeIp; this.id = id; @@ -18,20 +30,55 @@ private ComponentBuilder(final String edgeIp, final String id, final String cate this.actions = new ArrayList<>(); } + /** + * Method used to create a ComponentBuilder, with the basic information + * that a Component needs. + * + * @param edgeIp the ip of the edge where the component is connected. + * @param id the id of the component. + * @param category the category of the component. + * @return a ComponentBuilder contain the information passed and that can be enriched + * with more information if necessary. + */ public static ComponentBuilder create(final String edgeIp, final String id, final String category) { return new ComponentBuilder(edgeIp, id, category); } + /** + * Method used if necessery to add to the ComponentBuilder + * a list of names of the properties that the component has + * access to. + * + * @param properties list of the names of the properties of the component. + * @return a ComponentBuilder that can be enriched with more information + * if necessary. + */ public ComponentBuilder addProperties(final List properties) { this.properties = properties; return this; } + /** + * Method used if necessary to add to the ComponentBuilder + * a list of names of the actions that the component can + * perform. + * + * @param actions list of the names of the actions of the component. + * @return a ComponentBuilder that can be enriched with more information + * if necessary. + */ public ComponentBuilder addActions(final List actions) { this.actions = actions; return this; } + /** + * Method that finalize the creation of a Component, once all the information + * needed are added. + * + * @return the component created with all the information contained in the + * ComponentBuilder. + */ public Component build() { return new Component(edgeIp, id, category, properties, actions); } diff --git a/greenhouse_core/src/env/utility/component/ComponentUtility.java b/greenhouse_core/src/env/utility/component/ComponentUtility.java new file mode 100644 index 0000000..c70f470 --- /dev/null +++ b/greenhouse_core/src/env/utility/component/ComponentUtility.java @@ -0,0 +1,38 @@ +package utility.component; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * ComponentUtility is a class used to handle easily reseaches about + * components, without having to implement them more than once. + */ +public class ComponentUtility { + + /** + * Utility method used to seach in a list of components, components of a specific + * category that have actions, which can be called actuators. + * + * @param components the list of components used to seach for actuators + * of a specific category. + * @param category the category of the actuators that are wanted to be retrieved. + * @return the list of actuators of a specific category. + */ + public static List getActuatorsByCategory(final List components, final String category) { + return components.stream().filter(c -> c.getCategory().equals(category)) + .filter(c -> !c.getActions().isEmpty()).collect(Collectors.toList()); + } + + /** + * Utility method used to retrieve from a list of components only the components of a specific + * category. + * + * @param components the list of components used to search for components of a specific category. + * @param category the cateogry of the components that are wanted to be retrieved. + * @return the list of components that have a specific category. + */ + public static List getComponentsByCategory(final List components, final String category) { + return components.stream().filter(c -> c.getCategory().equals(category)).filter(c -> !c.getProperties().isEmpty()).collect(Collectors.toList()); + } + +} diff --git a/greenhouse_core/src/env/utility/sample/Sample.java b/greenhouse_core/src/env/utility/sample/Sample.java index ce4f8c2..b517246 100644 --- a/greenhouse_core/src/env/utility/sample/Sample.java +++ b/greenhouse_core/src/env/utility/sample/Sample.java @@ -1,25 +1,56 @@ package utility.sample; +/** + * Sample is a class used to contain the sample retrieved from + * a component, keeping track of the category of the component, + * the time when the sample was taken and the value of that sample. + */ public class Sample { private final String category; private final String samplingTime; private final int value; + /** + * Constructor to create a Sample. + * + * @param category the category of the Sample, which define the type of component + * that retrieved that sample. + * @param samplingTime the time when the sample has been retrieved. + * @param value the value of the sample retrieved. + */ public Sample(final String category, final String samplingTime, final int value) { this.category = category; this.samplingTime = samplingTime; this.value = value; } + /** + * A Sample has a category which is the same of the Component + * that retrieved that Sample. + * + * @return the category of the Sample. + */ public String getCategory() { return this.category; } + /** + * A Sample has a time when the sample has been retrieved by + * the Component. + * + * @return the time when the sample has been retrieved. + */ public String getSamplingTime() { return this.samplingTime; } + /** + * A Sample has a value that is the value retrieved from the + * Component, so from a physical sensor. + * + * @return the value of the Sample. + */ public int getValue() { return this.value; } diff --git a/greenhouse_core/src/env/utility/sample/SamplesUtility.java b/greenhouse_core/src/env/utility/sample/SamplesUtility.java index 168d848..23e091f 100644 --- a/greenhouse_core/src/env/utility/sample/SamplesUtility.java +++ b/greenhouse_core/src/env/utility/sample/SamplesUtility.java @@ -3,15 +3,38 @@ import java.util.List; import java.util.Optional; +/** + * Utility class for list of [[utility.sample.Sample]], in order to retrieve + * specific samples without having to implement the same methods in more + * classes. + */ public class SamplesUtility { - public static Sample getAvarageOfSamples(final List samples) { + /** + * Given a list of Samples, this methods calculates the average of them, + * return a Sample that is the average. + * + * @param samples the list of samples that are going to be used to calculate + * the average. + * @return a sample that is the average of all the samples given as argument. + */ + public static Sample getAverageOfSamples(final List samples) { Sample sample = samples.get(0); return new Sample(sample.getCategory(), sample.getSamplingTime(), (int) samples.stream().mapToInt(s -> s.getValue()).average().getAsDouble()); } + /** + * Given a list of samples and a category, this method is used to find + * the first sample in the list of the specified category. + * + * @param samples the list of sample to examin. + * @param category the category of the sample wanted. + * @return return an optional of the sample of the category specified + * if found, optional of empty otherwise. + */ public static Optional getSampleByCategory(final List samples, final String category) { return samples.stream().filter(s -> s.getCategory().equals(category)).findFirst(); } + } diff --git a/greenhouse_core/src/env/utility/setting/HourSetting.java b/greenhouse_core/src/env/utility/setting/HourSetting.java index 1d69048..1a0ebee 100644 --- a/greenhouse_core/src/env/utility/setting/HourSetting.java +++ b/greenhouse_core/src/env/utility/setting/HourSetting.java @@ -2,21 +2,48 @@ import java.time.LocalTime; +/** + * HourSetting is a specific kind of Setting. + * An HourSetting has a category, taken from its parent class, + * and it has a starting time and and ending time, defining a + * time range. + * This can be used when the setting are based on time. + */ public class HourSetting extends Setting { private final LocalTime fromTime; private final LocalTime toTime; + /** + * Constructor of HourSetting. + * + * @param category the category that the setting is going to regulate. + * @param fromTime the starting time of the setting. + * @param toTime the end time of the setting. + */ public HourSetting(final String category, final LocalTime fromTime, final LocalTime toTime) { super(category); this.fromTime = fromTime; this.toTime = toTime; } + /** + * An HourSetting has an initial time, which is used to know + * when that specific setting has to be applied. + * + * @return the starting time of the HourSetting. + */ public LocalTime getFromTime() { return fromTime; } + /** + * An HourSetting has an ending time, which is used + * to know when that specific setting has to + * be stopped to be applied. + * + * @return the ending time of the HourSetting. + */ public LocalTime getToTime() { return toTime; } diff --git a/greenhouse_core/src/env/utility/setting/RangeSetting.java b/greenhouse_core/src/env/utility/setting/RangeSetting.java index 27d478e..c460928 100644 --- a/greenhouse_core/src/env/utility/setting/RangeSetting.java +++ b/greenhouse_core/src/env/utility/setting/RangeSetting.java @@ -2,19 +2,47 @@ import utility.Pair; +/** + * RangeSetting is a specific kind of Setting. + * An RangeSetting has a category, taken from its parent class, + * and a pair which defines the minimum and maximum value + * accepted for a specific category of samples. + * This can be used when the setting are based on a range of values. + */ public class RangeSetting extends Setting { private Pair range; + /** + * Constructor of RangeSetting. + * + * @param category the category that the setting is going to regulate. + * @param range a pair which contains a pair of values, that are the minimum + * and the maximum values accepted from a specific category. + */ public RangeSetting(final String category, final Pair range) { super(category); this.range = range; } + /** + * A RangeSetting has a minimum value accepted from a specific category + * of sample. + * + * @return the minimum value accepted from the category that + * the setting regulates. + */ public int getMin() { return this.range.getX(); } + /** + * A RangeSetting has a maximum value accepted from a specific category + * of sample. + * + * @return the maximum value accepted from the category that the + * setting regulates. + */ public int getMax() { return this.range.getY(); } diff --git a/greenhouse_core/src/env/utility/setting/Setting.java b/greenhouse_core/src/env/utility/setting/Setting.java index f4b49b7..e394ddd 100644 --- a/greenhouse_core/src/env/utility/setting/Setting.java +++ b/greenhouse_core/src/env/utility/setting/Setting.java @@ -1,13 +1,29 @@ package utility.setting; +/** + * Setting is the father class of [[utility.setting.HourSetting]] and + * [[utility.setting.RangeSetting]], extracting their common trait, + * which is the category. + */ public class Setting { private final String category; + /** + * Constructor of Setting. + * + * @param category the category that the setting regulates. + */ public Setting(final String category) { this.category = category; } + /** + * A Setting has a category that specifies the category of + * samples that it regulates. + * + * @return the category of the setting. + */ public String getCategory() { return this.category; } diff --git a/greenhouse_core/src/env/utility/setting/Settings.java b/greenhouse_core/src/env/utility/setting/Settings.java index c577805..b469949 100644 --- a/greenhouse_core/src/env/utility/setting/Settings.java +++ b/greenhouse_core/src/env/utility/setting/Settings.java @@ -8,18 +8,37 @@ import utility.Pair; +/** + * Settings is a class used to contain all the settings of the greenhouse, + * which can be [[utility.setting.HourSetting]] and/or [[utility.setting.RangeSetting]]. + * The settings have also a creation date and an id. + */ public class Settings { private final String id; private final Date creationDate; private List settings; + /** + * Constructor of Settings. + * + * @param id the identification of the current settings of the greenhouse. + * @param creationDate the creation date of the settings. + */ public Settings(final String id, final Date creationDate) { this.id = id; this.creationDate = creationDate; this.settings = new ArrayList<>(); } + /** + * Mehod used to create a [[utility.setting.Setting]], given the + * category and a pair of values, that are the range of value accepted + * for a specific category of samples. + * + * @param category the category that the setting regulates. + * @param range a pair of values that is the range of values accepted. + */ public void createSetting(final String category, final Pair range) { if (isCategoryUnique(category)) { System.err.println("A setting for this category already exist."); @@ -28,6 +47,15 @@ public void createSetting(final String category, final Pair ra } } + /** + * Method used to create a [[utilituy.setting.Setting]], given the + * category the initial time and the ending time of validity of + * the setting. + * + * @param category the category that the setting regulates. + * @param fromTime the starting time of the validity of the setting. + * @param toTime the ending time of validity of the setting. + */ public void createSetting(final String category, final LocalTime fromTime, final LocalTime toTime) { if (isCategoryUnique(category)) { System.err.println("A setting for this category already exist."); @@ -36,25 +64,56 @@ public void createSetting(final String category, final LocalTime fromTime, final } } + /** + * Utility method used to check if exists another setting for the same category + * in the list of settings. + * + * @param category the category of the setting searched in the list. + * @return true if it exists another setting with the category specified, + * false otherwise. + */ private boolean isCategoryUnique(final String category) { return this.settings.stream().filter(s -> s.getCategory().equals(category)).findAny().isPresent(); } + /** + * Get a safe copy of the list of settings present. + * + * @return a safe copy of the list of settings. + */ public List getSettings(){ return new ArrayList<>(this.settings); } - - public Optional getSetting(final String category) { - return this.settings.stream().filter(s -> s.getCategory().equals(category)).findFirst(); - } + /** + * A setting has an unique identifier. + * + * @return the unique id of the setting. + */ public String getId() { return this.id; } + /** + * A setting has a creation date, which is the date + * the user created it. + * + * @return the creation date of the setting. + */ public Date getCreationDate() { return this.creationDate; } + + /** + * Method used to get a setting based on the category specified. + * + * @param category the category of the setting searched. + * @return an optional of the setting of the category specified if found, + * an optional of empty otherwise. + */ + public Optional getSetting(final String category) { + return this.settings.stream().filter(s -> s.getCategory().equals(category)).findFirst(); + } @Override public String toString() {