1 package org.simantics.annotation.ui;
4 import java.io.IOException;
7 import org.eclipse.core.runtime.NullProgressMonitor;
8 import org.simantics.Simantics;
9 import org.simantics.annotation.ui.wizard.AnnotationTypeExporter;
10 import org.simantics.annotation.ui.wizard.AnnotationTypeImportWizard;
11 import org.simantics.databoard.serialization.SerializationException;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Session;
15 import org.simantics.db.Statement;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.request.PropertyInfo;
19 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
20 import org.simantics.db.layer0.util.Layer0Utils;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.userComponent.ComponentTypeCommands;
23 import org.simantics.scl.runtime.tuple.Tuple2;
24 import org.simantics.utils.datastructures.Pair;
26 public class SCLAnnotation {
29 public static Resource newAnnotationType(WriteGraph graph, Resource parent) throws DatabaseException {
31 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);
32 Resource type = annotation.second;
37 public static Tuple2 newAnnotationType1(WriteGraph graph, Resource parent) throws DatabaseException {
39 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);
40 Tuple2 tuple = new Tuple2(annotation.first, annotation.second);
45 public static Resource newAnnotationValue(WriteGraph graph, Resource parent, Resource property) throws DatabaseException {
46 graph.markUndoPoint();
47 Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, property);
48 return annotationValue;
51 public static Resource newAnnotationValueWithName(WriteGraph graph, Resource parent, String name, Resource property) throws DatabaseException {
52 Resource existing = Layer0Utils.getPossibleChild(graph, parent, name);
53 if(existing != null) return existing;
54 Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, name, property);
55 return annotationValue;
58 public static Resource newAnnotationProperty(WriteGraph graph, Resource annotationType) throws DatabaseException {
60 Resource annotationProperty = ComponentTypeCommands.createPropertyWithDefaults(graph, annotationType);
62 return annotationProperty;
65 public static Resource advancedAnnotationProperty(WriteGraph graph, Resource annotationType, String name, String value, String unit, String range, String label, String description) throws DatabaseException {
67 Resource property = ComponentTypeCommands.createProperty(graph, annotationType, name, value, unit, range, label, description);
72 public static void removeAnnotationProperty(WriteGraph graph, Resource property) throws DatabaseException {
74 Resource annotationType = getAnnotationType(graph, property);
75 ComponentTypeCommands.removeProperty(graph, annotationType, property);
78 public static void setPropertyType(WriteGraph graph, Resource property, String type) throws DatabaseException {
80 Resource annotationType = getAnnotationType(graph, property);
81 ComponentTypeCommands.setRequiredType(graph, property, type);
82 ComponentTypeCommands.convertDefaultValue(graph, annotationType, property, type);
86 public static void setPropertyUnit(WriteGraph graph, Resource property, String unit) throws DatabaseException {
88 Resource annotationType = getAnnotationType(graph, property);
89 ComponentTypeCommands.setUnit(graph, property, annotationType, unit);
93 public static void setPropertyLabel(WriteGraph graph, Resource property, String label) throws DatabaseException {
95 ComponentTypeCommands.setLabel(graph, property, label);
98 public static void setPropertyRange(WriteGraph graph, Resource property, String range) throws DatabaseException {
100 Resource annotationType = getAnnotationType(graph, property);
101 ComponentTypeCommands.setRange(graph, property, annotationType, range);
104 public static void setPropertyDescription(WriteGraph graph, Resource property, String description) throws DatabaseException {
106 ComponentTypeCommands.setDescription(graph, property, description);
109 public static void setDefaultValue(WriteGraph graph, Resource property, String value) throws DatabaseException {
111 Resource annotationType = getAnnotationType(graph, property);
112 ComponentTypeCommands.setDefaultValue(graph, annotationType, property, value);
115 public static Resource attachAnnotation(WriteGraph graph, Resource parent, Resource annotationType) throws DatabaseException {
117 Resource annotation = AnnotationUtils.newAnnotation(graph, parent, annotationType);
122 public static Resource getAnnotationType(ReadGraph graph, Resource property) throws DatabaseException {
124 Layer0 L0 = Layer0.getInstance(graph);
125 Resource annotationType = graph.getPossibleObject(property, L0.PartOf);
126 return annotationType;
130 public static Resource saveAnnotation(WriteGraph graph, Resource annotation, Resource library, String name) throws DatabaseException {
131 return SCL.SaveModifier.saveAnnotation(graph, annotation, library, name);
134 public static void importAnnotationFromFile(File importLocation, Resource target) throws SerializationException, IOException, DatabaseException {
135 Session session = Simantics.getSession();
136 AnnotationTypeImportWizard.doImport(new NullProgressMonitor(), importLocation, session, target);
139 public static void exportAnnotationToFile(File location, Resource annotationType) throws DatabaseException, IOException {
140 AnnotationTypeExporter.doExport(location, annotationType);
143 public static void rename(WriteGraph graph, Resource resource, String newName) throws DatabaseException {
144 ComponentTypeCommands.rename(graph, resource, newName);
147 public static void copyAnnotationData(WriteGraph graph, Resource source, Resource target) throws DatabaseException {
148 // source and target are expected to be of the same type
149 Map<String, PropertyInfo> sps = graph.syncRequest(new UnescapedPropertyMapOfResource(source));
150 Map<String, PropertyInfo> tps = graph.syncRequest(new UnescapedPropertyMapOfResource(target));
152 for (PropertyInfo sp : sps.values()) {
153 PropertyInfo tp = tps.get(sp.name);
155 for (Statement srcstm : graph.getStatements(source, sp.predicate)) {
156 if (srcstm.isAsserted(source)) {
157 // Make sure the target value is asserted as well.
158 graph.deny(target, sp.predicate);
160 Resource property = srcstm.getObject();
161 // Remove existing property from source and link the target property
162 for (Statement tgtstm : graph.getStatements(target, tp.predicate)) {
163 if (!tgtstm.isAsserted(target))
166 // Re-link the source property to the target entity.
167 graph.deny(source, sp.predicate, property);
168 graph.claim(target, tp.predicate, property);