1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.annotation.ui;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.Comparator;
\r
18 import java.util.List;
\r
19 import java.util.Map;
\r
20 import java.util.Set;
\r
22 import org.eclipse.core.runtime.IStatus;
\r
23 import org.eclipse.core.runtime.Status;
\r
24 import org.eclipse.jface.resource.ImageDescriptor;
\r
25 import org.eclipse.jface.viewers.ISelection;
\r
26 import org.simantics.Simantics;
\r
27 import org.simantics.annotation.ontology.AnnotationResource;
\r
28 import org.simantics.databoard.Bindings;
\r
29 import org.simantics.databoard.util.URIStringUtils;
\r
30 import org.simantics.db.ReadGraph;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.db.Session;
\r
33 import org.simantics.db.VirtualGraph;
\r
34 import org.simantics.db.WriteGraph;
\r
35 import org.simantics.db.common.request.UnaryRead;
\r
36 import org.simantics.db.common.request.WriteRequest;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.layer0.util.Layer0Utils;
\r
39 import org.simantics.db.layer0.util.RemoverUtil;
\r
40 import org.simantics.db.layer0.variable.Variable;
\r
41 import org.simantics.db.layer0.variable.VariableBuilder;
\r
42 import org.simantics.db.layer0.variable.VariableMap;
\r
43 import org.simantics.db.layer0.variable.VariableMapImpl;
\r
44 import org.simantics.db.layer0.variable.Variables;
\r
45 import org.simantics.db.service.VirtualGraphSupport;
\r
46 import org.simantics.diagram.stubs.DiagramResource;
\r
47 import org.simantics.layer0.Layer0;
\r
48 import org.simantics.modeling.ModelingResources;
\r
49 import org.simantics.modeling.ModelingUtils;
\r
50 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
\r
51 import org.simantics.scenegraph.utils.NodeUtil;
\r
52 import org.simantics.scl.reflection.annotations.SCLValue;
\r
53 import org.simantics.scl.runtime.function.FunctionImpl1;
\r
54 import org.simantics.scl.runtime.tuple.Tuple;
\r
55 import org.simantics.scl.runtime.tuple.Tuple2;
\r
56 import org.simantics.scl.runtime.tuple.Tuple3;
\r
57 import org.simantics.utils.datastructures.Pair;
\r
58 import org.simantics.utils.strings.AlphanumComparator;
\r
59 import org.simantics.utils.ui.ISelectionUtils;
\r
60 import org.simantics.views.swt.client.base.ISWTViewNode;
\r
61 import org.simantics.views.swt.client.impl.SWTExplorer;
\r
62 import org.slf4j.Logger;
\r
63 import org.slf4j.LoggerFactory;
\r
65 import gnu.trove.map.hash.THashMap;
\r
66 import gnu.trove.set.hash.THashSet;
\r
69 * @author Antti Villberg
\r
70 * @author Tuukka Lehtonen
\r
74 private static final Logger LOGGER = LoggerFactory.getLogger(SCL.class);
\r
76 final public static String EMPTY = "";
\r
77 final public static String MAPPED = "Mapped";
\r
78 final public static String SELECTED = "Selected";
\r
80 final public static String NO_ANNOTATIONS = "No annotations";
\r
82 @SCLValue(type = "ReadGraph -> Resource -> Variable -> [(String, Resource)]")
\r
83 public static List<Tuple> availableSources(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
\r
85 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
\r
86 if (selection == null)
\r
87 return Collections.emptyList();
\r
88 List<Tuple3> sources = availableSourcesImpl(graph, selection);
\r
89 if (sources.isEmpty())
\r
90 return Collections.emptyList();
\r
91 List<Tuple> result = new ArrayList<Tuple>(sources.size());
\r
92 for(Tuple3 anno : sources) {
\r
93 result.add(new Tuple2(anno.get(0),""));
\r
100 * Gathers available annotation sources and always returns them in the same
\r
106 * @throws DatabaseException
\r
108 private static List<Tuple3> availableSourcesImpl(ReadGraph graph, Variable selection) throws DatabaseException {
\r
109 List<Variable> vars = gatherSourceVariables(graph, selection);
\r
110 if (vars.isEmpty())
\r
111 return Collections.emptyList();
\r
112 int size = vars.size();
\r
113 ArrayList<Tuple3> result = new ArrayList<Tuple3>(size);
\r
114 result.add(new Tuple3(sourceLabel(graph, vars.get(0)), SELECTED, vars.get(0)));
\r
115 for (int i = 1; i < size; ++i) {
\r
116 Variable v = vars.get(i);
\r
117 result.add(new Tuple3(sourceLabel(graph, v), MAPPED, v));
\r
123 * Gathers variables starting from source in a particular order each time.
\r
124 * First configuration components and second diagram elements.
\r
129 * @throws DatabaseException
\r
131 private static List<Variable> gatherSourceVariables(ReadGraph graph, Variable source) throws DatabaseException {
\r
132 Resource represents = source.getPossibleRepresents(graph);
\r
133 if (represents == null)
\r
134 return Collections.singletonList(source);
\r
136 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
137 ArrayList<Variable> result = new ArrayList<Variable>(4);
\r
138 for (Resource r : ModelingUtils.getElementCorrespondendences(graph, represents))
\r
139 addPossibleVariable(graph, r, result);
\r
140 for(Resource r : graph.getObjects(represents, MOD.DiagramToComposite))
\r
141 addPossibleVariable(graph, r, result);
\r
142 result.add(source);
\r
143 for(Resource r : graph.getObjects(represents, MOD.ComponentToElement))
\r
144 addPossibleVariable(graph, r, result);
\r
145 for(Resource r : graph.getObjects(represents, MOD.CompositeToDiagram))
\r
146 addPossibleVariable(graph, r, result);
\r
150 private static void addPossibleVariable(ReadGraph graph, Resource r, List<Variable> result) throws DatabaseException {
\r
151 Variable v = Variables.getPossibleVariable(graph, r);
\r
156 private static String sourceLabel(ReadGraph graph, Variable variable) throws DatabaseException {
\r
157 Resource represents = variable.getPossibleRepresents(graph);
\r
158 if(represents != null) {
\r
159 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
160 if(graph.isInstanceOf(represents, DIA.Diagram)) return "Diagram";
\r
161 else if(graph.isInstanceOf(represents, DIA.Flag)) return "Flag Element";
\r
162 else if(graph.isInstanceOf(represents, DIA.RouteGraphConnection)) return "Connection Element";
\r
163 else if(graph.isInstanceOf(represents, DIA.Monitor)) return "Monitor Element";
\r
164 else if(graph.isInstanceOf(represents, DIA.Element)) return "Diagram Element";
\r
165 else return variable.getName(graph);
\r
167 return variable.getURI(graph);
\r
171 @SuppressWarnings("unchecked")
\r
172 private static <T> T selectedSource(ReadGraph graph, Variable selection) throws DatabaseException {
\r
174 String name = selectedSourceName(graph, selection);
\r
175 for(Tuple tuple : availableSourcesImpl(graph, selection)) {
\r
176 if(tuple.get(1).equals(name)) return (T)tuple.get(2);
\r
183 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
\r
184 public static String selectedSource(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
186 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
187 String name = selectedSourceName(graph, selection);
\r
188 for(Tuple tuple : availableSourcesImpl(graph, selection)) {
\r
189 if(tuple.get(1).equals(name)) return (String)tuple.get(0);
\r
196 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
197 public static Object selectedSourceModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
199 return new FunctionImpl1<Object, Object>() {
\r
202 public Object apply(Object sourceKey) {
\r
204 Session s = Simantics.getSession();
\r
205 VirtualGraph vg = s.getService(VirtualGraphSupport.class).getWorkspacePersistent("preferences");
\r
206 s.syncRequest(new SetDefaultAnnotationSourceRequest(vg, context, sourceKey));
\r
207 } catch (DatabaseException e) {
\r
208 Activator.getDefault().getLog().log(
\r
209 new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to update default annotation source, see exception for details.", e));
\r
218 private static class SetDefaultAnnotationSourceRequest extends WriteRequest {
\r
220 private Variable context;
\r
221 private Object sourceKey;
\r
223 public SetDefaultAnnotationSourceRequest(VirtualGraph vg, Variable context, Object sourceKey) {
\r
225 this.context = context;
\r
226 this.sourceKey = sourceKey;
\r
230 public void perform(WriteGraph graph) throws DatabaseException {
\r
231 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
232 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
233 Resource model = Variables.getPossibleIndexRoot(graph, selection);
\r
235 List<Tuple3> annos = availableSourcesImpl(graph, selection);
\r
236 for(Tuple3 anno : annos) {
\r
237 if(anno.get(0).equals(sourceKey)) {
\r
238 graph.claimLiteral(model, ANNO.DefaultAnnotationSource, (String)anno.get(1), Bindings.STRING);
\r
246 private static final Comparator<? super Tuple3> AVAILABLE_ANNOTATION_SORTER = new Comparator<Tuple3>() {
\r
248 public int compare(Tuple3 o1, Tuple3 o2) {
\r
249 String s1 = (String) o1.c2;
\r
250 String s2 = (String) o2.c2;
\r
251 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(s1, s2);
\r
258 * @return list of (Variable annotation, Resource annotation, String name) tuples
\r
259 * @throws DatabaseException
\r
261 private static Collection<Tuple3> availableAnnotationsImpl(ReadGraph graph, Variable selection) throws DatabaseException {
\r
263 Layer0 L0 = Layer0.getInstance(graph);
\r
264 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
266 selection = selectedSource(graph, selection);
\r
268 ArrayList<Tuple3> result = new ArrayList<Tuple3>();
\r
269 for (Variable child : selection.getChildren(graph)) {
\r
270 Resource represents = child.getPossibleRepresents(graph);
\r
271 if (represents != null && graph.isInstanceOf(represents, ANNO.Annotation)) {
\r
272 String name = graph.getPossibleRelatedValue(represents, L0.HasName);
\r
274 result.add(new Tuple3(child, represents, name));
\r
277 for (Variable property : selection.getProperties(graph)) {
\r
278 Resource propertyResource = property.getPossibleRepresents(graph);
\r
279 if (propertyResource != null && graph.isInstanceOf(propertyResource, ANNO.Annotation)) {
\r
280 String propertyName = property.getName(graph);
\r
281 result.add(new Tuple3(property, propertyResource, propertyName));
\r
285 // Sort returned annotations by annotation property name to keep the results stable.
\r
286 Collections.sort(result, AVAILABLE_ANNOTATION_SORTER);
\r
292 private static Pair<Resource, String> defaultAnnotationTypeAndName(ReadGraph graph, Variable selection) throws DatabaseException {
\r
293 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
294 Resource model = Variables.getPossibleIndexRoot(graph, selection);
\r
295 Resource type = graph.getPossibleObject(model, ANNO.HasDefaultAnnotationType);
\r
296 String name = graph.getPossibleRelatedValue(model, ANNO.HasDefaultAnnotationName, Bindings.STRING);
\r
297 return Pair.make(type, name);
\r
300 private static String selectedAnnotationName(ReadGraph graph, Variable selection) throws DatabaseException {
\r
301 Pair<Resource, String> typeAndName = defaultAnnotationTypeAndName(graph, selection);
\r
302 Collection<Tuple3> available = availableAnnotationsImpl(graph, selection);
\r
303 if (!available.isEmpty()) {
\r
304 if (available.size() == 1 || typeAndName.first == null)
\r
305 return (String) available.iterator().next().c2;
\r
306 String firstTypeMatch = null;
\r
307 for (Tuple3 anno : available) {
\r
308 if (graph.isInstanceOf((Resource) anno.c1, typeAndName.first)) {
\r
309 if (firstTypeMatch == null)
\r
310 firstTypeMatch = (String) anno.c2;
\r
311 if (typeAndName.second != null && typeAndName.second.equals(anno.c2)) {
\r
312 // Ok, it just doesn't match better than this.
\r
313 return (String) anno.c2;
\r
317 if (firstTypeMatch != null)
\r
318 return firstTypeMatch;
\r
319 // Nothing => return the first one from list
\r
320 return (String)available.iterator().next().c2;
\r
322 return NO_ANNOTATIONS;
\r
325 private static String selectedSourceName(ReadGraph graph, Variable selection) throws DatabaseException {
\r
327 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
328 Resource model = Variables.getPossibleIndexRoot(graph, selection);
\r
331 String name = graph.getPossibleRelatedValue(model, ANNO.DefaultAnnotationSource, Bindings.STRING);
\r
333 for(Tuple tuple : availableSourcesImpl(graph, selection)) {
\r
334 if(tuple.get(1).equals(name)) return name;
\r
338 Set<String> available = new THashSet<String>();
\r
339 for(Tuple tuple : availableSourcesImpl(graph, selection)) available.add((String)tuple.get(1));
\r
340 if(available.isEmpty()) return EMPTY;
\r
342 if(available.contains(MAPPED)) return MAPPED;
\r
343 else return available.iterator().next();
\r
347 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
348 public static Object explorerInput(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
350 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
351 String selected = selectedAnnotationName(graph, selection);
\r
352 for(Tuple3 anno : availableAnnotationsImpl(graph, selection)) {
\r
353 if(selected.equals(anno.c2)) {
\r
361 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
\r
362 public static String descriptionText(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
364 String result = "";
\r
365 Variable sel = getSelectedAnnotationVariable(graph, context);
\r
368 Layer0 L0 = Layer0.getInstance(graph);
\r
369 Resource literal = sel.getPossibleRepresents(graph);
\r
370 if(literal != null) {
\r
371 Resource container = graph.getPossibleObject(literal, L0.PartOf);
\r
372 if(container != null) {
\r
373 Resource model = Variables.getPossibleIndexRoot(graph, sel);
\r
374 String modelURI = graph.getURI(model);
\r
375 String path = graph.getURI(literal);
\r
376 if(path.startsWith(modelURI)) path = path.substring(modelURI.length()+1);
\r
377 result += URIStringUtils.unescape(path);
\r
379 result += "The annotation is not attached to a library";
\r
389 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
390 public static Object explorerInput2(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
392 return ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
396 @SCLValue(type = "ReadGraph -> Resource -> Variable -> [(String, Resource)]")
\r
397 public static List<Tuple> availableAnnotations(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
\r
398 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
399 ArrayList<Tuple> result = new ArrayList<Tuple>();
\r
400 for(Tuple3 anno : availableAnnotationsImpl(graph, selection)) {
\r
401 result.add(new Tuple2(anno.c2, anno.c1));
\r
403 if(result.isEmpty()) result.add(new Tuple2(NO_ANNOTATIONS, ""));
\r
407 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
\r
408 public static String selectedAnnotation(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
410 final Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
411 return selectedAnnotationName(graph, selection);
\r
415 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
416 public static Object selectedAnnotationModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
418 return new FunctionImpl1<Object, Object>() {
\r
421 public Object apply(final Object _key) {
\r
423 Session s = Simantics.getSession();
\r
424 VirtualGraph vg = s.getService(VirtualGraphSupport.class).getWorkspacePersistent("preferences");
\r
425 s.async(new WriteRequest(vg) {
\r
428 public void perform(WriteGraph graph) throws DatabaseException {
\r
429 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
430 String key = (String)_key;
\r
431 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
432 for(Tuple3 anno : availableAnnotationsImpl(graph, selection)) {
\r
433 if(key.equals(anno.c2)) {
\r
434 Resource type = graph.getPossibleType((Resource) anno.c1, ANNO.Annotation);
\r
435 Resource model = Variables.getPossibleIndexRoot(graph, selection);
\r
436 graph.deny(model, ANNO.HasDefaultAnnotationType);
\r
437 graph.claim(model, ANNO.HasDefaultAnnotationType, type);
\r
438 graph.denyValue(model, ANNO.HasDefaultAnnotationName);
\r
439 graph.claimLiteral(model, ANNO.HasDefaultAnnotationName, key, Bindings.STRING);
\r
455 private static Resource getSelectionResource(Variable context) throws DatabaseException {
\r
456 return Simantics.getSession().syncRequest(new UnaryRead<Variable,Resource>(context) {
\r
459 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
460 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, parameter);
\r
461 Variable source = selectedSource(graph, selection);
\r
462 return source.getPossibleRepresents(graph);
\r
468 static class AddModifier extends FunctionImpl1<Object, Object> {
\r
470 private final Variable context;
\r
472 public AddModifier(Variable context) {
\r
473 this.context = context;
\r
476 private void doAdd(final Variable variable) throws DatabaseException {
\r
478 if(variable != null) {
\r
479 // We have a selected annotation
\r
480 AnnotationUtils.newAnnotation(variable);
\r
482 // No annotation selected
\r
483 Resource parent = getSelectionResource(context);
\r
485 AnnotationUtils.newAnnotation(parent);
\r
491 public Object apply(Object p0) {
\r
493 ISWTViewNode node = (ISWTViewNode)p0;
\r
494 SWTExplorer properties = (SWTExplorer)NodeUtil.browsePossible(node, "./Properties");
\r
495 if(properties == null) return null;
\r
497 doAdd((Variable)properties.input);
\r
498 } catch (DatabaseException e) {
\r
499 LOGGER.error("newAnnotationModifier failed", e);
\r
507 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
508 public static Object newAnnotationModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
509 return new AddModifier(context);
\r
512 private static Variable getSelectedAnnotationVariable(ReadGraph graph, Variable context) throws DatabaseException {
\r
513 Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
514 String selected = selectedAnnotationName(graph, selection);
\r
515 for(Tuple3 anno : availableAnnotationsImpl(graph, selection)) {
\r
516 if(anno.c2.equals(selected)) {
\r
517 return (Variable)anno.c0;
\r
523 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
524 public static Object removeAnnotationModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
526 return new RemoveModifier();
\r
530 public static class SaveModifier extends FunctionImpl1<Object, Object> {
\r
532 private boolean doSave(final Variable variable) {
\r
534 if(!AnnotationUtils.isAnnotation(variable)) return false;
\r
536 final Map<Resource, Pair<String, ImageDescriptor>> map = AnnotationUtils.findLibraries(variable);
\r
537 if(map == null) return false;
\r
539 AnnotationUtils.queryLibrary(map, selected -> {
\r
540 Simantics.getSession().async(new WriteRequest() {
\r
542 public void perform(WriteGraph graph) throws DatabaseException {
\r
543 Resource represents = variable.getPossibleRepresents(graph);
\r
544 if(represents != null && !selected.second.isEmpty()) {
\r
545 saveAnnotation(graph, represents, selected.first, selected.second);
\r
554 public static Resource saveAnnotation(WriteGraph graph, Resource annotation, Resource library, String name) throws DatabaseException {
\r
556 Layer0 L0 = Layer0.getInstance(graph);
\r
557 if(graph.hasStatement(annotation, L0.PartOf)) {
\r
558 graph.deny(annotation, L0.PartOf);
\r
560 graph.claim(library, L0.ConsistsOf, L0.PartOf, annotation);
\r
561 graph.claimLiteral(annotation, L0.HasName, name, Bindings.STRING);
\r
568 public Object apply(Object p0) {
\r
570 ISWTViewNode node = (ISWTViewNode)p0;
\r
571 SWTExplorer properties = (SWTExplorer)NodeUtil.browsePossible(node, "./Properties");
\r
572 if(properties == null) return null;
\r
573 ISelection selection = properties.lastSelection;
\r
574 if(selection == null || selection.isEmpty()) {
\r
575 doSave((Variable)properties.input);
\r
579 Collection<Variable> vars = ISelectionUtils.filterSetSelection(selection, Variable.class);
\r
580 if(vars.size() != 1) return null;
\r
582 Variable selected = vars.iterator().next();
\r
583 if(!doSave(selected))
\r
584 doSave((Variable)properties.input);
\r
592 static class RemoveModifier extends FunctionImpl1<Object, Object> {
\r
594 private boolean doRemove(final Variable variable) {
\r
596 if(!AnnotationUtils.isAnnotation(variable))
\r
599 Simantics.getSession().async(new WriteRequest() {
\r
601 public void perform(WriteGraph graph) throws DatabaseException {
\r
602 graph.markUndoPoint();
\r
603 Resource represents = variable.getPossibleRepresents(graph);
\r
604 if(represents != null) {
\r
605 Layer0 L0 = Layer0.getInstance(graph);
\r
606 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
607 if(graph.isInstanceOf(represents, ANNO.Annotation)) {
\r
608 Resource subject = variable.getParent(graph).getRepresents(graph);
\r
609 Resource predicate = variable.getPossiblePredicateResource(graph);
\r
610 if(predicate != null) {
\r
611 // This is a property annotation (no entry) - unlink
\r
612 graph.deny(subject, predicate);
\r
613 Layer0Utils.addCommentMetadata(graph, "Unlinked a property annotation " + graph.getRelatedValue2(predicate, L0.HasName, Bindings.STRING) + " " + predicate.toString() + " from " + graph.getRelatedValue2(subject, L0.HasName, Bindings.STRING) + " " + subject.toString());
\r
615 // This should be an entry annotation - remove from container
\r
616 graph.deny(subject, ANNO.Annotation_HasEntry, represents);
\r
617 Layer0Utils.addCommentMetadata(graph, "Removed an entry annotation " + graph.getRelatedValue2(subject, L0.HasName, Bindings.STRING) + " " + subject.toString() + " from its container " + graph.getRelatedValue2(represents, L0.HasName, Bindings.STRING));
\r
619 // If the annotation is not in any library remove it
\r
620 if(!graph.hasStatement(represents, L0.PartOf))
\r
621 RemoverUtil.remove(graph, represents);
\r
633 public Object apply(Object p0) {
\r
635 ISWTViewNode node = (ISWTViewNode)p0;
\r
636 SWTExplorer properties = (SWTExplorer)NodeUtil.browsePossible(node, "./Properties");
\r
637 if(properties == null) return null;
\r
638 ISelection selection = properties.lastSelection;
\r
639 if(selection == null || selection.isEmpty()) {
\r
640 doRemove((Variable)properties.input);
\r
644 Collection<Variable> vars = ISelectionUtils.filterSetSelection(selection, Variable.class);
\r
645 if(vars.size() != 1) return null;
\r
647 Variable selected = vars.iterator().next();
\r
648 if(!doRemove(selected))
\r
649 doRemove((Variable)properties.input);
\r
657 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
658 public static Object saveAnnotationModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
660 return new SaveModifier();
\r
664 @SCLValue(type = "VariableMap")
\r
665 public static VariableMap domainChildren = new VariableMapImpl() {
\r
667 private Map<String,Resource> children(ReadGraph graph, Resource resource) throws DatabaseException {
\r
668 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
669 Layer0 L0 = Layer0.getInstance(graph);
\r
670 Collection<Resource> objects = graph.getObjects(resource, ANNO.Annotation_HasEntry);
\r
671 THashMap<String, Resource> result = new THashMap<String, Resource>(objects.size());
\r
672 for(Resource r : objects) {
\r
673 String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
\r
675 if (result.put(name, r) != null)
\r
676 LOGGER.error("The database contains siblings with the same name " + name + " (resource=$" + resource.getResourceId() +").");
\r
683 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
\r
684 Map<String, Resource> children = children(graph,context.getRepresents(graph));
\r
685 Resource child = children.get(name);
\r
686 if(child == null) return null;
\r
687 VariableBuilder variableBuilder = graph.adapt(child, VariableBuilder.class);
\r
688 return variableBuilder.buildChild(graph, context, null, child);
\r
692 public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
\r
693 Map<String,Resource> childMap = children(graph,context.getRepresents(graph));
\r
694 if(childMap.isEmpty()) return map;
\r
695 if(map == null) map = new THashMap<String,Variable>();
\r
696 for(Map.Entry<String, Resource> entry : childMap.entrySet()) {
\r
697 String name = entry.getKey();
\r
698 Resource child = entry.getValue();
\r
699 VariableBuilder variableBuilder = graph.adapt(child, VariableBuilder.class);
\r
700 Variable var = variableBuilder.buildChild(graph, context, null, child);
\r
702 map.put(name, var);
\r
704 System.err.println("No adapter for " + child + " in " + context.getURI(graph));
\r