1 package org.simantics.modeling;
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;
17 public class NewComponentType {
19 public static Resource createComponentType(WriteGraph graph, Resource library) throws DatabaseException {
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);
28 CommonDBUtils.selectClusterSet(graph, library);
29 Resource componentType = graph.newResource();
30 graph.newClusterSet(componentType);
31 CommonDBUtils.selectClusterSet(graph, componentType);
33 graph.claim(componentType, L0.PartOf, library);
35 Resource indexRoot = graph.sync(new PossibleIndexRoot(library));
38 Resource supertype = graph.getSingleObject(indexRoot, MOD.StructuralModel_HasComponentTypeSupertype);
39 graph.claim(componentType, L0.Inherits, null, supertype);
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, "");
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);
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);
64 Resource symbolDiagramType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolDiagramType);
65 if(symbolDiagramType == null) symbolDiagramType = DIA.Composite;
67 Resource symbolType = graph.getPossibleObject(indexRoot, MOD.StructuralModel_HasSymbolType);
68 if(symbolType == null) symbolType = DIA.DefinedElement;
71 Resource symbol = new ModelingUtils(graph).createSymbol2("Symbol", symbolDiagramType, symbolType);
72 graph.claim(componentType, MOD.ComponentTypeToSymbol, symbol);
73 graph.claim(componentType, L0.ConsistsOf, symbol);
75 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
76 graph.addMetadata(cm.add("Created new user component " + name + ", resource " + componentType));