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

com.github.dakusui.jcunit8.factorspace.fsm.Edge Maven / Gradle / Ivy

package com.github.dakusui.jcunit8.factorspace.fsm;

import com.github.dakusui.jcunit.fsm.*;

public class Edge implements Stimulus {
  public final State  from;
  public final Action action;
  public final Args        args;
  public final State  to;

  Edge(State from, Action action, Args args, State to) {
    this.from = from;
    this.action = action;
    this.args = args;
    this.to = to;
  }

  public boolean isPossible() {
    return action.parameters().size() == args.size() &&
        from.expectation(action, args).state.equals(to);
  }

  public boolean isValid() {
    return from.expectation(action, args).getType() == OutputType.VALUE_RETURNED;
  }

  @Override
  public void accept(Player player) {
    player.visit(this);
  }

  @Override
  public String toString() {
    return String.format("%s.%s(%s)->%s", this.from, this.action, this.args, this.to);
  }

  public static class Builder {
    private final State  from;
    private       Action action;
    private       Args        args;
    private       State  to;

    Builder(State from) {
      this.from = from;
    }

    public static  Builder from(State from) {
      return new Builder<>(from);
    }

    public Builder with(Action action, Args args) {
      this.action = action;
      this.args = args;
      return this;
    }

    public Builder to(State to) {
      this.to = to;
      return this;
    }

    public Edge build() {
      assert action.numParameterFactors() == args.size();
      if (!to.equals(from.expectation(action, args).state)) {
        throw new RuntimeException();
      }
      return new Edge<>(from, action, args, to);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy