package org.simantics.db.layer0.validation;
import gnu.trove.set.hash.THashSet;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.simantics.db.Issue;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.utils.Functions;
import org.simantics.db.common.utils.Logger;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
import org.simantics.db.layer0.util.DomainProcessor3;
import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
import org.simantics.layer0.Layer0;
public class ValidationUtils {
/**
* Performs L0.Constraint validations for the specified resource and puts
* the resulting issues in the specified {@link Issue} set. If the specified
* issue set is null
, a new set will be created and returned.
* Otherwise the method function will return the same set as its return
* value.
*
* @param graph
* database read access handle
* @param r
* resource to validate against ontology-defined constraints
* @param result
* a set for the resulting issues or null
to
* allocate a new set if necessary
* @return the discovered set of issues in r
* @throws DatabaseException
*/
public static Set validateConstraints(ReadGraph graph, Resource r, Set result) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(graph);
for (Resource constraint : graph.syncRequest(new GetConstraints(r))) {
try {
Resource function = graph.getSingleObject(constraint, L0.Constraint_Validator);
@SuppressWarnings("unchecked")
List issues = (List)Functions.exec(graph, function, graph, r);
if (issues != null && !issues.isEmpty()) {
if (result == null)
result = new THashSet();
result.addAll(issues);
}
} catch (Throwable t) {
Logger.defaultLogError(t);
}
}
return result != null ? result : Collections.emptySet();
}
public static Set validateConstraints(ReadGraph graph, Resource r) throws DatabaseException {
return validateConstraints(graph, r, null);
}
public static Set validateConstraintsForDomain(ReadGraph graph, Resource r) throws DatabaseException {
Set result = null;
DomainProcessor3 dp = ModelTransferableGraphSourceRequest.getDomainOnly(graph, null, r);
for(Map.Entry status : dp.getStatus().entrySet()) {
if(ExtentStatus.INTERNAL.equals(status.getValue())) {
Resource rr = status.getKey();
Set issues = validateConstraints(graph, rr, result);
if (!issues.isEmpty())
result = issues;
}
}
return result != null ? result : Collections.emptySet();
}
}