1.3.1 • Published 6 years ago

grakn v1.3.1

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
6 years ago

Grakn Node.js Client

A Node.js client for Grakn

Requires Grakn 1.3.0 && Node >= 6.5.0

Installation

To install the Grakn client, simply run:

npm install grakn

You will also need access to a Grakn database. Head here to get started with Grakn.

Quickstart

Begin by importing Grakn:

const Grakn = require('grakn');

Now you can create a new session and open a new Grakn transaction:

const grakn = new Grakn('localhost:48555');
const session = grakn.session('keyspace');
const tx = await session.transaction(Grakn.txType.WRITE);

Execute Graql query (this example works inside an async function):

const resultIterator = await tx.query("match $x isa person; limit 10; get;"); // This will return an Iterator of ConceptMap Answer
const answer = await resultIterator.next(); // Take first ConceptMap Answer
const person = answer.map().get('x'); // Access map in Answer with answer.map() and take Concept associated to variable x from 'match $x isa person; get;'
tx.close();

API Reference

First create a new Grakn object with

// URI must be a string containing host address and gRPC port of a running Grakn instance, e.g. "localhost:48555"
const grakn = new Grakn(URI);

on the Grakn object the following methods are available:

Grakn

MethodReturn typeDescription
session(String keyspace)SessionReturn a new Session bound to the specified keyspace
async keyspaces().delete(String keyspace)voidDeletes the specified keyspace
async keyspaces().retrieve()Array of StringRetrieves all available keyspaces

on the Session the following methods are available:

Session

MethodReturn typeDescription
async transaction(Grakn.txType)TransactionReturn a new Transaction bound to the keyspace of this session
close()voidThis must be used to correctly terminate session and close communication with server.

Once obtained a Transaction you will be able to:

Transaction

MethodReturn typeDescription
async query(String graqlQuery[, { infer }])Iterator of AnswerExecutes a Graql query on the session keyspace. It's possible to specify whether to enable inference passing an object with infer property set to true or false. Inference is ON by default.
async commit()voidCommit current Transaction, persisting changes in the graph. After committing, the transaction will be closed and you will need to get a new one from the session
async close()voidCloses current Transaction without committing. This makes the transaction unusable.
async getConcept(String conceptId)Concept or nullRetrieves a Concept by ConceptId
async getSchemaConcept(String label)SchemaConcept or nullRetrieves a SchemaConcept by label
async getAttributesByValue(attributeValue, Grakn.dataType)Iterator of AttributeGet all Attributes holding the value provided, if any exists
async putEntityType(String label)EntityTypeCreate a new EntityType with super-type entity, or return a pre-existing EntityType with the specified label
async putRelationshipType(String label)RelationshipTypeCreate a new RelationshipType with super-type relation, or return a pre-existing RelationshipType with the specified label
async putAttributeType(String label, Grakn.dataType)AttributeTypeCreate a new AttributeType with super-type attribute, or return a pre-existing AttributeType with the specified label and DataType
async putRole(String label)RoleCreate a Role, or return a pre-existing Role, with the specified label.
async putRule(String label, String when, String then)RuleCreate a Rule, or return a pre-existing Rule, with the specified label

Iterator

Some of the following Concept methods return an Iterator, on every iterator the following methods are available:

MethodReturn typeDescription
async next()IteratorElement or nullRetrieves next element or returns null when no more elements are available
async collect()Array of IteratorElementConsumes the iterator and collect all the elements into an array
async collectConcepts()Array of ConceptConsumes the iterator and return array of Concepts. This helper is only available on Iterator containing ConceptMap Answer, returned by transaction.query().. It is useful when one wants to work directly on Concepts without the need to traverse the result map or access the explanation.

IteratorElement

Element handled by iterators, depending on the type of iterator this can be a type of Concept or an Answer.

Answer

This object represents a query answer and it is contained in the Iterator returned by transaction.query() method.
There are different types of Answer, based on the type of query executed a different type of Answer will be returned:

Query TypeAnswer Type
defineConceptMap
undefineConceptMap
getConceptMap
insertConceptMap
deleteConceptMap
aggregate count/min/max/sum/mean/stdValue
aggregate groupAnswerGroup
compute count/min/max/sum/mean/stdValue
compute pathConceptList
compute clusterConceptSet
compute centralityConceptSetMeasure

ConceptMap

