{"version":3,"file":"claimgraph.cjs","sources":["../../../src/rdf-and-cd/claimgraph.js"],"sourcesContent":["// A claimgraph is expressed as list of rdf quads.\n// A quad contains four Terms, subject, property, object and graph.\n// A Term may be either:\n// an IRI: `{ Iri: '<iri>' }`,\n// a blank node: `{ Blank: '<string>' }`,\n// a literal node: `{ Literal: { value: '<string>', datatype: '<iri>' } }`\n// or the default graph: `{ DefaultGraph: true }`\n//\n// In compliance with https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal\n// a literal node must have a language tag if and only if datatype is\n// `http://www.w3.org/1999/02/22-rdf-syntax-ns#langString`\n//\n// The value of any DefualtGraph object must be `true`.\n//   Valid: `{ DefaultGraph: true }`\n//   Invalid: `{ DefaultGraph: false }`\n//   Invalid: `{ DefaultGraph: '' }`\n//   Invalid: `{ DefaultGraph: null }`\n//\n// ```\n// { Literal: {\n//    value: '<string>',\n//    datatype: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString',\n//    language: '<tag>',\n// } }\n// ```\n//\n// This module implements utilities for operating on claimgraphs if the above format.\n\nimport { assert } from '../utils/assert';\nimport { deepClone, assertValidClaimGraph } from './common';\n\nexport const claims = { Iri: 'https://www.dock.io/rdf2020#claimsV1' };\n\n// Convert from the json-ld claimgraph representation\n// https://github.com/digitalbazaar/jsonld.js/blob/983cd849f56180e2ee4552ca25dd52b293174830/lib/toRdf.js\n// to this module's representation.\nexport function fromJsonldjsCg(jcg) {\n  return jcg.map(({\n    subject, predicate, object, graph,\n  }) => [subject, predicate, object, graph].map(fromJsonldjsNode));\n}\n\n// convert a single node from json-ld claimgraph representation to this module's representation.\nexport function fromJsonldjsNode(jn) {\n  let ret;\n  switch (jn.termType) {\n    case 'DefaultGraph':\n      return { DefaultGraph: true };\n    case 'NamedNode':\n      return { Iri: jn.value };\n    case 'BlankNode':\n      return { Blank: jn.value };\n    case 'Literal':\n      assert(\n        jn.datatype.termType === 'NamedNode',\n        'The datatype of an RDF literal must be an IRI',\n      );\n      ret = { Literal: { value: jn.value, datatype: jn.datatype.value } };\n      if (\n        jn.datatype.value\n        === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'\n      ) {\n        assert(\n          jn.language !== undefined,\n          'Language tagged strings are expected to have a language tag.',\n        );\n        ret.Literal.language = jn.language;\n      }\n      return ret;\n    default:\n      throw new TypeError(`Unknown node type: ${jn.termType}`);\n  }\n}\n\n// Blank node name generator\n// An object of this type won't produce the same name twice, even accross multiple calls to\n// reallocateNames.\nexport class Namer {\n  constructor() {\n    this.nextBlank = 0;\n  }\n\n  // get the next available blank node\n  next() {\n    const ret = { Blank: `_:b${this.nextBlank}` };\n    this.nextBlank++;\n    return ret;\n  }\n\n  // modify claimgraph replacing blank nodes with names allocated by this namer\n  reallocateNames(claimgraph) {\n    const blanks = allBlanks(claimgraph);\n    const renames = {};\n    for (const name of blanks) {\n      renames[name] = this.next();\n    }\n    for (const claim of claimgraph) {\n      for (let i = 0; i < claim.length; i++) {\n        if (claim[i].Blank !== undefined) {\n          claim[i] = renames[claim[i].Blank];\n        }\n      }\n    }\n  }\n}\n\n// Merge multiple claimgraphs without conflating blank node names.\n// When merging, blank nodes are renamed as required in order to avoid conflicts.\n// Input is a list of claimgraphs.\nexport function merge(claimgraphs) {\n  claimgraphs.forEach(assertValidClaimGraph);\n  const cgs = deepClone(claimgraphs);\n  const namer = new Namer();\n  for (const cg of cgs) {\n    namer.reallocateNames(cg);\n  }\n  return cgs.flat(1);\n}\n\n// return the Set() of all bank node names in a claimgraph\nfunction allBlanks(cg) {\n  const ret = new Set();\n  for (const claim of cg) {\n    for (const node of claim) {\n      if (node.Blank !== undefined) {\n        ret.add(node.Blank);\n      }\n    }\n  }\n  return ret;\n}\n"],"names":["assert","assertValidClaimGraph","deepClone"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;A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