Package com.nomagic.magicdraw.validation
Class DiagramValidator
- java.lang.Object
-
- com.nomagic.magicdraw.validation.DiagramValidator
-
- All Implemented Interfaces:
ValidationRule
- Direct Known Subclasses:
OpenedDiagramValidator
@OpenApiAll public abstract class DiagramValidator extends java.lang.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 and etc.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Class<? extends Element>,java.util.Collection<SmartListenerConfig>>configsprivate Constraintconstraintprivate com.nomagic.magicdraw.uml.symbols.DiagramListenerAdapter2diagramListenerDiagram listener to listen for changes in diagram.private com.nomagic.magicdraw.validation.ConstrainedElementFilterDescriptorfilterDescriptorprivate java.beans.PropertyChangeListenerneedRunListenerprivate booleanneedsRunFlag indicating if the annotations should be recalculatedprivate Projectprojectprivate com.nomagic.magicdraw.validation.ScopeListenerscopeChangeListenerprivate com.nomagic.magicdraw.validation.ScopeProviderscopeProviderprivate com.nomagic.magicdraw.core.diagram.DiagramUsedElementsChangeListenerusedElementsListenerListens for changes of used model elements.
-
Constructor Summary
Constructors Constructor Description DiagramValidator()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected booleanacceptDiagram(DiagramPresentationElement diagram)private com.nomagic.magicdraw.uml.symbols.DiagramListenerAdapter2createDiagramListener()private com.nomagic.magicdraw.core.diagram.DiagramUsedElementsChangeListenercreateDiagramUsedElementsChangeListener()voiddispose()Unregisters all listeners that were registered and releases other resources.abstract java.util.Set<Annotation>getAnnotations(Project project, Constraint constraint)protected java.util.Collection<DiagramPresentationElement>getDiagrams()protected java.util.stream.Stream<DiagramPresentationElement>getDiagramsStream()protected java.util.List<PresentationElement>getPresentationElements(Element element)protected java.util.List<PresentationElement>getPresentationElements(Element element, java.util.Collection<DiagramPresentationElement> diagrams)protected ProjectgetProject()Get the project.voidinit(Project project, Constraint constraint)This is the first method that is invoked on the implementation of theValidationRuleinterface.private booleanisConstrainedByFilter(DiagramPresentationElement diagram)booleanisElementInScope(Element element)If DiagramValidator is to be used in passive validation, then model part to validate can be selected.booleanneedsRun()Returns whether the validation rule needs to run.protected booleanneedsRun(java.beans.PropertyChangeEvent event)Check if validator needs to re-run after given change event occurred.private voidregisterConfigs()private voidremoveScopeListenerFromProvider()java.util.Set<Annotation>run(Project project, Constraint constraint)Run the validation.protected voidsetNeedsRun(boolean run)Sets flag if this validator has to be re-run, meaning ifgetAnnotations(Project, Constraint)method should be called again.(package private) voidsetScopeProvider(com.nomagic.magicdraw.validation.ScopeProvider scopeProvider)private voidunregisterConfigs()
-
-
-
Field Detail
-
needsRun
private volatile boolean needsRun
Flag indicating if the annotations should be recalculated
-
diagramListener
private com.nomagic.magicdraw.uml.symbols.DiagramListenerAdapter2 diagramListener
Diagram listener to listen for changes in diagram.
-
usedElementsListener
private com.nomagic.magicdraw.core.diagram.DiagramUsedElementsChangeListener usedElementsListener
Listens for changes of used model elements. DiagramListenerAdapter2 does not fire any events if used elements changes for non symbol diagrams
-
project
private Project project
-
scopeProvider
private com.nomagic.magicdraw.validation.ScopeProvider scopeProvider
-
constraint
private Constraint constraint
-
needRunListener
private final java.beans.PropertyChangeListener needRunListener
-
scopeChangeListener
private final com.nomagic.magicdraw.validation.ScopeListener scopeChangeListener
-
configs
@CheckForNull private java.util.Map<java.lang.Class<? extends Element>,java.util.Collection<SmartListenerConfig>> configs
-
filterDescriptor
@CheckForNull private com.nomagic.magicdraw.validation.ConstrainedElementFilterDescriptor filterDescriptor
-
-
Method Detail
-
init
public void init(Project project, Constraint constraint)
Description copied from interface:ValidationRuleThis is the first method that is invoked on the implementation of theValidationRuleinterface. Implementation can initialize internal state if required.- Specified by:
initin interfaceValidationRule- Parameters:
project- a project of the constraintconstraint- a constraint object
-
createDiagramListener
private com.nomagic.magicdraw.uml.symbols.DiagramListenerAdapter2 createDiagramListener()
-
createDiagramUsedElementsChangeListener
private com.nomagic.magicdraw.core.diagram.DiagramUsedElementsChangeListener createDiagramUsedElementsChangeListener()
-
needsRun
protected boolean needsRun(java.beans.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 java.util.Set<Annotation> run(Project project, Constraint constraint)
Description copied from interface:ValidationRuleRun the validation.- Specified by:
runin interfaceValidationRule- Parameters:
project- a project of the constraintconstraint- a constraint object- Returns:
- set of
Annotationobjects
-
getAnnotations
public abstract java.util.Set<Annotation> getAnnotations(Project project, Constraint constraint)
ReturnAnnotationobject for eachElementorPresentationElementwhich should be annotated. Only the elements represented in diagrams returned bygetDiagrams()method should be analyzed.- Parameters:
project- currently active projectconstraint- a validation rule or a legend item. Use this element to constructAnnotationobjects.- Returns:
- annotations targeting elements from the diagrams in scope
-
needsRun
public boolean needsRun()
Description copied from interface:ValidationRuleReturns whether the validation rule needs to run.- Specified by:
needsRunin interfaceValidationRule- 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 ifgetAnnotations(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:
disposein interfaceValidationRule
-
getProject
protected Project getProject()
Get the project.- Returns:
- project
-
getDiagramsStream
protected final java.util.stream.Stream<DiagramPresentationElement> getDiagramsStream()
- Returns:
- Stream of the same diagrams as returned by
getDiagrams()method. Convenience method.
-
getDiagrams
protected final java.util.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
-
isConstrainedByFilter
private boolean isConstrainedByFilter(DiagramPresentationElement diagram)
-
getPresentationElements
protected java.util.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 java.util.List<PresentationElement> getPresentationElements(Element element, java.util.Collection<DiagramPresentationElement> diagrams)
- Parameters:
element- element to get presentation elements fordiagrams- diagrams of interest- Returns:
- presentation elements of the given element from given diagrams
-
setScopeProvider
void setScopeProvider(com.nomagic.magicdraw.validation.ScopeProvider scopeProvider)
-
removeScopeListenerFromProvider
private void removeScopeListenerFromProvider()
-
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
-
registerConfigs
private void registerConfigs()
-
unregisterConfigs
private void unregisterConfigs()
-
-