All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.studio.agent.debug.ScopeNodeEvaluator Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.studio.agent.debug;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.snapscript.core.Context;
import org.snapscript.core.scope.Scope;
import org.snapscript.studio.agent.debug.ExpressionProcessor;
import org.snapscript.studio.agent.debug.ExpressionScopeNode;
import org.snapscript.studio.agent.debug.ScopeNode;
import org.snapscript.studio.agent.debug.ScopeNodeBuilder;
import org.snapscript.studio.agent.debug.VariableNameEncoder;

public class ScopeNodeEvaluator {

   private final ExpressionProcessor processor; 
   private final VariableNameEncoder encoder;
   private final Context context;
   
   public ScopeNodeEvaluator(Context context, Scope scope) {
      this.processor = new ExpressionProcessor(context, scope); // this keeps expression cache
      this.encoder = new VariableNameEncoder();
      this.context = context;
   }
   
   public Map> expand(Set expand, String expression, boolean refresh) {
      Map> variables = new HashMap>();
      ScopeNodeBuilder builder = new ScopeNodeBuilder(variables, context);
      ScopeNode node = new ExpressionScopeNode(builder, processor, encoder, expression, refresh);
      
      if(!expand.isEmpty()) {
         for(String path : expand) {
            String[] parts = path.split("\\.");
            expand(node, parts, 0);
         }
      } else {
         node.getNodes(); // expand root
      }
      return variables;
   }

   private void expand(ScopeNode node, String[] parts, int index) {
      List children = node.getNodes();
      String match = encoder.decode(parts[index]);
      
      for(ScopeNode child : children) {
         String name = child.getName();
         
         if(name.equals(match)) {
            expand(child, parts, index+1);
            break;
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy