Skip to content

Instantly share code, notes, and snippets.

@FlorentCollin
Last active July 20, 2020 12:09
Show Gist options
  • Select an option

  • Save FlorentCollin/dc739c79962e35594fc8d34be7de566e to your computer and use it in GitHub Desktop.

Select an option

Save FlorentCollin/dc739c79962e35594fc8d34be7de566e to your computer and use it in GitHub Desktop.

How to define expenses in RDF

The schema.org define two schemas interesting for our use case, https://schema.org/Person and https://schema.org/BuyAction. The BuyAction schema will be use to store a specific expense (see for example form G103 3.1 "auditory and oral messages").

This schema contains 4 properties that we are going to use to store the expenses linked to the form:

  • price
  • priceCurrency
  • description
  • identifier

And the agent property of the BuyAction schema can link a Person to a BuyAction. Therefore we can store an expense by defining theses triples.

  1. BuyAction agent <person_ref>
  2. BuyAction identifier <field_id>
  3. BuyAction description "auditory and oral messages"
  4. BuyAction price 123
  5. BuyAction priceCurrrency "EUR"

Let's look at how we can convert these triples into a typescript code:

interface BuyActionData {
    identifier: string;
    description: string;
    price: number;
    priceCurrency: string;

}

function createExpense(doc: TripleDocument, person: TripleSubject, buyActionData: BuyActionData) {
    const buyAction = doc.addSubject();
    // Set the type to BuyAction
    buyAction.addRef(rdf.type, schema.BuyAction);
    // Add all the triples needed to define the BuyAction
    buyAction.addRef(schema.agent, person.asRef());
    buyAction.addString(schema.identifier, buyActionData.identifier);
    buyAction.addString(schema.description, buyActionData.description);
    buyAction.addInteger(schema.price, buyActionData.price);
    buyAction.addString(schema.priceCurrency, buyActionData.priceCurrency);
    // Don't forget that it is not save yet, doc.save([buyAction]) must be called for that
    return buyAction; 
}

Usage example:

const doc = createAppDocument(appStorage, 'dummyExpense.ttl');
const person = doc.addSubject();
person.addRef(rdf.type, schema.Person);
person.addString(schema.givenName, 'John');
person.addString(schema.familyName, 'Smith');
// The identifier must be unique, and this identifier needs to be used when you want to access the data
// Here I used '3.1' for the identifier however we should define an identifier for each field in the form
// This identifier should be common between the declaration team and the front end team
const buyAction = createExpense(doc, person, {identifier: '3.1', description: 'auditory and oral messages', price: 43, priceCurrency: 'EUR'});
doc.save([person, buyAction]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment