Class ProjectCopyVisitor

  • All Implemented Interfaces:
    ICustomDatatypeVisitor, IModelVisitor

    public class ProjectCopyVisitor
    extends AbstractProjectVisitor
    Creates a deep copy of a Project.
    This visitor may change the ordering of elements inside the copied projects as it tries first to copy all elements, which do not conflict and retries to copy depending elements after needed elements have been copied. For instance:
    
     project p {
       CP1 decl;
       compound CP1 {}
     }
     
    will be translated to
    
     project p {
       compound CP1 {}
       CP1 decl;
     }
     
    Author:
    El-Sharkawy
    • Field Detail

      • copiedProjects

        private java.util.Map<Project,​Project> copiedProjects
        Tuple of (oldProject, copiedProject) containing the mapping of copied projects.
      • copiedElements

        private java.util.Map<ContainableModelElement,​ContainableModelElement> copiedElements
        Tuple of (oldElement, copiedElement) containing the mapping of copied model elements. Needed for building parents and references, and to check whether an element was already copied or not.
      • incompleteElements

        private UncopiedElementsContainer incompleteElements
        Stores elements which could not be copied (completely).
      • parents

        private java.util.Deque<IModelElement> parents
        Already translated model elements, which should be used as current parent of a model element. Used as a Stack.
      • copiedProject

        private Project copiedProject
        The copied projects (starting point). Will be null if only imports are translated.
      • visitLocalElements

        private boolean visitLocalElements
        Indicates that the currently visited elements shall not be added to the current parent, i.e., are local elements. true: Elements shall not be added false: Usual behavior
      • currentlyFrozen

        private IFreezable[] currentlyFrozen
        Temporary frozen elements, needed for creating FreezeVariableType. Not fine, but this is needed to transfer these elements from one visitation method to another were its needed. Needed to create the data type for the selector DecisionVariableDeclaration while visiting a FreezeBlock.
      • frozenElementsOK

        private boolean frozenElementsOK
      • restranslationSucceeded

        private boolean restranslationSucceeded
    • Constructor Detail

      • ProjectCopyVisitor

        public ProjectCopyVisitor​(Project originProject,
                                  FilterType filterType)
        Sole constructor for creating a copy of a Project.
        Parameters:
        originProject - The project where the visiting shall start
        filterType - Specifies whether project imports shall be considered or not. All considered projects (and imports) will be deep copied.
    • Method Detail

      • getCopiedElement

        ContainableModelElement getCopiedElement​(ContainableModelElement orgElement)
        Returns a copied element if it was copied already.
        Parameters:
        orgElement - The original element, which should be copied
        Returns:
        The copied element or null if it was not copied so far.
      • getCopiedParent

        IModelElement getCopiedParent​(IModelElement orgParent)
        Returns the copied parent (should only be used by the CSTCopyVisitor).
        Parameters:
        orgParent - The original parent of an element which needs to be copied on the fly outside of this visitor.
        Returns:
        The copied parent, a Project, a ContainableModelElement, or null if it was not copied so far.
      • getDeclarationMapping

        java.util.Map<AbstractVariable,​AbstractVariable> getDeclarationMapping()
        Returns the currently copied declarations, must not be modified from out side.
        Returns:
        An unmodifiable map of (original element, copied element).
      • reCopyUnresolvedElements

        private void reCopyUnresolvedElements()
        Tries to translate elements at the end of the visitation, which could not be translated during the regular visitation, because of missing elements.
      • resolveUncopyableCSTs

        private boolean resolveUncopyableCSTs()
        Part of the end of the coping: Tries to fix csts of incomplete copied AbstractVariables and Constraints.
        Returns:
        true if at least one element was resolved, false no elements have been resolved, maybe because there was nothing to do.
      • resolveIncompleteAssignBlocks

        private boolean resolveIncompleteAssignBlocks()
        Part of the end of the coping: Tries to fix AttributeAssignment.Assignments of incomplete copied AttributeAssignments.
        Returns:
        true if at least one element was resolved, false no elements have been resolved, maybe because there was nothing to do.
      • resolveIncompleteOperations

        private boolean resolveIncompleteOperations()
        Part of the end of the coping: Tries to fix ConstraintSyntaxTrees of incomplete copied OperationDefinitions.
        Returns:
        true if at least one element was resolved, false no elements have been resolved, maybe because there was nothing to do.
      • retryCopy

        private boolean retryCopy​(java.util.Iterator<? extends IModelElement> itr)
        Retries to copy not copied, original elements. Part of the end of the copying process.
        Parameters:
        itr - An iterator of incompleteElements, pointing to original elements, which could not be copied.
        Returns:
        true if at least one element was resolved, false no elements have been resolved, maybe because there was nothing to do.
      • buildParents

        private boolean buildParents​(IModelElement orgElement)
        Fills up the parents stack in the correct order for elements which are copied at the end of the visitation process as part of the visitProject(Project) method. This cleans up the current parent stack before filling it up with the currently needed parents.
        Parameters:
        orgElement - The element which shall be copied in the next step, for which the parent stack shall be filled correctly.
        Returns:
        true if the copied parents could be retrieved correctly, false if at least one needed parent was not copied and could not be added to the parent stack (in this case the stack is cleaned up).
      • getCopiedProject

        public Project getCopiedProject()
        Returns the copy of the visited project. This is the starting projects for all copied (imported) projects.
        Please note that this will only return any results if the original project was visited by this visitor.
        Returns:
        The copied project or null if FilterType.ONLY_IMPORTS was passed to this constructor.
      • getUncopiedElements

        public java.util.List<UncopiedElement> getUncopiedElements()
        Maybe called after visiting to verify that all elements could successfully be copied.
        Returns:
        A list of failing elements, should be empty if copying was successful.
      • getAllCopiedProjects

        public java.util.Set<Project> getAllCopiedProjects()
        Returns a set of all copied projects (including copies of imported projects).
        Returns:
        All copied projects.
      • addToCurrentParent

        private void addToCurrentParent​(ContainableModelElement copiedElement)
        Adds the copied element to the current parent (parents, considering needed class casts.
        Parameters:
        copiedElement - The copied element to add to the current parent.
      • addToParent

        private void addToParent​(ContainableModelElement copiedElement,
                                 IModelElement parent)
        Adds the specified element to the given parent.
        Parameters:
        copiedElement - The element, which shall be added to the parent (consider visitLocalElements).
        parent - The parent where to add the element.
      • getTranslatedType

        IDatatype getTranslatedType​(IDatatype originalType)
        Returns the copied IDatatype for the given one.
        Parameters:
        originalType - A IDatatype used in the original project.
        Returns:
        The copied IDatatype, the same IDatatype if it is a basis type, or null if the datatype wasn't translated so far.
      • setComment

        private void setComment​(ContainableModelElement copiedElement,
                                ContainableModelElement originalElement)
        Helper method for copying the comment from the original to the copied element. Should be called after creating the copy and before adding it to the parent.
        Parameters:
        copiedElement - The currently created copy
        originalElement - The original from which the copy is created from.
      • visitDecisionVariableDeclaration

        public void visitDecisionVariableDeclaration​(DecisionVariableDeclaration decl)
        Description copied from interface: IModelVisitor
        This method is used for visiting a decision variable declaration.
        Parameters:
        decl - One declaration which should be visited.
      • copyDefaultValue

        private void copyDefaultValue​(AbstractVariable decl,
                                      AbstractVariable copiedDecl)
        Copies the default value from the original declaration to the copied declaration.
        Parameters:
        decl - The original declaration from where the default value shall be copied from.
        copiedDecl - The target declaration.
      • visitAttribute

        public void visitAttribute​(Attribute attribute)
        Description copied from interface: IModelVisitor
        Method for visiting an attribute.
        Parameters:
        attribute - The attribute which should be visited.
      • visitConstraint

        public void visitConstraint​(Constraint constraint)
        Description copied from interface: IModelVisitor
        Method for visiting a constraint.
        Parameters:
        constraint - The constraint which should be visited.
      • visitFreezeBlock

        public void visitFreezeBlock​(FreezeBlock freeze)
        Description copied from interface: IModelVisitor
        Method for visiting a freeze block.
        Parameters:
        freeze - The freeze block which should be visited.
      • visitOperationDefinition

        public void visitOperationDefinition​(OperationDefinition opdef)
        Description copied from interface: IModelVisitor
        Method for visiting an operation definition.
        Parameters:
        opdef - The operation definition which should be visited.
      • visitPartialEvaluationBlock

        public void visitPartialEvaluationBlock​(PartialEvaluationBlock block)
        Description copied from interface: IModelVisitor
        Method for visiting a partial evaluation block.
        Parameters:
        block - The partial evaluation block which should be visited.
      • visitProjectInterface

        public void visitProjectInterface​(ProjectInterface iface)
        Description copied from interface: IModelVisitor
        Method for visiting a project interface.
        Parameters:
        iface - The interface which should be visited.
      • visitComment

        public void visitComment​(Comment comment)
        Description copied from interface: IModelVisitor
        Method for visiting a comment.
        Parameters:
        comment - the comment to visit
      • visitAttributeAssignment

        public void visitAttributeAssignment​(AttributeAssignment assignment)
        Description copied from interface: IModelVisitor
        Visits an attribute assignment.
        Parameters:
        assignment - the assignment to visit
      • visitCompoundAccessStatement

        public void visitCompoundAccessStatement​(CompoundAccessStatement access)
        Description copied from interface: IModelVisitor
        Visits a compound access statement (may occur in freeze blocks.
        Parameters:
        access - the access statement
      • visitEnum

        public void visitEnum​(Enum eenum)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting an enum.
        Parameters:
        eenum - The enum which should be visited.
      • visitOrderedEnum

        public void visitOrderedEnum​(OrderedEnum eenum)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting an ordered enum.
        Parameters:
        eenum - The enum which should be visited.
      • visitCompound

        public void visitCompound​(Compound compound)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting a compound.
        Parameters:
        compound - The compound which should be visited.
      • visitDerivedDatatype

        public void visitDerivedDatatype​(DerivedDatatype datatype)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting a derived datatype.
        Parameters:
        datatype - The datatype which should be visited.
      • visitEnumLiteral

        public void visitEnumLiteral​(EnumLiteral literal)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting an enum literal.
        Parameters:
        literal - The literal which should be visited.
      • visitReference

        public void visitReference​(Reference reference)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting a reference.
        Parameters:
        reference - The reference which should be visited.
      • visitSequence

        public void visitSequence​(Sequence sequence)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting a sequence.
        Parameters:
        sequence - The sequence which should be visited.
      • visitSet

        public void visitSet​(Set set)
        Description copied from interface: ICustomDatatypeVisitor
        Method for visiting a set.
        Parameters:
        set - The set which should be visited.