55 UniqueIdProperty ,
66 Relationship ,
77 RelationshipTo ,
8+ RelationshipFrom ,
89 ArrayProperty ,
910 StructuredRel ,
1011 db ,
@@ -193,17 +194,24 @@ class NeoDocument(StructuredNode):
193194 related = Relationship ("NeoDocument" , "RELATED" , model = RelatedRel )
194195
195196 @classmethod
196- def to_cre_def (self , node ):
197+ def to_cre_def (self , node , parse_links = True ):
197198 raise Exception (f"Shouldn't be parsing a NeoDocument" )
198199
200+ @classmethod
201+ def get_links (self , links_dict ):
202+ links = []
203+ for key in links_dict :
204+ links .extend ([cre_defs .Link (c .to_cre_def (c , parse_links = False ), key ) for c in links_dict [key ]])
205+ return links
206+
199207
200208class NeoNode (NeoDocument ):
201209 doctype = StringProperty ()
202210 version = StringProperty (required = True )
203211 hyperlink = StringProperty ()
204212
205213 @classmethod
206- def to_cre_def (self , node ):
214+ def to_cre_def (self , node , parse_links = True ):
207215 raise Exception (f"Shouldn't be parsing a NeoNode" )
208216
209217
@@ -213,7 +221,7 @@ class NeoStandard(NeoNode):
213221 section_id = StringProperty ()
214222
215223 @classmethod
216- def to_cre_def (self , node ) -> cre_defs .Standard :
224+ def to_cre_def (self , node , parse_links = True ) -> cre_defs .Standard :
217225 return cre_defs .Standard (
218226 name = node .name ,
219227 id = node .document_id ,
@@ -231,7 +239,7 @@ class NeoTool(NeoStandard):
231239 tooltype = StringProperty (required = True )
232240
233241 @classmethod
234- def to_cre_def (self , node ) -> cre_defs .Tool :
242+ def to_cre_def (self , node , parse_links = True ) -> cre_defs .Tool :
235243 return cre_defs .Tool (
236244 name = node .name ,
237245 id = node .document_id ,
@@ -247,7 +255,7 @@ def to_cre_def(self, node) -> cre_defs.Tool:
247255
248256class NeoCode (NeoNode ):
249257 @classmethod
250- def to_cre_def (self , node ) -> cre_defs .Code :
258+ def to_cre_def (self , node , parse_links = True ) -> cre_defs .Code :
251259 return cre_defs .Code (
252260 name = node .name ,
253261 id = node .document_id ,
@@ -260,17 +268,24 @@ def to_cre_def(self, node) -> cre_defs.Code:
260268
261269class NeoCRE (NeoDocument ): # type: ignore
262270 external_id = StringProperty ()
271+ contained_in = RelationshipFrom ('NeoCRE' , 'CONTAINS' , model = ContainsRel )
263272 contains = RelationshipTo ("NeoCRE" , "CONTAINS" , model = ContainsRel )
264273 linked = RelationshipTo ("NeoStandard" , "LINKED_TO" , model = LinkedToRel )
265274 same_as = RelationshipTo ("NeoStandard" , "SAME" , model = SameRel )
266275
267276 @classmethod
268- def to_cre_def (self , node ) -> cre_defs .CRE :
277+ def to_cre_def (self , node , parse_links = True ) -> cre_defs .CRE :
269278 return cre_defs .CRE (
270279 name = node .name ,
271280 id = node .document_id ,
272281 description = node .description ,
273282 tags = node .tags ,
283+ links = self .get_links ({
284+ 'Contains' : [* node .contains , * node .contained_in ],
285+ 'Linked To' : node .linked ,
286+ 'Same as' : node .same_as ,
287+ 'Related' : node .related
288+ }) if parse_links else []
274289 )
275290
276291
@@ -464,19 +479,19 @@ def format_segment(seg: StructuredRel, nodes):
464479 ][0 ]
465480
466481 return {
467- "start" : NEO_DB .parse_node (start_node ),
468- "end" : NEO_DB .parse_node (end_node ),
482+ "start" : NEO_DB .parse_node_no_links (start_node ),
483+ "end" : NEO_DB .parse_node_no_links (end_node ),
469484 "relationship" : relation_map [type (seg )],
470485 }
471486
472487 def format_path_record (rec ):
473488 return {
474- "start" : NEO_DB .parse_node (rec .start_node ),
475- "end" : NEO_DB .parse_node (rec .end_node ),
489+ "start" : NEO_DB .parse_node_no_links (rec .start_node ),
490+ "end" : NEO_DB .parse_node_no_links (rec .end_node ),
476491 "path" : [format_segment (seg , rec .nodes ) for seg in rec .relationships ],
477492 }
478493
479- return [NEO_DB .parse_node (rec ) for rec in base_standard ], [
494+ return [NEO_DB .parse_node_no_links (rec ) for rec in base_standard ], [
480495 format_path_record (rec [0 ]) for rec in (path_records + path_records_all )
481496 ]
482497
@@ -490,6 +505,10 @@ def standards(self) -> List[str]:
490505 @staticmethod
491506 def parse_node (node : NeoDocument ) -> cre_defs .Document :
492507 return node .to_cre_def (node )
508+
509+ @staticmethod
510+ def parse_node_no_links (node : NeoDocument ) -> cre_defs .Document :
511+ return node .to_cre_def (node , parse_links = False )
493512
494513
495514class CRE_Graph :
@@ -1514,30 +1533,8 @@ def text_search(self, text: str) -> List[Optional[cre_defs.Document]]:
15141533
15151534 def get_root_cres (self ):
15161535 """Returns CRES that only have "Contains" links"""
1517- linked_groups = aliased (InternalLinks )
1518- linked_cres = aliased (InternalLinks )
1519- cres = (
1520- self .session .query (CRE )
1521- .filter (
1522- ~ CRE .id .in_ (
1523- self .session .query (InternalLinks .cre ).filter (
1524- InternalLinks .type == cre_defs .LinkTypes .Contains ,
1525- )
1526- )
1527- )
1528- .filter (
1529- ~ CRE .id .in_ (
1530- self .session .query (InternalLinks .group ).filter (
1531- InternalLinks .type == cre_defs .LinkTypes .PartOf ,
1532- )
1533- )
1534- )
1535- .all ()
1536- )
1537- result = []
1538- for c in cres :
1539- result .extend (self .get_CREs (external_id = c .external_id ))
1540- return result
1536+ neo_cres = NeoCRE .nodes .has (contained_in = False )
1537+ return [c .to_cre_def (c ) for c in neo_cres ]
15411538
15421539 def get_embeddings_by_doc_type (self , doc_type : str ) -> Dict [str , List [float ]]:
15431540 res = {}
0 commit comments