]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/validation/ValidationUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / validation / ValidationUtils.java
1 package org.simantics.db.layer0.validation;\r
2 \r
3 import gnu.trove.set.hash.THashSet;\r
4 \r
5 import java.util.Collections;\r
6 import java.util.List;\r
7 import java.util.Map;\r
8 import java.util.Set;\r
9 \r
10 import org.simantics.db.Issue;\r
11 import org.simantics.db.ReadGraph;\r
12 import org.simantics.db.Resource;\r
13 import org.simantics.db.common.utils.Functions;\r
14 import org.simantics.db.common.utils.Logger;\r
15 import org.simantics.db.exception.DatabaseException;\r
16 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;\r
17 import org.simantics.db.layer0.util.DomainProcessor3;\r
18 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
19 import org.simantics.layer0.Layer0;\r
20 \r
21 public class ValidationUtils {\r
22 \r
23     /**\r
24      * Performs L0.Constraint validations for the specified resource and puts\r
25      * the resulting issues in the specified {@link Issue} set. If the specified\r
26      * issue set is <code>null</code>, a new set will be created and returned.\r
27      * Otherwise the method function will return the same set as its return\r
28      * value.\r
29      * \r
30      * @param graph\r
31      *            database read access handle\r
32      * @param r\r
33      *            resource to validate against ontology-defined constraints\r
34      * @param result\r
35      *            a set for the resulting issues or <code>null</code> to\r
36      *            allocate a new set if necessary\r
37      * @return the discovered set of issues in <code>r</code>\r
38      * @throws DatabaseException\r
39      */\r
40     public static Set<Issue> validateConstraints(ReadGraph graph, Resource r, Set<Issue> result) throws DatabaseException {\r
41         Layer0 L0 = Layer0.getInstance(graph);\r
42 \r
43         for (Resource constraint : graph.syncRequest(new GetConstraints(r))) {\r
44             try {\r
45                 Resource function = graph.getSingleObject(constraint, L0.Constraint_Validator);\r
46                 @SuppressWarnings("unchecked")\r
47                 List<Issue> issues = (List<Issue>)Functions.exec(graph, function, graph, r);\r
48                 if (issues != null && !issues.isEmpty()) {\r
49                     if (result == null)\r
50                         result = new THashSet<Issue>();\r
51                     result.addAll(issues);\r
52                 }\r
53             } catch (Throwable t) {\r
54                 Logger.defaultLogError(t);\r
55             }\r
56         }\r
57 \r
58         return result != null ? result : Collections.<Issue>emptySet();\r
59     }\r
60 \r
61     public static Set<Issue> validateConstraints(ReadGraph graph, Resource r) throws DatabaseException {\r
62         return validateConstraints(graph, r, null);\r
63     }\r
64 \r
65     public static Set<Issue> validateConstraintsForDomain(ReadGraph graph, Resource r) throws DatabaseException {\r
66         Set<Issue> result = null;\r
67 \r
68         DomainProcessor3 dp = ModelTransferableGraphSourceRequest.getDomainOnly(graph, null, r);\r
69         for(Map.Entry<Resource,ExtentStatus> status : dp.getStatus().entrySet()) {\r
70             if(ExtentStatus.INTERNAL.equals(status.getValue())) {\r
71                 Resource rr = status.getKey();\r
72                 Set<Issue> issues = validateConstraints(graph, rr, result);\r
73                 if (!issues.isEmpty())\r
74                     result = issues;\r
75             }\r
76         }\r
77 \r
78         return result != null ? result : Collections.<Issue>emptySet();\r
79     }\r
80 \r
81 }\r