import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.request.ResourceRead;
-import org.simantics.db.common.utils.NameUtils;
-import org.simantics.db.exception.AdaptionException;
+import org.simantics.db.common.utils.CommonDBUtils;
import org.simantics.db.exception.DatabaseException;
import org.simantics.layer0.Layer0;
return enumerate(graph, resource);
}
+ @Deprecated
public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
- Layer0 L0 = Layer0.getInstance(graph);
- String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
- if(label != null)
- return label;
- return safeName(graph, resource);
+ return CommonDBUtils.getEnumerationValueName(graph, resource);
}
-
+
public static EnumerationValue<Resource> enumerate(ReadGraph graph, Resource resource) throws DatabaseException {
Layer0 l0 = Layer0.getInstance(graph);
Set<Resource> types = graph.getTypes(resource);
Collection<Resource> values = graph.getObjects(type, l0.ConsistsOf);
List<EnumeratedValue<Resource>> result = new ArrayList<EnumeratedValue<Resource>>(values.size());
for (Resource value : values) {
- result.add(new EnumeratedValue<Resource>(getEnumerationValueName(graph, value), value));
+ result.add(new EnumeratedValue<Resource>(CommonDBUtils.getEnumerationValueName(graph, value), value));
}
Enumeration<Resource> enumeration = new Enumeration<Resource>(result);
return new EnumerationValue<Resource>(enumeration, enumeration.find(resource));
return null;
}
- private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
- return graph.syncRequest(new NameRequest(value));
- }
-
- public static class NameRequest extends ResourceRead<String> {
- public NameRequest(Resource resource) {
- super(resource);
- }
- @Override
- public String perform(ReadGraph graph) throws DatabaseException {
- try {
- return graph.adapt(resource, String.class);
- } catch (AdaptionException e) {
- return NameUtils.getSafeName(graph, resource);
- }
- }
- }
-
}
import org.simantics.db.common.request.PossibleChild;
import org.simantics.db.common.request.PossibleObjectWithType;
import org.simantics.db.common.request.PossibleOwner;
+import org.simantics.db.common.request.ResourceRead;
import org.simantics.db.common.request.RuntimeEnvironmentRequest;
+import org.simantics.db.exception.AdaptionException;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.InvalidResourceReferenceException;
import org.simantics.db.service.ClusterUID;
if(!graph.isInstanceOf(child, type)) return null;
return child;
}
-
+
+ public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
+ if(label != null)
+ return label;
+ return safeName(graph, resource);
+ }
+
+ private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
+ return graph.syncRequest(new StringAdapterRequest(value));
+ }
+
+ public static class StringAdapterRequest extends ResourceRead<String> {
+ public StringAdapterRequest(Resource resource) {
+ super(resource);
+ }
+ @Override
+ public String perform(ReadGraph graph) throws DatabaseException {
+ try {
+ return graph.adapt(resource, String.class);
+ } catch (AdaptionException e) {
+ return NameUtils.getSafeName(graph, resource);
+ }
+ }
+ }
+
}
--- /dev/null
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import java.util.Collection;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
+import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest;
+
+public class ComponentTypePropertiesResultRequest extends HeadlessComponentTypePropertiesResultRequest {
+
+ private final Collection<ComponentTypeViewerSection> sections;
+
+ /**
+ * @param componentTypeViewer
+ */
+ public ComponentTypePropertiesResultRequest(Resource componentType, Collection<ComponentTypeViewerSection> sections) {
+ super(componentType);
+ this.sections = sections;
+ }
+
+ @Override
+ protected void readSectionSpecificData(ReadGraph graph, ComponentTypeViewerPropertyInfo info) throws DatabaseException {
+
+ Object sectionSpecificData = null;
+ double priority = Double.NEGATIVE_INFINITY;
+ if(sections != null) {
+ for(ComponentTypeViewerSection section : sections) {
+ Object temp = section.getSectionSpecificData(graph, info);
+ if(temp != null) {
+ double sectionPriority = section.getDataPriority();
+ if(sectionPriority > priority) {
+ sectionSpecificData = temp;
+ priority = sectionPriority;
+ }
+ }
+ }
+ }
+
+ info.sectionSpecificData = sectionSpecificData;
+
+ }
+
+}
\ No newline at end of file
package org.simantics.modeling.ui.componentTypeEditor;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.resource.JFaceResources;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.simantics.Simantics;
-import org.simantics.browsing.ui.graph.impl.GetEnumerationValue;
import org.simantics.databoard.Bindings;
import org.simantics.databoard.binding.Binding;
-import org.simantics.databoard.binding.error.BindingException;
import org.simantics.databoard.type.Datatype;
-import org.simantics.databoard.type.NumberType;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.NamedResource;
-import org.simantics.db.common.request.IsEnumeratedValue;
-import org.simantics.db.common.request.UniqueRead;
-import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.db.procedure.Listener;
import org.simantics.db.request.Read;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.ui.Activator;
-import org.simantics.operation.Layer0X;
-import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
import org.simantics.utils.ui.ErrorLogger;
import org.simantics.utils.ui.SWTUtils;
}
private void createGraphListener() {
- Simantics.getSession().asyncRequest(new UniqueRead<ComponentTypePropertiesResult>() {
- @Override
- public ComponentTypePropertiesResult perform(ReadGraph graph) throws DatabaseException {
- List<ComponentTypeViewerPropertyInfo> result = new ArrayList<>();
- List<NamedResource> connectionPoints = new ArrayList<>();
- Layer0 L0 = Layer0.getInstance(graph);
- Layer0X L0X = Layer0X.getInstance(graph);
- StructuralResource2 STR = StructuralResource2.getInstance(graph);
-
- boolean typeIsImmutable = graph.isImmutable(data.componentType)
- || graph.hasStatement(data.componentType, STR.ComponentType_Locked)
- || Layer0Utils.isPublished(graph, data.componentType)
- || Layer0Utils.isContainerPublished(graph, data.componentType);
-
- for(Resource relation : graph.getObjects(data.componentType, L0.DomainOf)) {
- if(graph.isSubrelationOf(relation, L0.HasProperty)) {
- String name = graph.getRelatedValue(relation, L0.HasName);
- String type = graph.getPossibleRelatedValue(relation, L0.RequiresValueType);
- String label = graph.getPossibleRelatedValue(relation, L0.HasLabel);
- if (label == null)
- label = ""; //$NON-NLS-1$
- String description = graph.getPossibleRelatedValue(relation, L0.HasDescription);
- if (description == null)
- description = ""; //$NON-NLS-1$
- NumberType numberType = null;
- if(type == null)
- type = "Double"; //$NON-NLS-1$
- String unit = graph.getPossibleRelatedValue(relation, L0X.HasUnit, Bindings.STRING);
- String defaultValue = "0"; //$NON-NLS-1$
- String expression = null;
-
- for(Resource assertion : graph.getAssertedObjects(data.componentType, relation)) {
- try {
- expression = graph.getPossibleRelatedValue(assertion, L0.SCLValue_expression, Bindings.STRING);
- if(expression != null) {
- defaultValue = "=" + expression; //$NON-NLS-1$
- } else if (graph.sync(new IsEnumeratedValue(assertion))) {
- defaultValue = GetEnumerationValue.getEnumerationValueName(graph, assertion);
- } else {
- Datatype dt = getPossibleDatatype(graph, assertion);
- if (dt == null)
- continue;
- if (dt instanceof NumberType)
- numberType = (NumberType) dt;
- Binding binding = Bindings.getBinding(dt);
- Object value = graph.getValue(assertion, binding);
- try {
- defaultValue = binding.toString(value, true);
- } catch (BindingException e) {
- ErrorLogger.defaultLogError(e);
- }
- }
- } catch(DatabaseException e) {
- ErrorLogger.defaultLogError(e);
- }
- }
-
- String valid = expression != null ? DerivedPropertiesSection.validateMonitorExpression(graph, data.componentType, relation, expression) : null;
-
- boolean immutable = typeIsImmutable || graph.isImmutable(relation);
- ComponentTypeViewerPropertyInfo info =
- new ComponentTypeViewerPropertyInfo(relation, name, type, defaultValue, numberType, unit, label, description, expression, valid, immutable);
-
- Object sectionSpecificData = null;
- double priority = Double.NEGATIVE_INFINITY;
- for(ComponentTypeViewerSection section : sections) {
- Object temp = section.getSectionSpecificData(graph, info);
- if(temp != null) {
- double sectionPriority = section.getDataPriority();
- if(sectionPriority > priority) {
- sectionSpecificData = temp;
- priority = sectionPriority;
- }
- }
- }
- info.sectionSpecificData = sectionSpecificData;
-
- result.add(info);
-
- } else if (graph.isInstanceOf(relation, STR.ConnectionRelation)) {
- NamedResource nr = new NamedResource(NameUtils.getSafeName(graph, relation), relation);
- connectionPoints.add(nr);
- }
- }
- Collections.sort(result);
- return new ComponentTypePropertiesResult(result, connectionPoints, typeIsImmutable);
- }
- }, new Listener<ComponentTypePropertiesResult>() {
+ Simantics.getSession().asyncRequest(new ComponentTypePropertiesResultRequest(this.data.componentType, this.sections), new Listener<ComponentTypePropertiesResult>() {
@Override
public void execute(final ComponentTypePropertiesResult result) {
SWTUtils.asyncExec(data.form.getDisplay(), new Runnable() {
});
}
- protected Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
- Binding binding = Bindings.getBindingUnchecked(Datatype.class);
- for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {
- Datatype dt = graph.getPossibleValue(dataTypeResource, binding);
- if (dt != null)
- return dt;
- }
- return null;
- }
-
public void setFocus() {
data.form.setFocus();
}
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.function.DbConsumer;
import org.simantics.db.layer0.QueryIndexUtils;
-import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.userComponent.ComponentTypeCommands;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
import org.simantics.scl.runtime.function.Function2;
import org.simantics.scl.runtime.function.Function4;
import org.simantics.utils.datastructures.Pair;
import org.eclipse.ui.forms.widgets.Section;
import org.simantics.db.ReadGraph;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
public interface ComponentTypeViewerSection {
import org.simantics.modeling.ui.Activator;
import org.simantics.modeling.ui.componentTypeEditor.LiftPropertiesDialog.LiftedProperty;
import org.simantics.modeling.userComponent.ComponentTypeCommands;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
import org.simantics.selectionview.SelectionViewResources;
import org.simantics.structural.stubs.StructuralResource2;
import org.simantics.utils.datastructures.Pair;
import org.simantics.db.layer0.variable.Variable;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.scl.CompileProceduralSCLMonitorRequest;
-import org.simantics.modeling.scl.CompileSCLMonitorRequest;
import org.simantics.modeling.userComponent.ComponentTypeCommands;
-import org.simantics.scl.runtime.SCLContext;
-import org.simantics.scl.runtime.function.Function1;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
+import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest;
import org.simantics.scl.runtime.function.Function4;
import org.simantics.structural.stubs.StructuralResource2;
import org.slf4j.Logger;
if(graph.isInstanceOf(componentType, STR.ProceduralComponentType)) {
CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context);
} else {
- compileAndEvaluate(graph, context, expression);
+ HeadlessComponentTypePropertiesResultRequest.compileAndEvaluate(graph, context, expression);
}
} catch (Exception e) {
return null;
}
- public static void compileAndEvaluate(ReadGraph graph, Variable context, String expression) throws DatabaseException {
- SCLContext sclContext = SCLContext.getCurrent();
- Object oldGraph = sclContext.get("graph");
- try {
- CompileSCLMonitorRequest compileSCLMonitorRequest = new ValidationCompilationRequest(graph, context, expression);
- Function1<Variable,Object> exp = graph.syncRequest(compileSCLMonitorRequest);
- sclContext.put("graph", graph);
- //return exp.apply(context.getParent(graph));
- } catch (DatabaseException e) {
- throw (DatabaseException)e;
- } catch (Throwable t) {
- throw new DatabaseException(t);
- } finally {
- sclContext.put("graph", oldGraph);
- }
- }
-
@Override
public void update(ComponentTypePropertiesResult result) {
if (table.isDisposed())
return 100.0;
}
- private static final class ValidationCompilationRequest extends CompileSCLMonitorRequest {
- private final String expression;
-
- private ValidationCompilationRequest(ReadGraph graph, Variable context, String expression)
- throws DatabaseException {
- super(graph, context);
- this.expression = expression;
- }
-
- @Override
- protected String getExpressionText(ReadGraph graph) throws DatabaseException {
- return expression;
- }
-
- @Override
- public int hashCode() {
- return super.hashCode() + 37 * expression.hashCode();
- }
-
- @Override
- public boolean equals(Object obj) {
- return super.equals(obj) && ((ValidationCompilationRequest)obj).expression.equals(expression);
- }
- }
-
}
\ No newline at end of file
import org.simantics.Simantics;
import org.simantics.databoard.binding.error.BindingException;
import org.simantics.databoard.serialization.SerializationException;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.UniqueRead;
import org.simantics.db.common.utils.Logger;
import org.simantics.db.exception.DatabaseException;
import org.simantics.graph.refactoring.FixExportedOntology;
import org.simantics.modeling.ModelingUtils;
import org.simantics.modeling.ModelingUtils.LibraryInfo;
+import org.simantics.modeling.utils.DumpOntologyStructure;
import org.simantics.utils.ui.dialogs.ShowMessage;
import org.slf4j.LoggerFactory;
try {
Path input = Paths.get(location.toURI());
FixExportedOntology.createTGAndPGraph(input);
+ DumpOntologyStructure data = Simantics.sync(new UniqueRead<DumpOntologyStructure>() {
+
+ @Override
+ public DumpOntologyStructure perform(ReadGraph graph) throws DatabaseException {
+ DumpOntologyStructure result = new DumpOntologyStructure();
+ result.read(graph, info.library.getResource());
+ return result;
+ }
+
+ });
+ data.write(new File(new File(location.getParent(), location.getName() + ".dump"), info.library.getName()));
} catch (Exception e) {
LOGGER.error("Could not generate TG and Pgraph", e);
}
-package org.simantics.modeling.ui.componentTypeEditor;
+package org.simantics.modeling.utils;
import java.util.List;
-package org.simantics.modeling.ui.componentTypeEditor;
+package org.simantics.modeling.utils;
import org.simantics.databoard.annotations.Optional;
import org.simantics.databoard.type.NumberType;
--- /dev/null
+package org.simantics.modeling.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.utils.CommonDBUtils;
+import org.simantics.db.common.utils.NameUtils;
+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.structural.stubs.StructuralResource2;
+import org.simantics.structural2.variables.Connection;
+import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
+import org.simantics.utils.FileUtils;
+
+public class DumpOntologyStructure {
+
+ private Resource ontology;
+
+ private Set<Resource> containers = new HashSet<>();
+ private Set<Resource> folders = new HashSet<>();
+ private Map<Resource, String> names = new HashMap<>();
+ private Map<Resource,Resource> parents = new HashMap<>();
+ private Map<Resource, File> libraryFolders = new HashMap<>();
+ private Set<Resource> modules = new HashSet<>();
+ private Map<Resource, String> moduleCodes = new HashMap<>();
+ private Set<Resource> pgraphs = new HashSet<>();
+ private Map<Resource, String> pgraphCodes = new HashMap<>();
+ private Set<Resource> graphFiles = new HashSet<>();
+ private Map<Resource, byte[]> graphFileBytes = new HashMap<>();
+ private Set<Resource> componentTypes = new HashSet<>();
+ private Map<Resource, String> componentTypeDumps = new HashMap<>();
+ private Set<Resource> components = new HashSet<>();
+ private Map<Resource, String> componentDumps = new HashMap<>();
+
+ private void readNameAndParent(ReadGraph graph, Resource container, Resource r) throws DatabaseException {
+ parents.put(r, container);
+ names.put(r, NameUtils.getSafeName(graph, r));
+ }
+
+ private void readLibraries(ReadGraph graph, Resource container) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.Library)) {
+ folders.add(r);
+ names.put(r, NameUtils.getSafeName(graph, r));
+ parents.put(r, container);
+ readNameAndParent(graph, container, r);
+ readLibraries(graph, r);
+ }
+ containers.addAll(folders);
+ }
+
+ private void readComponentTypes(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ for(Resource container : containers) {
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.ComponentType)) {
+ folders.add(r);
+ componentTypes.add(r);
+ readNameAndParent(graph, container, r);
+ ComponentTypePropertiesResult data = graph.syncRequest(new HeadlessComponentTypePropertiesResultRequest(r));
+ StringBuilder dump = new StringBuilder();
+ for(ComponentTypeViewerPropertyInfo pi : data.getProperties()) {
+ dump.append(pi.name);
+ dump.append(" ");
+ 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);
+ }
+ dump.append("\n");
+ }
+ for(NamedResource nr : data.getConnectionPoints()) {
+ dump.append("cp ");
+ dump.append(nr.getName());
+ dump.append("\n");
+ }
+ componentTypeDumps.put(r, dump.toString());
+ }
+ }
+ containers.addAll(folders);
+ }
+
+ private void readComposites(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ for(Resource container : containers) {
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.Composite)) {
+ folders.add(r);
+ readNameAndParent(graph, container, r);
+ }
+ }
+ containers.addAll(folders);
+ }
+
+ private void readComponents(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ for(Resource container : containers) {
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.Component)) {
+ if(folders.contains(r))
+ continue;
+ components.add(r);
+ readNameAndParent(graph, container, r);
+ Variable v = Variables.getVariable(graph, r);
+ TreeMap<String,Variable> properties = new TreeMap<>();
+ for(Variable property : v.getProperties(graph))
+ properties.put(property.getName(graph), property);
+ StringBuilder dump = new StringBuilder();
+ 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);
+ for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) {
+ dump.append(" ");
+ dump.append(desc.getRelativeRVI(graph, v));
+ }
+ dump.append("\n");
+ }
+ }
+ componentDumps.put(r, dump.toString());
+ }
+ }
+ }
+
+ private void readSCLModules(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ for(Resource container : containers) {
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.SCLModule)) {
+ String code = graph.getPossibleRelatedValue(r, L0.SCLModule_definition, Bindings.STRING);
+ if(code != null) {
+ moduleCodes.put(r, code);
+ }
+ modules.add(r);
+ readNameAndParent(graph, container, r);
+ }
+ }
+ }
+
+ private void readPGraphs(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ for(Resource container : containers) {
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.PGraph)) {
+ String code = graph.getPossibleRelatedValue(r, L0.PGraph_definition, Bindings.STRING);
+ if(code != null) {
+ pgraphCodes.put(r, code);
+ }
+ pgraphs.add(r);
+ readNameAndParent(graph, container, r);
+ }
+ }
+ }
+
+ private void readGraphFiles(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ GraphFileResource GF = GraphFileResource.getInstance(graph);
+ for(Resource container : containers) {
+ for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, GF.File)) {
+ byte[] bytes = graph.getPossibleRelatedValue(r, GF.HasFiledata, Bindings.BYTE_ARRAY);
+ if(bytes != null) {
+ graphFileBytes.put(r, bytes);
+ }
+ graphFiles.add(r);
+ readNameAndParent(graph, container, r);
+ }
+ }
+ }
+
+ public void read(ReadGraph graph, Resource ontology) throws DatabaseException {
+ this.ontology = ontology;
+ containers.add(ontology);
+ readLibraries(graph, ontology);
+ readComponentTypes(graph);
+ readComposites(graph);
+ readComponents(graph);
+ readSCLModules(graph);
+ readPGraphs(graph);
+ readGraphFiles(graph);
+ }
+
+ private File escapeFile(File file) {
+
+ if(file.exists())
+ return file;
+
+ return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName()));
+
+ }
+
+ public void write(File unsafeFolder) throws IOException {
+ File folder = escapeFile(unsafeFolder);
+ if(folder.exists())
+ FileUtils.deleteAll(folder);
+ folder.mkdirs();
+ writeLibraries(folder);
+ writeSCLModules(folder);
+ writePGraphs(folder);
+ writeGraphFiles(folder);
+ writeComponentTypes(folder);
+ writeComponents(folder);
+ }
+
+ private File getFolder(File root, Resource library) {
+ if(ontology.equals(library))
+ return root;
+ Resource parent = parents.get(library);
+ File parentFolder = getFolder(root, parent);
+ return new File(parentFolder, FileUtils.escapeFileName(names.get(library)));
+ }
+
+ private File getParentFolder(File root, Resource r) {
+ Resource parent = parents.get(r);
+ return getFolder(root, parent);
+ }
+
+ private File getFile(File root, Resource r) {
+ return new File(getParentFolder(root, r), FileUtils.escapeFileName(names.get(r)));
+ }
+
+ private void writeLibraries(File rootFolder) {
+ for(Resource library : folders) {
+ File folder = getFolder(rootFolder, library);
+ folder.mkdirs();
+ libraryFolders.put(library, folder);
+ }
+ }
+
+ private void writeSCLModules(File rootFolder) throws IOException {
+ for(Resource r : modules) {
+ FileUtils.writeFile(getFile(rootFolder, r), moduleCodes.get(r).getBytes());
+ }
+ }
+
+ private void writePGraphs(File rootFolder) throws IOException {
+ for(Resource r : pgraphs) {
+ FileUtils.writeFile(getFile(rootFolder, r), pgraphCodes.get(r).getBytes());
+ }
+ }
+
+ private void writeGraphFiles(File rootFolder) throws IOException {
+ for(Resource r : graphFiles) {
+ FileUtils.writeFile(getFile(rootFolder, r), graphFileBytes.get(r));
+ }
+ }
+
+ private void writeComponentTypes(File rootFolder) throws IOException {
+ for(Resource r : componentTypes) {
+ File folder = getFolder(rootFolder, r);
+ File file = new File(folder, "__interface__");
+ FileUtils.writeFile(file, componentTypeDumps.get(r).getBytes());
+ }
+ }
+
+ private void writeComponents(File rootFolder) throws IOException {
+ for(Resource r : components) {
+ FileUtils.writeFile(getFile(rootFolder, r), componentDumps.get(r).getBytes());
+ }
+ }
+
+}
--- /dev/null
+package org.simantics.modeling.utils;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.NumberType;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.IsEnumeratedValue;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.CommonDBUtils;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PropertyInfo;
+import org.simantics.db.layer0.request.PropertyInfoRequest;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;
+import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.scl.CompileProceduralSCLMonitorRequest;
+import org.simantics.modeling.scl.CompileSCLMonitorRequest;
+import org.simantics.operation.Layer0X;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.utils.ui.ErrorLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HeadlessComponentTypePropertiesResultRequest extends UniqueRead<ComponentTypePropertiesResult> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(HeadlessComponentTypePropertiesResultRequest.class);
+
+ protected final Resource componentType;
+
+ /**
+ * @param componentTypeViewer
+ */
+ public HeadlessComponentTypePropertiesResultRequest(Resource componentType) {
+ this.componentType = componentType;
+ }
+
+ protected void readSectionSpecificData(ReadGraph graph, ComponentTypeViewerPropertyInfo info) throws DatabaseException {
+ }
+
+ @Override
+ public ComponentTypePropertiesResult perform(ReadGraph graph) throws DatabaseException {
+ List<ComponentTypeViewerPropertyInfo> result = new ArrayList<>();
+ List<NamedResource> connectionPoints = new ArrayList<>();
+ Layer0 L0 = Layer0.getInstance(graph);
+ Layer0X L0X = Layer0X.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+ boolean typeIsImmutable = graph.isImmutable(componentType)
+ || graph.hasStatement(componentType, STR.ComponentType_Locked)
+ || Layer0Utils.isPublished(graph, componentType)
+ || Layer0Utils.isContainerPublished(graph, componentType);
+
+ for(Resource relation : graph.getObjects(componentType, L0.DomainOf)) {
+ if(graph.isSubrelationOf(relation, L0.HasProperty)) {
+ String name = graph.getRelatedValue(relation, L0.HasName);
+ String type = graph.getPossibleRelatedValue(relation, L0.RequiresValueType);
+ String label = graph.getPossibleRelatedValue(relation, L0.HasLabel);
+ if (label == null)
+ label = ""; //$NON-NLS-1$
+ String description = graph.getPossibleRelatedValue(relation, L0.HasDescription);
+ if (description == null)
+ description = ""; //$NON-NLS-1$
+ NumberType numberType = null;
+ if(type == null)
+ type = "Double"; //$NON-NLS-1$
+ String unit = graph.getPossibleRelatedValue(relation, L0X.HasUnit, Bindings.STRING);
+ String defaultValue = "0"; //$NON-NLS-1$
+ String expression = null;
+
+ for(Resource assertion : graph.getAssertedObjects(componentType, relation)) {
+ try {
+ expression = graph.getPossibleRelatedValue(assertion, L0.SCLValue_expression, Bindings.STRING);
+ if(expression != null) {
+ defaultValue = "=" + expression; //$NON-NLS-1$
+ } else if (graph.sync(new IsEnumeratedValue(assertion))) {
+ defaultValue = CommonDBUtils.getEnumerationValueName(graph, assertion);
+ } else {
+ Datatype dt = getPossibleDatatype(graph, assertion);
+ if (dt == null)
+ continue;
+ if (dt instanceof NumberType)
+ numberType = (NumberType) dt;
+ Binding binding = Bindings.getBinding(dt);
+ Object value = graph.getValue(assertion, binding);
+ try {
+ defaultValue = binding.toString(value, true);
+ } catch (BindingException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+ }
+ } catch(DatabaseException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+ }
+
+ String valid = expression != null ? validateMonitorExpression(graph, componentType, relation, expression) : null;
+
+ boolean immutable = typeIsImmutable || graph.isImmutable(relation);
+ ComponentTypeViewerPropertyInfo info =
+ new ComponentTypeViewerPropertyInfo(relation, name, type, defaultValue, numberType, unit, label, description, expression, valid, immutable);
+
+ readSectionSpecificData(graph, info);
+
+ result.add(info);
+
+ } else if (graph.isInstanceOf(relation, STR.ConnectionRelation)) {
+ NamedResource nr = new NamedResource(NameUtils.getSafeName(graph, relation), relation);
+ connectionPoints.add(nr);
+ }
+ }
+ Collections.sort(result);
+ return new ComponentTypePropertiesResult(result, connectionPoints, typeIsImmutable);
+ }
+
+ private Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
+ Binding binding = Bindings.getBindingUnchecked(Datatype.class);
+ for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {
+ Datatype dt = graph.getPossibleValue(dataTypeResource, binding);
+ if (dt != null)
+ return dt;
+ }
+ return null;
+ }
+
+ public static String validateMonitorExpression(final RequestProcessor processor, final Resource componentType, final Resource relation, final String expression) {
+
+ try {
+ return processor.sync(new UniqueRead<String>() {
+
+ @Override
+ public String perform(ReadGraph graph) throws DatabaseException {
+
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+ // TODO: this is a bit hackish but should get the job done in most parts and
+ // importantly indicates something for the user
+ PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(relation), TransientCacheAsyncListener.<PropertyInfo>instance());
+ Variable parent = new StandardGraphChildVariable(null, null, componentType) {
+
+ public Resource getType(ReadGraph graph) throws DatabaseException {
+ return componentType;
+ };
+
+ public Variable getPossibleProperty(ReadGraph graph, String name) throws DatabaseException {
+ Variable prop = super.getPossibleProperty(graph, name);
+ if (prop != null) {
+ return prop;
+ } else {
+ return getChild(graph, name);
+ }
+ };
+ };
+
+ for(Resource literal : graph.getAssertedObjects(componentType, relation)) {
+
+ try {
+ Variable context = new StandardGraphPropertyVariable(parent, null, null, info, literal);
+ if(graph.isInstanceOf(componentType, STR.ProceduralComponentType)) {
+ CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context);
+ } else {
+ compileAndEvaluate(graph, context, expression);
+ }
+
+ } catch (Exception e) {
+ String msg = e.getMessage();
+ int index = msg.indexOf(":"); //$NON-NLS-1$
+ if(index > 0) msg = msg.substring(index);
+ return msg;
+ }
+ }
+ return null;
+ }
+
+ });
+ } catch (DatabaseException e) {
+ LOGGER.error("Could not validate ", e);
+ }
+ return null;
+ }
+
+ public static void compileAndEvaluate(ReadGraph graph, Variable context, String expression) throws DatabaseException {
+ SCLContext sclContext = SCLContext.getCurrent();
+ Object oldGraph = sclContext.get("graph");
+ try {
+ CompileSCLMonitorRequest compileSCLMonitorRequest = new ValidationCompilationRequest(graph, context, expression);
+ Function1<Variable,Object> exp = graph.syncRequest(compileSCLMonitorRequest);
+ sclContext.put("graph", graph);
+ //return exp.apply(context.getParent(graph));
+ } catch (DatabaseException e) {
+ throw (DatabaseException)e;
+ } catch (Throwable t) {
+ throw new DatabaseException(t);
+ } finally {
+ sclContext.put("graph", oldGraph);
+ }
+ }
+
+ private static final class ValidationCompilationRequest extends CompileSCLMonitorRequest {
+ private final String expression;
+
+ private ValidationCompilationRequest(ReadGraph graph, Variable context, String expression)
+ throws DatabaseException {
+ super(graph, context);
+ this.expression = expression;
+ }
+
+ @Override
+ protected String getExpressionText(ReadGraph graph) throws DatabaseException {
+ return expression;
+ }
+
+ @Override
+ public int hashCode() {
+ return super.hashCode() + 37 * expression.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj) && ((ValidationCompilationRequest)obj).expression.equals(expression);
+ }
+ }
+
+}
\ No newline at end of file
import org.simantics.db.common.request.IsEnumeratedValue;
import org.simantics.db.common.request.UniqueRead;
import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.CommonDBUtils;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.util.Layer0Utils;
Resource possibleValue = context.getParent(graph).getPossibleRepresents(graph);
if(possibleValue != null) {
if(graph.syncRequest(new IsEnumeratedValue(possibleValue))) {
- return GetEnumerationValue.getEnumerationValueName(graph, possibleValue);
+ return CommonDBUtils.getEnumerationValueName(graph, possibleValue);
}
}