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