]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/validate/ValidateStructural.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / validate / ValidateStructural.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.structural2.validate;\r
13 \r
14 import gnu.trove.set.hash.THashSet;\r
15 \r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.common.utils.NameUtils;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
21 import org.simantics.layer0.Layer0;\r
22 import org.simantics.structural.stubs.StructuralResource2;\r
23 \r
24 public class ValidateStructural {\r
25 \r
26         ReadGraph g;\r
27         Layer0 b;\r
28         StructuralResource2 sr;\r
29         boolean validateTypesRecursively;\r
30         THashSet<Resource> validatedTypes = new THashSet<Resource>();\r
31         \r
32         IErrorReporter reporter = new StdoutReporter();\r
33         \r
34         public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) {\r
35                 this.g = g;\r
36                 this.b = Layer0.getInstance(g);\r
37                 this.sr = StructuralResource2.getInstance(g);\r
38                 this.validateTypesRecursively = validateTypesRecursively;\r
39         }\r
40         \r
41         public void setReporter(IErrorReporter reporter) {\r
42                 this.reporter = reporter;\r
43         }\r
44         \r
45         /**\r
46          * Checks that all components reachable from rootComposite recursively by ConsistsOf\r
47          * have unique names.\r
48          * @param rootComposite\r
49          * @throws DatabaseException\r
50          */\r
51         private void checkUniqueNames(Resource rootComposite) throws DatabaseException {\r
52                 THashSet<String> names = new THashSet<String>();\r
53                 checkUniqueNames(rootComposite, names);\r
54         }\r
55 \r
56         private void checkUniqueNames(Resource composite, THashSet<String> names) throws DatabaseException {\r
57                 for(Resource component : g.getObjects(composite, b.ConsistsOf)) {\r
58                         String name = null;\r
59                         try {\r
60                                 name = g.getRelatedValue(component, b.HasName);\r
61                         } catch(DatabaseException e) {\r
62                                 reporter.report("Component does not have a name or there are two names.", component);\r
63                         }\r
64                         if(name != null)\r
65                                 if(!names.add(name))\r
66                                         reporter.report("Name '" + name + "' is not unique.", component);\r
67                         if(g.isInstanceOf(component, sr.Composite))\r
68                                 checkUniqueNames(composite, names);\r
69                 }\r
70         }\r
71         \r
72         public void validateComponent(Resource component) throws DatabaseException {\r
73                 if(!g.isInstanceOf(component, sr.Component)) {\r
74                         reporter.report(NameUtils.getSafeName(g, component) +\r
75                                         " is not a component.", component);\r
76                         return;\r
77                 }\r
78                 if(g.isInstanceOf(component, sr.Composite))\r
79                         for(Resource child : g.getObjects(component, b.ConsistsOf))\r
80                                 validateComponent(child);\r
81                 else\r
82                         ; // TODO\r
83         }\r
84         \r
85         public void validateRootComposite(Resource rootComposite) throws DatabaseException {\r
86                 checkUniqueNames(rootComposite);\r
87                 validateComponent(rootComposite);\r
88         }\r
89         \r
90         public void validateComponentType(Resource componentType) throws DatabaseException {\r
91                 Resource rootComposite = null;\r
92                 try {\r
93                         rootComposite = g.getPossibleObject(componentType, sr.IsDefinedBy);\r
94                 } catch(ManyObjectsForFunctionalRelationException e) {\r
95                         reporter.report("A user component " + NameUtils.getSafeName(g, componentType) \r
96                                         + " has multiple definitions.", componentType);\r
97                 }\r
98                 if(rootComposite != null)\r
99                         validateRootComposite(rootComposite);   \r
100         }\r
101         \r
102 }\r