1 package org.simantics.db.layer0.validation;
3 import java.util.ArrayList;
4 import java.util.Collections;
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;
20 import gnu.trove.set.hash.THashSet;
22 public class ValidationUtils {
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
32 * database read access handle
34 * resource to validate against ontology-defined constraints
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
41 public static Set<Issue> validateConstraints(ReadGraph graph, Resource r, Set<Issue> result) throws DatabaseException {
42 Layer0 L0 = Layer0.getInstance(graph);
44 for (Resource constraint : graph.syncRequest(new GetConstraints(r))) {
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()) {
51 result = new THashSet<Issue>();
52 result.addAll(issues);
54 } catch (Throwable t) {
55 Logger.defaultLogError(t);
59 return result != null ? result : Collections.<Issue>emptySet();
62 public static Set<Issue> validateConstraints(ReadGraph graph, Resource r) throws DatabaseException {
63 return validateConstraints(graph, r, null);
66 public static List<Issue> validateConstraintsForDomain(ReadGraph graph, Resource r) throws DatabaseException {
67 Set<Issue> result = null;
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())
79 return result != null ? new ArrayList(result) : Collections.<Issue>emptyList();