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

swim.system.lane.SpatialLaneModel Maven / Gradle / Ivy

// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swim.system.lane;

import java.util.Iterator;
import java.util.Map;
import swim.api.LaneException;
import swim.api.Link;
import swim.api.data.SpatialData;
import swim.collections.FingerTrieSeq;
import swim.concurrent.Cont;
import swim.concurrent.Stage;
import swim.math.Z2Form;
import swim.spatial.SpatialMap;
import swim.structure.Form;
import swim.structure.Record;
import swim.structure.Value;
import swim.system.LaneModel;
import swim.system.LaneRelay;
import swim.system.LaneView;
import swim.system.Push;
import swim.system.WarpBinding;
import swim.system.warp.WarpLaneModel;
import swim.warp.CommandMessage;

public class SpatialLaneModel extends WarpLaneModel, SpatialLaneUplink> {

  protected final Z2Form shapeForm;
  protected int flags;
  protected SpatialData data;

  SpatialLaneModel(Z2Form shapeForm, int flags) {
    this.shapeForm = shapeForm;
    this.flags = flags;
    this.data = null;
  }

  public SpatialLaneModel(Z2Form shapeForm) {
    this(shapeForm, 0);
  }

  @Override
  public String laneType() {
    return "spatial";
  }

  @Override
  protected SpatialLaneUplink createWarpUplink(WarpBinding link) {
    return new SpatialLaneUplink(this, link, this.createUplinkAddress(link));
  }

  @Override
  public void onCommand(Push push) {
    this.awaitStart();
    final CommandMessage message = push.message();
    final Value payload = message.body();
    final String tag = payload.tag();
    if ("update".equals(tag)) {
      final Value header = payload.header("update");
      final Value key = header.get("key");
      final Value shape = header.get("shape");
      final S shapeObject = this.shapeForm.cast(shape);
      final Value value = payload.body();
      new SpatialLaneRelayUpdate(this, message, push.cont(), key, shapeObject, value).run();
    } else if ("move".equals(tag)) {
      final Value header = payload.header("move");
      final Value key = header.get("key");
      final Value oldShape = header.get("from");
      final Value newShape = header.get("to");
      final S oldShapeObject = this.shapeForm.cast(oldShape);
      final S newShapeObject = this.shapeForm.cast(newShape);
      final Value value = payload.body();
      new SpatialLaneRelayMove(this, message, push.cont(), key, oldShapeObject, newShapeObject, value).run();
    } else if ("remove".equals(tag)) {
      final Value header = payload.header("remove");
      final Value key = header.get("key");
      final Value shape = header.get("shape");
      final S shapeObject = this.shapeForm.cast(shape);
      new SpatialLaneRelayRemove(this, message, push.cont(), key, shapeObject).run();
    } else if ("clear".equals(tag)) {
      new SpatialLaneRelayClear(this, message, push.cont()).run();
    } else {
      push.trap(new LaneException("unknown subcommand: " + payload));
    }
  }

  @SuppressWarnings("unchecked")
  protected void cueDownKey(Value key) {
    FingerTrieSeq> uplinks;
    do {
      uplinks = (FingerTrieSeq>) LaneModel.UPLINKS.get(this);
      for (int i = 0, n = uplinks.size(); i < n; i += 1) {
        uplinks.get(i).cueDownKey(key);
      }
    } while (uplinks != LaneModel.UPLINKS.get(this));
  }

  @Override
  protected void didOpenLaneView(SpatialLaneView view) {
    view.setLaneBinding(this);
  }

  public final boolean isResident() {
    return (this.flags & SpatialLaneModel.RESIDENT) != 0;
  }

  public SpatialLaneModel isResident(boolean isResident) {
    if (this.data != null) {
      this.data.isResident(isResident);
    }
    if (isResident) {
      this.flags |= SpatialLaneModel.RESIDENT;
    } else {
      this.flags &= ~SpatialLaneModel.RESIDENT;
    }
    final Object views = LaneModel.VIEWS.get(this);
    if (views instanceof SpatialLaneView) {
      ((SpatialLaneView) views).didSetResident(isResident);
    } else if (views instanceof LaneView[]) {
      final LaneView[] viewArray = (LaneView[]) views;
      for (int i = 0, n = viewArray.length; i < n; i += 1) {
        ((SpatialLaneView) viewArray[i]).didSetResident(isResident);
      }
    }
    return this;
  }

  public final boolean isTransient() {
    return (this.flags & SpatialLaneModel.TRANSIENT) != 0;
  }

  public SpatialLaneModel isTransient(boolean isTransient) {
    if (this.data != null) {
      this.data.isTransient(isTransient);
    }
    if (isTransient) {
      this.flags |= SpatialLaneModel.TRANSIENT;
    } else {
      this.flags &= ~SpatialLaneModel.TRANSIENT;
    }
    final Object views = LaneModel.VIEWS.get(this);
    if (views instanceof SpatialLaneView) {
      ((SpatialLaneView) views).didSetTransient(isTransient);
    } else if (views instanceof LaneView[]) {
      final LaneView[] viewArray = (LaneView[]) views;
      for (int i = 0, n = viewArray.length; i < n; i += 1) {
        ((SpatialLaneView) viewArray[i]).didSetTransient(isTransient);
      }
    }
    return this;
  }

  public Value get(Value key) {
    if (key != null) {
      return this.data.get(key);
    } else {
      return Value.absent();
    }
  }

  @SuppressWarnings("unchecked")
  public  V put(SpatialLaneView view, K keyObject, S shapeObject, V newObject) {
    final Form keyForm = view.keyForm;
    final Form valueForm = view.valueForm;
    final Value key = keyForm.mold(keyObject).toValue();
    final Value newValue = valueForm.mold(newObject).toValue();
    final SpatialLaneRelayUpdate relay = new SpatialLaneRelayUpdate(this, this.stage(), key, shapeObject, newValue);
    relay.keyForm = (Form) keyForm;
    relay.shapeForm = (Z2Form) this.shapeForm;
    relay.valueForm = (Form) valueForm;
    relay.keyObject = keyObject;
    relay.oldObject = newObject;
    relay.newObject = newObject;
    relay.run();
    if (relay.valueForm != valueForm && valueForm != null) {
      relay.oldObject = valueForm.cast(relay.oldValue);
      if (relay.oldObject == null) {
        relay.oldObject = valueForm.unit();
      }
    }
    return (V) relay.oldObject;
  }

  @SuppressWarnings("unchecked")
  public  V move(SpatialLaneView view, K keyObject,
                       S oldShapeObject, S newShapeObject, V newObject) {
    final Form keyForm = view.keyForm;
    final Form valueForm = view.valueForm;
    final Value key = keyForm.mold(keyObject).toValue();
    final Value newValue = valueForm.mold(newObject).toValue();
    final SpatialLaneRelayMove relay = new SpatialLaneRelayMove(this, this.stage(), key, oldShapeObject, newShapeObject, newValue);
    relay.keyForm = (Form) keyForm;
    relay.shapeForm = (Z2Form) this.shapeForm;
    relay.valueForm = (Form) valueForm;
    relay.keyObject = keyObject;
    relay.oldObject = newObject;
    relay.newObject = newObject;
    relay.run();
    if (relay.valueForm != valueForm && valueForm != null) {
      relay.oldObject = valueForm.cast(relay.oldValue);
      if (relay.oldObject == null) {
        relay.oldObject = valueForm.unit();
      }
    }
    return (V) relay.oldObject;
  }

  @SuppressWarnings("unchecked")
  public  V remove(SpatialLaneView view, K keyObject, S shapeObject) {
    final Form keyForm = view.keyForm;
    final Form valueForm = view.valueForm;
    final Value key = keyForm.mold(keyObject).toValue();
    final SpatialLaneRelayRemove relay = new SpatialLaneRelayRemove(this, this.stage(), key, shapeObject);
    relay.keyForm = (Form) keyForm;
    relay.shapeForm = (Z2Form) this.shapeForm;
    relay.valueForm = (Form) valueForm;
    relay.keyObject = keyObject;
    relay.run();
    if (relay.valueForm != valueForm && valueForm != null) {
      relay.oldObject = valueForm.cast(relay.oldValue);
      if (relay.oldObject == null) {
        relay.oldObject = valueForm.unit();
      }
    }
    return (V) relay.oldObject;
  }

  public void clear(SpatialLaneView view) {
    final SpatialLaneRelayClear relay = new SpatialLaneRelayClear(this, this.stage());
    relay.run();
  }

  public Iterator> iterator(S shape) {
    return this.data.iterator(shape);
  }

  public Iterator> iterator() {
    return this.data.iterator();
  }

  public Iterator keyIterator() {
    return this.data.keyIterator();
  }

  public Iterator valueIterator() {
    return this.data.valueIterator();
  }

  protected void openStore() {
    this.data = this.laneContext.store().spatialData(this.laneUri().toString(), this.shapeForm)
                                        .isTransient(this.isTransient())
                                        .isResident(this.isResident());
  }

  @Override
  protected void willLoad() {
    this.openStore();
    super.willLoad();
  }

  static final int RESIDENT = 1 << 0;
  static final int TRANSIENT = 1 << 1;

}

final class SpatialLaneRelayUpdate extends LaneRelay, SpatialLaneView> {

  final Link link;
  final CommandMessage message;
  final Cont cont;
  final Value key;
  final S shapeObject;
  Form keyForm;
  Z2Form shapeForm;
  Form valueForm;
  Object keyObject;
  Value oldValue;
  Object oldObject;
  Value newValue;
  Object newObject;

  SpatialLaneRelayUpdate(SpatialLaneModel model, CommandMessage message, Cont cont, Value key, S shapeObject, Value newValue) {
    super(model, 4);
    this.link = null;
    this.message = message;
    this.cont = cont;
    this.key = key;
    this.shapeObject = shapeObject;
    this.newValue = newValue;
  }

  SpatialLaneRelayUpdate(SpatialLaneModel model, Link link, Value key, S shapeObject, Value newValue) {
    super(model, 1, 3, null);
    this.link = link;
    this.message = null;
    this.cont = null;
    this.key = key;
    this.shapeObject = shapeObject;
    this.newValue = newValue;
  }

  SpatialLaneRelayUpdate(SpatialLaneModel model, Stage stage, Value key, S shapeObject, Value newValue) {
    super(model, 1, 3, stage);
    this.link = null;
    this.message = null;
    this.cont = null;
    this.key = key;
    this.shapeObject = shapeObject;
    this.newValue = newValue;
  }

  @Override
  protected void beginPhase(int phase) {
    if (phase == 2) {
      this.oldValue = this.model.data.put(this.key, this.shapeObject, this.newValue);
      if (this.valueForm != null) {
        this.oldObject = this.valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = this.valueForm.unit();
        }
      }
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  protected boolean runPhase(SpatialLaneView view, int phase, boolean preemptive) {
    if (phase == 0) {
      if (preemptive) {
        view.laneWillCommand(this.message);
      }
      return view.dispatchWillCommand(this.message.body(), preemptive);
    } else if (phase == 1) {
      final Form keyForm = (Form) view.keyForm;
      if (this.keyForm != keyForm && keyForm != null) {
        this.keyForm = keyForm;
        this.keyObject = keyForm.cast(this.key);
        if (this.keyObject == null) {
          this.keyObject = keyForm.unit();
        }
      }
      final Form valueForm = (Form) view.valueForm;
      if (this.valueForm != valueForm && valueForm != null) {
        this.valueForm = valueForm;
        this.oldObject = valueForm.cast(this.newValue);
        if (this.oldObject == null) {
          this.oldObject = valueForm.unit();
        }
      }
      if (preemptive) {
        this.newObject = ((SpatialLaneView) view)
            .laneWillUpdate(this.keyObject, this.shapeObject, this.oldObject);
      }
      final Map.Entry result = ((SpatialLaneView) view)
          .dispatchWillUpdate(this.link, this.keyObject, this.shapeObject, this.oldObject, preemptive);
      this.newObject = result.getValue();
      if (this.oldObject != this.newObject) {
        this.oldObject = this.newObject;
        this.newValue = valueForm.mold(this.newObject).toValue();
        this.oldValue = this.newValue;
      }
      return result.getKey();
    } else if (phase == 2) {
      final Form keyForm = (Form) view.keyForm;
      if (this.keyForm != keyForm && keyForm != null) {
        this.keyForm = keyForm;
        this.keyObject = keyForm.cast(this.key);
        if (this.keyObject == null) {
          this.keyObject = keyForm.unit();
        }
      }
      final Form valueForm = (Form) view.valueForm;
      if (this.valueForm != valueForm && valueForm != null) {
        this.valueForm = valueForm;
        this.oldObject = valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = valueForm.unit();
        }
        this.newObject = valueForm.cast(this.newValue);
        if (this.newObject == null) {
          this.newObject = valueForm.unit();
        }
      }
      if (preemptive) {
        ((SpatialLaneView) view).laneDidUpdate(this.keyObject, this.shapeObject, this.newObject, this.oldObject);
      }
      return ((SpatialLaneView) view).dispatchDidUpdate(this.link, this.keyObject, this.shapeObject, this.newObject, this.oldObject, preemptive);
    } else if (phase == 3) {
      if (preemptive) {
        view.laneDidCommand(this.message);
      }
      return view.dispatchDidCommand(this.message.body(), preemptive);
    } else {
      throw new AssertionError(); // unreachable
    }
  }

  @Override
  protected void done() {
    this.model.cueDownKey(this.key);
    if (this.cont != null) {
      try {
        this.cont.bind(this.message);
      } catch (Throwable error) {
        if (Cont.isNonFatal(error)) {
          this.cont.trap(error);
        } else {
          throw error;
        }
      }
    }
  }

}

final class SpatialLaneRelayMove extends LaneRelay, SpatialLaneView> {

  final Link link;
  final CommandMessage message;
  final Cont cont;
  final Value key;
  final S oldShapeObject;
  final S newShapeObject;
  Form keyForm;
  Z2Form shapeForm;
  Form valueForm;
  Object keyObject;
  Value oldValue;
  Object oldObject;
  Value newValue;
  Object newObject;

  SpatialLaneRelayMove(SpatialLaneModel model, CommandMessage message, Cont cont,
                       Value key, S oldShapeObject, S newShapeObject, Value newValue) {
    super(model, 4);
    this.link = null;
    this.message = message;
    this.cont = cont;
    this.key = key;
    this.oldShapeObject = oldShapeObject;
    this.newShapeObject = newShapeObject;
    this.newValue = newValue;
  }

  SpatialLaneRelayMove(SpatialLaneModel model, Link link, Value key,
                       S oldShapeObject, S newShapeObject, Value newValue) {
    super(model, 1, 3, null);
    this.link = link;
    this.message = null;
    this.cont = null;
    this.key = key;
    this.oldShapeObject = oldShapeObject;
    this.newShapeObject = newShapeObject;
    this.newValue = newValue;
  }

  SpatialLaneRelayMove(SpatialLaneModel model, Stage stage, Value key,
                       S oldShapeObject, S newShapeObject, Value newValue) {
    super(model, 1, 3, stage);
    this.link = null;
    this.message = null;
    this.cont = null;
    this.key = key;
    this.oldShapeObject = oldShapeObject;
    this.newShapeObject = newShapeObject;
    this.newValue = newValue;
  }

  @Override
  protected void beginPhase(int phase) {
    if (phase == 2) {
      this.oldValue = model.data.move(this.key, this.oldShapeObject, this.newShapeObject, this.newValue);
      if (this.valueForm != null) {
        this.oldObject = this.valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = this.valueForm.unit();
        }
      }
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  protected boolean runPhase(SpatialLaneView view, int phase, boolean preemptive) {
    if (phase == 0) {
      if (preemptive) {
        view.laneWillCommand(this.message);
      }
      return view.dispatchWillCommand(this.message.body(), preemptive);
    } else if (phase == 1) {
      final Form keyForm = (Form) view.keyForm;
      if (this.keyForm != keyForm && keyForm != null) {
        this.keyForm = keyForm;
        this.keyObject = keyForm.cast(this.key);
        if (this.keyObject == null) {
          this.keyObject = keyForm.unit();
        }
      }
      final Form valueForm = (Form) view.valueForm;
      if (this.valueForm != valueForm && valueForm != null) {
        this.valueForm = valueForm;
        this.oldObject = valueForm.cast(this.newValue);
        if (this.oldObject == null) {
          this.oldObject = valueForm.unit();
        }
      }
      if (preemptive) {
        this.newObject = ((SpatialLaneView) view).laneWillMove(this.keyObject, this.newShapeObject, this.oldObject, this.oldShapeObject);
      }
      final Map.Entry result = ((SpatialLaneView) view).dispatchWillMove(this.link, this.keyObject, this.newShapeObject, this.oldObject, this.oldShapeObject, preemptive);
      this.newObject = result.getValue();
      if (this.oldObject != this.newObject) {
        this.oldObject = this.newObject;
        this.newValue = valueForm.mold(this.newObject).toValue();
        this.oldValue = this.newValue;
      }
      return result.getKey();
    } else if (phase == 2) {
      final Form keyForm = (Form) view.keyForm;
      if (this.keyForm != keyForm && keyForm != null) {
        this.keyForm = keyForm;
        this.keyObject = keyForm.cast(this.key);
        if (this.keyObject == null) {
          this.keyObject = keyForm.unit();
        }
      }
      final Form valueForm = (Form) view.valueForm;
      if (this.valueForm != valueForm && valueForm != null) {
        this.valueForm = valueForm;
        this.oldObject = valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = valueForm.unit();
        }
        this.newObject = valueForm.cast(this.newValue);
        if (this.newObject == null) {
          this.newObject = valueForm.unit();
        }
      }
      if (preemptive) {
        ((SpatialLaneView) view).laneDidMove(this.keyObject, this.newShapeObject, this.newObject, this.oldShapeObject, this.oldObject);
      }
      return ((SpatialLaneView) view).dispatchDidMove(this.link, this.keyObject, this.newShapeObject, this.newObject, this.oldShapeObject, this.oldObject, preemptive);
    } else if (phase == 3) {
      if (preemptive) {
        view.laneDidCommand(this.message);
      }
      return view.dispatchDidCommand(this.message.body(), preemptive);
    } else {
      throw new AssertionError(); // unreachable
    }
  }

  @Override
  protected void done() {
    final Record header = Record.create(3).slot("key", this.key)
                                          .slot("from", this.shapeForm.mold(this.oldShapeObject).toValue())
                                          .slot("to", this.shapeForm.mold(this.newShapeObject).toValue());
    this.model.sendDown(Record.create(1).attr("move", header));
    if (this.cont != null) {
      try {
        this.cont.bind(this.message);
      } catch (Throwable error) {
        if (Cont.isNonFatal(error)) {
          this.cont.trap(error);
        } else {
          throw error;
        }
      }
    }
  }

}

final class SpatialLaneRelayRemove extends LaneRelay, SpatialLaneView> {

  final Link link;
  final CommandMessage message;
  final Cont cont;
  final Value key;
  final S shapeObject;
  Form keyForm;
  Z2Form shapeForm;
  Form valueForm;
  Object keyObject;
  Value oldValue;
  Object oldObject;

  SpatialLaneRelayRemove(SpatialLaneModel model, CommandMessage message, Cont cont, Value key, S shapeObject) {
    super(model, 4);
    this.link = null;
    this.message = message;
    this.cont = cont;
    this.key = key;
    this.shapeObject = shapeObject;
  }

  SpatialLaneRelayRemove(SpatialLaneModel model, Link link, Value key, S shapeObject) {
    super(model, 1, 3, null);
    this.link = link;
    this.message = null;
    this.cont = null;
    this.key = key;
    this.shapeObject = shapeObject;
  }

  SpatialLaneRelayRemove(SpatialLaneModel model, Stage stage, Value key, S shapeObject) {
    super(model, 1, 3, stage);
    this.link = null;
    this.message = null;
    this.cont = null;
    this.key = key;
    this.shapeObject = shapeObject;
  }

