]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/validate/ValidateStructural.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / validate / ValidateStructural.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.structural2.validate;
13
14 import gnu.trove.set.hash.THashSet;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.utils.NameUtils;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.structural.stubs.StructuralResource2;
23
24 public class ValidateStructural {
25
26         ReadGraph g;
27         Layer0 b;
28         StructuralResource2 sr;
29         boolean validateTypesRecursively;
30         THashSet<Resource> validatedTypes = new THashSet<Resource>();
31         
32         IErrorReporter reporter = new StdoutReporter();
33         
34         public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) {
35                 this.g = g;
36                 this.b = Layer0.getInstance(g);
37                 this.sr = StructuralResource2.getInstance(g);
38                 this.validateTypesRecursively = validateTypesRecursively;
39         }
40         
41         public void setReporter(IErrorReporter reporter) {
42                 this.reporter = reporter;
43         }
44         
45         /**
46          * Checks that all components reachable from rootComposite recursively by ConsistsOf
47          * have unique names.
48          * @param rootComposite
49          * @throws DatabaseException
50          */
51         private void checkUniqueNames(Resource rootComposite) throws DatabaseException {
52                 THashSet<String> names = new THashSet<String>();
53                 checkUniqueNames(rootComposite, names);
54         }
55
56         private void checkUniqueNames(Resource composite, THashSet<String> names) throws DatabaseException {
57                 for(Resource component : g.getObjects(composite, b.ConsistsOf)) {
58                         String name = null;
59                         try {
60                                 name = g.getRelatedValue(component, b.HasName);
61                         } catch(DatabaseException e) {
62                                 reporter.report("Component does not have a name or there are two names.", component);
63                         }
64                         if(name != null)
65                                 if(!names.add(name))
66                                         reporter.report("Name '" + name + "' is not unique.", component);
67                         if(g.isInstanceOf(component, sr.Composite))
68                                 checkUniqueNames(composite, names);
69                 }
70         }
71         
72         public void validateComponent(Resource component) throws DatabaseException {
73                 if(!g.isInstanceOf(component, sr.Component)) {
74                         reporter.report(NameUtils.getSafeName(g, component) +
75                                         " is not a component.", component);
76                         return;
77                 }
78                 if(g.isInstanceOf(component, sr.Composite))
79                         for(Resource child : g.getObjects(component, b.ConsistsOf))
80                                 validateComponent(child);
81                 else
82                         ; // TODO
83         }
84         
85         public void validateRootComposite(Resource rootComposite) throws DatabaseException {
86                 checkUniqueNames(rootComposite);
87                 validateComponent(rootComposite);
88         }
89         
90         public void validateComponentType(Resource componentType) throws DatabaseException {
91                 Resource rootComposite = null;
92                 try {
93                         rootComposite = g.getPossibleObject(componentType, sr.IsDefinedBy);
94                 } catch(ManyObjectsForFunctionalRelationException e) {
95                         reporter.report("A user component " + NameUtils.getSafeName(g, componentType) 
96                                         + " has multiple definitions.", componentType);
97                 }
98                 if(rootComposite != null)
99                         validateRootComposite(rootComposite);   
100         }
101         
102 }