Comments

clemens.tolboom’s picture

naught101’s picture

Absolutely, this would be great. Potentially we'd need to patch devel_generate (See #616160: Extending devel generate), but even then, relations are more complex than the average entity, as they rely on other entities.

That said, what we'd basically need to do, assuming we don't rely on devel_generate:
1. loop over each relation type (assume relation types are already created by the user).
2. For each relation type, get the available endpoint types (checking if the relation type is directional)
3. randomly choose an endpoint type from those available, then do an EFQ with entityOrderBy('RAND()') (if that works)
4. Repeat an n-1 times, where min_arity<=n<=max_arity
5. create the relations.

I'll have a go.

clemens.tolboom’s picture

From the relation.test

function createRelationTypes() {
    $this->relation_types['symmetric'] = array(
      'relation_type'   => 'symm' . $this->randomName(),
      'label' => 'symm' . $this->randomString(),
      'source_bundles' => array('node:article', 'node:page', 'term:*'),
    );
    $this->relation_types['directional'] = array(
      'relation_type'   => 'dir' . $this->randomName(),
      'label' => 'dir' . $this->randomString(),
      'directional' => TRUE,
      'source_bundles' => array('node:article'),
      'target_bundles' => array('node:page'),
    );
    $this->relation_types['octopus'] = array(
      'relation_type'   => 'octo' . $this->randomName(),
      'label' => 'octo' . $this->randomString(),
      'min_arity' => 3,
      'max_arity' => 5,
      'source_bundles' => array('node:article', 'node:page'),
    );
    foreach ($this->relation_types as $relation_type) {
      relation_type_save($relation_type);
    }
  }

and testDropzonePickCreate we can can boil some code to generate this I guess.

naught101’s picture

Status: Active » Needs review
StatusFileSize
new2.88 KB

This basically works for me (without drush integration), although it creates (broken) relations to entity_id 0 sometimes, for some unknown reason.

run drush ev "relation_generate_relations();" and it'll generate 10 valid relations of what ever relation types you have created.

naught101’s picture

StatusFileSize
new2.45 KB

This works better. I don't really get why array_rand() returns a key... oh well.

This is still highly inefficient, but it works well enough for development work.

clemens.tolboom’s picture

Status: Needs review » Needs work

Please merge patch #4 and #5 :-)

naught101’s picture

StatusFileSize
new2.57 KB

Sorry, completely wrong patch.

clemens.tolboom’s picture

Status: Needs work » Needs review
StatusFileSize
new4.8 KB

I added a form to /admin/config/development/generate/relation

Works nice but when selecting kill we should add 'x relations deleted' I guess.

Furthermore it's hard to find new relations ... or do I miss a path?

[Stock Response - Needs review ]

The attached patch needs review

naught101’s picture

Status: Needs review » Fixed

Thanks Clemens! Modified slightly and committed (d99982b). Let's call this fixed, we can revisit if EFQ introduces random ordering.

clemens.tolboom’s picture

Status: Fixed » Needs review
StatusFileSize
new4.38 KB

On an empty database generating relations gives errors.

I added required to relation types fields and checked for generated rids count

I'm not sure whether this test is enough

if ($available_types) {
clemens.tolboom’s picture

Status: Needs review » Needs work

The patch in #10 is ok when there are no 'Source and Target bundles' defined on ie admin/structure/relation/manage/relation-name

But not when there is no content.

naught101’s picture

hrm... maybe just something warning that you need content before making relations? There's always going to be problems if people look for content that does exist though, including taxonomy terms and files, and what ever other entities they've defined as endpoints in their relations..

What can we really do about that? We'd have to do blank EFQ calls to all entity types at the beginning, and blacklist those with no results. Seems a bit expensive.

clemens.tolboom’s picture

@naught101 : we discussed the creation of relationships through code and ended up with

sa="@drupal.d7 --yes"

drush $sa site-install
drush $sa en devel_generate coder_review views_ui advanced_help graphapi_relation

drush $sa genu 10
drush $sa genc 10 0

drush $sa vset devel_error_handler 2

drush $sa ev '$type = relation_type_create(array("relation_type" => "donation", "directional" => TRUE,
    "source_bundles" => array("node:*"), "target_bundles" => array("node:*"),
        "label" => "donated to", "reverse_label" => "received donation from", ));
  relation_type_save($type);'
drush $sa ev '$type = relation_type_create(array("relation_type" => "similar", "directional" => FALSE,
      "source_bundles" => array("node:*"), "target_bundles" => array("node:*"),
          "label" => "is similar to"));
    relation_type_save($type);'
drush $sa ev 'relation_generate_relations();'

Why can't we get these relation types into the relation_generate code? We do have entity_get_info() to pop-out a list of entity:bundle combinations right?

chx’s picture

Why is this still open? My nice little relation-generate drush command does not work?

