MCPcopy
hub / github.com/apache/caldera / save_fact

Method save_fact

app/objects/secondclass/c_link.py:272–308  ·  view source on GitHub ↗
(self, operation, fact, score, relationship)

Source from the content-addressed store, hash-verified

270 self.relationships.append(relationship)
271
272 async def save_fact(self, operation, fact, score, relationship):
273 knowledge_svc_handle = BaseService.get_service('knowledge_svc')
274 all_facts = await operation.all_facts() if operation else self.facts
275 source = operation.id if operation else self.id
276 rl = [relationship] if relationship else []
277 if all([fact.trait, fact.value]):
278 if operation and operation.source:
279 if any([(fact.trait, fact.value) == (x.trait, x.value) for x in
280 await knowledge_svc_handle.get_facts(criteria=dict(source=operation.source.id))]):
281 source = operation.source.id
282 fact.source = source # Manual addition to ensure the check works correctly
283 if not await knowledge_svc_handle.check_fact_exists(fact, all_facts):
284 f_gen = Fact(trait=fact.trait, value=fact.value, source=source, score=score, collected_by=[self.paw],
285 technique_id=self.ability.technique_id, links=[self.id], relationships=rl,
286 origin_type=OriginType.LEARNED)
287 self.facts.append(f_gen)
288 await knowledge_svc_handle.add_fact(f_gen)
289 else:
290 existing_fact = (await knowledge_svc_handle.get_facts(criteria=dict(trait=fact.trait,
291 value=fact.value,
292 source=fact.source)))[0]
293 if self.id not in existing_fact.links:
294 existing_fact.links.append(self.id)
295 if relationship not in existing_fact.relationships:
296 existing_fact.relationships.append(relationship)
297 if self.paw not in existing_fact.collected_by and existing_fact not in self.used:
298 existing_fact.collected_by.append(self.paw)
299 await knowledge_svc_handle.update_fact(criteria=dict(trait=fact.trait, value=fact.value,
300 source=fact.source),
301 updates=dict(links=existing_fact.links,
302 relationships=existing_fact.relationships,
303 collected_by=existing_fact.collected_by))
304 existing_local_record = [x for x in self.facts if x.trait == fact.trait and x.value == fact.value]
305 if existing_local_record:
306 existing_local_record[0].links = existing_fact.links
307 else:
308 self.facts.append(existing_fact)
309
310
311async def update_scores(operation, increment, used, facts):

Calls 7

FactClass · 0.90
get_serviceMethod · 0.80
all_factsMethod · 0.45
get_factsMethod · 0.45
check_fact_existsMethod · 0.45
add_factMethod · 0.45
update_factMethod · 0.45