  @Override
  protected void beginPhase(int phase) {
    if (phase == 2) {
      this.oldValue = model.data.remove(this.key, this.shapeObject);
      if (this.valueForm != null) {
        this.oldObject = this.valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = this.valueForm.unit();
        }
      }
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  protected boolean runPhase(SpatialLaneView view, int phase, boolean preemptive) {
    if (phase == 0) {
      if (preemptive) {
        view.laneWillCommand(this.message);
      }
      return view.dispatchWillCommand(this.message.body(), preemptive);
    } else if (phase == 1) {
      final Form keyForm = (Form) view.keyForm;
      if (this.keyForm != keyForm && keyForm != null) {
        this.keyForm = keyForm;
        this.keyObject = keyForm.cast(this.key);
        if (this.keyObject == null) {
          this.keyObject = keyForm.unit();
        }
      }
      final Form valueForm = (Form) view.valueForm;
      if (this.valueForm != valueForm && valueForm != null) {
        this.valueForm = valueForm;
        this.oldObject = valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = valueForm.unit();
        }
      }
      if (preemptive) {
        ((SpatialLaneView) view).laneWillRemove(this.keyObject, this.shapeObject);
      }
      return ((SpatialLaneView) view).dispatchWillRemove(this.link, this.keyObject, this.shapeObject, preemptive);
    } else if (phase == 2) {
      final Form keyForm = (Form) view.keyForm;
      if (this.keyForm != keyForm && keyForm != null) {
        this.keyForm = keyForm;
        this.keyObject = keyForm.cast(this.key);
        if (this.keyObject == null) {
          this.keyObject = keyForm.unit();
        }
      }
      final Form valueForm = (Form) view.valueForm;
      if (this.valueForm != valueForm && valueForm != null) {
        this.valueForm = valueForm;
        this.oldObject = valueForm.cast(this.oldValue);
        if (this.oldObject == null) {
          this.oldObject = valueForm.unit();
        }
      }
      if (preemptive) {
        ((SpatialLaneView) view).laneDidRemove(this.keyObject, this.shapeObject);
      }
      return ((SpatialLaneView) view).dispatchDidRemove(this.link, this.keyObject, this.shapeObject, this.oldObject, preemptive);
    } else if (phase == 3) {
      if (preemptive) {
        view.laneDidCommand(this.message);
      }
      return view.dispatchDidCommand(this.message.body(), preemptive);
    } else {
      throw new AssertionError(); // unreachable
    }
  }

  @Override
  protected void done() {
    final Record header = Record.create(2).slot("key", this.key).slot("shape", this.shapeForm.mold(this.shapeObject).toValue());
    this.model.sendDown(Record.create(1).attr("move", header));
    if (this.cont != null) {
      try {
        this.cont.bind(this.message);
      } catch (Throwable error) {
        if (Cont.isNonFatal(error)) {
          this.cont.trap(error);
        } else {
          throw error;
        }
      }
    }
  }

}

final class SpatialLaneRelayClear extends LaneRelay, SpatialLaneView> {

  final Link link;
  final CommandMessage message;
  final Cont cont;

  SpatialLaneRelayClear(SpatialLaneModel model, CommandMessage message, Cont cont) {
    super(model, 4);
    this.link = null;
    this.message = message;
    this.cont = cont;
  }

  SpatialLaneRelayClear(SpatialLaneModel model, Link link) {
    super(model, 1, 3, null);
    this.link = link;
    this.message = null;
    this.cont = null;
  }

  SpatialLaneRelayClear(SpatialLaneModel model, Stage stage) {
    super(model, 1, 3, stage);
    this.link = null;
    this.message = null;
    this.cont = null;
  }

  @Override
  protected void beginPhase(int phase) {
    if (phase == 2) {
      this.model.data.clear();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  protected boolean runPhase(SpatialLaneView view, int phase, boolean preemptive) {
    if (phase == 0) {
      if (preemptive) {
        view.laneWillCommand(this.message);
      }
      return view.dispatchWillCommand(this.message.body(), preemptive);
    } else if (phase == 1) {
      if (preemptive) {
        view.laneWillClear();
      }
      return view.dispatchWillClear(this.link, preemptive);
    } else if (phase == 2) {
      if (preemptive) {
        view.laneDidClear();
      }
      return view.dispatchDidClear(this.link, preemptive);
    } else if (phase == 3) {
      if (preemptive) {
        view.laneDidCommand(this.message);
      }
      return view.dispatchDidCommand(this.message.body(), preemptive);
    } else {
      throw new AssertionError(); // unreachable
    }
  }

  @Override
  protected void done() {
    this.model.sendDown(Record.create(1).attr("clear"));
    if (this.cont != null) {
      try {
        this.cont.bind(this.message);
      } catch (Throwable error) {
        if (Cont.isNonFatal(error)) {
          this.cont.trap(error);
        } else {
          throw error;
        }
      }
    }
  }

}