1 var fs = require('fs'); 
  2 var rdfstore = require('./rdfstore');
  3 
  4 /**
  5  * Class representing an EARL document
  6  * See http://www.w3.org/TR/EARL10-Schema/#TestResult
  7  * @constructor
  8  */
  9 var EarlDocument = function () {
 10   // Initialise rdf store
 11   this.store = new rdfstore.Store();
 12   this.graph = this.store.rdf.createGraph();
 13   
 14   /**
 15    * List of assertions of the EARL document
 16    */
 17   this.assertions = new Object();
 18   
 19   // Set general prefixs
 20   this.store.rdf.setPrefix("earl", "http://www.w3.org/ns/earl#");
 21   this.store.rdf.setPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
 22   this.store.rdf.setPrefix("dct", "http://purl.org/dc/terms/");
 23   
 24   // Can be used to add other prefixs
 25   this.setPrefix = function(prefix, url) {
 26       this.store.rdf.setPrefix(prefix, url);
 27   }
 28 };
 29 /**
 30  * Add a triple to the RDF document
 31  * @param {String} subject the subject of the RDF triple
 32  * @param {String} predicate the predicate of the RDF triple
 33  * @param {String} object the object ot the RDF triple
 34  */
 35 EarlDocument.prototype.addTriple = function (subject, predicate, object) {
 36     this.graph.add(this.store.rdf.createTriple(subject, predicate, object));
 37 }
 38 /**
 39  * Add an assertion
 40  * @param {String} identifier Unique string which identify the assertion
 41  * @param {String} subject of the assertion
 42  * @param {String} test name of the tests
 43  */
 44 EarlDocument.prototype.addAssertion = function(identifier, subject, test) {
 45     return this.assertions[identifier] = new EarlAssertion(this, identifier, subject, test);
 46 };
 47 
 48 /**
 49  * Return an assertion given by parameter
 50  * @param {String} identifier
 51  * @returns EarlAssertion
 52  */
 53 EarlDocument.prototype.getAssertion = function(identifier) {
 54     return this.assertions[identifier];
 55 };
 56 
 57 /**
 58  * Write the EARL document to an N3 file on the disk
 59  * @param {String} file the file to be wrote on the disk (extension included)
 60  */
 61 EarlDocument.prototype.writeFile = function(file) {
 62   var n3file = this.graph.toNT();
 63   fs.open(file, 'w+', 666, function( e, id ) {
 64     fs.write( id, n3file, null, 'utf8', function(){
 65       fs.close(id, function(){
 66         console.log('File (' + file + ') wrote on the disk');
 67       });
 68     });
 69   });
 70 };
 71 
 72 /**
 73  * Represents an assertion
 74  * See http://www.w3.org/TR/EARL10-Schema/#Assertion
 75  * @constructor
 76  */
 77 var EarlAssertion = function(Document, identifier, subject, test) {
 78   // Will be used to save the reference of the EarResult (aggregation)
 79   this.Document = Document;
 80   
 81   this.result = Object();
 82   this.identifier = identifier;
 83   this.subject = subject;
 84   this.test = test;
 85 };
 86 
 87 /**
 88  * Create a result associated to an assertion
 89  */
 90 EarlAssertion.prototype.addResult = function(outcome) {
 91     this.result = new EarlResult(this,outcome);
 92 }
 93 
 94 EarlAssertion.prototype.toRDF = function() {
 95   this.Document.addTriple(  this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve(this.identifier)),
 96                             this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve("rdf:type")),
 97                             this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve("earl:Assertion")) );
 98 
 99   this.Document.addTriple(  this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve(this.identifier)),
100                             this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve("earl:subject")),
101                             this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve(this.subject)) );
102 
103   this.Document.addTriple(  this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve(this.identifier)),
104                             this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve("earl:test")),
105                             this.Document.store.rdf.createNamedNode(this.Document.store.rdf.resolve(this.test)) );
106                               
107   console.log("Assertion " + this.identifier + " saved into the RDF graph");
108 };
109 /**
110  * @constructor
111  */
112 var EarlResult = function(EarlAssertion, outcome) {
113     this.Assertion = EarlAssertion;
114     this.identifier = EarlAssertion.identifier + "Result";
115     this.outcome = outcome;
116 };
117 /**
118  * Save the EarlResult to the rdf graph
119  */
120 EarlResult.prototype.toRDF = function() {
121     EarlAssertion.Document.addTriple( EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve(EarlAssertion.identifier)),
122 																	    EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve("earl:result")),
123 																	    EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve(this.identifier)) );
124 
125     EarlAssertion.Document.addTriple( EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve(this.identifier)),
126 																	    EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve("rdf:type")),
127 																	    EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve("earl:TestResult")) );
128 
129     EarlAssertion.Document.addTriple( EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve(this.identifier)),
130 																    EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve("earl:outcome")),
131 																    EarlAssertion.Document.store.rdf.createNamedNode(EarlAssertion.Document.store.rdf.resolve(this.outcome)) );
132 };
133 
134 // Exposing public functions
135 exports.EarlDocument = EarlDocument;
136 exports.EartAssertion = EarlAssertion;
137 exports.EartResult = EarlResult;