Discussion Forum

Inserting a Relation Instance with an Entity Instance

This is probably a silly question, but here goes. I’m trying to automate the process of creating typeql statements. Creating insert statements for entities and their attributes is straightforward. But, along with an entity’s insert statement, I’d like to insert a relation associated with that entity.

To be more concrete, here’s a screenshot of part of the app I’m putting together:

Reading from right to left, the entity instance is given a name (test, in this case), and the entity type is selected from the listbox (gene, in this case). Moving to the right, all attributes for “gene” and its parents (chromosome and biological-entity) are listed. When the user selects relevant attributes, a dialog asks for the attribute’s value.

The listbox on the far right currently just displays any relations associated with “gene” and its parents. I’d like for the user to select relevant relations/roles, and then generate an insert statement for that relation along with the entity instance for “gene”. Since it is necessary to also identify an instance for the other part(s) of the relation, I would add a dummy instance that would have to be updated later.

That brings me to the crux of the problem - the syntax of the insert statement(s). The entity and dummy statements would look like this:

  insert $x isa gene, has name "gggggggg", has length "23400", has gene-name "genename", 
      has located_on_chromosome_number "4";
 insert $y isa protein, has name "dummy";

Then, the insert statement for the relation instance would look like this:

insert $z (encoding-gene: $x, encoded-protein: $y) isa gene-protein-encoding;

In the typeQL documentation, the usual way to insert a relation instance is to precede it with a match statement for the gene and dummy instance. Do the gene and dummy insert statements serve the same role as match statements for a relation insert? Is there another way to include a relation instance with an entity instance without knowing the other instance member(s) of the relation?

Bottom line - being able to combine an entity and relation instance is just a matter of efficiency. The alternative, I guess, is to instantiate all entities first and then cycle through them using the match-insert approach in the typeQL documentation.

After the screenshot, the next sentence should be “Reading from left to right …”.

The creation of a “dummy” protein is strange. I would go with a approach like the docs does:

  1. user creates an entity with applicable attributes
  2. later, user can link together preexisting concepts based on some lookup (eg. an attribute value)

Have you run into a case where this approach is too slow?
You could speed it up by optionally creating complete relations to entities that exist already at the time the user is creating the new entity.

If you want to automate without writing code, there’s also TypeDB Loader: https://github.com/typedb-osi/typedb-loader
However this won’t help with manual data entry!

Thanks Joshua.
Yes, I can certainly take the approach as in the docs. But, let me explain my thought process a bit.

I’m looking at potentially thousands of instance entries. Many, if not most, of them will have a relation role.

To follow the docs approach, assuming only two roles per relation, I have to write five lines of code: 2 lines for the original entity instances inserts (one for each role), 2 more lines for those instances in the “match” clause, and one more line for the insert relation clause.

Two of those lines of code are redundant - the match clause to identify the role players. That’s why I originally asked if the entity instance insert statement would also serve to identify the role players, just as the match clause does in the docs approach. If this were the case, that eliminates 40% of the code that I have to generate (i.e., instead of 5 lines per entity, I’d enter 3 lines as described in the original post). Of course, this is at the expense of having to locate and substitute values for the dummy protein.

That’s the sense in which the “approach is too slow” as you asked. Generating all the lines of code to insert into the db seems the most time-consuming part of the process. I’m looking for an efficient way to do so.

As I said, I’ll fall back to the docs approach if needed. But, my primary question still stands: can the insert statement for an entity serve the same purpose as the match statement when adding a relation instance? If the answer is ‘no’, then I’ll use the docs approach.

After more thought, I do think this is a silly topic. I should not have initiated it to begin with.
Is there a way to delete it so it doesn’t waste someone’s time? I apologize for wasting yours.

No worries at all! No such thing as a silly question if you learned something :slight_smile: