]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/services/ComponentNamingUtil.java
Generic <String, Object> property map for tree node structure
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / services / ComponentNamingUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.services;
13
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.util.URIStringUtils;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied2;
23 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.operation.Layer0X;
28 import org.simantics.project.IProject;
29 import org.simantics.structural.stubs.StructuralResource2;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public final class ComponentNamingUtil {
35
36     private final static boolean DEBUG = false;
37
38     public static final String validateInstanceName(ReadGraph graph, IProject project, Resource configurationRoot,
39             Resource composite, Resource componentType, String proposition, boolean acceptProposition) throws DatabaseException, NamingException {
40         if (DEBUG) {
41             System.out.println("validateInstanceName(project=" + project + ", configurationRoot=" + configurationRoot
42                     + ", composite=" + composite + ", componentType=" + componentType + ", proposition=" + proposition + ")");
43             System.out.println("  INVOKED FROM: " + new Exception().getStackTrace()[1].toString());
44         }
45
46         ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
47         if (strategy != null) {
48             proposition = strategy.validateInstanceName(graph, configurationRoot, composite, componentType, proposition, acceptProposition);
49         }
50
51         // As a fallback method, try to deduce the name based on type properties.
52         if (proposition == null) {
53             proposition = fallbackNameGeneration(graph, composite, componentType);
54         }
55
56         return proposition;
57     }
58
59     public static final String validateInstanceName(ReadGraph graph, IProject project, Resource configurationRoot, Resource component, String proposition, boolean acceptProposition) throws DatabaseException, NamingException {
60         
61         Layer0 L0 = Layer0.getInstance(graph);
62         Resource composite = graph.getPossibleObject(component, L0.PartOf);
63         
64         ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
65         if (strategy != null) {
66             return strategy.validateInstanceName(graph, configurationRoot, component, proposition, acceptProposition);
67         } else {
68                 return null;
69         }
70         
71     }
72     
73     public static final String findFreshInstanceName(ReadGraph graph, IProject project, Resource configurationRoot,
74             Resource composite, Resource componentType) throws DatabaseException, NamingException {
75
76         String proposition = null;
77
78         if (DEBUG) {
79             System.out.println("findFreshInstanceName(project=" + project + ", configurationRoot=" + configurationRoot
80                     + ", composite=" + composite + ", componentType=" + componentType + ")");
81             System.out.println("  INVOKED FROM: " + new Exception().getStackTrace()[1].toString());
82         }
83
84         ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
85         if (strategy != null) {
86             proposition = strategy.findFreshInstanceName(graph, configurationRoot, composite, componentType);
87         }
88
89         // As a fallback method, try to deduce the name based on type properties.
90         if (proposition == null) {
91             proposition = fallbackNameGeneration(graph, composite, componentType);
92         }
93
94         return proposition;
95     }
96
97     public static final String findFreshEscapedInstanceName(ReadGraph graph, IProject project, Resource configurationRoot,
98             Resource composite, Resource componentType) throws DatabaseException, NamingException {
99         if (DEBUG) {
100             System.out.println("findFreshEscapedInstanceName(project=" + project + ", configurationRoot=" + configurationRoot
101                     + ", composite=" + composite + ", componentType=" + componentType + ")");
102             new Exception("trace").printStackTrace();
103         }
104         String proposition = findFreshInstanceName(graph, project, configurationRoot, composite, componentType);
105         proposition = URIStringUtils.escape(proposition);
106         return proposition;
107     }
108
109     /**
110      * @param graph
111      * @param project
112      * @param configurationRoot
113      * @param composite
114      * @param componentType
115      * @param externallyReserved
116      * @return
117      * @throws DatabaseException
118      * @throws NamingException
119      */
120     public static final String validateInstanceName(
121             ReadGraph graph,
122             IProject project,
123             Resource configurationRoot,
124             Resource composite,
125             Resource componentType,
126             Set<String> externallyReserved)
127                     throws DatabaseException, NamingException
128     {
129         if (DEBUG) {
130             System.out.println("validateInstanceName(project=" + project + ", configurationRoot=" + configurationRoot
131                     + ", composite=" + composite + ", componentType=" + componentType + ")");
132             System.out.println("  INVOKED FROM: " + new Exception().getStackTrace()[1].toString());
133         }
134
135         ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
136         if (strategy == null)
137             throw new NamingException("No naming strategy available, project=" + project + ", composite=" + composite + ", componentType=" + componentType);
138
139         String proposition = ComponentNamingUtil.generateProposition(graph, composite, componentType);
140         List<String> validated = strategy.validateInstanceNames(graph, configurationRoot, Collections.singletonList(proposition), true, externallyReserved);
141         proposition = validated.get(0);
142
143         return proposition;
144     }
145
146     /**
147      * @param graph
148      * @param project
149      * @param configurationRoot
150      * @param propositions
151      * @param externallyReserved
152      * @return
153      * @throws DatabaseException
154      * @throws NamingException
155      */
156     public static final List<String> validateInstanceNames(
157             ReadGraph graph,
158             IProject project,
159             Resource configurationRoot,
160             List<String> propositions,
161             boolean acceptProposition,
162             Set<String> externallyReserved)
163                     throws DatabaseException, NamingException
164     {
165         ComponentNamingStrategy strategy = findNamingStrategy(graph, project, configurationRoot);
166         if (strategy == null)
167             throw new NamingException("No naming strategy available, project=" + project);
168         return strategy.validateInstanceNames(graph, configurationRoot, propositions, acceptProposition, externallyReserved);
169     }
170
171     /**
172      * This is a fallback name generation method that deduces propositions
173      * container-locally based on component type prefixes or names.
174      * 
175      * @param graph
176      * @param composite
177      * @param componentType
178      * @return
179      * @throws DatabaseException
180      */
181     private static String fallbackNameGeneration(ReadGraph graph, Resource container, Resource componentType)
182             throws DatabaseException {
183         return NameUtils.findFreshName(graph, generateProposition(graph, container, componentType), container);
184     }
185
186     public static String generateProposition(ReadGraph graph, String containerGeneratedNamePrefix, Resource componentType) throws DatabaseException {
187         Layer0X L0X = Layer0X.getInstance(graph);
188         StringBuilder proposition = new StringBuilder();
189         if (containerGeneratedNamePrefix != null)
190             proposition.append(containerGeneratedNamePrefix);
191         String componentPrefix = graph.getPossibleRelatedValue(componentType, L0X.HasGeneratedNamePrefix, Bindings.STRING);
192         if (componentPrefix == null) {
193             Layer0 L0 = Layer0.getInstance(graph);
194             componentPrefix = graph.getPossibleRelatedValue(componentType, L0.HasName);
195             if (componentPrefix == null)
196                 componentPrefix = "Entity";
197         }
198         proposition.append(componentPrefix);
199         return proposition.toString();
200     }
201
202     public static String generateProposition(ReadGraph graph, Resource container, Resource componentType) throws DatabaseException {
203         Layer0X L0X = Layer0X.getInstance(graph);
204         String containerPrefix = graph.getPossibleRelatedValue(container, L0X.HasGeneratedNamePrefix, Bindings.STRING);
205         return generateProposition(graph, containerPrefix != null ? containerPrefix : "", componentType);
206     }
207
208     public static final ComponentNamingStrategy findNamingStrategy(ReadGraph graph, IProject project, Resource composite) throws DatabaseException {
209         // Primarily, use naming function from composite
210         ComponentNamingStrategy strategy = graph.syncRequest(
211                 new PossibleRelatedValueImplied2<ComponentNamingStrategy>(composite, StructuralResource2.getInstance(graph).Composite_namingFunction),
212                 TransientCacheListener.<ComponentNamingStrategy>instance());
213         if (strategy == null && project != null) {
214             // If not available, try to find ComponentNamingStrategy for project
215             strategy = project.getHint(ComponentNamingStrategy.PROJECT_KEY);
216         }
217         return strategy;
218     }
219
220 }