Copying Blocks Between AI2 Projects (Among Other Things): Part 2

Scope

This discussion follows on from Part 1. Read that post to get up to speed.

Audience

Aimed at the same audience as for Part 1. Read that post etc., etc.

Caution

I think this caution is worth repeating (just in case you decide to skip reading Part 1).

It should go without saying that any changes that are made to an AI2 project using methods that are not provided by the tools available must be done with extreme care, with the understanding that the results could be a mess. So make sure there are backups – and backups of backups – and check, double-check and triple-check any work before submitting the results to AI2 using the feature to import .aia files.

Confusion Reigns…

Part 1 ended by showing a top level XML structure contained within a .bky file buried inside a .aia file:

Screen1 Formatted XML

Figure 1: Screen1 Formatted XML

A preliminary examination of the XML structure shows that for every programming block associated with a specific screen (given by a {screen name}.bky file in the .aia structure — in this case it’s Screen1, the name of the core screen for all AI2 projects) there is a corresponding entry (conveniently also called block) in the structure.

If your screen (let’s call it yourScreen for clarity) has five programming blocks associated with it, there will be five entries called block in the XML structure for yourScreen.bky. It’s that simple.

And that’s probably where the simplicity ends 🙂

On the face of it, you might think that to get one or more programming blocks from a source project pasted into a target project all you have to do is:

  1. Export the source project (containing your desired programming blocks) to a .aia file on your computer;
  2. Do the same for the target project that is to receive the desired programming blocks;
  3. Find which source block in the source .bky file within the source .aia file (puff, pant, gasp) corresponds to the programming block you want to copy to the target project;
  4. Copy it to the clipboard;
  5. Paste/insert it into the target .bky structure in the target .aia file associated with the target project;
  6. Save the target .aia file;
  7. And then finally import the target .aia file into your AI2 projects (where it may appear as a new project if you name the .aia file accordingly so as not to overwrite your original project)

I know I did. But it’s not that simple. The reason it’s not that simple probably explains why there is no Copy Between Projects function provided with AI2. Not yet, anyway.

One reason may be this: every programming block associated with yourScreen — and every object within each one of those programming blocks — appears to be assigned a unique numeric ID by the code running on the AI2 server, and if you insert a block copied from another project, the IDs in the inserted block probably have to be changed to fit into the sequence of IDs already in use in the target .bky file.

If this is not done, AI2 will refuse to load any of the programming blocks in the set associated with the same screen:

Inconsistent ID Values Results In .aia Import Failure

Figure 2: .aia Import Failure

I know this happens because that’s the error I got when I inserted the XML for a programming block into a .aia file without modifying the id values accordingly.

I discovered that it’s also not sufficient to use your own unique ID values. If for example there are six objects, it is not OK to use ID values 1, 2, 3, 7, 12, 19 — the sequence of ID values associated with the first object to the last has to be contiguous (i.e.: 1, 2, 3, 4, 5, 6).

So it is not possible to simply Copy/Paste as you would in any other environment. AI2 appears to use those IDs as part of its validation of any imported .aia file, so every Paste-type action would need to determine the highest ID value in use in the relevant .bky file in the target .aia file and adjust the IDs assigned to every object in the current Copy’d programming block to follow on from that highest value.

If your Copy action encompasses several programming blocks then it’s probable that all of them would need to be edited and adjusted, for every Paste-type action performed on every project.

(I’m assuming here that the version numbers and other identifications of AI2 and supporting technologies (.yail and .scm) will be the same; if some time has elapsed between the creation of the source and target .aia files then this may not be a safe assumption and could pose an additional complication that needs to be addressed.)

Here’s where it really becomes confusing.

I created a test project with one screen (the core Screen1) and just one programming block (a text variable with 26 letters of the alphabet assigned).

I exported that project as a .aia file to my computer. I opened (but didn’t extract) the .aia file with 7-ZIP so that I could edit it and then save the changes — thus preserving the structure of the .aia file but allowing the content of one part (the .bky file) to be changed.

From within the .aia file I had opened with 7-ZIP I opened the screen1.bky file using XML Notepad 2007. This was to be my target file.

I looked at the XML content of the .bky file, in particular the value for the block id, expecting it to be 1, since there was just the one programming block.

It wasn’t 1. It was 3. Hmmm.

So I deleted everything and tried again. This time the id value was 7. I tried again. It was 15.

So I figured that maybe this was an artefact, and resolved to proceed to the next step: copying a programming block from another project (that had already been exported as a .aia file to the computer and fully extracted using 7-ZIP), pasting it into the test project currently opened with 7-ZIP (but not extracted to disk).

I renumbered all the id values so that they began at 1 and incremented by 1, to make a contiguous set of values for id.

Then I saved the edited screen1.bky file so that the .aia file was updated, and attempted to import the modified .aia test file into AI2.

The import failed — with exactly the same error as before (shown in Figure 2).

Let’s have a look inside one of the block objects in a .bky file inside a .aia file to see what it contains. I’ve deliberately chosen a programming block from one of my projects that has been pulled out from a procedure to show that even non-executable programming blocks that trigger Warnings (because they’re not associated with any procedure and so cannot be executed) can be included in a .aia file. This is what it looks like in AI2 online:

A Programming Block Without A Procedure

Figure 3: A Programming Block Without A Procedure

There are five programming blocks here: a “join” object and four text objects. This is how it looks in the .bky file when the top level block is expanded and formatted by XML Notepad 2007:

Example Block As It Looks In .aia File

Figure 4: Example Block As It Looks In .aia File

The left panel shows “nodes” — the programming block objects and their properties — and the right panel shows any values associated with them.

Note how there are blocks within blocks — which is what you’d expect, given the nature of the programming block shown earlier.

You can see that there’s a block with a “type” listed as “text_join“, which is the “parent” block, and four “child” blocks — each is a “text” programming block in its own right.

The top level block has an ID (id) with a numeric value of 235. Each child block has an id that continues the numeric sequence: 236, 237, 238 and 239. You can see that it’s not exactly rocket science, but it is a little complex.

The top level id value (235) shows that there are (presumably) 234 programming objects preceding the one we’re dealing with. Note that this doesn’t imply anything about the sequence of execution of the previous programming blocks.

So if the programming block depicted above (Figure 3) is copied to a project screen that already has a total of 50 programming objects, the id values for this programming block would have to be changed from 235239 to 51, 52, 53, 54 and 55 in order to be acceptable to AI2.

Or so I thought. But now it appears that there’s something else stopping the successful import of a modified .aia file into AI2. I have yet to determine what that might be.

More to follow…

Advertisements
This entry was posted in android programming, copying blocks between projects, copying blocks between screens, examining .aia files, XML-related. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s