RDF

RDF

Experimental RDF emulation on top of a MutableData

Constructor

new RDF()

Source:

Methods

add(subjectopt, predicateopt, objectopt, provenanceopt)

Adds RDF statement to graph

Parameters:
Name Type Attributes Description
subject NamedNode | BlankNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_subject

predicate NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_predicate

object LiteralNode | NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_object

provenance NamedNode <optional>

https://www.w3.org/TR/2014/REC-n-quads-20140225/#sec-intro

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triple = {
          predicate : rdf.vocabs.RDFS('isDefinedBy'),
          object : DBP('Pluto')
      };
      rdf.add(id, triple.predicate, triple.object)
  } catch (err) {
    throw err;
  }
};

any(subjectopt, predicateopt, objectopt, provenanceopt) → {LiteralNode|BlankNode|NamedNode}

Retrieves one node, the first to match any combination of subject, predicate, or object. A null value is a wildcard.

Parameters:
Name Type Attributes Description
subject NamedNode | BlankNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_subject

predicate NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_predicate

object LiteralNode | NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_object

provenance NamedNode <optional>

https://www.w3.org/TR/2014/REC-n-quads-20140225/#sec-intro

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triples = [
          {
              predicate : rdf.vocabs.RDFS('isDefinedBy'),
              object : DBP('Pluto')
          },
          {
              predicate : rdf.sym("http://dbpedia.org/property/atmosphereComposition"),
              object : DBP("Methane")
          },
          {
              predicate : rdf.vocabs.RDF('type'),
              object : DBP("Dwarf_planet")
          },
          {
              predicate : rdf.sym("http://dbpedia.org/ontology/discovered"),
              object : literalNode
          }
      ];
      // The subject of each triple is the same in this example
      triples.forEach( triple => rdf.add(id, triple.predicate, triple.object) );
      const result = rdf.any(subject, predicate, object);
  } catch (err) {
    throw err;
  }
};

(async) append() → {Promise.<NameAndTag>}

Append the triples to the RDF document into the underlying MutableData on the network

Source:
Example
// Assumes MutableData interface has been obtained
const toEncrypt = false;
const asyncFn = async () => {
    try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triple = {
          predicate : rdf.vocabs.RDFS('isDefinedBy'),
          object : DBP('Pluto')
      };
      rdf.add(id, triple.predicate, triple.object)
      let nameAndTag = await rdf.commit(toEncrypt);
      rdf.add(id, triple.predicate, triple.object)
      const newTriple = {
          predicate : rdf.vocabs.RDF('type'),
          object : DBP("Dwarf_planet")
      };
      rdf.add(id, newTriple.predicate, newTriple.object)
      nameAndTag = await rdf.append();
    } catch(err) {
        throw err;
    }
};

bnode() → {BlankNode}

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
    const rdf = await mData.emulateAs('RDF');
    const blankNode = rdf.bnode();
  } catch (err) {
    throw err;
  }
};

(async) commit(toEncryptopt) → {Promise.<NameAndTag>}

Commit the RDF document to the underlying MutableData on the network

Parameters:
Name Type Attributes Default Description
toEncrypt Boolean <optional>
false

flag to encrypt the data to be committed

Source:
Throws:
Example
// Assumes MutableData interface has been obtained
const toEncrypt = false;
const asyncFn = async () => {
    try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triple = {
          predicate : rdf.vocabs.RDFS('isDefinedBy'),
          object : DBP('Pluto')
      };
      rdf.add(id, triple.predicate, triple.object)
      const nameAndTag = await rdf.commit(toEncrypt);
    } catch(err) {
        throw err;
    }
};

each(subjectopt, predicateopt, objectopt, provenanceopt) → {Array}

Retrieves all nodes matching any combination of subject, predicate, or object. A null value is a wildcard.

Parameters:
Name Type Attributes Description
subject NamedNode | BlankNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_subject

predicate NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_predicate

object LiteralNode | NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_object

provenance NamedNode <optional>

