From f6eca2e3318519a716e5fbc5bd9cf287ac852cfa Mon Sep 17 00:00:00 2001
From: Peter Powers <pmpowers@usgs.gov>
Date: Mon, 23 May 2022 10:49:44 -0600
Subject: [PATCH] added path to source tree

---
 .../earthquake/nshmp/model/ModelLoader.java   |  8 ++++
 .../earthquake/nshmp/model/SourceTree.java    | 39 +++++++++----------
 2 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/src/main/java/gov/usgs/earthquake/nshmp/model/ModelLoader.java b/src/main/java/gov/usgs/earthquake/nshmp/model/ModelLoader.java
index ac86bf7d..a99e23e9 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/model/ModelLoader.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/model/ModelLoader.java
@@ -363,6 +363,7 @@ abstract class ModelLoader {
       System.out.println("    tree: [" + feature.id + "] " + root.relativize(geojson));
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(geojson)
           .name(feature.name)
           .id(feature.id)
           .setting(data.tectonicSetting())
@@ -532,6 +533,7 @@ abstract class ModelLoader {
       }
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(dir)
           .name(name)
           .id(info.id)
           .setting(data.tectonicSetting())
@@ -751,6 +753,7 @@ abstract class ModelLoader {
       data.interfaceFeatureMap(readInterfaceFeatures(dir).orElseThrow());
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(dir)
           .name(name)
           .id(info.id)
           .setting(data.tectonicSetting())
@@ -853,6 +856,7 @@ abstract class ModelLoader {
       data.interfaceFeatureMap(readInterfaceFeatures(dir).orElseThrow());
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(dir)
           .name(name)
           .id(info.id)
           .setting(data.tectonicSetting())
@@ -958,6 +962,7 @@ abstract class ModelLoader {
       System.out.println("    tree: [" + info.id + "] " + name);
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(dir)
           .name(name)
           .id(info.id)
           .setting(data.tectonicSetting())
@@ -1084,6 +1089,7 @@ abstract class ModelLoader {
       System.out.println("    tree: [" + info.id + "] " + root.relativize(dir));
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(dir)
           .name(name)
           .id(info.id)
           .setting(data.tectonicSetting())
@@ -1206,6 +1212,7 @@ abstract class ModelLoader {
           Path.of(feature.name));
 
       SourceTree tree = SourceTree.builder()
+          .path(geojson.getParent())
           .name(feature.name)
           .id(feature.id)
           .setting(data.tectonicSetting())
@@ -1230,6 +1237,7 @@ abstract class ModelLoader {
       System.out.println("    tree: [" + info.id + "] " + root.relativize(dir));
 
       SourceTree.Builder treeBuilder = SourceTree.builder()
+          .path(dir)
           .name(name)
           .id(info.id)
           .setting(data.tectonicSetting())
diff --git a/src/main/java/gov/usgs/earthquake/nshmp/model/SourceTree.java b/src/main/java/gov/usgs/earthquake/nshmp/model/SourceTree.java
index c0c7fb1e..91da5593 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/model/SourceTree.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/model/SourceTree.java
@@ -23,9 +23,7 @@ import com.google.common.base.Strings;
 import com.google.common.collect.Iterables;
 import com.google.common.graph.EndpointPair;
 import com.google.common.graph.Graphs;
-import com.google.common.graph.ImmutableNetwork;
 import com.google.common.graph.MutableNetwork;
-import com.google.common.graph.Network;
 import com.google.common.graph.NetworkBuilder;
 import com.google.common.graph.Traverser;
 
@@ -56,6 +54,7 @@ public class SourceTree extends
    * only one incident edge. Leaf weights are rounded to 8 decimal places.
    */
 
+  private final Path path;
   private final String name;
   private final int id;
   private final TectonicSetting setting;
@@ -65,12 +64,10 @@ public class SourceTree extends
   private final Map<Leaf, List<Branch<Path>>> branches;
   private final Map<Leaf, Double> weights;
   private final List<Leaf> leaves;
-  // private final Map<String, Leaf> leafIds;
-
-  // not currently used directly
-  private final Network<Node, Branch<Path>> tree;
+  // private final Network<Node, Branch<Path>> tree;
 
   SourceTree(Builder builder) {
+    this.path = builder.path;
     this.name = builder.name;
     this.id = builder.id;
     this.setting = builder.setting;
@@ -80,17 +77,14 @@ public class SourceTree extends
     this.branches = builder.branches;
     this.weights = builder.leafWeights;
     this.leaves = builder.leaves;
-    // this.leafIds = builder.leafIds;
-    this.tree = ImmutableNetwork.copyOf(builder.tree);
-
-    // List<String> branchStrs = branches.keySet().stream()
-    // .map(this::branchString)
-    // .sorted()
-    // .collect(toList());
-    // branchStrs.forEach(System.out::println);
+    // this.tree = ImmutableNetwork.copyOf(builder.tree);
+  }
+
+  /** The model path to this tree. */
+  public Path path() {
+    return path;
   }
 
-  /** The name of this tree. */
   @Override
   public String name() {
     return name;
@@ -174,7 +168,6 @@ public class SourceTree extends
 
   @Override
   public String toString() {
-    // nodes().toString() + NEWLINE +
     return this.stream()
         .sorted((b1, b2) -> b1.value().name().compareTo(b2.value().name()))
         .map(branch -> "  ↳         " +
@@ -197,6 +190,7 @@ public class SourceTree extends
     private int index = 1;
 
     /* Required fields. */
+    private Path path;
     private String name;
     private Integer id;
     private TectonicSetting setting;
@@ -211,10 +205,16 @@ public class SourceTree extends
     private Map<Leaf, List<Branch<Path>>> branches;
     private Map<Leaf, Double> leafWeights;
     private List<Leaf> leaves;
-    // private Map<String, Leaf> leafIds;
 
     private Builder() {}
 
+    /* Set the model path to the root of the tree. */
+    Builder path(Path path) {
+      checkState(this.path == null, "Tree path already set");
+      this.path = checkNotNull(path);
+      return this;
+    }
+
     /* Set the name of the tree. */
     Builder name(String name) {
       checkState(this.name == null, "Tree name already set");
@@ -295,6 +295,7 @@ public class SourceTree extends
 
     /* Create a new source tree from this builder. */
     SourceTree build() {
+      checkNotNull(path);
       checkNotNull(name);
       checkNotNull(id);
       checkNotNull(setting);
@@ -348,10 +349,6 @@ public class SourceTree extends
               Entry::getKey,
               e -> leafWeight(e.getValue())));
       leaves = List.copyOf(branches.keySet());
-      // leafIds = leaves.stream().collect(
-      // toUnmodifiableMap(
-      // Leaf::toString,
-      // leaf -> leaf));
 
       /*
        * Assign weights to RuptureSets. It would be nice if a RuptureSet didn't
-- 
GitLab