Acre Tutorial One - Album Lister

What is an Acre Application?

At its simplest, an Acre application is:

The tutorial you're reading right now is the index file for an Acre application that you will copy and create your own version of.

This tutorial will walk you through the process of creating a very simple application that only uses these two files to return a list of all the albums by an artist in Freebase. Some knowledge of HTML and JavaScript will help you understand how the code in this application works, but you can also cut-and-paste this code into your own applications, make minor changes, and create your own applications without much additional knowledge or effort.

A Basic Overview of Acre Scripting

There are three aspects of the Acre language that are used in this tutorial:

API Commands - Acre API commands like acre.mqlread and acre.require let Acre make requests to the Freebase API and Acre server, like requesting information from the Freebase database or a file from a library on the server.

Template scripting - Acre templating commands are used within the body of an HTML file to execute operations when the pages loads or when a user interacts with the page content. In this way Acre scripting is the same as JavaScript, in that it can be used to perform logical operations and manipulate the appearance of page elements within the browser.

Text Substitution - text substitution lets Acre take the results of any of the operations above and insert them into a text string. For example, after getting the results of an acre.mqlread, you might want to insert the number of results into a sentence like "There are __ results in the Freebase database." This is accomplished through the use of a $.

Creating Your First Acre Application

1. Clone This Application and Create a New Template File

An important feature of App Editor is the ability to clone files or applications created by you or someone else. This lets you easily re-use or adapt code from other applications.

  1. Make sure you are a registered Freebase user, then open this link in a new window and log in to App Editor:
    http://www.freebase.com/tools/appeditor#app=/user/willmoffat/simple-album-lister

    You'll see the following files in this application:


    If you'd like a preview of how the application works, open sample code and click the Open Preview link.

  2. In the menu bar go to Clone > Clone this app, and save a copy of this application in your user namespace.
  3. Click New next to Templates and create a new blank template file.
  4. In the File menu select re-name and create a name for your file. Eventually you will re-name this file index, but for now choose another name. This is where you will enter the code for your first Acre application.
  5. Insert the following code into your template file to set it up as an HTML file:
<html>
<body>
</body>
</html>
All the code that you will enter in the following steps will go between the <body></body> tags.

2. Create Static HTML Markup

Enter any text you want to display on your page and add HTML markup to format it. For example:
<h1>Album Lister</h1>
<p>This Acre application makes a query to the Freebase database to find all the albums in the database by a specific musical artist.</p>

We recommend that you write all your code as valid XHTML, so all tags should be closed and in lower-case. We also recommend that, after each stage of your applicaiton development, you use the Open Preview link to view the changes you've made.

3. Formulate a MQL Query and Create a MQL Query File

The Metaweb Query Language (MQL) is the mechanism for retrieving information from the Freebase database. The Metaweb Query Language Reference Guide provides complete information about Freebase architecture and how to create simple and complex queries, while the Query Editor tool provides an environment to test them.

MQL queries, stored in Queries files, are an essential component of Acre applications, since the basic functionality of any Acre application is to retrieve information from Freebase through a MQL query and then present the results in a template. For this sample application the MQL query is very simple. If you open the police-albums file you'll see the following code:

{
   "type":"/music/artist",
   "name":"The Police",
   "album":[]
}

This query is asking for a list of items (indicated by the []) associated with the album property that is part of the musical artist type, and, specifically, from the musical artist known as The Police. You could change this query to ask for any albums from any other musical artist in Freebase simply by changing the name value.

  1. Click new next to Queries.
  2. Copy and paste the code above into your new file.
  3. Change "The Police" to any other musical artist in Freebase (the complete list of all 386,000+ artists is here: http://www.freebase.com/view/music/artist)
  4. Select re-name from the File menu and name your query.
  5. Select Save from the File menu.
It is possible that someone who has been typed as a Musical Artist does not have any entries for the Albums property, in which case you'll get an error when you run your application. You should test your query and see what results are returned by clicking Open Preview in your Queries file.

4. Require the MQL Query in the Template File

  1. Open the template file you created in Step 1 by clicking on it.
  2. Copy this code and paste it into your template file:
<acre:script>
    var query = acre.require("police-albums").query;
    var result = acre.freebase.MqlRead(query).result;
</acre:script>

Here you can see the JavaScript-like aspects of Acre. Like JavaScript, Acre scripting commands can be embedded in the HTML file by placing them within <acre:script> tags, and the scripting will be executed when the page loads. In this case, two variables, query and result, are being set, and their values are globally available for use in the rest of the file.

For query, the acre.require API method is used to retrieve the query file and use its contents to set the value of query, while result uses that value to perform a read of the Freebase database through the acre.freebase.MqlRead API method. If you created a new query file in Step 3, you would substitute its name for "police-albums" to use that query in your application.

5. Display the Number of Results by Creating a Result Object and Using Text Substition

Copy and paste this code into your template:

The Police have ${result.album.length} albums in Freebase

Here you can see the object-oriented aspect of JavaScript, and Acre, in action.

Queries to Freebase, and the results, are in the JavaScript Object Notation (JSON) data format. A JavaScript Object is simply a name:value pair, or collection of name: value pairs, where each name is a property of the object, and the value is the value of that property. If you look back at the query you created in Step 3, we used JSON to define the object we were interested in by using the type and name properties, and then said we wanted to know what values were associated with the album property.

When our query returns results, it returns name:value pairs that become the properties of our result object. You can see how this works by using the Query Editor.

  1. Open query editor in another window. Note that you can also access Query Editor through the Help menu in App Editor.
  2. Copy the MQL query you used in Step 3 into the left pane.
  3. Click read.

The query results show the property name (albums) and its values. We can now use a JavaScript function like .length to find out more about the property album in the object result. Having performend this operation, we can insert the results into the text using $ substitution. Note that in the example above, we enclosed the results in {}. This is because we want the browser to process a complex JavaScript expression before performing the substitution. If we had a simple JavaScript value to substitute, we could use only the $, as you'll see in the next step.

If your query result contained information about different properties, you could use other functions on them in a similar way.

A very simple description of JSON, with examples that should look familiar, is here: http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)

6. Use Acre Templating Functions to Display the Results

Copy and paste the code below into your template:

<ul>
  <li acre:for="album in result.album">$album</li>
</ul>

Many of the Acre templating commands can be directly appended to an HTML tag like an HTML attribute (and are appropriately called attributes in the Acre documentation). In this case, appending acre:for to the <li> tag means that for every album value in the album property of the result object, list the name of that album in place of $album.

7. Require the Standard Freebase Attribution Information

Any application that uses Freebase data must attribute Freebase as the data source, and include the CC-BY license operation. The code below requires a standard attribution logo and information from a library on the Freebase server:
${acre.require("/freebase/apps/attribution/templates").blanket()}

8. Share Your Freebase Application with the World

You can now link directly into your application using the following URL format: http://[application name].[user name].user.dev.sandbox-freebaseapps.com/index This tutorial has covered the basic components of a Freebase application, including creating a MQL query, an introduction to JSON, and an introduction to the Acre API and templating language. The next tutorial will show you how to create an extended MQL query that a user can enter through HTML form inputs, how to include images from Freebase in your results, and how to incorporate the Freebase autocomplete feature to help users select the correct search terms.