MethodReturn typeDescription
map()Map<String, Concept>Returns result map in which every variable name (key) is linked to a Concept.
explanation()Explanation or nullReturns an Explanation object if the current Answer contains inferred Concepts, null otherwise.

Value

MethodReturn typeDescription
number()NumberReturns numeric value of the Answer.
explanation()Explanation or nullReturns an Explanation object if the current Answer contains inferred Concepts, null otherwise.

ConceptList

MethodReturn typeDescription
list()Array of StringReturns list of Concept IDs.
explanation()Explanation or nullReturns an Explanation object if the current Answer contains inferred Concepts, null otherwise.

ConceptSet

MethodReturn typeDescription
set()Set of StringReturns a set containing Concept IDs.
explanation()Explanation or nullReturns an Explanation object if the current Answer contains inferred Concepts, null otherwise.

ConceptSetMeasure

MethodReturn typeDescription
measurement()NumberReturns numeric value that is associated to the set of Concepts contained in the current Answer.
set()Set of StringReturns a set containing Concept IDs.
explanation()Explanation or nullReturns an Explanation object if the current Answer contains inferred Concepts, null otherwise.

AnswerGroup

MethodReturn typeDescription
owner()ConceptReturns the Concepts which is the group owner.
answers()Array of AnswerReturns list of Answers that belongs to this group.
explanation()Explanation or nullReturns an Explanation object if the current Answer contains inferred Concepts, null otherwise.

Explanation

MethodReturn typeDescription
queryPattern()StringReturns a query pattern that describes how the owning answer was retrieved.
answers()Array of AnswerSet of deducted/factual answers that allowed us to determine that the owning answer is true

Concepts hierarchy

Grakn is composed of different types of Concepts, that have a specific hierarchy

                                                Concept
                                                /       \
                                              /           \
                                            /               \
                                          /                   \
                                  SchemaConcept                    Thing
                                  /     |    \                    /   |  \
                                /       |     \                 /     |    \
                              /         |      \              /       |      \
                            Type      Rule    Role     Entity    Attribute   Relationship
                        /     |   \
                      /       |     \
                    /         |       \
            EntityType  AttributeType  RelationshipType

All Concepts are bound to the transaction that has been used to retrieve them. If for any reason a trasaction gets closed all the concepts bound to it won't be able to communicate with the database anymore, so all the methods won't work and the user will have to re-query for the needed concepts.


Concept

These methods are available on every type of Concept

MethodReturn typeDescription
async delete()voidDelete concept
isSchemaConcept()BooleanCheck whether this Concept is a SchemaConcept
isType()BooleanCheck whether this Concept is a Type
isThing()BooleanCheck whether this Concept is a Thing
isAttributeType()BooleanCheck whether this Concept is an AttributeType
isEntityType()BooleanCheck whether this Concept is an EntityType
isRelationshipType()BooleanCheck whether this Concept is a RelationshipType
isRole()BooleanCheck whether this Concept is a Role
isRule()BooleanCheck whether this Concept is a Rule
isAttribute()BooleanCheck whether this Concept is an Attribute
isEntity()BooleanCheck whether this Concept is a Entity
isRelationship()BooleanCheck whether this Concept is a Relationship

Schema concept

A SchemaConcept concept has all the Concept methods plus the following:

MethodReturn typeDescription
async label()StringGet label of this SchemaConcept
async label(String value)voidSet label of this SchemaConcept
async isImplicit()BooleanReturns true when the SchemaConcept is implicit, i.e. when it's been created by Grakn and not explicitly by the user, false when explicitly created by the user.
async sup(Type)voidSet direct super SchemaConcept of this SchemaConcept
async sup()SchemaConcept or nullGet direct super SchemaConcept of this SchemaConcept
async subs()Iterator of SchemaConceptGet all indirect subs of this SchemaConcept.
async sups()Iterator of SchemaConceptGet all indirect sups of this SchemaConcept.

Thing

A Thing concept has all the Concept methods plus the following:

