]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/ontologymodule/SCLReservedWordsEscapingChildMapOfResource.java
Property following functions value and possibleValue to ontology modules
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / ontologymodule / SCLReservedWordsEscapingChildMapOfResource.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.scl.ontologymodule;
13
14 import java.util.Collection;
15 import java.util.Map;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.ResourceRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.service.CollectionSupport;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.scl.compiler.common.names.SCLReservedWords;
25 import org.simantics.utils.Development;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class SCLReservedWordsEscapingChildMapOfResource extends ResourceRead<Map<String, Resource>> {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(SCLReservedWordsEscapingChildMapOfResource.class);
32
33         public SCLReservedWordsEscapingChildMapOfResource(Resource resource) {
34             super(resource);
35         }
36         
37         @Override
38         public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
39             Layer0 L0 = Layer0.getInstance(graph);
40             Collection<Resource> objects = graph.getObjects(resource, L0.ConsistsOf);
41             CollectionSupport cs = graph.getService(CollectionSupport.class);
42             Map<String,Resource> result = cs.createObjectResourceMap(String.class, objects.size());
43             for(Resource r : objects) {
44                 String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
45                 if(name != null) {
46                     if(SCLReservedWords.RESERVED_WORDS_SET.contains(name))
47                         name = name + "_";
48                     Resource old = result.put(name, r);
49                     if (old != null)
50                         LOGGER.error("The database contains siblings with the same name " + name + " (resource=$" + resource.getResourceId() + ", child=$" + r.getResourceId() + ", previous child=$" + old.getResourceId() + ").");
51                 } else {
52                         if(Development.DEVELOPMENT)
53                             LOGGER.error("The database contains a child with no unique name (resource=$" + resource.getResourceId() + ", child=$" + r.getResourceId() + ").");
54                 }
55             }
56             return result;
57         }       
58         
59 }