Skip to content

Commit

Permalink
Parse, Formula and Statement models to TS linkeddata#355
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Oct 14, 2019
1 parent d25af46 commit e419e0a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
39 changes: 29 additions & 10 deletions src/formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,27 @@ import { ValueType, Bindings } from './types'
* @module formula
*/
export default class Formula extends Node {

static termType: 'Graph';

/**
* The stored statements
*/
statements: Statement[];
statements: ReadonlyArray<Statement>;

/**
* The additional constraints
*/
constraints: ReadonlyArray<any>;

/**
* The additional constraints
*/
initBindings: {
[id: string]: Node;
}

optional: ReadonlyArray<any>

/**
* Initializes this formula
Expand Down Expand Up @@ -48,25 +65,23 @@ export default class Formula extends Node {
* Gets a namespace for the specified namespace's URI
* @param nsuri The URI for the namespace
*/
ns(nsuri: string): (ln: string) => NamedNode;

static termType: string;
ns (nsuri: string) { return Namespace(nsuri) }

/** Add a statement from its parts
* @param {Node} subject - the first part of the statemnt
* @param {Node} predicate - the second part of the statemnt
* @param {Node} obbject - the third part of the statemnt
* @param {Node} graph - the last part of the statemnt
*/
add (subject: Node, predicate: NamedNode, object: Node, graph: NamedNode) {
return this.statements.push(new Statement(subject, predicate, object, graph))
add (subject: Node, predicate: NamedNode, object: Node, graph?: NamedNode) {
return (this.statements as Statement[]).push(new Statement(subject, predicate, object, graph))
}

/** Add a statment object
* @param {Statement} statement - an existing constructed statement to add
*/
addStatement (st: Statement): number {
return this.statements.push(st)
return (this.statements as Statement[]).push(st)
}

/**
Expand Down Expand Up @@ -268,7 +283,7 @@ export default class Formula extends Node {
o?: Node | null,
g?: Node | null
): Node[] {
var elt, i, l, m, q
var elt: Statement, i, l, m, q
var len, len1, len2, len3
var results = []
var sts = this.statementsMatching(s, p, o, g)
Expand Down Expand Up @@ -515,7 +530,8 @@ export default class Formula extends Node {
return this.NTtoURI(this.findTypesNT(subject))
}

/** Trace statements which connect directly, or through bnodes
/**
* Trace statements which connect directly, or through bnodes
*
* @param subject - The node to start looking for statments
* @param doc - The document to be searched, or null to search all documents
Expand Down Expand Up @@ -749,7 +765,7 @@ export default class Formula extends Node {
* Gets an named node for an URI
* @param uri The URI
*/
sym(uri: string | NamedNode): NamedNode {
sym(uri: string | NamedNode, name?: string): NamedNode {
if (name) {
throw new Error('This feature (kb.sym with 2 args) is removed. Do not assume prefix mappings.')
}
Expand Down Expand Up @@ -875,6 +891,9 @@ export default class Formula extends Node {
toString(): string {
return '{' + this.statements.join('\n') + '}'
}

variable (name: string): Variable

/**
* Gets the number of statements in this formulat that matches the specified pattern
* @param s The subject
Expand Down
7 changes: 0 additions & 7 deletions src/namespace.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import NamedNode from './named-node'

/**
* Gets a namespace for the specified namespace's URI
* @param nsuri The URI for the namespace
*/
export default function Namespace (nsuri: string) {
return function (ln: string) {
return new NamedNode(nsuri + (ln || ''))
}
}
4 changes: 4 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import sparqlUpdateParser from './patch-parser'
import * as Util from './util'
import Formula from './formula';

/**
* Set of handled MIME types
*/
export type mimeTypes =
'application/ld+json' |
'application/n-quads' |
Expand Down Expand Up @@ -78,6 +81,7 @@ export default function parse (
throw new Error("Don't know how to parse " + contentType + ' yet')
}
} catch (e) {
throw e
executeErrorCallback(e)
}

Expand Down
35 changes: 22 additions & 13 deletions src/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ import { Bindings, ValueType } from './types'
import Literal from './literal'
import Node from './node-internal'
import Collection from './collection'
import { NamedNode } from './index';
import DefaultGraph from './default-graph';
import BlankNode from './blank-node';
import Variable from './variable';

export type SubjectType = NamedNode | Variable | BlankNode
export type PredicateType = NamedNode | Variable
export type ObjectType = NamedNode | Literal | Collection | BlankNode | Variable
export type GraphType = NamedNode | DefaultGraph | Variable

/** A Statement represents an RDF Triple or Quad. */
export default class Statement {
/** The subject of the triple. What the Statement is about. */
subject: Node;
subject: SubjectType

/** The relationship which is asserted between the subject and object */
predicate: Node;
predicate: PredicateType

/** The thing or data value which is asserted to be related to the subject */
object: Node | Literal | Collection;
object: ObjectType

/**
* The why param is a named node of the document in which the triple when
* it is stored on the web.
*/
why: Node;
why?: GraphType

/**
* Construct a new Triple Statment
Expand All @@ -37,21 +46,21 @@ export default class Statement {
* powerful update() which can update more than one docment.
*/
constructor (
subject: Node,
predicate: Node,
object: Node | ValueType,
why,
subject: SubjectType,
predicate: PredicateType,
object: ObjectType,
why?: GraphType,
) {
this.subject = Node.fromValue(subject) as Node
this.predicate = Node.fromValue(predicate) as Node
this.object = Node.fromValue(object) as Node
this.why = why // property currently used by rdflib
this.subject = Node.fromValue(subject) as SubjectType
this.predicate = Node.fromValue(predicate) as PredicateType
this.object = Node.fromValue(object) as ObjectType
this.why = why as GraphType // property currently used by rdflib
}

/**
* The graph param is a named node of the document in which the triple is stored on the web.
*/
get graph (): Node {
get graph (): GraphType {
return this.why
}

Expand Down

0 comments on commit e419e0a

Please sign in to comment.