]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/NewComponentType.java
Dynamic terminals and connections
[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         graph.claimLiteral(componentType, L0X.HasGeneratedNamePrefix, "");
46
47         // Substructure
48         Resource substructureType = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSubstructureType);
49         Resource composite = new InstantiateRequest(substructureType).perform(graph);
50         graph.claim(componentType, STR.IsDefinedBy, composite);
51         // These are added to give the configuration a proper URI.
52         graph.claimLiteral(composite, L0.HasName, "Configuration");
53         graph.claim(componentType, L0.ConsistsOf, composite);
54
55         // Attach trigger to keep component type interface structures up-to-date
56         Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);
57         if (diagram != null) {
58             Resource componentTypeUpdater = graph.newResource();
59             graph.claim(componentTypeUpdater, L0.InstanceOf, null, MOD.ComponentTypeUpdater);
60             graph.claim(diagram, L0X.HasTrigger, componentTypeUpdater);
61         }
62
63         Resource symbolDiagramType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolDiagramType);
64         if(symbolDiagramType == null) symbolDiagramType = DIA.Composite;
65
66         Resource symbolType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolType);
67         if(symbolType == null) symbolType = DIA.DefinedElement;
68
69         // Symbol
70         Resource symbol = new ModelingUtils(graph).createSymbol2("Symbol", symbolDiagramType, symbolType);
71         graph.claim(componentType, MOD.ComponentTypeToSymbol, symbol);
72         graph.claim(componentType, L0.ConsistsOf, symbol);
73         
74         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
75         graph.addMetadata(cm.add("Created new user component " + name + ", resource " + componentType));
76         
77         return componentType;
78     }
79 }