Skip to content

Commit

Permalink
feat(core/Edge.java): add methods for target ID matching and source/t…
Browse files Browse the repository at this point in the history
…arget id updates

- Added `anyMatchByTargetId` method to check if there is a target with a specific ID by comparing IDs or value mappings.
- Created `withSourceAndTargetIdsUpdated` method to update both the source and target IDs in an Edge, leveraging a Node and provided functions for new IDs.

work on #73
  • Loading branch information
bsorrentino committed Feb 5, 2025
1 parent f86c519 commit 4971ad4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/src/main/java/org/bsc/langgraph4j/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public EdgeValue<State> target() {
return targets.get(0);
}

public boolean anyMatchByTargetId( String targetId ) {
return targets().stream().anyMatch(v ->
( v.id() != null ) ?
Objects.equals( v.id(), targetId ) :
v.value().mappings().containsValue( targetId )

);
}

public Edge<State> withSourceAndTargetIdsUpdated(Node<State> node,
Function<String,String> newSourceId,
Function<String,String> newTargetId ) {

var newTargets = targets().stream()
.map( t -> t.withTargetIdUpdated( node.id(), newTargetId ))
.toList();
return new Edge<>( newSourceId.apply(sourceId), newTargets);

}

public void validate( @NonNull Collection<Node<State>> nodes) throws GraphStateException {
if ( !Objects.equals(sourceId(),START) && !nodes.contains(new Node<State>(sourceId()))) {
throw StateGraph.Errors.missingNodeReferencedByEdge.exception(sourceId());
Expand Down

0 comments on commit 4971ad4

Please sign in to comment.