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.structural2.validate;
14 import gnu.trove.set.hash.THashSet;
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;
24 public class ValidateStructural {
28 StructuralResource2 sr;
29 boolean validateTypesRecursively;
30 THashSet<Resource> validatedTypes = new THashSet<Resource>();
32 IErrorReporter reporter = new StdoutReporter();
34 public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) {
36 this.b = Layer0.getInstance(g);
37 this.sr = StructuralResource2.getInstance(g);
38 this.validateTypesRecursively = validateTypesRecursively;
41 public void setReporter(IErrorReporter reporter) {
42 this.reporter = reporter;
46 * Checks that all components reachable from rootComposite recursively by ConsistsOf
48 * @param rootComposite
49 * @throws DatabaseException
51 private void checkUniqueNames(Resource rootComposite) throws DatabaseException {
52 THashSet<String> names = new THashSet<String>();
53 checkUniqueNames(rootComposite, names);
56 private void checkUniqueNames(Resource composite, THashSet<String> names) throws DatabaseException {
57 for(Resource component : g.getObjects(composite, b.ConsistsOf)) {
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);
66 reporter.report("Name '" + name + "' is not unique.", component);
67 if(g.isInstanceOf(component, sr.Composite))
68 checkUniqueNames(composite, names);
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);
78 if(g.isInstanceOf(component, sr.Composite))
79 for(Resource child : g.getObjects(component, b.ConsistsOf))
80 validateComponent(child);
85 public void validateRootComposite(Resource rootComposite) throws DatabaseException {
86 checkUniqueNames(rootComposite);
87 validateComponent(rootComposite);
90 public void validateComponentType(Resource componentType) throws DatabaseException {
91 Resource rootComposite = null;
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);
98 if(rootComposite != null)
99 validateRootComposite(rootComposite);