]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
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
new file mode 100644 (file)
index 0000000..4fd123e
--- /dev/null
@@ -0,0 +1,102 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.structural2.validate;\r
+\r
+import gnu.trove.set.hash.THashSet;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+public class ValidateStructural {\r
+\r
+       ReadGraph g;\r
+       Layer0 b;\r
+       StructuralResource2 sr;\r
+       boolean validateTypesRecursively;\r
+       THashSet<Resource> validatedTypes = new THashSet<Resource>();\r
+       \r
+       IErrorReporter reporter = new StdoutReporter();\r
+       \r
+       public ValidateStructural(ReadGraph g, boolean validateTypesRecursively) {\r
+               this.g = g;\r
+               this.b = Layer0.getInstance(g);\r
+               this.sr = StructuralResource2.getInstance(g);\r
+               this.validateTypesRecursively = validateTypesRecursively;\r
+       }\r
+       \r
+       public void setReporter(IErrorReporter reporter) {\r
+               this.reporter = reporter;\r
+       }\r
+       \r
+       /**\r
+        * Checks that all components reachable from rootComposite recursively by ConsistsOf\r
+        * have unique names.\r
+        * @param rootComposite\r
+        * @throws DatabaseException\r
+        */\r
+       private void checkUniqueNames(Resource rootComposite) throws DatabaseException {\r
+               THashSet<String> names = new THashSet<String>();\r
+               checkUniqueNames(rootComposite, names);\r
+       }\r
+\r
+       private void checkUniqueNames(Resource composite, THashSet<String> names) throws DatabaseException {\r
+               for(Resource component : g.getObjects(composite, b.ConsistsOf)) {\r
+                       String name = null;\r
+                       try {\r
+                               name = g.getRelatedValue(component, b.HasName);\r
+                       } catch(DatabaseException e) {\r
+                               reporter.report("Component does not have a name or there are two names.", component);\r
+                       }\r
+                       if(name != null)\r
+                               if(!names.add(name))\r
+                                       reporter.report("Name '" + name + "' is not unique.", component);\r
+                       if(g.isInstanceOf(component, sr.Composite))\r
+                               checkUniqueNames(composite, names);\r
+               }\r
+       }\r
+       \r
+       public void validateComponent(Resource component) throws DatabaseException {\r
+               if(!g.isInstanceOf(component, sr.Component)) {\r
+                       reporter.report(NameUtils.getSafeName(g, component) +\r
+                                       " is not a component.", component);\r
+                       return;\r
+               }\r
+               if(g.isInstanceOf(component, sr.Composite))\r
+                       for(Resource child : g.getObjects(component, b.ConsistsOf))\r
+                               validateComponent(child);\r
+               else\r
+                       ; // TODO\r
+       }\r
+       \r
+       public void validateRootComposite(Resource rootComposite) throws DatabaseException {\r
+               checkUniqueNames(rootComposite);\r
+               validateComponent(rootComposite);\r
+       }\r
+       \r
+       public void validateComponentType(Resource componentType) throws DatabaseException {\r
+               Resource rootComposite = null;\r
+               try {\r
+                       rootComposite = g.getPossibleObject(componentType, sr.IsDefinedBy);\r
+               } catch(ManyObjectsForFunctionalRelationException e) {\r
+                       reporter.report("A user component " + NameUtils.getSafeName(g, componentType) \r
+                                       + " has multiple definitions.", componentType);\r
+               }\r
+               if(rootComposite != null)\r
+                       validateRootComposite(rootComposite);   \r
+       }\r
+       \r
+}\r