Class Checks

java.lang.Object
com.amalgamasimulation.validation.Checks

public class Checks extends Object
A set of checks for scenario validation. Each check looks at one field of a model object. If the value is wrong, the check adds a problem (Problem) to the ValidationResult.

The text of the problem comes from PlatformMessages, and the label of the field comes from FeatureLabels. For example, notNull(EObject, EStructuralFeature) adds the problem "Field is not set (Speed)" for a field without a value.

By default, problems have the Problem.Severity.ERROR severity and no tab. To change this, use a copy of Checks:

A copy adds problems to the same ValidationResult, and the original Checks does not change. You can combine copies:checks.warnings().onTab(tab).

Each field check returns true if the value is correct. If the value is wrong, the check returns false and adds a problem. Use the result to skip the next checks of the same object.

Example:


 ValidationResult result = new ValidationResult();
 Checks checks = new Checks(result, labels);
 checks.notNull(truck, MyPackage.Literals.TRUCK__TYPE);
 checks.onTab(MyMessages.messages().TAB_GENERAL).positive(truckType, MyPackage.Literals.TRUCK_TYPE__SPEED);
 checks.warnings().reportIf(scenario.getTrucks().isEmpty(), scenario, MyMessages.messages().NO_TRUCKS);
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates checks that add errors (Problem.Severity.ERROR) without a tab.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    dateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature)
    Checks that the begin date is not later than the end date.
    boolean
    dateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature, LocalDateTime defaultBegin, LocalDateTime defaultEnd)
    boolean
    distribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, boolean isRequired)
    Checks a field with a random distribution (RealDistribution), for example the duration of an operation.
    boolean
    distribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, Predicate<org.apache.commons.math3.distribution.RealDistribution> isInvalid, boolean isRequired)
    Checks the distribution in the same way as distribution(EObject, EStructuralFeature, boolean).
    boolean
    greaterThan(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, double bound)
    Checks that the numeric value of the field is greater than bound.
    boolean
    latitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the value of the field is a latitude from -90 to 90, bounds included.
    boolean
    longitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the value of the field is a longitude from -180 to 180, bounds included.
    boolean
    negative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the numeric value of the field is less than zero.
    boolean
    nonEmptyDateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature)
    Checks that the begin date is strictly earlier than the end date.
    boolean
    nonNegative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the numeric value of the field is not less than zero.
    boolean
    notEmpty(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that a list field is not empty.
    boolean
    notNull(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the field has a value.
    Returns a copy that adds problems with the title of a property panel tab.
    boolean
    percent(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the value of the field is a percent from 0 to 100, bounds included.
    boolean
    positive(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
    Checks that the numeric value of the field is greater than zero.
    void
    report(Problem problem)
    Adds a ready problem as is.
    void
    report(org.eclipse.emf.ecore.EObject object, String message)
    Adds a problem with a ready text, with the severity and the tab of this Checks.
    void
    reportIf(boolean hasProblem, org.eclipse.emf.ecore.EObject object, String message)
    Adds a problem if hasProblem is true.
    Returns a copy that adds warnings (Problem.Severity.WARNING).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Checks

      public Checks(ValidationResult result, FeatureLabels labels)
      Creates checks that add errors (Problem.Severity.ERROR) without a tab.
      Parameters:
      result - the result where the checks add problems
      labels - the labels of fields for the texts of problems
  • Method Details

    • warnings

      public Checks warnings()
      Returns a copy that adds warnings (Problem.Severity.WARNING). The tab stays the same. Exception: the distribution checks select the severity of their problems themselves.
      Returns:
      a new Checks with the same result
    • onTab

      public Checks onTab(String tab)
      Returns a copy that adds problems with the title of a property panel tab. The severity stays the same. After a double click on such a problem, the property panel opens this tab.
      Parameters:
      tab - the title of the tab, as in the property panel; null for no tab
      Returns:
      a new Checks with the same result
    • report

      public void report(org.eclipse.emf.ecore.EObject object, String message)
      Adds a problem with a ready text, with the severity and the tab of this Checks. Use this method for checks that are not among the standard ones.
      Parameters:
      object - the model object of the problem; null if the problem is about the whole scenario
      message - the text for the user; the method adds it as is
    • reportIf

      public void reportIf(boolean hasProblem, org.eclipse.emf.ecore.EObject object, String message)
      Adds a problem if hasProblem is true. Otherwise works as report(EObject, String).
      Parameters:
      hasProblem - true if there is a problem
      object - the model object of the problem; null if the problem is about the whole scenario
      message - the text for the user
    • report

      public void report(Problem problem)
      Adds a ready problem as is. The method does not apply the severity and the tab of this Checks, because the problem already has them. Use this method for subclasses of Problem with extra data.
      Parameters:
      problem - the problem to add
    • notNull

      public boolean notNull(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the field has a value. If there is no value, adds the problem "Field is not set (label)".

      Use this method for fields with one value. For a list field it always returns true, because EMF returns an empty list for it instead of null. For lists, use notEmpty(EObject, EStructuralFeature).

      Parameters:
      object - the object to check
      feature - the field of the object
      Returns:
      true if the field has a value
    • notEmpty

      public boolean notEmpty(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that a list field is not empty. If the list is empty, adds the problem "List is empty (label)".

      The field must be a list. For a field with one value, the method throws ClassCastException.

      Parameters:
      object - the object to check
      feature - the list field of the object
      Returns:
      true if the list has at least one element
    • positive

      public boolean positive(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the numeric value of the field is greater than zero. If not, adds the problem "Value should be a positive number (label)".

      If there is no value (null), the method adds this same problem instead of "Field is not set". If the field does not contain a number, the method does not check it and returns true.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      Returns:
      true if the value exists and fits
    • negative

      public boolean negative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the numeric value of the field is less than zero. If not, adds the problem "Value should be a negative number (label)".

      If there is no value (null), the method adds this same problem instead of "Field is not set". If the field does not contain a number, the method does not check it and returns true.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      Returns:
      true if the value exists and fits
    • nonNegative

      public boolean nonNegative(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the numeric value of the field is not less than zero. If not, adds the problem "Value should be a non-negative number (label)".

      If there is no value (null), the method adds this same problem instead of "Field is not set". If the field does not contain a number, the method does not check it and returns true.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      Returns:
      true if the value exists and fits
    • greaterThan

      public boolean greaterThan(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, double bound)
      Checks that the numeric value of the field is greater than bound. If not, adds the problem "Value (label) should be more than bound".

      If there is no value, or the field does not contain a number, the method also adds this problem. The text shows the bound as a double, for example 5.0.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      bound - the lower bound; the bound itself does not fit
      Returns:
      true if the value is greater than the bound
    • latitude

      public boolean latitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the value of the field is a latitude from -90 to 90, bounds included. If not, adds the problem "Value should be from -90 to 90 (label)".

      If there is no value, or the field does not contain a number, the method does not check it and returns true.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      Returns:
      true if there is no value or the value is in the range
    • longitude

      public boolean longitude(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the value of the field is a longitude from -180 to 180, bounds included. If not, adds the problem "Value should be from -180 to 180 (label)".

      If there is no value, or the field does not contain a number, the method does not check it and returns true.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      Returns:
      true if there is no value or the value is in the range
    • percent

      public boolean percent(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)
      Checks that the value of the field is a percent from 0 to 100, bounds included. If not, adds the problem "Value should be from 0 to 100 (label)".

      If there is no value, or the field does not contain a number, the method does not check it and returns true.

      Parameters:
      object - the object to check
      feature - the numeric field of the object
      Returns:
      true if there is no value or the value is in the range
    • dateRange

      public boolean dateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature)
      Checks that the begin date is not later than the end date. Equal dates are allowed. If the begin is later than the end, adds the problem "Inconsistent duration (begin - end)". The text shows only the dates, without the labels of the fields.

      If one of the dates is missing, the method does not check the range. Both fields must have the LocalDateTime type, otherwise the method throws ClassCastException.

      Parameters:
      object - the object to check
      beginFeature - the field of the begin date
      endFeature - the field of the end date
      Returns:
      true if the range is correct or one of the dates is missing
    • dateRange

      public boolean dateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature, LocalDateTime defaultBegin, LocalDateTime defaultEnd)
      Checks the range in the same way as dateRange(EObject, EStructuralFeature, EStructuralFeature). If a date field is empty, the method uses the default date, for example the begin or the end date of the scenario.
      Parameters:
      object - the object to check
      beginFeature - the field of the begin date
      endFeature - the field of the end date
      defaultBegin - the begin if the begin field is empty; can be null
      defaultEnd - the end if the end field is empty; can be null
      Returns:
      true if the range is correct or one of the dates is missing
    • nonEmptyDateRange

      public boolean nonEmptyDateRange(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature beginFeature, org.eclipse.emf.ecore.EStructuralFeature endFeature)
      Checks that the begin date is strictly earlier than the end date. Unlike dateRange(EObject, EStructuralFeature, EStructuralFeature), equal dates are also a problem. If one of the dates is missing, the method does not check the range.
      Parameters:
      object - the object to check
      beginFeature - the field of the begin date
      endFeature - the field of the end date
      Returns:
      true if the begin is earlier than the end or one of the dates is missing
    • distribution

      public boolean distribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, boolean isRequired)
      Checks a field with a random distribution (RealDistribution), for example the duration of an operation. The distribution must not give negative values.

      The method adds up to two problems:

      • a warning (Problem.Severity.WARNING) if this is a plain normal distribution and not a TruncatedNormalDistribution;
      • an error (Problem.Severity.ERROR) "Value should be a non-negative number (label)" if the lower bound of the distribution is less than zero.

      The lower bound of a plain normal distribution is minus infinity. So for such a distribution the method adds both problems.

      The severity of these problems does not depend on warnings(). The tab from onTab(String) applies.

      If there is no value, the method adds the error "Field is not set (label)" only if isRequired is true.

      Parameters:
      object - the object to check
      feature - the field with the distribution
      isRequired - true if the field is required
      Returns:
      true if there are no problems
    • distribution

      public boolean distribution(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, Predicate<org.apache.commons.math3.distribution.RealDistribution> isInvalid, boolean isRequired)
      Checks the distribution in the same way as distribution(EObject, EStructuralFeature, boolean). The difference: the error comes from the custom condition isInvalid, for example "the mean is not greater than zero".

      The text of the error is always "Value should be a non-negative number". If the condition checks something else, the text can be different from the condition.

      Parameters:
      object - the object to check
      feature - the field with the distribution
      isInvalid - the condition of the error: true if the distribution does not fit
      isRequired - true if the field is required
      Returns:
      true if there are no problems