1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.services;
14 import java.util.Collections;
15 import java.util.List;
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;
32 * @author Tuukka Lehtonen
34 public final class ComponentNamingUtil {
36 private final static boolean DEBUG = false;
38 public static final String validateInstanceName(ReadGraph graph, IProject project, Resource configurationRoot,
39 Resource composite, Resource componentType, String proposition, boolean acceptProposition) throws DatabaseException, NamingException {
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());
46 ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
47 if (strategy != null) {
48 proposition = strategy.validateInstanceName(graph, configurationRoot, composite, componentType, proposition, acceptProposition);
51 // As a fallback method, try to deduce the name based on type properties.
52 if (proposition == null) {
53 proposition = fallbackNameGeneration(graph, composite, componentType);
59 public static final String validateInstanceName(ReadGraph graph, IProject project, Resource configurationRoot, Resource component, String proposition, boolean acceptProposition) throws DatabaseException, NamingException {
61 Layer0 L0 = Layer0.getInstance(graph);
62 Resource composite = graph.getPossibleObject(component, L0.PartOf);
64 ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
65 if (strategy != null) {
66 return strategy.validateInstanceName(graph, configurationRoot, component, proposition, acceptProposition);
73 public static final String findFreshInstanceName(ReadGraph graph, IProject project, Resource configurationRoot,
74 Resource composite, Resource componentType) throws DatabaseException, NamingException {
76 String proposition = null;
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());
84 ComponentNamingStrategy strategy = findNamingStrategy(graph, project, composite);
85 if (strategy != null) {
86 proposition = strategy.findFreshInstanceName(graph, configurationRoot, composite, componentType);
89 // As a fallback method, try to deduce the name based on type properties.
90 if (proposition == null) {
91 proposition = fallbackNameGeneration(graph, composite, componentType);
97 public static final String findFreshEscapedInstanceName(ReadGraph graph, IProject project, Resource configurationRoot,
98 Resource composite, Resource componentType) throws DatabaseException, NamingException {
100 System.out.println("findFreshEscapedInstanceName(project=" + project + ", configurationRoot=" + configurationRoot
101 + ", composite=" + composite + ", componentType=" + componentType + ")");
102 new Exception("trace").printStackTrace();
104 String proposition = findFreshInstanceName(graph, project, configurationRoot, composite, componentType);
105 proposition = URIStringUtils.escape(proposition);
112 * @param configurationRoot
114 * @param componentType
115 * @param externallyReserved
117 * @throws DatabaseException
118 * @throws NamingException
120 public static final String validateInstanceName(
123 Resource configurationRoot,
125 Resource componentType,
126 Set<String> externallyReserved)
127 throws DatabaseException, NamingException
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());
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);
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);
149 * @param configurationRoot
150 * @param propositions
151 * @param externallyReserved
153 * @throws DatabaseException
154 * @throws NamingException
156 public static final List<String> validateInstanceNames(
159 Resource configurationRoot,
160 List<String> propositions,
161 boolean acceptProposition,
162 Set<String> externallyReserved)
163 throws DatabaseException, NamingException
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);
172 * This is a fallback name generation method that deduces propositions
173 * container-locally based on component type prefixes or names.
177 * @param componentType
179 * @throws DatabaseException
181 private static String fallbackNameGeneration(ReadGraph graph, Resource container, Resource componentType)
182 throws DatabaseException {
183 return NameUtils.findFreshName(graph, generateProposition(graph, container, componentType), container);
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";
198 proposition.append(componentPrefix);
199 return proposition.toString();
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);
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);