Class DiagramValidator

java.lang.Object
com.nomagic.magicdraw.validation.DiagramValidator
All Implemented Interfaces:
ValidationRule
Direct Known Subclasses:
OpenedDiagramValidator

@OpenApiAll public abstract class DiagramValidator extends Object implements ValidationRule
Rule to validate elements of diagrams by a given scope (all diagrams in project, only active diagrams or diagrams from a specific scope). Rule is triggered after various diagram related events like changes in the owned symbols, switching the active diagram.
  • Constructor Details

    • DiagramValidator

      public DiagramValidator()
  • Method Details

    • init

      public void init(Project project, Constraint constraint)
      Description copied from interface: ValidationRule
      This is the first method that is invoked on the implementation of the ValidationRule interface. Implementation can initialize internal state if required.
      Specified by:
      init in interface ValidationRule
      Parameters:
      project - a project of the constraint
      constraint - a constraint object
    • needsRun

      protected boolean needsRun(PropertyChangeEvent event)
      Check if validator needs to re-run after given change event occurred.
      Parameters:
      event - event occurred.
      Returns:
      true need to validate.
    • run

      public final Set<Annotation> run(Project project, Constraint constraint)
      Description copied from interface: ValidationRule
      Run the validation.
      Specified by:
      run in interface ValidationRule
      Parameters:
      project - a project of the constraint
      constraint - a constraint object
      Returns:
      set of Annotation objects
    • getAnnotations

      public abstract Set<Annotation> getAnnotations(Project project, Constraint constraint)
      Return Annotation object for each Element or PresentationElement which should be annotated. Only the elements represented in diagrams returned by getDiagrams() method should be analyzed.
      Parameters:
      project - currently active project
      constraint - a validation rule or a legend item. Use this element to construct Annotation objects.
      Returns:
      annotations targeting elements from the diagrams in scope
    • needsRun

      public boolean needsRun()
      Description copied from interface: ValidationRule
      Returns whether the validation rule needs to run.
      Specified by:
      needsRun in interface ValidationRule
      Returns:
      true if validation rule needs to run, otherwise false
    • setNeedsRun

      protected void setNeedsRun(boolean run)
      Sets flag if this validator has to be re-run, meaning if getAnnotations(Project, Constraint) method should be called again. Actual re-run is done by the validation engine.
      Parameters:
      run - true to mark validator for rerun
    • dispose

      public void dispose()
      Unregisters all listeners that were registered and releases other resources.
      Specified by:
      dispose in interface ValidationRule
    • getProject

      protected Project getProject()
      Get the project.
      Returns:
      project
    • getDiagramsStream

      protected final Stream<DiagramPresentationElement> getDiagramsStream()
      Returns:
      Stream of the same diagrams as returned by getDiagrams() method. Convenience method.
    • getDiagrams

      protected final Collection<DiagramPresentationElement> getDiagrams()
      Returns:
      collection of diagrams for which used elements annotations should be calculated in getAnnotations(Project, Constraint) method.
    • acceptDiagram

      protected boolean acceptDiagram(DiagramPresentationElement diagram)
      Returns:
      true if the diagram in scope should be returned by getDiagrams() method
    • getPresentationElements

      protected List<PresentationElement> getPresentationElements(Element element)
      Parameters:
      element - element to get presentation elements for
      Returns:
      presentation elements of the given element from all diagrams that are currently in scope
    • getPresentationElements

      protected List<PresentationElement> getPresentationElements(Element element, Collection<DiagramPresentationElement> diagrams)
      Parameters:
      element - element to get presentation elements for
      diagrams - diagrams of interest
      Returns:
      presentation elements of the given element from given diagrams
    • isElementInScope

      public boolean isElementInScope(Element element)
      If DiagramValidator is to be used in passive validation, then model part to validate can be selected. So certain elements used in the diagram should be validated (they are in selected model scope) and certain should be ignored.

      Only annotate the element, or it's symbol if the element is in scope. Recommendation is to call this method for failing elements only as the last check, since many calls to this method might affect performance in certain scenarios

      Parameters:
      element - element which is used in any of the diagrams in scope
      Returns:
      true if the element is in the model scope for the current validation