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.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;
19 public class SCLRelationInfoRequest extends UnaryRead<Resource,SCLRelationInfo> {
20 private static final Logger LOGGER = LoggerFactory.getLogger(SCLRelationInfoRequest.class);
22 private SCLRelationInfoRequest(Resource resource) {
27 public SCLRelationInfo perform(ReadGraph graph) throws DatabaseException {
28 Layer0 L0 = Layer0.getInstance(graph);
29 if(!graph.isSubrelationOf(parameter, L0.HasProperty))
32 String name = graph.getPossibleRelatedValue(parameter, L0.HasName);
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));
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));
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));
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);
67 return new SCLRelationInfo(type, name);
70 public static SCLRelationInfo getRelationInfo(Resource resource) {
72 return SCLCompilationRequestProcessor.getRequestProcessor().syncRequest(new SCLRelationInfoRequest(resource), TransientCacheListener.instance());
73 } catch(DatabaseException e) {
74 LOGGER.error("SCLRelationInfoRequest failed.", e);