package org.simantics.modeling; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.TreeMap; import java.util.TreeSet; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.utils.CommonDBUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.graphfile.ontology.GraphFileResource; import org.simantics.layer0.Layer0; import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo; import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.structural2.variables.Connection; import org.simantics.structural2.variables.VariableConnectionPointDescriptor; public class ContentDumps { private static Charset UTF8 = StandardCharsets.UTF_8; public static byte[] sclModuleContentDump(ReadGraph graph, Resource resource) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); String def = graph.getRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING); return def.getBytes(UTF8); } public static byte[] pgraphContentDump(ReadGraph graph, Resource resource) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); String def = graph.getRelatedValue(resource, L0.PGraph_definition, Bindings.STRING); return def.getBytes(UTF8); } public static byte[] graphFileContentDump(ReadGraph graph, Resource resource) throws DatabaseException { GraphFileResource GF = GraphFileResource.getInstance(graph); return graph.getRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY); } public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException { StringBuilder dump = new StringBuilder(); Variable v = Variables.getVariable(graph, resource); TreeSet types = new TreeSet<>(); for(Resource t : graph.getPrincipalTypes(resource)) { types.add(graph.getURI(t)); } for(String uri : types) { dump.append(uri); dump.append("\n"); } TreeMap properties = new TreeMap<>(); for(Variable property : v.getProperties(graph)) properties.put(property.getName(graph), property); for(Variable property : properties.values()) { String possibleValue = property.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING); if(possibleValue != null) { dump.append(property.getName(graph)); dump.append(" "); dump.append(possibleValue); dump.append("\n"); } if(property.getClassifications(graph).contains(StructuralResource2.URIs.ConnectionRelation)) { dump.append(property.getName(graph)); Connection c = property.getValue(graph); TreeSet rvis = new TreeSet<>(); for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) { rvis.add(desc.getRelativeRVI(graph, v)); } for(String rvi : rvis) { dump.append(" "); dump.append(rvi); } dump.append("\n"); } } return dump.toString().getBytes(UTF8); } public static byte[] genericParameterTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); StructuralResource2 STR = StructuralResource2.getInstance(graph); Resource componentType = CommonDBUtils.possibleObjectWithType(graph, resource, L0.PartOf, STR.ComponentType); ComponentTypeViewerPropertyInfo pi = HeadlessComponentTypePropertiesResultRequest.readPropertyInfo(graph, resource, componentType, false); StringBuilder dump = new StringBuilder(); dump.append(pi.type); dump.append(" "); dump.append(pi.defaultValue); if(pi.unit != null) { dump.append(" "); dump.append(pi.unit); } dump.append(" "); dump.append(pi.label); if(pi.description != null) { dump.append(" "); dump.append(pi.description); } return dump.toString().getBytes(UTF8); } }