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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.structural2.validate;
\r
14 import gnu.trove.set.hash.THashSet;
\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
24 public class ValidateStructural {
\r
28 StructuralResource2 sr;
\r
29 boolean validateTypesRecursively;
\r
30 THashSet<Resource> validatedTypes = new THashSet<Resource>();
\r
32 IErrorReporter reporter = new StdoutReporter();
\r
34 public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) {
\r
36 this.b = Layer0.getInstance(g);
\r
37 this.sr = StructuralResource2.getInstance(g);
\r
38 this.validateTypesRecursively = validateTypesRecursively;
\r
41 public void setReporter(IErrorReporter reporter) {
\r
42 this.reporter = reporter;
\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
51 private void checkUniqueNames(Resource rootComposite) throws DatabaseException {
\r
52 THashSet<String> names = new THashSet<String>();
\r
53 checkUniqueNames(rootComposite, names);
\r
56 private void checkUniqueNames(Resource composite, THashSet<String> names) throws DatabaseException {
\r
57 for(Resource component : g.getObjects(composite, b.ConsistsOf)) {
\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
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
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
78 if(g.isInstanceOf(component, sr.Composite))
\r
79 for(Resource child : g.getObjects(component, b.ConsistsOf))
\r
80 validateComponent(child);
\r
85 public void validateRootComposite(Resource rootComposite) throws DatabaseException {
\r
86 checkUniqueNames(rootComposite);
\r
87 validateComponent(rootComposite);
\r
90 public void validateComponentType(Resource componentType) throws DatabaseException {
\r
91 Resource rootComposite = null;
\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
98 if(rootComposite != null)
\r
99 validateRootComposite(rootComposite);
\r