https://www.w3.org/TR/2014/REC-n-quads-20140225/#sec-intro

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triples = [
          {
              predicate : rdf.vocabs.RDFS('isDefinedBy'),
              object : DBP('Pluto')
          },
          {
              predicate : rdf.sym("http://dbpedia.org/property/atmosphereComposition"),
              object : DBP("Methane")
          },
          {
              predicate : rdf.vocabs.RDF('type'),
              object : DBP("Dwarf_planet")
          },
          {
              predicate : rdf.sym("http://dbpedia.org/ontology/discovered"),
              object : literalNode
          }
      ];
      // The subject of each triple is the same in this example
      triples.forEach( triple => rdf.add(id, triple.predicate, triple.object) );
      const result = rdf.each(subject, predicate, object);
  } catch (err) {
    throw err;
  }
};

literal(value, languageOrDatatype) → {LiteralNode}

Parameters:
Name Type Description
value String | Number

Literal value

languageOrDatatype String

Either i18n language tag or XSD URI data type

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
    const rdf = await mData.emulateAs('RDF');
    const discoveryDate = new Date("18 Feb 1930");
    const dateTimeDataType = "http://www.w3.org/2001/XMLSchema#dateTime";
    let literalNode = rdf.literal(discoveryDate.toISOString(), dateTimeDataType);
    console.log( JSON.stringify(literalNode) );

    // Alternatively
    literalNode = rdf.literal("Aardvark", "en-US");
    console.log( JSON.stringify(literalNode) );
  } catch (err) {
    throw err;
  }
};

namespace(uri) → {function}

Creates resource namespace prefix and returns function to create NamedNode.

Parameters:
Name Type Description
uri String

Absolute namespace URI

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
    const rdf = await mData.emulateAs('RDF');
    const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
    const dwarfPlanetNodeResource = DBP("Dwarf_planet");
    const formerPlanetNodeResource = DBP("Former_planet");
  } catch (err) {
    throw err;
  }
};

(async) nowOrWhenFetched(ids, toDecryptopt) → {Promise.<Array>}

Fetch the RDF data stored in the underlying MutableData on the network and load it in memory to allow manipulating triples before commit them again.

Parameters:
Name Type Attributes Default Description
ids Array.<String>

list of RDF graph IDs to use as a filter for fetching graphs, e.g. ['safe://mywebid.mypubname', 'safe://mypubname']

toDecrypt Boolean <optional>
false

flag to decrypt the data being fetched

Source:
Throws:
Example
// Assumes MutableData interface has been obtained

const ids = [];
const toEncrypt = false;
const toDecrypt = false;
const asyncFn = async () => {
    try {
        const rdf = await mData.emulateAs('RDF');
        const id = rdf.sym("safe://pluto.astronomy");
        const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
        const triples = [
            {
                predicate : rdf.vocabs.RDFS('isDefinedBy'),
                object : DBP('Pluto')
            },
            {
                predicate : rdf.sym("http://dbpedia.org/property/atmosphereComposition"),
                object : DBP("Methane")
            },
            {
                predicate : rdf.vocabs.RDF('type'),
                object : DBP("Dwarf_planet")
            },
            {
                predicate : rdf.sym("http://dbpedia.org/ontology/discovered"),
                object : literalNode
            }
        ];
        // The subject of each triple is the same in this example
        triples.forEach( triple => rdf.add(id, triple.predicate, triple.object) );
        const nameAndTag = await rdf.commit(toEncrypt);
        const entryGraphArray = await rdf.nowOrWhenFetched(ids, toDecrypt);
    } catch(err) {
        throw err;
    }
};

parse(data, mimeType, id) → {Promise.<String>}

Parse serialised RDF data and place into graph store

Parameters:
Name Type Description
data String

Serialised RDF

mimeType String
id String

Arbitrary absolute URI to identify graph

Source:
Example
// Assumes MutableData interface has been obtained
  const mimeType = "text/turtle";
  const asyncFn = async () => {
      try {
          const rdf = await mData.emulateAs('RDF');
          const id = rdf.sym("safe://pluto.astronomy");
          const serialised = `
          @prefix : <#>.
          @prefix ont: <http://dbpedia.org/ontology/>.
          @prefix XML: <http://www.w3.org/2001/XMLSchema#>.
          @prefix pro: <http://dbpedia.org/property/>.
          @prefix res: <http://dbpedia.org/resource/>.
          @prefix n0: <http://www.w3.org/1999/02/22-this-syntax-ns#>.
          @prefix thi: <http://www.w3.org/2000/01/this-schema#>.

          <>
              ont:discovered "1930-02-18T08:00:00.000Z"^^XML:dateTime;
              pro:atmosphereComposition res:Methane;
              n0:isDefinedBy res:Pluto;
              thi:type res:Dwarf_planet.
          `;
          var parsedData = await rdf.parse(serialised, mimeType, id);
      } catch(err) {
          throw err;
      }
      console.log(parsedData);
  };

