createEntities adds new entities to the graph, skipping duplicates by name. It returns the new entities that were actually added.
(entities []Entity)
| 184 | // createEntities adds new entities to the graph, skipping duplicates by name. |
| 185 | // It returns the new entities that were actually added. |
| 186 | func (k knowledgeBase) createEntities(entities []Entity) ([]Entity, error) { |
| 187 | graph, err := k.loadGraph() |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | |
| 192 | var newEntities []Entity |
| 193 | for _, entity := range entities { |
| 194 | if !slices.ContainsFunc(graph.Entities, func(e Entity) bool { return e.Name == entity.Name }) { |
| 195 | newEntities = append(newEntities, entity) |
| 196 | graph.Entities = append(graph.Entities, entity) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if err := k.saveGraph(graph); err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | |
| 204 | return newEntities, nil |
| 205 | } |
| 206 | |
| 207 | // createRelations adds new relations to the graph, skipping exact duplicates. |
| 208 | // It returns the new relations that were actually added. |