import "Simantics/DB" import "Extras/HashMap" as Map import "Simantics/Entity" import "Simantics/Model" import "Simantics/Diagram" import "http://www.simantics.org/Modeling-1.2" as MOD import "http://www.simantics.org/ModelingTemplate2d-1.0" as TMPL applyConnectionTypes :: Map.T (DiagramElement Resource) (Resource,Resource) -> () applyConnectionTypes elementMap = let isConnection ((Connection _ _ _),_) = True isConnection _ = False in iter (\(a,(source,target)) -> applyConnectionType target) (filter isConnection (Map.entries elementMap)) // ---- doElementPostProcessing (elementMap :: Map.T (DiagramElement Resource) (Resource,Resource)) (elementPostProcessing :: DiagramElement Resource -> Resource -> Resource -> ()) = do list = sortByCluster (Map.entries elementMap) (\(a,(source,target)) -> target) iter (\(a,(source,target)) -> elementPostProcessing a source target) list translateDiagrams (sourceModel :: Model) (targetModel :: Model) (folderType :: Resource) (compositeType :: Resource) (diagramConstructor :: Diagram -> [(DiagramElement Resource,Resource)]) (diagramPostProcessing :: Diagram -> Diagram -> ()) (elementPostProcessing :: DiagramElement Resource -> Resource -> Resource -> ()) (sourceDiagrams :: [Diagram]) = do elementMap = Map.create() :: Map.T (DiagramElement Resource) (Resource,Resource) // Join map contains all joins of the model joinMap = createJoinMap () for sourceDiagrams (translateDiagram sourceModel targetModel folderType compositeType diagramConstructor joinMap elementMap diagramPostProcessing) applyConnectionTypes elementMap targetDiagrams = map f sourceDiagrams :: [Diagram] where f sourceDiagram = fromJust $ getTargetDiagram targetModel $ sourceDiagram for targetDiagrams $ \diagram -> syncActivateOnce diagram doElementPostProcessing elementMap elementPostProcessing translateDiagram (sourceModel :: Model) (targetModel :: Model) (folderType :: Resource) (compositeType :: Resource) (f :: Diagram -> [(DiagramElement Resource,Resource)]) (joinMap :: Dynamic -> Resource) (elementMap :: Map.T (DiagramElement Resource) (Resource,Resource)) (diagramPostProcessing :: Diagram -> Diagram -> ()) (sourceDiagram :: Diagram) = do disableDependencies targetDiagram = translateDiagram2 targetModel sourceDiagram folderType compositeType f joinMap elementMap targetDiagram' = diagramResourceOf targetDiagram denyByPredicate targetDiagram' TMPL.HasDrawingTemplate execJust (possibleDrawingTemplate targetModel sourceDiagram) (\template -> claim targetDiagram' TMPL.HasDrawingTemplate template) diagramPostProcessing sourceDiagram targetDiagram () possibleDrawingTemplate :: Model -> Diagram -> Maybe Resource possibleDrawingTemplate model d = do d' = diagramResourceOf d library = fromJust $ possibleResourceChild model "Diagram Templates" match (possibleObject d' TMPL.HasDrawingTemplate) with Just dt -> match (possibleNameOf dt) with Just name -> possibleResourceChild library name _ -> Nothing _ -> Nothing compositeOf :: Resource -> Resource compositeOf d = singleObject d MOD.DiagramToComposite diagramOf :: Resource -> Resource diagramOf d = singleObject d MOD.CompositeToDiagram makeSpec :: Model -> Diagram -> Resource -> Resource -> DiagramSpec makeSpec targetModel sourceDiagram folderType compositeType = match (getTargetDiagram targetModel sourceDiagram) with Just targetDiagram -> ExistingDiagram targetDiagram Nothing -> NewDiagram targetModel (pathOf sourceDiagram) folderType compositeType getTargetDiagram (targetModel :: Model) (sourceDiagram :: Diagram) = (possibleDiagram targetModel $ pathOf sourceDiagram) :: Maybe Diagram store :: Map.T (DiagramElement Resource) (Resource,Resource) -> (DiagramElement Resource) -> Resource -> Resource -> () store elementMap a b c = do Map.put elementMap a (b,c) () getTargetComponent (targetDiagram :: Diagram) (sourceComponent :: Resource) = (possibleResourceChild targetDiagram (fromJust $ possibleNameOf sourceComponent)) :: Maybe Resource translateDiagram2 (targetModel :: Model) (sourceDiagram :: Diagram) (folderType :: Resource) (compositeType :: Resource) (f :: Diagram -> [(DiagramElement Resource,Resource)]) (joinMap :: Dynamic -> Resource) (elementMap :: Map.T (DiagramElement Resource) (Resource,Resource)) = do spec = makeSpec targetModel sourceDiagram folderType compositeType elementSpecs = f sourceDiagram in1 = (filter (not . isReferring) elementSpecs + filter isReferring elementSpecs) where isReferring ((Connection _ _ _),_) = True isReferring ((Monitor _ _ _ _),_) = True isReferring _ = False in2 = map (\(a,b)->a) in1 (diagram, elements) = createDiagramR spec joinMap in2 iter (\((a,b),c) -> store elementMap a b c) (zip in1 elements) diagram :: Diagram