Mercurial > code > home > repos > homeauto
view service/rfid_pn532/rdf.nim @ 507:f3c1d2e7c5df
add missing files for the record
Ignore-this: 8541c95ef1644cf85b311259602d2892
author | drewp@bigasterisk.com |
---|---|
date | Sun, 21 Apr 2019 03:28:21 -0700 |
parents | |
children |
line wrap: on
line source
import algorithm import hashes import json import sequtils import sets import strformat import strutils import rdf_nodes type Namespace* = object of RootObj prefix: string proc initNamespace*(prefix: string): Namespace = result.prefix = prefix proc `[]`*(self: Namespace, tail: string): Uri = initUri(self.prefix & tail) type Quad* = tuple[s: Uri, p: Uri, o: RdfNode, g: Uri] proc toJsonLd*(quads: HashSet[Quad]): string = var graphs: HashSet[Uri] = toSet[Uri]([]) graphs.init() for q in quads: graphs.incl(q.g) var graphUris = toSeq[Uri](graphs.items) graphUris.sort(cmpUri) var graphJson = newJArray() for g in graphUris: var quadsInGraph: seq[JsonNode] for q in quads: if q.g == g: quadsInGraph.add(%* {"@id": $q.s, $q.p: [toJsonLdObject(q.o)]}) graphJson.add(%* {"@graph": quadsInGraph, "@id": $g}) $graphJson proc hash*(x: Quad): Hash = hash(1) type Patch* = object of RootObj addQuads*: HashSet[Quad] delQuads*: HashSet[Quad] proc toJson*(self: Patch): string = $ %* {"patch" : {"adds": self.addQuads.toJsonLd(), "deletes": self.delQuads.toJsonLd()}} type Graph* = object of RootObj stmts*: HashSet[Quad] proc len*(self: Graph): int = len(self.stmts) proc initGraph*(): Graph = result.stmts.init() proc applyPatch*(self: var Graph, p: Patch) = self.stmts.excl(p.delQuads) self.stmts.incl(p.addQuads) proc toNquads*(self: var Graph): string = var lines: seq[string] = @[] for q in self.stmts: lines.add(&"{q.s.toNt()} {q.p.toNt()} {q.o.toNt()} {q.g.toNt()} .\n") return lines.join("") proc toNtriples*(stmts: openArray[Quad]): string = var lines: seq[string] = @[] for q in stmts: lines.add(&"{q.s.toNt()} {q.p.toNt()} {q.o.toNt()} .\n") return lines.join("")