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.ArrayList;
15 import java.util.Comparator;
16 import java.util.Formatter;
17 import java.util.List;
19 import java.util.TreeSet;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.Indexing;
25 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
26 import org.simantics.db.common.request.PossibleIndexRoot;
27 import org.simantics.db.common.request.UnaryRead;
28 import org.simantics.db.common.utils.NameUtils;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.genericrelation.IndexQueries;
31 import org.simantics.db.service.GraphChangeListenerSupport;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.scl.runtime.function.Function;
34 import org.simantics.scl.runtime.function.Function1;
35 import org.simantics.scl.runtime.tuple.Tuple4;
36 import org.simantics.structural.stubs.StructuralResource2;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import gnu.trove.set.hash.THashSet;
43 * A component naming strategy implementation for structural models based on an
44 * SCL function that lists used names in a model.
47 * The type of the function is expected to be:
48 * <code>ReadGraph => Resource -> String -> Integer -> List<Map<String,Object>></code>
50 * @author Tuukka Lehtonen
52 * @see ComponentNamingStrategy
54 public class CaseInsensitiveComponentFunctionNamingStrategy extends ComponentNamingStrategyBase {
56 private static final Logger LOGGER = LoggerFactory.getLogger(CaseInsensitiveComponentFunctionNamingStrategy.class);
57 protected static final boolean DEBUG_INDEX_SEARCH = false | DEBUG_ALL;
59 @SuppressWarnings("rawtypes")
60 private final Function index;
63 * A filter that is applied to all user-provided name propositions before
66 private Function1<String, String> propositionPreFilter;
69 * Construct an index-based naming strategy with "%s %d" as the generated
70 * name format. See {@link Formatter} for the format specification.
72 * @param index the index function for looking up used names. The function
74 * <code>ReadGraph => Resource -> String -> Integer -> List<Map<String,Object>></code>
76 @SuppressWarnings("rawtypes")
77 public CaseInsensitiveComponentFunctionNamingStrategy(Function index) {
82 * Construct an index-based naming strategy with the specified format as the
83 * generated name format. See {@link Formatter} for the format
86 * @param generatedNameFormat the format to use for generated names, see
88 * @param index the index function for looking up used names. The function
90 * <code>ReadGraph => Resource -> String -> Integer -> List<Map<String,Object>></code>
92 @SuppressWarnings("rawtypes")
93 public CaseInsensitiveComponentFunctionNamingStrategy(String generatedNameFormat, Function index) {
94 super(generatedNameFormat);
99 * Construct an index-based naming strategy with the specified format as the
100 * generated name format. See {@link Formatter} for the format
103 * @param generatedNameFormat the format to use for generated names, see
105 * @param index the index function for looking up used names. The function
107 * <code>ReadGraph => Resource -> String -> Integer -> List<Map<String,Object>></code>
108 * @param an optional function to
110 @SuppressWarnings("rawtypes")
111 public CaseInsensitiveComponentFunctionNamingStrategy(String generatedNameFormat, Function index, Function1<String, String> propositionPreFilter) {
112 super(generatedNameFormat);
114 this.propositionPreFilter = propositionPreFilter;
117 CaseInsensitiveComponentNamingStrategy2 fallbackStrategy = null;
120 public String validateInstanceName(ReadGraph graph,
121 Resource configurationRoot, Resource component, String proposition, boolean acceptProposition)
122 throws NamingException, DatabaseException {
124 String lowercaseProposition = proposition.toLowerCase();
126 Layer0 L0 = Layer0.getInstance(graph);
127 String name = graph.getPossibleRelatedValue(component, L0.HasName, Bindings.STRING);
130 String lowercaseName = name.toLowerCase();
131 if(lowercaseName.startsWith(lowercaseProposition)) {
133 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(configurationRoot));
134 if (indexRoot != null) {
136 synchronized (this) {
138 String search = "Name:" + lowercaseName + "*";
139 @SuppressWarnings("unchecked")
140 List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);
142 Set<Resource> rs = new THashSet<Resource>();
143 for (Resource componentResult : components) {
144 if (DEBUG_INDEX_SEARCH)
145 LOGGER.info(getClass().getSimpleName() + ": found " + componentResult);
146 String n = graph.getPossibleRelatedValue(componentResult, L0.HasName, Bindings.STRING);
147 if (n != null && n.toLowerCase().equals(lowercaseName))
148 rs.add(componentResult);
151 Cache c = getCache(graph, indexRoot);
154 if(!c.getRequested().contains(name)) {
155 c.addRequested(name);
158 } else if(rs.size() == 1) {
159 if(component.equals(rs.iterator().next())) {
172 StructuralResource2 STR = StructuralResource2.getInstance(graph);
173 Resource container = graph.getSingleObject(component, L0.PartOf);
174 Resource componentType = graph.getSingleType(component, STR.Component);
175 return validateInstanceName(graph, configurationRoot, container, componentType, proposition, acceptProposition);
179 static class ComponentsRequest extends UnaryRead<Tuple4, Set<String>>{
181 public ComponentsRequest(Tuple4 parameter) {
186 public Set<String> perform(ReadGraph graph) throws DatabaseException {
188 Resource indexRoot = (Resource)parameter.get(0);
189 Function index = (Function)parameter.get(1);
190 String search = (String)parameter.get(2);
191 Comparator<Object> comparator = (Comparator<Object>)parameter.get(3);
193 List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);
194 Set<String> reserved = new TreeSet<String>(comparator);
196 Layer0 L0 = Layer0.getInstance(graph);
197 for (Resource componentResult : components) {
198 if (DEBUG_INDEX_SEARCH)
199 LOGGER.info(getClass().getSimpleName() + ": found " + componentResult);
200 String name = graph.getPossibleRelatedValue(componentResult, L0.HasName, Bindings.STRING);
205 LOGGER.warn("found " + reserved.size() + " components");
214 public String validateInstanceName(ReadGraph graph, Resource configurationRoot, Resource container,
215 Resource componentType, String proposition, boolean acceptProposition) throws NamingException, DatabaseException {
216 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(configurationRoot));
217 if (indexRoot == null) {
218 LOGGER.warn("Could not find index root from configuration root '" + NameUtils.getSafeName(graph, configurationRoot, true) + "'");
219 if(fallbackStrategy == null)
221 new CaseInsensitiveComponentNamingStrategy2(graph.getService(GraphChangeListenerSupport.class), generatedNameFormat);
222 return fallbackStrategy.validateInstanceName(graph, configurationRoot, container, componentType, proposition, acceptProposition);
225 if (propositionPreFilter != null)
226 proposition = propositionPreFilter.apply(proposition);
228 synchronized (this) {
230 String search = "Name:" + proposition + "*";
232 Set<String> reserved = graph.syncRequest(new ComponentsRequest(new Tuple4(indexRoot, index, search, getComparator())), TransientCacheAsyncListener.instance());
234 Cache cache = getCache(graph, indexRoot);
236 findStartsWithMatches(cache.getRequested(), proposition, reserved);
238 String result = findFreshName(reserved, proposition, acceptProposition);
239 cache.addRequested(result);
241 if (DEBUG_INDEX_SEARCH)
242 LOGGER.info(getClass().getSimpleName() + ": validated instance name " + result);
249 public List<String> validateInstanceNames(
251 Resource configurationRoot,
252 List<String> propositions,
253 boolean acceptProposition,
254 Set<String> externallyReserved) throws NamingException, DatabaseException
256 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(configurationRoot));
257 if (indexRoot == null)
258 throw new NamingException("Could not find index root from configuration root '" + NameUtils.getSafeName(graph, configurationRoot, true) + "'");
260 Layer0 L0 = Layer0.getInstance(graph);
262 synchronized (this) {
263 List<String> result = new ArrayList<String>(propositions.size());
264 Set<String> reserved = new TreeSet<String>(getComparator());
266 for (String proposition : propositions) {
267 if (propositionPreFilter != null)
268 proposition = propositionPreFilter.apply(proposition);
270 String search = "Name:" + IndexQueries.quoteTerm( proposition );
271 @SuppressWarnings("unchecked")
272 List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);
274 if (DEBUG_INDEX_SEARCH)
275 LOGGER.info(getClass().getSimpleName() + ": found " + components.size()
276 + " index results for index root " + indexRoot + " & configurationRoot " + configurationRoot
277 + " & proposition '" + proposition + "':");
280 for (Resource componentResult : components) {
281 if (DEBUG_INDEX_SEARCH)
282 LOGGER.info(getClass().getSimpleName() + ": found " + componentResult);
283 String name = graph.getPossibleRelatedValue(componentResult, L0.HasName, Bindings.STRING);
288 if (externallyReserved != null)
289 reserved.addAll(externallyReserved);
290 reserved.addAll(result);
291 String name = findFreshName(reserved, proposition, acceptProposition);
295 if (DEBUG_INDEX_SEARCH)
296 LOGGER.info(getClass().getSimpleName() + ": validated instance name " + proposition + " -> " + name);
299 if (DEBUG_INDEX_SEARCH)
300 LOGGER.info(getClass().getSimpleName() + ": validated instance names " + propositions + " -> " + result);
306 private Cache getCache(ReadGraph graph, Resource root) throws DatabaseException {
307 Cache cache = Indexing.getCache(root, Cache.class);
308 if(cache != null) return cache;
309 synchronized (this) {
310 cache = Indexing.getCache(root, Cache.class);
311 if(cache != null) return cache;
312 return Indexing.createCache(root, new Cache(caseInsensitive));
318 private final Set<String> requested;
320 Cache(boolean caseInsensitive) {
321 this.requested = new TreeSet<String>(getComparator(caseInsensitive));
324 public Set<String> getRequested() {
328 public void addRequested(String name) {
333 public String toString() {
334 return getClass().getSimpleName() + ": " + requested;