]> gerrit.simantics Code Review - simantics/platform.git/blob
6662463c5b50fc0613e62bd342cc64f2571202b9
[simantics/platform.git] /
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.UnaryRead;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
15 import org.simantics.scl.db.SCLCompilationRequestProcessor;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class SCLRelationInfoRequest extends UnaryRead<Resource,SCLRelationInfo> {
20     private static final Logger LOGGER = LoggerFactory.getLogger(SCLRelationInfoRequest.class);
21     
22     private SCLRelationInfoRequest(Resource resource) {
23         super(resource);
24     }
25
26     @Override
27     public SCLRelationInfo perform(ReadGraph graph) throws DatabaseException {
28         Layer0 L0 = Layer0.getInstance(graph);
29         if(!graph.isSubrelationOf(parameter, L0.HasProperty))
30             return null;
31         
32         String name = graph.getPossibleRelatedValue(parameter, L0.HasName);
33         if(name == null)
34             return null;
35         
36         String valueType = graph.getPossibleRelatedValue(parameter, L0.RequiresValueType);
37         if(valueType == null) {
38             Collection<Resource> rangeTypes = graph.getObjects(parameter, L0.HasRange);
39             if(rangeTypes.size() != 1) {
40                 LOGGER.warn("Couldn't find SCLtype for {} because it has multiple range types.", graph.getURI(parameter));
41                 return null;
42             }
43             
44             Resource range = rangeTypes.iterator().next();
45             Collection<Resource> assertedValueTypes = graph.getAssertedObjects(range, L0.HasValueType);
46             if(assertedValueTypes.size() != 1) {
47                 LOGGER.warn("Couldn't find SCL type for {} because its range {} has multiple asserted value types.", graph.getURI(parameter), graph.getURI(range));
48                 return null;
49             }
50             
51             Resource assertedValueType = assertedValueTypes.iterator().next();
52             valueType = graph.getPossibleValue(assertedValueType, Bindings.STRING);
53             if(valueType == null) {
54                 LOGGER.warn("Couldn't find SCL type for {} because value type assertion of {} is missing a value.", graph.getURI(parameter), graph.getURI(range));
55                 return null;
56             }
57         }
58         
59         Type type;
60         try {
61             type = Types.parseType(valueType);
62         } catch (SCLTypeParseException e) {
63             LOGGER.warn("Couldn't parse the value type of relation {}. Definition was '{}'.", graph.getURI(parameter), valueType);
64             return null;
65         }
66         
67         return new SCLRelationInfo(type, name);
68     }
69     
70     public static SCLRelationInfo getRelationInfo(Resource resource) {
71         try {
72             return SCLCompilationRequestProcessor.getRequestProcessor().syncRequest(new SCLRelationInfoRequest(resource), TransientCacheListener.instance());
73         } catch(DatabaseException e) {
74             LOGGER.error("SCLRelationInfoRequest failed.", e);
75             return null;
76         }
77     }
78
79 }