depth aware graph generator

This commit is contained in:
2023-12-08 19:59:19 +02:00
parent ba25850191
commit ce587387c5
+9 -46
View File
@@ -27,6 +27,7 @@ final class Graph
* */ * */
public function getRootRecords(): Collection public function getRootRecords(): Collection
{ {
return $this->records->where("isRoot", true)->values(); return $this->records->where("isRoot", true)->values();
} }
@@ -35,58 +36,19 @@ final class Graph
return !empty($this->records); return !empty($this->records);
} }
// public function getRootRecordsWithChildren(): Collection
// {
// $rootRecords = $this->records->where("isRoot", true)->values();
// return $rootRecords->map([$this, 'findChildren']);
// }
//
// public function findChildren(QueryRecord $record): QueryRecord
// {
// $recordEdges = $this->edges
// ->where("source", $record->id)
// ->values()
// ->sortBy("rank")
// ->groupBy("field")->toArray();
//
//
// foreach ($recordEdges as $field => $edges) {
// $recordEdges[$field] = [];
// foreach ($edges as $anEdge) {
// $aRecord = $this->records->where("id", $anEdge->target)->first();
// if (empty($aRecord)) {
// continue;
// }
// $recordEdges[$field][] = $this->findChildren($aRecord);
// }
// }
//
// $record->_children = new Collection($recordEdges);
// return $record;
// }
public function tree(): Collection public function tree(): Collection
{ {
return $this->getRootRecords() return $this->getRootRecords()
->map([$this, 'findParents']) ->map([$this, 'findParents'])
->map([$this, 'findChildren']) ->map([$this, 'findChildren'])
; ;
return $rootRecords;
return $this->records->filter(function (QueryRecord $record) {
return $this->edges->filter(fn(Edge $ed) => $ed->target == $record->id)->isEmpty();
})->values()
->map([$this, 'findChildren']);
} }
public function findChildren(QueryRecord $record): QueryRecord public function findChildren(QueryRecord $record, int $depth = 1): QueryRecord
{ {
$recordEdges = $this->edges->filter(fn(Edge $ed) => $ed->source === $record->id) $recordEdges = $this->edges->filter(fn(Edge $ed) => $ed->source === $record->id)
->unique(fn(Edge $ed) => $ed->targetSchema.$ed->field.$ed->target.$ed->source) ->unique(fn(Edge $ed) => $ed->targetSchema.$ed->field.$ed->target.$ed->source.$ed->depth)
->where("depth", $depth)
->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); ->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values();
$groupRecordEdges = []; $groupRecordEdges = [];
@@ -103,7 +65,7 @@ final class Graph
continue; continue;
} }
$children[$field][] = $this->findChildren($aRecord[0]); $children[$field][] = $this->findChildren($aRecord[0], $depth + 1);
} }
} }
$record->_children = $children; $record->_children = $children;
@@ -111,9 +73,10 @@ final class Graph
} }
public function findParents(QueryRecord $record): QueryRecord public function findParents(QueryRecord $record, int $depth = 1): QueryRecord
{ {
$recordEdges = $this->parentEdges->filter(fn(Edge $ed) => $ed->target === $record->id)->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values();
$recordEdges = $this->parentEdges->filter(fn(Edge $ed) => $ed->target === $record->id)->where("depth", $depth)->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values();
$groupRecordEdges = []; $groupRecordEdges = [];
foreach ($recordEdges as $element) { foreach ($recordEdges as $element) {
@@ -129,7 +92,7 @@ final class Graph
continue; continue;
} }
$parents[$field][] = $this->findParents($aRecord[0]); $parents[$field][] = $this->findParents($aRecord[0],$depth + 1);
} }
} }
$record->_parents = $parents; $record->_parents = $parents;