X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.structural2%2Fsrc%2Forg%2Fsimantics%2Fstructural2%2Fvalidate%2FValidateStructural.java;h=626045805e4adf83f35ab65dbaad1030b0b45ca7;hp=4fd123e4fb69b11d716f3e3acbea71469858199e;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/validate/ValidateStructural.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/validate/ValidateStructural.java index 4fd123e4f..626045805 100644 --- a/bundles/org.simantics.structural2/src/org/simantics/structural2/validate/ValidateStructural.java +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/validate/ValidateStructural.java @@ -1,102 +1,102 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.structural2.validate; - -import gnu.trove.set.hash.THashSet; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; -import org.simantics.layer0.Layer0; -import org.simantics.structural.stubs.StructuralResource2; - -public class ValidateStructural { - - ReadGraph g; - Layer0 b; - StructuralResource2 sr; - boolean validateTypesRecursively; - THashSet validatedTypes = new THashSet(); - - IErrorReporter reporter = new StdoutReporter(); - - public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) { - this.g = g; - this.b = Layer0.getInstance(g); - this.sr = StructuralResource2.getInstance(g); - this.validateTypesRecursively = validateTypesRecursively; - } - - public void setReporter(IErrorReporter reporter) { - this.reporter = reporter; - } - - /** - * Checks that all components reachable from rootComposite recursively by ConsistsOf - * have unique names. - * @param rootComposite - * @throws DatabaseException - */ - private void checkUniqueNames(Resource rootComposite) throws DatabaseException { - THashSet names = new THashSet(); - checkUniqueNames(rootComposite, names); - } - - private void checkUniqueNames(Resource composite, THashSet names) throws DatabaseException { - for(Resource component : g.getObjects(composite, b.ConsistsOf)) { - String name = null; - try { - name = g.getRelatedValue(component, b.HasName); - } catch(DatabaseException e) { - reporter.report("Component does not have a name or there are two names.", component); - } - if(name != null) - if(!names.add(name)) - reporter.report("Name '" + name + "' is not unique.", component); - if(g.isInstanceOf(component, sr.Composite)) - checkUniqueNames(composite, names); - } - } - - public void validateComponent(Resource component) throws DatabaseException { - if(!g.isInstanceOf(component, sr.Component)) { - reporter.report(NameUtils.getSafeName(g, component) + - " is not a component.", component); - return; - } - if(g.isInstanceOf(component, sr.Composite)) - for(Resource child : g.getObjects(component, b.ConsistsOf)) - validateComponent(child); - else - ; // TODO - } - - public void validateRootComposite(Resource rootComposite) throws DatabaseException { - checkUniqueNames(rootComposite); - validateComponent(rootComposite); - } - - public void validateComponentType(Resource componentType) throws DatabaseException { - Resource rootComposite = null; - try { - rootComposite = g.getPossibleObject(componentType, sr.IsDefinedBy); - } catch(ManyObjectsForFunctionalRelationException e) { - reporter.report("A user component " + NameUtils.getSafeName(g, componentType) - + " has multiple definitions.", componentType); - } - if(rootComposite != null) - validateRootComposite(rootComposite); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.structural2.validate; + +import gnu.trove.set.hash.THashSet; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.layer0.Layer0; +import org.simantics.structural.stubs.StructuralResource2; + +public class ValidateStructural { + + ReadGraph g; + Layer0 b; + StructuralResource2 sr; + boolean validateTypesRecursively; + THashSet validatedTypes = new THashSet(); + + IErrorReporter reporter = new StdoutReporter(); + + public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) { + this.g = g; + this.b = Layer0.getInstance(g); + this.sr = StructuralResource2.getInstance(g); + this.validateTypesRecursively = validateTypesRecursively; + } + + public void setReporter(IErrorReporter reporter) { + this.reporter = reporter; + } + + /** + * Checks that all components reachable from rootComposite recursively by ConsistsOf + * have unique names. + * @param rootComposite + * @throws DatabaseException + */ + private void checkUniqueNames(Resource rootComposite) throws DatabaseException { + THashSet names = new THashSet(); + checkUniqueNames(rootComposite, names); + } + + private void checkUniqueNames(Resource composite, THashSet names) throws DatabaseException { + for(Resource component : g.getObjects(composite, b.ConsistsOf)) { + String name = null; + try { + name = g.getRelatedValue(component, b.HasName); + } catch(DatabaseException e) { + reporter.report("Component does not have a name or there are two names.", component); + } + if(name != null) + if(!names.add(name)) + reporter.report("Name '" + name + "' is not unique.", component); + if(g.isInstanceOf(component, sr.Composite)) + checkUniqueNames(composite, names); + } + } + + public void validateComponent(Resource component) throws DatabaseException { + if(!g.isInstanceOf(component, sr.Component)) { + reporter.report(NameUtils.getSafeName(g, component) + + " is not a component.", component); + return; + } + if(g.isInstanceOf(component, sr.Composite)) + for(Resource child : g.getObjects(component, b.ConsistsOf)) + validateComponent(child); + else + ; // TODO + } + + public void validateRootComposite(Resource rootComposite) throws DatabaseException { + checkUniqueNames(rootComposite); + validateComponent(rootComposite); + } + + public void validateComponentType(Resource componentType) throws DatabaseException { + Resource rootComposite = null; + try { + rootComposite = g.getPossibleObject(componentType, sr.IsDefinedBy); + } catch(ManyObjectsForFunctionalRelationException e) { + reporter.report("A user component " + NameUtils.getSafeName(g, componentType) + + " has multiple definitions.", componentType); + } + if(rootComposite != null) + validateRootComposite(rootComposite); + } + +}