]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/NewComponentType.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / NewComponentType.java
1 package org.simantics.modeling;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.Resource;
5 import org.simantics.db.WriteGraph;
6 import org.simantics.db.common.CommentMetadata;
7 import org.simantics.db.common.request.PossibleIndexRoot;
8 import org.simantics.db.common.utils.CommonDBUtils;
9 import org.simantics.db.common.utils.NameUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.request.InstantiateRequest;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.operation.Layer0X;
15 import org.simantics.structural.stubs.StructuralResource2;
16
17 public class NewComponentType {
18         
19         public static Resource createComponentType(WriteGraph graph, Resource library) throws DatabaseException {
20
21         ModelingResources MOD = ModelingResources.getInstance(graph);
22         Layer0 L0 = Layer0.getInstance(graph);
23         Layer0X L0X = Layer0X.getInstance(graph);
24         StructuralResource2 STR = StructuralResource2.getInstance(graph);
25         DiagramResource DIA = DiagramResource.getInstance(graph);
26
27         // New component type
28         CommonDBUtils.selectClusterSet(graph, library);
29         Resource componentType = graph.newResource();
30         graph.newClusterSet(componentType);
31         CommonDBUtils.selectClusterSet(graph, componentType);
32         
33         graph.claim(componentType, L0.PartOf, library);
34         
35         Resource indexRoot = graph.sync(new PossibleIndexRoot(library));
36
37         // Supertype
38         Resource supertype = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSupertype);
39         graph.claim(componentType, L0.Inherits, null, supertype);
40
41         // Name
42         String defaultName = graph.getRelatedValue(indexRoot, MOD.StructuralModel_HasDefaultComponentTypeName, Bindings.STRING);
43         String name = NameUtils.findFreshName(graph, defaultName, library);
44         graph.claimLiteral(componentType, L0.HasName, name + "@1");
45         // #7457: replaced by assertion in STR.ComponentType
46         //graph.claimLiteral(componentType, L0X.HasGeneratedNamePrefix, "");
47
48         // Substructure
49         Resource substructureType = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSubstructureType);
50         Resource composite = new InstantiateRequest(substructureType).perform(graph);
51         graph.claim(componentType, STR.IsDefinedBy, composite);
52         // These are added to give the configuration a proper URI.
53         graph.claimLiteral(composite, L0.HasName, "Configuration");
54         graph.claim(componentType, L0.ConsistsOf, composite);
55
56         // Attach trigger to keep component type interface structures up-to-date
57         Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);
58         if (diagram != null) {
59             Resource componentTypeUpdater = graph.newResource();
60             graph.claim(componentTypeUpdater, L0.InstanceOf, null, MOD.ComponentTypeUpdater);
61             graph.claim(diagram, L0X.HasTrigger, componentTypeUpdater);
62         }
63
64         Resource symbolDiagramType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolDiagramType);
65         if(symbolDiagramType == null) symbolDiagramType = DIA.Composite;
66
67         Resource symbolType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolType);
68         if(symbolType == null) symbolType = DIA.DefinedElement;
69
70         // Symbol
71         Resource symbol = new ModelingUtils(graph).createSymbol2("Symbol", symbolDiagramType, symbolType);
72         graph.claim(componentType, MOD.ComponentTypeToSymbol, symbol);
73         graph.claim(componentType, L0.ConsistsOf, symbol);
74         
75         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
76         graph.addMetadata(cm.add("Created new user component " + name + ", resource " + componentType));
77         
78         return componentType;
79     }
80 }