1 package org.simantics.db.layer0.validation;
3 import gnu.trove.set.hash.THashSet;
5 import java.util.Collections;
10 import org.simantics.db.Issue;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.utils.Functions;
14 import org.simantics.db.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
17 import org.simantics.db.layer0.util.DomainProcessor3;
18 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
19 import org.simantics.layer0.Layer0;
21 public class ValidationUtils {
24 * Performs L0.Constraint validations for the specified resource and puts
25 * the resulting issues in the specified {@link Issue} set. If the specified
26 * issue set is <code>null</code>, a new set will be created and returned.
27 * Otherwise the method function will return the same set as its return
31 * database read access handle
33 * resource to validate against ontology-defined constraints
35 * a set for the resulting issues or <code>null</code> to
36 * allocate a new set if necessary
37 * @return the discovered set of issues in <code>r</code>
38 * @throws DatabaseException
40 public static Set<Issue> validateConstraints(ReadGraph graph, Resource r, Set<Issue> result) throws DatabaseException {
41 Layer0 L0 = Layer0.getInstance(graph);
43 for (Resource constraint : graph.syncRequest(new GetConstraints(r))) {
45 Resource function = graph.getSingleObject(constraint, L0.Constraint_Validator);
46 @SuppressWarnings("unchecked")
47 List<Issue> issues = (List<Issue>)Functions.exec(graph, function, graph, r);
48 if (issues != null && !issues.isEmpty()) {
50 result = new THashSet<Issue>();
51 result.addAll(issues);
53 } catch (Throwable t) {
54 Logger.defaultLogError(t);
58 return result != null ? result : Collections.<Issue>emptySet();
61 public static Set<Issue> validateConstraints(ReadGraph graph, Resource r) throws DatabaseException {
62 return validateConstraints(graph, r, null);
65 public static Set<Issue> validateConstraintsForDomain(ReadGraph graph, Resource r) throws DatabaseException {
66 Set<Issue> result = null;
68 DomainProcessor3 dp = ModelTransferableGraphSourceRequest.getDomainOnly(graph, null, r);
69 for(Map.Entry<Resource,ExtentStatus> status : dp.getStatus().entrySet()) {
70 if(ExtentStatus.INTERNAL.equals(status.getValue())) {
71 Resource rr = status.getKey();
72 Set<Issue> issues = validateConstraints(graph, rr, result);
73 if (!issues.isEmpty())
78 return result != null ? result : Collections.<Issue>emptySet();