MethodReturn typeDescription
async isInferred()BooleanReturns true if this Thing is inferred by Reasoner, false otherwise
async type()TypeReturns a Type which is the type of this Thing. This Thing is an instance of that type.
async relationships(...Role)Iterator of RelationshipReturns Relationships which this Thing takes part in, which may optionally be narrowed to a particular set according to the Roles you are interested in
async attributes(...AttributeType)Iterator of AttributeReturns Attributes attached to this Thing, which may optionally be narrowed to a particular set according to the AttributeTypes you are interested in
async roles()Iterator of RoleReturns the Roles that this Thing is currently playing
async keys(...Attributetype)Iterator of AttributeReturns a collection of Attribute attached to this Thing as a key, which may optionally be narrowed to a particular set according to the AttributeTypes you are interested in
async has(Attribute)voidAttaches the provided Attribute to this Thing
async unhas(Attribute)voidRemoves the provided Attribute from this Thing

Attribute

An Attribute concept has all the Thing methods plus the following:

MethodReturn typeDescription
async value()StringGet value of this Attribute
async owners()Iterator of ThingReturns the set of all Things that possess this Attribute

Relationship

A Relationship concept has all the Thing methods plus the following:

MethodReturn typeDescription
async rolePlayersMap()Map<Role, Set<Thing>>Returns a Map that links all the Roles of this Relationship to all the Things that are playing each Role
async rolePlayers(...Role)Iterator of ThingReturns a list of every Thing involved in this Relationship, optionally filtered by Roles played
async assign(Role, Thing)voidExpands this Relationship to include a new role player (Thing) which is playing a specific Role
async unassign(Role, Thing)voidRemoves the Thing which is playing a Role in this Relationship.
NB: There are no specific methods for `Entity` concept.

Type

A Type concept has all the SchemaConcept methods plus the following:

MethodReturn typeDescription
async isAbstract(Boolean)voidSets the Type to be abstract - which prevents it from having any instances
async isAbstract()BooleanReturns true if the type is set to abstract, false otherwise
async playing()Iterator of RoleReturns all the Roles which instances of this Type can indirectly play
async plays(Role)voidAdd a new Role to the ones that the instances of this Type are allowed to play
async attributes()Iterator of AttributeTypeThe AttributeTypes which this Type is linked with.
async instances()Iterator of ThingGet all indirect instances of this Type
async keys()Iterator of AttributeTypeThe AttributeTypes which this Type is linked with as a key
async key(AttributeType)voidCreates an implicit RelationshipType which allows this Type and a AttributeType to be linked in a strictly one-to-one mapping.
async has(AttributeType)voidAdd a new AttributeType which the instances of this Type are allowed to have attached to themselves
async unplay(Role)voidDelete a Role from the ones that the instances of this Type are allowed to play
async unhas(AttributeType)voidDelete AttributeType from the ones that the instances of this Type are allowed to have attached to themselves
async unkey(AttributeType)voidDelete AttributeType from available keys

AttributeType

An AttributeType concept has all the Type methods plus the following:

MethodReturn typeDescription
async create(value)AttributeCreate new Attribute of this type with the provided value. The value provided must conform to the DataType specified for this AttributeType
async attribute(value)Attribute or nullRetrieve the Attribute with the provided value if it exists
async dataType()StringGet the data type to which instances of the AttributeType must have
async regex()String or nullRetrieve the regular expression to which instances of this AttributeType must conform to, or null if no regular expression is set
async regex(String regex)voidSet the regular expression that instances of the AttributeType must conform to

RelationshipType

A RelationshipType concept has all the Type methods plus the following:

MethodReturn typeDescription
async create()RelationshipCreates and returns a new Relationship instance, whose direct type will be this type
async roles()Iterator of RoleReturns a list of the RoleTypes which make up this RelationshipType
async relates(Role)voidSets a new Role for this RelationshipType
async unrelate(Role)voidDelete a Role from this RelationshipType

EntityType

An EntityType concept has all the Type methods plus the following:

MethodReturn typeDescription
async create()EntityCreates and returns a new Entity instance, whose direct type will be this type

Role

A Role concept has all the SchemaConcept methods plus the following:

MethodReturn typeDescription
async relationships()Iterator of RelationshipTypeReturns the RelationshipTypes that this Role takes part in.
async players()Iterator of TypeReturns a collection of the Types that can play this Role

Rule

A Rule concept has all the SchemaConcept methods plus the following:

MethodReturn typeDescription
async getWhen()StringRetrieves the when part of this Rule. When this query is satisfied the "then" part of the rule is executed
async getThen()StringRetrieves the then part of this Rule. This query is executed when the "when" part of the rule is satisfied
1.3.1

6 years ago

1.3.0

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

7 years ago

0.0.5

7 years ago