Generating random data

When we build apps we generally have little to no data available. If you, like me, do not have the skill to imagine how the interaction with your application would be when the application is more saturated with data then you will inevitably have to refactor your designs, interfaces and even methods (like ‘in code’ man). While it is true that you will have to do that anyway it is still very handy to have ‘almost real data’.

This post is about random data generators and a library that helps you load that random data in a mu.semte.ch back end.

random-data-generator

On this github repository you can find a project that if you clone it will help you to generate random data for your mu.semte.ch application. It features several handy helper methods to

  • a date
  • a number
  • a name
  • a telephone number
  • a quote

The idea is that this list increases with every iteration of the library. Now it is not too unreasonable that you build random data using these building blocks. You can for instance define a function that generates a random person:

function getRandomPerson() {
  return {
      name: getRandomName(),
      dateOfBirth: getRandomDate(new Date("01-01-1925"), new Date("01-01-2005")),
      favoriteQuote: getRandomQuote()
  }
}

Calling this function will then provide you with a random person and now you can fill a list with them.

Describing your domain model

Just as in mu-cl-resources you will have to describe your domain model. The parser cannot get your model just from the JSON objects. This is as easy as defining:

  • the class
  • the URI base
  • the properties

So for the person objects that get generated by the previous function this could be something like:

var personClass = "http://example.com/Person";
var personBase = "http://exampl.com/persons/";
var personProperties = [
    {   key: "name",
        predicate: "http://xmlns.com/foaf/0.1/name",
        type: {
            type: "string",
            options: {}
        }
    },
    {
        key: "dateOfBirth",
        predicate: "http://xmlns.com/foaf/0.1/birthDate",
        type: {
            type: "date",
            options: {}
        }
    },
    {
        key: "favoriteQuote",
        predicate: "http://example.com/favoriteQuote",
        type: {
            type: "string",
            options: {}
        }
    }
];

Now you can just loop over all the random persons in your list and call one function to save them to the triple store:

writeObjectToStore(randomPerson, personClass, personBase, personProperties);

This method will handle everything from adding UUID’s as needed for mu.semte.ch, inserting the correct datatypes in the triple store and linking the relations as you would expect them to be.

Conclusion

There is more information on how to do this for complex models on the github repository’s page. This library enables faster and consistent testing with more data. Another useful application for it could be to provide demo apps with data. I have developed more than 1 really really cool application where you will just have to take my word for it because all data in it is sensitive data. With random data generators I can publish these applications without having to worry about that.