Skip to content

Commit

Permalink
refactor(core/EdgeValue.java): update EdgeValue implementation to use…
Browse files Browse the repository at this point in the history
… record and add withTargetIdUpdated method

- Adding a new method, `withTargetIdUpdated`, which updates the `id` field while copying other values or modifying mappings based on the input function.

work on #73
  • Loading branch information
bsorrentino committed Feb 5, 2025
1 parent 4971ad4 commit 9891961
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions core/src/main/java/org/bsc/langgraph4j/EdgeValue.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package org.bsc.langgraph4j;

import lombok.Value;
import lombok.experimental.Accessors;
import org.bsc.langgraph4j.state.AgentState;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Value
@Accessors(fluent = true)
public class EdgeValue<State extends AgentState> {
/**
*
* @param <State>
* @param id The unique identifier for the edge value.
* @param value The condition associated with the edge value.
*/
record EdgeValue<State extends AgentState>( String id, EdgeCondition<State> value) {

/**
* The unique identifier for the edge value.
*/
String id;
EdgeValue<State> withTargetIdUpdated(String from, Function<String, String> targetId) {
if( id != null ) {
// if(Objects.equals( from, id ) ) {
// return this;
// }
return new EdgeValue<>( targetId.apply(id), null );
}

/**
* The condition associated with the edge value.
*/
EdgeCondition<State> value;
if( !value.mappings().containsValue(from) ) {
return this;
}

var newMappings = value.mappings().entrySet().stream()
.map( e ->
Objects.equals( e.getValue(), from ) ?
new AbstractMap.SimpleEntry<>(e.getKey(), targetId.apply(from) ) : e
)
.collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ));

return new EdgeValue<>(null, new EdgeCondition<>( value.action(), newMappings));

}
}

0 comments on commit 9891961

Please sign in to comment.