RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package zephyr.plugin.core.api.internal.codeparser.codetree; 00002 00003 import java.lang.reflect.Field; 00004 import java.util.ArrayList; 00005 import java.util.Arrays; 00006 import java.util.Collection; 00007 import java.util.HashMap; 00008 import java.util.HashSet; 00009 import java.util.Map; 00010 import java.util.Set; 00011 00012 import zephyr.plugin.core.api.internal.codeparser.interfaces.CodeNode; 00013 import zephyr.plugin.core.api.internal.codeparser.interfaces.CodeParser; 00014 import zephyr.plugin.core.api.internal.codeparser.interfaces.ParentNode; 00015 import zephyr.plugin.core.api.internal.codeparser.interfaces.PopupHandler; 00016 import zephyr.plugin.core.api.internal.codeparser.interfaces.Traverser; 00017 import zephyr.plugin.core.api.internal.monitoring.abstracts.DataMonitorAdapter; 00018 import zephyr.plugin.core.api.internal.monitoring.abstracts.MonitorContainerNode; 00019 import zephyr.plugin.core.api.internal.monitoring.abstracts.MonitoredDataTraverser; 00020 import zephyr.plugin.core.api.monitoring.annotations.Monitor; 00021 import zephyr.plugin.core.api.monitoring.annotations.Popup; 00022 import zephyr.plugin.core.api.synchronization.Clock; 00023 00024 public class CodeTrees { 00025 static public boolean isPrimitive(Class<? extends Object> fieldClass) { 00026 if (fieldClass.isPrimitive()) 00027 return true; 00028 if (Boolean.class.equals(fieldClass)) 00029 return true; 00030 return Number.class.isAssignableFrom(fieldClass); 00031 } 00032 00033 static public Object getValueFromField(Field field, Object parentInstance) { 00034 try { 00035 return field.get(parentInstance); 00036 } catch (IllegalArgumentException e) { 00037 e.printStackTrace(); 00038 } catch (IllegalAccessException e) { 00039 e.printStackTrace(); 00040 } 00041 return null; 00042 } 00043 00044 static public void traverse(Traverser traverser, CodeNode root) { 00045 boolean visitChildren = traverser.inNode(root); 00046 if (visitChildren && root instanceof ParentNode) { 00047 ParentNode parent = (ParentNode) root; 00048 for (int i = 0; i < parent.nbChildren(); i++) 00049 traverse(traverser, parent.getChild(i)); 00050 } 00051 traverser.outNode(root); 00052 } 00053 00054 public static String labelOf(Field field) { 00055 if (field == null) 00056 return ""; 00057 Monitor annotation = field.getAnnotation(Monitor.class); 00058 if (annotation == null) 00059 return field.getName(); 00060 String label = annotation.label(); 00061 if (label.isEmpty() && !annotation.emptyLabel()) 00062 label = field.getName(); 00063 return label; 00064 } 00065 00066 public static int levelOf(Field field) { 00067 if (field == null) 00068 return 0; 00069 Monitor annotation = field.getAnnotation(Monitor.class); 00070 if (annotation == null) 00071 return 0; 00072 return annotation.level(); 00073 } 00074 00075 public static ClassNode findParent(CodeNode codeNode, Class<?> target) { 00076 CodeNode currentNode = codeNode; 00077 while (currentNode != null) { 00078 if (currentNode instanceof ClassNode && target.isInstance(((ClassNode) currentNode).instance())) 00079 return (ClassNode) currentNode; 00080 currentNode = currentNode.parent(); 00081 } 00082 return null; 00083 } 00084 00085 public static Clock clockOf(CodeNode codeNode) { 00086 CodeNode currentNode = codeNode; 00087 while (currentNode != null) { 00088 if (currentNode instanceof ClockNode) 00089 return ((ClockNode) currentNode).clock(); 00090 currentNode = currentNode.parent(); 00091 } 00092 return null; 00093 } 00094 00095 public static Map<Clock, Collection<CodeNode>> sortByClock(CodeNode[] supported) { 00096 Map<Clock, Collection<CodeNode>> result = new HashMap<Clock, Collection<CodeNode>>(); 00097 for (CodeNode codeNode : supported) { 00098 Clock clockNode = clockOf(codeNode); 00099 Collection<CodeNode> nodes = result.get(clockNode); 00100 if (nodes == null) { 00101 nodes = new ArrayList<CodeNode>(); 00102 result.put(clockNode, nodes); 00103 } 00104 nodes.add(codeNode); 00105 } 00106 return result; 00107 } 00108 00109 public static boolean[] toBooleans(CodeNode[] codeNodes, int displayedIndex) { 00110 boolean[] result = new boolean[codeNodes.length]; 00111 Arrays.fill(result, false); 00112 if (displayedIndex != -1) 00113 result[displayedIndex] = true; 00114 return result; 00115 } 00116 00117 public static boolean[] toBooleansAsTrue(CodeNode[] codeNodes) { 00118 boolean[] result = new boolean[codeNodes.length]; 00119 Arrays.fill(result, true); 00120 return result; 00121 } 00122 00123 public static Set<String> parseLabels(CodeNode codeNode) { 00124 final Set<String> labels = new HashSet<String>(); 00125 DataMonitorAdapter monitorAdapter = new DataMonitorAdapter() { 00126 @Override 00127 public void add(MonitorContainerNode codeNode) { 00128 String nodePath = CodeTrees.mergePath(codeNode); 00129 for (String monitoredLabel : codeNode.createLabels()) 00130 labels.add(nodePath + monitoredLabel); 00131 } 00132 }; 00133 MonitoredDataTraverser traverser = new MonitoredDataTraverser(monitorAdapter, 00134 MonitoredDataTraverser.MonitorEverythingLevel); 00135 CodeTrees.traverse(traverser, codeNode); 00136 return labels; 00137 } 00138 00139 public static String mergePath(MonitorContainerNode codeNode) { 00140 return CodeTrees.mergePath(((CodeNode) codeNode).path()); 00141 } 00142 00143 public static String nodeInfo(CodeNode codeNode) { 00144 if (codeNode instanceof ClassNode) { 00145 Object instance = ((ClassNode) codeNode).instance(); 00146 if (instance instanceof String) 00147 return "'" + instance + "'"; 00148 return instance.getClass().getSimpleName(); 00149 } 00150 if (codeNode instanceof PrimitiveNode) 00151 return String.valueOf(((PrimitiveNode) codeNode).value()); 00152 if (codeNode instanceof AbstractPrimitives) 00153 return String.valueOf(((AbstractPrimitives) codeNode).size()); 00154 return ""; 00155 } 00156 00157 public static String mergePath(String[] path) { 00158 StringBuilder result = new StringBuilder(measureLength(path) + path.length); 00159 for (String label : path) { 00160 if (!label.isEmpty()) { 00161 result.append(label); 00162 result.append("/"); 00163 } 00164 } 00165 return result.length() > 0 ? result.substring(0, result.length() - 1) : result.toString(); 00166 } 00167 00168 private static int measureLength(String[] path) { 00169 int result = 0; 00170 for (String s : path) 00171 result += s.length(); 00172 return result; 00173 } 00174 00175 public static void popupIFN(CodeParser codeParser, Field field, CodeNode codeNode) { 00176 if (field == null || codeNode == null) 00177 return; 00178 if (!field.isAnnotationPresent(Popup.class)) 00179 return; 00180 if (codeParser instanceof PopupHandler) 00181 ((PopupHandler) codeParser).popup(codeNode); 00182 } 00183 }