package org.simantics.modeling.scl.ontologymodule; import java.util.Collection; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.UnaryRead; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException; import org.simantics.scl.db.SCLCompilationRequestProcessor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SCLRelationInfoRequest extends UnaryRead { private static final Logger LOGGER = LoggerFactory.getLogger(SCLRelationInfoRequest.class); private SCLRelationInfoRequest(Resource resource) { super(resource); } @Override public SCLRelationInfo perform(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); if(!graph.isSubrelationOf(parameter, L0.HasProperty)) return null; String name = graph.getPossibleRelatedValue(parameter, L0.HasName); if(name == null) return null; String valueType = graph.getPossibleRelatedValue(parameter, L0.RequiresValueType); if(valueType == null) { Collection rangeTypes = graph.getObjects(parameter, L0.HasRange); if(rangeTypes.size() != 1) { LOGGER.warn("Couldn't find SCLtype for {} because it has multiple range types.", graph.getURI(parameter)); return null; } Resource range = rangeTypes.iterator().next(); Collection assertedValueTypes = graph.getAssertedObjects(range, L0.HasValueType); if(assertedValueTypes.size() != 1) { LOGGER.warn("Couldn't find SCL type for {} because its range {} has multiple asserted value types.", graph.getURI(parameter), graph.getURI(range)); return null; } Resource assertedValueType = assertedValueTypes.iterator().next(); valueType = graph.getPossibleValue(assertedValueType, Bindings.STRING); if(valueType == null) { LOGGER.warn("Couldn't find SCL type for {} because value type assertion of {} is missing a value.", graph.getURI(parameter), graph.getURI(range)); return null; } } Type type; try { type = Types.parseType(valueType); } catch (SCLTypeParseException e) { LOGGER.warn("Couldn't parse the value type of relation {}. Definition was '{}'.", graph.getURI(parameter), valueType); return null; } return new SCLRelationInfo(type, name); } public static SCLRelationInfo getRelationInfo(Resource resource) { try { return SCLCompilationRequestProcessor.getRequestProcessor().syncRequest(new SCLRelationInfoRequest(resource), TransientCacheListener.instance()); } catch(DatabaseException e) { LOGGER.error("SCLRelationInfoRequest failed.", e); return null; } } }