@@ -441,24 +441,38 @@ def mutate_del_triple(child):
441
441
return new_child
442
442
443
443
444
- def mutate_expand_node (child , pb_en_out_link ):
445
- """Expands a random node by adding a new var-only triple to it.
446
-
447
- Randomly selects a node. Then (depending on the probability pb_en_out_link)
448
- adds an outgoing or incoming triple with two new vars to it.
444
+ def _mutate_expand_node_helper (node , pb_en_out_link = config .MUTPB_EN_OUT_LINK ):
445
+ """Adds a new var-only triple to node.
449
446
450
- :arg pb_en_out_link: Probability to create an outgoing triple.
451
- :return: A child with the added outgoing/incoming triple.
447
+ :param pb_en_out_link: Probability to create an outgoing triple.
448
+ :return: The new triple, node and var
452
449
"""
453
- # TODO: can maybe be improved by sparqling
454
- nodes = list (child .nodes )
455
- node = random .choice (nodes )
456
450
var_edge = gen_random_var ()
457
451
var_node = gen_random_var ()
458
452
if random .random () < pb_en_out_link :
459
453
new_triple = (node , var_edge , var_node )
460
454
else :
461
455
new_triple = (var_node , var_edge , node )
456
+ return new_triple , var_node , var_edge
457
+
458
+
459
+ def mutate_expand_node (
460
+ child , node = None , pb_en_out_link = config .MUTPB_EN_OUT_LINK ):
461
+ """Expands a random node by adding a new var-only triple to it.
462
+
463
+ Randomly selects a node. Then adds an outgoing or incoming triple with two
464
+ new vars to it.
465
+
466
+ :param child: The GraphPattern to expand a node in.
467
+ :param node: If given the node to expand, otherwise
468
+ :param pb_en_out_link: Probability to create an outgoing triple.
469
+ :return: A child with the added outgoing/incoming triple.
470
+ """
471
+ # TODO: can maybe be improved by sparqling
472
+ if not node :
473
+ nodes = list (child .nodes )
474
+ node = random .choice (nodes )
475
+ new_triple , _ , _ = _mutate_expand_node_helper (node , pb_en_out_link )
462
476
return child + (new_triple ,)
463
477
464
478
@@ -742,7 +756,6 @@ def mutate(
742
756
pb_ae = config .MUTPB_AE ,
743
757
pb_dt = config .MUTPB_DT ,
744
758
pb_en = config .MUTPB_EN ,
745
- pb_en_out_link = config .MUTPB_EN_OUT_LINK ,
746
759
pb_fv = config .MUTPB_FV ,
747
760
pb_id = config .MUTPB_ID ,
748
761
pb_iv = config .MUTPB_IV ,
@@ -773,7 +786,7 @@ def mutate(
773
786
child = mutate_del_triple (child )
774
787
775
788
if random .random () < pb_en :
776
- child = mutate_expand_node (child , pb_en_out_link )
789
+ child = mutate_expand_node (child )
777
790
if random .random () < pb_ae :
778
791
child = mutate_add_edge (child )
779
792
0 commit comments