1 package org.simantics.modeling.scl.ontologymodule;
3 import java.util.Collection;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.common.request.BinaryRead;
10 import org.simantics.db.common.request.UnaryRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.scl.compiler.environment.Environment;
14 import org.simantics.scl.compiler.environment.Environments;
15 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
16 import org.simantics.scl.compiler.types.Type;
17 import org.simantics.scl.db.SCLCompilationRequestProcessor;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class SCLRelationInfoRequest extends BinaryRead<Resource, Environment, SCLRelationInfo> {
22 private static final Logger LOGGER = LoggerFactory.getLogger(SCLRelationInfoRequest.class);
24 private SCLRelationInfoRequest(Resource resource, Environment environment) {
25 super(resource, environment);
29 public SCLRelationInfo perform(ReadGraph graph) throws DatabaseException {
30 Layer0 L0 = Layer0.getInstance(graph);
31 if(!graph.isSubrelationOf(parameter, L0.HasProperty))
34 String name = graph.getPossibleRelatedValue(parameter, L0.HasName);
38 String valueType = graph.getPossibleRelatedValue(parameter, L0.RequiresValueType);
39 if(valueType == null) {
40 Collection<Resource> rangeTypes = graph.getObjects(parameter, L0.HasRange);
41 if(rangeTypes.size() != 1) {
42 LOGGER.warn("Couldn't find SCLtype for {} because it has multiple range types.", graph.getURI(parameter));
46 Resource range = rangeTypes.iterator().next();
47 Collection<Resource> assertedValueTypes = graph.getAssertedObjects(range, L0.HasValueType);
48 if(assertedValueTypes.size() != 1) {
49 LOGGER.warn("Couldn't find SCL type for {} because its range {} has multiple asserted value types.", graph.getURI(parameter), graph.getURI(range));
53 Resource assertedValueType = assertedValueTypes.iterator().next();
54 valueType = graph.getPossibleValue(assertedValueType, Bindings.STRING);
55 if(valueType == null) {
56 LOGGER.warn("Couldn't find SCL type for {} because value type assertion of {} is missing a value.", graph.getURI(parameter), graph.getURI(range));
63 type = Environments.getType(parameter2, valueType);
64 } catch (SCLExpressionCompilationException e) {
65 LOGGER.warn("Couldn't parse the value type of relation {}. Definition was '{}'.", graph.getURI(parameter), valueType);
69 return new SCLRelationInfo(type, name);
72 public static SCLRelationInfo getRelationInfo(Resource resource, Environment environment) {
74 return SCLCompilationRequestProcessor.getRequestProcessor().syncRequest(new SCLRelationInfoRequest(resource, environment), TransientCacheListener.instance());
75 } catch(DatabaseException e) {
76 LOGGER.error("SCLRelationInfoRequest failed.", e);