]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/ontologymodule/SCLRelationInfoRequest.java
Merge "Use proper environment to resolve SCL types in ontology module"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / ontologymodule / SCLRelationInfoRequest.java
1 package org.simantics.modeling.scl.ontologymodule;
2
3 import java.util.Collection;
4
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;
20
21 public class SCLRelationInfoRequest extends BinaryRead<Resource, Environment, SCLRelationInfo> {
22     private static final Logger LOGGER = LoggerFactory.getLogger(SCLRelationInfoRequest.class);
23     
24     private SCLRelationInfoRequest(Resource resource, Environment environment) {
25         super(resource, environment);
26     }
27
28     @Override
29     public SCLRelationInfo perform(ReadGraph graph) throws DatabaseException {
30         Layer0 L0 = Layer0.getInstance(graph);
31         if(!graph.isSubrelationOf(parameter, L0.HasProperty))
32             return null;
33         
34         String name = graph.getPossibleRelatedValue(parameter, L0.HasName);
35         if(name == null)
36             return null;
37         
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));
43                 return null;
44             }
45             
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));
50                 return null;
51             }
52             
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));
57                 return null;
58             }
59         }
60         
61         Type type;
62         try {
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);
66             return null;
67         }
68         
69         return new SCLRelationInfo(type, name);
70     }
71     
72     public static SCLRelationInfo getRelationInfo(Resource resource, Environment environment) {
73         try {
74             return SCLCompilationRequestProcessor.getRequestProcessor().syncRequest(new SCLRelationInfoRequest(resource, environment), TransientCacheListener.instance());
75         } catch(DatabaseException e) {
76             LOGGER.error("SCLRelationInfoRequest failed.", e);
77             return null;
78         }
79     }
80
81 }