@@ -364,6 +364,12 @@ def _mutate_merge_var_helper(vars_):
364
364
365
365
366
366
def mutate_merge_var_mix (child ):
367
+ """Merges two variables into one.
368
+
369
+ Considers both node variables and edge variables together.
370
+ It is possible to merge an edge and a node too.
371
+ Randomly chooses a variable to replace and a variable to merge into.
372
+ """
367
373
vars_ = child .vars_in_graph
368
374
rand_vars , merge_able_vars = _mutate_merge_var_helper (vars_ )
369
375
@@ -377,6 +383,12 @@ def mutate_merge_var_mix(child):
377
383
378
384
379
385
def mutate_merge_var_sep (child ):
386
+ """Merges two variables into one.
387
+
388
+ Considers the node variables and edge variables separately.
389
+ Either merges 2 node variables or 2 edge variable, depending on a random
390
+ choice.Randomly chooses a variable to replace and a variable to merge into.
391
+ """
380
392
node_vars = {n for n in child .nodes if isinstance (n , Variable )}
381
393
rand_node_vars , merge_able_node_vars = _mutate_merge_var_helper (node_vars )
382
394
@@ -414,6 +426,13 @@ def mutate_del_triple(child):
414
426
415
427
416
428
def mutate_expand_node (child , pb_en_out_link ):
429
+ """Expands a random node of the pattern by adding a new triple to it.
430
+
431
+ The variables to be attached to this node, to form a triple, are chosen
432
+ randomly.Depending on the probability, makes it an outgoing edge or an
433
+ incoming edge.
434
+ :return: The modified child, with the added triple.
435
+ """
417
436
# TODO: can maybe be improved by sparqling
418
437
nodes = list (child .nodes )
419
438
node = random .choice (nodes )
@@ -427,6 +446,11 @@ def mutate_expand_node(child, pb_en_out_link):
427
446
428
447
429
448
def mutate_add_edge (child ):
449
+ """Chooses any 2 nodes from the pattern, and adds an edge between them.
450
+
451
+ The edge is labeled with a new randomly chosen variable.
452
+ :return: Modified child, with the new edge
453
+ """
430
454
# TODO: can maybe be improved by sparqling
431
455
nodes = list (child .nodes )
432
456
if len (nodes ) < 2 :
@@ -438,6 +462,12 @@ def mutate_add_edge(child):
438
462
439
463
440
464
def mutate_increase_dist (child ):
465
+ """increases distance between source and target by one hop.
466
+
467
+ Adds a triple, to either the source var or the target var.
468
+ Interchange the new node with source/target variable to increase distance.
469
+ :return: The modified child, with the new triple.
470
+ """
441
471
if not child .complete ():
442
472
return child
443
473
var_node = gen_random_var ()
@@ -497,6 +527,10 @@ def mutate_fix_var(
497
527
sample_n = config .MUTPB_FV_SAMPLE_MAXN ,
498
528
limit = config .MUTPB_FV_QUERY_LIMIT ,
499
529
):
530
+ """Chooses a random variable from the pattern(node or edge).
531
+
532
+ Substitutes it with all possible fixed variables.
533
+ """
500
534
assert isinstance (child , GraphPattern )
501
535
assert isinstance (gtp_scores , GTPScores )
502
536
0 commit comments