clemens.tolboom’s picture

@chx I didn't knew we had a drush command now for generating relations.

Nice solution moving the code out of relation.admin.inc to relation.drush.inc :)

But we still fail to have a command to generate relation types.

drush $sa ev '$type = relation_type_create(array("relation_type" => "donation", "directional" => TRUE,
    "source_bundles" => array("node:*"), "target_bundles" => array("node:*"),
        "label" => "donated to", "reverse_label" => "received donation from", ));
  relation_type_save($type);'
drush $sa ev '$type = relation_type_create(array("relation_type" => "similar", "directional" => FALSE,
      "source_bundles" => array("node:*"), "target_bundles" => array("node:*"),
          "label" => "is similar to"));
    relation_type_save($type);'
clemens.tolboom’s picture

Status: Needs work » Needs review
StatusFileSize
new1.37 KB

Attached patch fixes some args.

clemens.tolboom’s picture

The patch still needs review but the test for how many relations created is puzzling me.

Currently doing a

drush genrel

generates 20 relations instead of the default setting of 10. So testing count($new_rids) == $number_relations is incorrect. $number_relations is half of count($new_rids).

This is due to arity as for each arity a relation is created ... see

$arity = rand($relation_type->min_arity, $relation_type->min_arity);
    for ($i = $number_relations; $i > 0; $i--) { // start new relation
      $entity_keys = array();
      for ($r_index = 0; $r_index < $arity; $r_index++) {

Note that min_arity is used twice! So this part I don't get :(

Replacing the error flow by

count($new_rids) > $number_relations

feels awkward but it is still better when used by

drush genrel
chx’s picture

Title: Add support for devel to generate relations » Relation generate doesnt generate the correct number of relations
Category: feature » bug
Status: Needs review » Needs work

Sure. That'd be a bug report. As for generating types, I dunno, drush can't generate node types why would it generate relation types?

naught101’s picture

Status: Needs work » Closed (works as designed)

Not a bug. drush genrel 10 generates 10 relations for each relation type. You have 2 types defined, so it generates 20 relations.

clemens.tolboom’s picture

Title: Relation generate doesnt generate the correct number of relations » Add support for devel to generate relations
Category: bug » feature
Status: Closed (works as designed) » Needs review

Please apply patch #16 then as that fixed arg defaults. With last remark from #17

Replacing the error flow by

count($new_rids) > $number_relations
naught101’s picture

Status: Needs review » Active

Ah, sorry.

+++ b/relation.drush.inc
@@ -112,6 +112,6 @@ function relation_generate_relations($number_relations = 10, $types = array(), $
+  relation_generate_message(t('Generated @number_relations relations.', array('@number_relations' => count($new_rids))), (count($new_rids) == $number_relations) ? 'ok' : 'error');

This doesn't make much sense as an error message. Shouldn't it be something like "Generated count($new_rids) relations. Should have generated $num_rel"?

Also, status should be "status", not "ok"

clemens.tolboom’s picture

Status: Active » Needs work

Ehrm ... the status value should be (and was) ok. I added the 'error' and (needed to) fixed the arg for

+++ b/relation.drush.inc
@@ -29,19 +29,19 @@ function relation_drush_command() {
-function relation_generate_message($message) {
+function relation_generate_message($message, $status) {
   if (function_exists('drush_log')) {

Fixed argument

I got a message "Generated 10 ..." when these was no generation at all. Because I had no entities and/or relation_types.

+++ b/relation.drush.inc
@@ -112,6 +112,6 @@ function relation_generate_relations($number_relations = 10, $types = array(), $
-  relation_generate_message(t('Generated @number_relations relations.', array('@number_relations' => $number_relations)), 'ok');
+  relation_generate_message(t('Generated @number_relations relations.', array('@number_relations' => count($new_rids))), (count($new_rids) == $number_relations) ? 'ok' : 'error');

Operator == should be changed into >=

This still gives an error if for some reasons no relation is created.

naught101’s picture

+++ b/relation.drush.inc
@@ -29,19 +29,19 @@ function relation_drush_command() {
+    drupal_set_message($message, $status);

Ah, confusion. drush_log() accepts 'ok', but drupal_set_message() only accepts 'status'

Message should probably just say "generated @numrel relations for each defined relation type." or something then, no?

Is there any reason why it would generate some relations, but not all of them? If not, we probably don't need a numerical comparison, just a "Whoops, relation creation failed"...

clemens.tolboom’s picture

Ah ... now I get it :p

dsm knows 'status', 'warning', 'error' so indeed we need to transform between 'ok' and 'status' depending on the context.

naught101’s picture

Component: Code » API
Status: Needs work » Closed (fixed)

Closing this as fixed, since the feature has been in for ages. If there are still problems, let's open new issues, this one is confusing.