package org.simantics.interop.mapping; import java.util.ArrayList; import java.util.Collection; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.interop.mapping.data.GraphNode; import org.simantics.interop.mapping.data.Identifiable; import org.simantics.interop.mapping.data.Link; import org.simantics.utils.datastructures.MapList; public class MappingTools { public static Resource getSymbolType(ReadGraph g, GraphNode node) throws DatabaseException { Resource type = node.getHint(MappingHints.KEY_GENERATE_TYPE); if (type == null) { ModuleResolver resolver = node.getHint(MappingHints.KEY_RESOLVER); if (resolver != null) type = resolver.getSymbolType(g); } return type; } public static Resource getTerminal(ReadGraph g, Link link) throws DatabaseException { Resource terminal = link.getHint(MappingHints.KEY_CONNECTION_TERMINAL); if (terminal == null) { ModuleResolver resolver = link.from().getHint(MappingHints.KEY_RESOLVER); if (resolver != null) terminal = resolver.getTerminal(g, link); } return terminal; } public static void assignGenerationRule(GraphNode node, int index, GenerationRule rule) { MapList rules = node.getHint(MappingHints.KEY_GENERATION_RULES); if (rules == null) { rules = new MapList(); node.setHint(MappingHints.KEY_GENERATION_RULES, rules); } rules.add(index,rule); } public static void createEmptyGenerationRules(GraphNode node) { node.setHint(MappingHints.KEY_GENERATION_RULES, new MapList()); } public static void copyGenerationRules(GraphNode from, GraphNode to) { MapList rules = from.getHint(MappingHints.KEY_GENERATION_RULES); if (rules == null) return; MapList toRules = to.getHint(MappingHints.KEY_GENERATION_RULES); if (toRules == null) { toRules = new MapList(); to.setHint(MappingHints.KEY_GENERATION_RULES, toRules); } for (Integer i : rules.getKeys()) { for (GenerationRule r : rules.getValues(i)) toRules.add(i,r); } } public static void copyHints(GraphNode from, GraphNode to) { to.setHints(from.getHints()); createEmptyGenerationRules(to); copyGenerationRules(from, to); reconnectParent(from, to); } public static void copyDefaultHints(GraphNode from, GraphNode to) { to.setHints(from.getHints()); createEmptyGenerationRules(to); reconnectParent(from, to); } private static void reconnectParent(GraphNode from, GraphNode to) { GraphNode parent = from.getHint(MappingHints.KEY_PARENT_NODE); if (parent != null) { setParentNode(parent, to); } } // public static boolean hasModuleTypeAssigned(GraphNode node) { // return (node.containsHint(MappingHints.KEY_RESOLVER) || node.containsHint(MappingHints.KEY_GENERATE_TYPE) || node.containsHint(MappingHints.KEY_GENERATED_SYMBOL)); // } public static void disposeNodes(Collection> nodes) { for (GraphNode node : nodes) { node.destroy(); } nodes.clear(); } public static void setParentNode(GraphNode parent, GraphNode child) { child.setHint(MappingHints.KEY_PARENT_NODE, parent); Collection> children = parent.getHint(MappingHints.KEY_CHILD_NODE); if (children == null) { children = new ArrayList>(); parent.setHint(MappingHints.KEY_CHILD_NODE, children); } children.add(child); } public static void unsetParentNode(GraphNode child) { GraphNode parent = child.getHint(MappingHints.KEY_PARENT_NODE); if (parent == null) return; Collection> children = parent.getHint(MappingHints.KEY_CHILD_NODE); children.remove(child); child.removeHint(MappingHints.KEY_PARENT_NODE); } }