view src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/IndexTreeEditor.java @ 146:371b6ddb78f2

repair putAttributeIndex and deleteAttributeIndex
author one
date Fri, 21 Nov 2014 12:46:06 +0900
parents 72f454eb04ec
children feb2346ace19
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl;


import java.util.Iterator;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;

import fj.P2;
import fj.data.List;
import fj.data.Option;
import fj.data.TreeMap;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Direction;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traverser;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Triple;
import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexEditor;

public class IndexTreeEditor {

  private final Traverser traverser;

  
  public IndexTreeEditor(Traverser traverser)
  {
    this.traverser = traverser;
  }
  
  
  public Either<Error,Triple<LoggingNode, TreeMap<TreeNode, TreeNode>,TreeMap<String,TreeMap<String,List<TreeNode>>>>> edit(TreeNode root,NodePath path,NodeEditor editor,TreeMap<TreeNode, TreeNode> parentIndex, IndexEditor indexEditor)
  {
    DefaultEvaluator e = new DefaultEvaluator(path);
    Either<Error, Traversal> either = traverser.traverse(root,e);
    
    if(either.isA()){
      return DefaultEither.newA(either.a());
    }
    
    Traversal t = either.b();
    Either<Error,Triple<LoggingNode, TreeMap<TreeNode, TreeNode>,TreeMap<String,TreeMap<String,List<TreeNode>>>>> ret = clone(t,editor,parentIndex, indexEditor);
    
    return ret;
  }
  
  private Either<Error,Triple<LoggingNode, TreeMap<TreeNode, TreeNode>,TreeMap<String,TreeMap<String,List<TreeNode>>>>> clone(Traversal t,NodeEditor editor , TreeMap<TreeNode, TreeNode> parentIndex, IndexEditor indexEditor)
  {
    // copying nodes from bottom to root
    TreeMap<TreeNode, TreeNode> newParentIndex = parentIndex;
    List<Direction<TreeNode>> path = List.nil();
    for (Direction<TreeNode> direction : t) {
      path = path.cons(direction);
    }

    // target
    Direction<TreeNode> targetDirection = path.head();
    TreeNode target = targetDirection.getTarget();
    Iterator<TreeNode> targetDeleteChildren = target.getChildren().iterator();
    IndexEditor alreadyDeleteTargetIndexEditor = indexEditor.delete(target);
    
    for (;targetDeleteChildren.hasNext();) {
      TreeNode targetDeleteChild = targetDeleteChildren.next();
      newParentIndex = newParentIndex.delete(targetDeleteChild);
    }
    
    
    Either<Error, LoggingNode> either = editor.edit(target);
    if (either.isA()) {
      return DefaultEither.newA(either.a());
    }

    LoggingNode newWrap = either.b();

    // top
    int pos = targetDirection.getPosition();
    TreeNode child = newWrap.getWrap();
    
    IndexEditor alreadyEditTargetIndexEditor = alreadyDeleteTargetIndexEditor.edit(child);
    IndexEditor alreadyAddTargetIndexEditor = alreadyEditTargetIndexEditor.add(child);
    Iterator<TreeNode> targetPutChildren = child.getChildren().iterator();

    for (; targetPutChildren.hasNext();) {
      TreeNode targetPutChild = targetPutChildren.next();
      newParentIndex = newParentIndex.set(targetPutChild, child);
    }

    for (Direction<TreeNode> parentDirection : path.tail()) {
      TreeNode updateTargetNode = parentDirection.getTarget();
      TreeNodeChildren chs = updateTargetNode.getChildren();
      
      alreadyDeleteTargetIndexEditor = alreadyAddTargetIndexEditor.delete(updateTargetNode);
          
      Iterator<TreeNode> deleteParentIndexChildren = chs.iterator();
      for (;deleteParentIndexChildren.hasNext();) {
        TreeNode deleteParentIndexChild = deleteParentIndexChildren.next();
        newParentIndex = newParentIndex.delete(deleteParentIndexChild);
      }
      
      Either<Error, TreeNode> ret = chs.replaceNode(pos, child);
      if (ret.isA()) {
        return DefaultEither.newA(ret.a());
      }
      
      TreeNode newParent = ret.b();
      alreadyAddTargetIndexEditor = alreadyDeleteTargetIndexEditor.add(newParent);
       Iterator<TreeNode> putParentIndexChildren = newParent.getChildren().iterator();
      
      for (;putParentIndexChildren.hasNext();) {
        TreeNode putParentIndexChild = putParentIndexChildren.next();
        newParentIndex = newParentIndex.set(putParentIndexChild, newParent);
      }
      
      child = newParent;
      pos = parentDirection.getPosition();
    }

    TreeNode newRoot = child;
    LoggingNode logNode = editor.wrap(newRoot, newWrap.getOperationLog());
    
        
    TreeMap<String,TreeMap<String,List<TreeNode>>> indexList = alreadyAddTargetIndexEditor.getIndex();
    Triple<LoggingNode, TreeMap<TreeNode, TreeNode>,TreeMap<String,TreeMap<String,List<TreeNode>>>> triple = new Triple<LoggingNode, TreeMap<TreeNode, TreeNode>,TreeMap<String,TreeMap<String,List<TreeNode>>>>(logNode,newParentIndex,indexList);
    return DefaultEither.newB(triple);
  }
   
}