removeMany(subjectopt, predicateopt, objectopt, provenanceopt)

Remove all statements matching any combination of subject, predicate or object

Parameters:
Name Type Attributes Description
subject NamedNode | BlankNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_subject

predicate NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_predicate

object LiteralNode | NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_object

provenance NamedNode <optional>

https://www.w3.org/TR/2014/REC-n-quads-20140225/#sec-intro

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triples = [
          {
              predicate : rdf.vocabs.RDFS('isDefinedBy'),
              object : DBP('Pluto')
          },
          {
              predicate : rdf.sym("http://dbpedia.org/property/atmosphereComposition"),
              object : DBP("Methane")
          },
          {
              predicate : rdf.vocabs.RDF('type'),
              object : DBP("Dwarf_planet")
          },
          {
              predicate : rdf.sym("http://dbpedia.org/ontology/discovered"),
              object : literalNode
          }
      ];
      // The subject of each triple is the same in this example
      triples.forEach( triple => rdf.add(id, triple.predicate, triple.object) );
      rdf.removeMany(subject, predicate, object);
  } catch (err) {
    throw err;
  }
};

(async) serialise(mimeType) → {Promise.<String>}

Serialise RDF data

Parameters:
Name Type Description
mimeType String
Source:
Example
// Assumes MutableData interface has been obtained
const mimeType = "text/turtle";
const asyncFn = async () => {
    try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triple = {
          predicate : rdf.vocabs.RDFS('isDefinedBy'),
          object : DBP('Pluto')
      };
      rdf.add(id, triple.predicate, triple.object)
      const serialised = await rdf.serialise(mimeType);
    } catch(err) {
        throw err;
    }
    console.log(serialised);
};

setId(id)

Sets ID for graph store

Parameters:
Name Type Description
id String

ID representing current in-memory graph store.

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
    const rdf = await mData.emulateAs('RDF');
    const id = rdf.sym("safe://pluto.astronomy");
    rdf.setId(id.uri);
    console.log(rdf.id);
  } catch (err) {
    throw err;
  }
};

statementsMatching(subjectopt, predicateopt, objectopt, provenanceopt) → {Array}

Retrieves all statements matching any combination of subject, predicate, or object. A null value is a wildcard.

Parameters:
Name Type Attributes Description
subject NamedNode | BlankNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_subject

predicate NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_predicate

object LiteralNode | NamedNode | null <optional>

https://www.w3.org/TR/rdf-schema/#ch_object

provenance NamedNode <optional>

https://www.w3.org/TR/2014/REC-n-quads-20140225/#sec-intro

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
      const rdf = await mData.emulateAs('RDF');
      const id = rdf.sym("safe://pluto.astronomy");
      const subject = id;
      const predicate = null;
      const object = null;
      const DBP = rdf.namespace( 'http://dbpedia.org/resource/' );
      const triples = [
          {
              predicate : rdf.vocabs.RDFS('isDefinedBy'),
              object : DBP('Pluto')
          },
          {
              predicate : rdf.sym("http://dbpedia.org/property/atmosphereComposition"),
              object : DBP("Methane")
          },
          {
              predicate : rdf.vocabs.RDF('type'),
              object : DBP("Dwarf_planet")
          },
          {
              predicate : rdf.sym("http://dbpedia.org/ontology/discovered"),
              object : literalNode
          }
      ];
      // The subject of each triple is the same in this example
      triples.forEach( triple => rdf.add(id, triple.predicate, triple.object) );
      const result = rdf.statementsMatching(subject, predicate, object);
  } catch (err) {
    throw err;
  }
};

sym() → {NamedNode}

Creates an RDF resource identified by a URI

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
    const rdf = await mData.emulateAs('RDF');
    const predicateUri = "http://dbpedia.org/ontology/discovered";
    const predicateResource = rdf.sym(predicateUri);
  } catch (err) {
    throw err;
  }
};