createRelations adds new relations to the graph, skipping exact duplicates. It returns the new relations that were actually added.
(relations []Relation)
| 207 | // createRelations adds new relations to the graph, skipping exact duplicates. |
| 208 | // It returns the new relations that were actually added. |
| 209 | func (k knowledgeBase) createRelations(relations []Relation) ([]Relation, error) { |
| 210 | graph, err := k.loadGraph() |
| 211 | if err != nil { |
| 212 | return nil, err |
| 213 | } |
| 214 | |
| 215 | var newRelations []Relation |
| 216 | for _, relation := range relations { |
| 217 | exists := slices.ContainsFunc(graph.Relations, func(r Relation) bool { |
| 218 | return r.From == relation.From && |
| 219 | r.To == relation.To && |
| 220 | r.RelationType == relation.RelationType |
| 221 | }) |
| 222 | if !exists { |
| 223 | newRelations = append(newRelations, relation) |
| 224 | graph.Relations = append(graph.Relations, relation) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if err := k.saveGraph(graph); err != nil { |
| 229 | return nil, err |
| 230 | } |
| 231 | |
| 232 | return newRelations, nil |
| 233 | } |
| 234 | |
| 235 | // addObservations appends new observations to existing entities. |
| 236 | // It returns the new observations that were actually added. |