1 package org.simantics.interop.mapping;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
6 import org.simantics.db.ReadGraph;
\r
7 import org.simantics.db.Resource;
\r
8 import org.simantics.db.exception.DatabaseException;
\r
9 import org.simantics.interop.mapping.data.GraphNode;
\r
10 import org.simantics.interop.mapping.data.Identifiable;
\r
11 import org.simantics.interop.mapping.data.Link;
\r
12 import org.simantics.utils.datastructures.MapList;
\r
14 public class MappingTools {
\r
16 public static Resource getSymbolType(ReadGraph g, GraphNode<Identifiable> node) throws DatabaseException {
\r
17 Resource type = node.getHint(MappingHints.KEY_GENERATE_TYPE);
\r
19 ModuleResolver resolver = node.getHint(MappingHints.KEY_RESOLVER);
\r
20 if (resolver != null)
\r
21 type = resolver.getSymbolType(g);
\r
27 public static Resource getTerminal(ReadGraph g, Link<Identifiable> link) throws DatabaseException {
\r
28 Resource terminal = link.getHint(MappingHints.KEY_CONNECTION_TERMINAL);
\r
29 if (terminal == null) {
\r
30 ModuleResolver resolver = link.from().getHint(MappingHints.KEY_RESOLVER);
\r
31 if (resolver != null)
\r
32 terminal = resolver.getTerminal(g, link);
\r
38 public static void assignGenerationRule(GraphNode<Identifiable> node, int index, GenerationRule rule) {
\r
39 MapList<Integer,GenerationRule> rules = node.getHint(MappingHints.KEY_GENERATION_RULES);
\r
40 if (rules == null) {
\r
41 rules = new MapList<Integer,GenerationRule>();
\r
42 node.setHint(MappingHints.KEY_GENERATION_RULES, rules);
\r
44 rules.add(index,rule);
\r
48 public static void createEmptyGenerationRules(GraphNode<Identifiable> node) {
\r
49 node.setHint(MappingHints.KEY_GENERATION_RULES, new MapList<Integer,GenerationRule>());
\r
52 public static void copyGenerationRules(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {
\r
53 MapList<Integer,GenerationRule> rules = from.getHint(MappingHints.KEY_GENERATION_RULES);
\r
56 MapList<Integer,GenerationRule> toRules = to.getHint(MappingHints.KEY_GENERATION_RULES);
\r
57 if (toRules == null) {
\r
58 toRules = new MapList<Integer,GenerationRule>();
\r
59 to.setHint(MappingHints.KEY_GENERATION_RULES, toRules);
\r
61 for (Integer i : rules.getKeys()) {
\r
62 for (GenerationRule r : rules.getValues(i))
\r
67 public static void copyHints(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {
\r
68 to.setHints(from.getHints());
\r
69 createEmptyGenerationRules(to);
\r
70 copyGenerationRules(from, to);
\r
71 reconnectParent(from, to);
\r
74 public static void copyDefaultHints(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {
\r
75 to.setHints(from.getHints());
\r
76 createEmptyGenerationRules(to);
\r
77 reconnectParent(from, to);
\r
80 private static void reconnectParent(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {
\r
81 GraphNode<Identifiable> parent = from.getHint(MappingHints.KEY_PARENT_NODE);
\r
82 if (parent != null) {
\r
83 setParentNode(parent, to);
\r
88 // public static boolean hasModuleTypeAssigned(GraphNode<Identifiable> node) {
\r
89 // return (node.containsHint(MappingHints.KEY_RESOLVER) || node.containsHint(MappingHints.KEY_GENERATE_TYPE) || node.containsHint(MappingHints.KEY_GENERATED_SYMBOL));
\r
92 public static <T> void disposeNodes(Collection<GraphNode<T>> nodes) {
\r
93 for (GraphNode<T> node : nodes) {
\r
99 public static void setParentNode(GraphNode<?> parent, GraphNode<?> child) {
\r
100 child.setHint(MappingHints.KEY_PARENT_NODE, parent);
\r
101 Collection<GraphNode<?>> children = parent.getHint(MappingHints.KEY_CHILD_NODE);
\r
102 if (children == null) {
\r
103 children = new ArrayList<GraphNode<?>>();
\r
104 parent.setHint(MappingHints.KEY_CHILD_NODE, children);
\r
106 children.add(child);
\r
109 public static void unsetParentNode(GraphNode<?> child) {
\r
110 GraphNode<Identifiable> parent = child.getHint(MappingHints.KEY_PARENT_NODE);
\r
111 if (parent == null)
\r
114 Collection<GraphNode<?>> children = parent.getHint(MappingHints.KEY_CHILD_NODE);
\r
115 children.remove(child);
\r
116 child.removeHint(MappingHints.KEY_PARENT_NODE);
\r