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

com.weaverplatform.sp4rql.model.restriction.path.PathNode Maven / Gradle / Ivy

package com.weaverplatform.sp4rql.model.restriction.path;

import com.weaverplatform.sp4rql.model.token.ReferenceToken;
import com.weaverplatform.util.IndentStringBuilder;

import java.util.HashSet;

public class PathNode implements IndentStringBuilder.Serializable  {

  private boolean start = false;
  private boolean end = false;

  private HashSet next = new HashSet<>();
  private HashSet previous = new HashSet<>();

  public void addNext(ReferenceToken token, PathNode node) {
    PathRecord record = new PathRecord(this, node, false, false);
    record.addKey(token);
    next.add(record);
    node.previous.add(record.routeBack());
  }

  public void addNextReversed(ReferenceToken token, PathNode node) {
    PathRecord record = new PathRecord(this, node, true, false);
    record.addKey(token);
    next.add(record);
    node.previous.add(record.routeBack());
  }

  public void addNextNot(ReferenceToken token, PathNode node) {
    PathRecord record = new PathRecord(this, node, false, true);
    record.addKey(token);
    next.add(record);
    node.previous.add(record.routeBack());
  }

  public void addNextReversedNot(ReferenceToken token, PathNode node) {
    PathRecord record = new PathRecord(this, node, true, true);
    record.addKey(token);
    next.add(record);
    node.previous.add(record.routeBack());
  }

  public HashSet getNext() {
    return next;
  }

  public HashSet getPrevious() {
    return previous;
  }

  public void start() {
    this.start = true;
  }

  public boolean isStart() {
    return start;
  }

  public void end() {
    this.end = true;
  }

  public boolean isEnd() {
    return end;
  }

  @Override
  public void buildString(IndentStringBuilder builder) {
    toString(builder, new HashSet<>());
  }

  public void toString(IndentStringBuilder builder, HashSet visited) {
    if (isStart()) {
      builder.append("->");
    }
    if (!visited.contains(this)) {
      builder.append(String.format("(%d)", this.hashCode()));
    } else {
      builder.append(String.format("(%d@)", this.hashCode()));
    }
    if (isEnd()) {
      builder.append("X");
    }

    if (!visited.contains(this)) {
      visited.add(this);
      for(PathRecord record : next) {
        for(ReferenceToken token : record.keys) {
          builder.newline(1);
          if(record.negated) {
            builder.append("!");
          }
          if(record.reversed) {
            builder.append("^");
          }
          token.buildString(builder);
          builder.newline(1);
          record.to.toString(builder, visited);
          builder.updateLevel(-1);
          builder.updateLevel(-1);
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy