Exam­ples

A Weighted Graph

This doc­u­ment will give you a first impres­sion on how to store data within the edges of a graph.

sonesdev-02

The given graph is a weighted graph con­nect­ing a sin­gle type of ver­tices – Tags – using a sin­gle type of edges – their weighted rela­tion­ships.


Cre­at­ing the “Tag” ver­tex type

For the cre­ation of sin­gle ver­tex type we again use the CREATE TYPE state­ment. In this exam­ple the attribute “Name” is a sin­gle value basic type, whereas the attribute “Relat­ed­Tags” is a mul­ti­value ref­er­ence type with a spe­cial edge type “WEIGHTED<Double>“. This means that we want to store a numeric dou­ble pre­ci­sion value within every edge of our graph. The result­ing GQL state­ment is the following:

CREATE TYPE Tag
  ATTRIBUTES (
    String  Name,
    List<WEIGHTED(Double,DEFAULT=1)<Tag>> RelatedTags
  )
  INDICES (Name)


The optional para­me­ter “DEFAULT=1” within the CREATE TYPE state­ment will assume a value of “1” when­ever no other value was given within an INSERT or UPDATE state­ment.


Insert­ing vertices

For insert­ing ver­tices we use the INSERT state­ment. Within an insert state­ment all attrib­utes are optional by default, thus there is no need to store null–val­ues.

INSERT INTO Tag VALUES (Name = 'Summer')
INSERT INTO Tag VALUES (Name = 'Sun')
INSERT INTO Tag VALUES (Name = 'Diving')
INSERT INTO Tag VALUES (Name = 'Fun')


Insert­ing edges

UPDATE Tag SET (RelatedTags = SETOF(Name = 'Summer' : (0.5)) ) WHERE Name = 'Sun'
UPDATE Tag SET (RelatedTags = SETOF(Name = 'Fun'    : (4.0)) ) WHERE Name = 'Diving'
UPDATE Tag SET (RelatedTags = SETOF(Name = 'Diving' : (5.1)) ) WHERE Name = 'Fun'
UPDATE Tag SET (RelatedTags = SETOF(Name = 'Sun'    : (1.0),
                                    Name = 'Fun'    : (5.3)) ) WHERE Name = 'Summer'


Insert­ing ver­tices and edges simultaneously

INSERT INTO Tag VALUES (Name = 'Water', RelatedTags = SETOF(Name='Sun'    : (3.1),
                                                            Name='Diving' : (9.1),
                                                            Name='Fun'    : (9.1)))


SELECT­ing data from the graph

Using the Web­Shell and XML as out­put for­mat we now can run the fol­low­ing queries against the graph: FROM Tag SELECT *

<QueryResult>

  <Query ResultType="Successful">FROM Tag SELECT *</Query>

  <DBObject>
    <Attribute Name="Name"        Type="String">Summer</Attribute>
    <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Sun</Attribute>
    <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Diving</Attribute>
    <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Fun</Attribute>
    <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Water</Attribute>
    <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
  </DBObject>

  <Duration resolution="ms">1</Duration>

</QueryResult>
<QueryResult>

  <Query ResultType="Successful">FROM Tag SELECT * DEPTH 1</Query>

  <DBObject>
    <Attribute Name="Name"        Type="String">Summer</Attribute>
    <Attribute Name="RelatedTags" Type="LIST" Count="2">
      <DBObject Weight="5,3">
        <Attribute Name="Name"        Type="String">Fun</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>

      <DBObject Weight="1">
        <Attribute Name="Name"        Type="String">Sun</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>
    </Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Sun</Attribute>
    <Attribute Name="RelatedTags" Type="LIST" Count="1">
      <DBObject Weight="0,5">
        <Attribute Name="Name"        Type="String">Summer</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>
    </Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Diving</Attribute>
    <Attribute Name="RelatedTags" Type="LIST" Count="1">
      <DBObject Weight="4">
        <Attribute Name="Name"        Type="String">Fun</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>
    </Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Fun</Attribute>
    <Attribute Name="RelatedTags" Type="LIST" Count="1">
      <DBObject Weight="5,1">
        <Attribute Name="Name"        Type="String">Diving</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>
    </Attribute>
  </DBObject>

  <DBObject>
    <Attribute Name="Name"        Type="String">Water</Attribute>
    <Attribute Name="RelatedTags" Type="LIST" Count="3">
      <DBObject Weight="9,1">
        <Attribute Name="Name"        Type="String">Diving</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>

      <DBObject Weight="9,1">
        <Attribute Name="Name"        Type="String">Fun</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>

      <DBObject Weight="3,1">
        <Attribute Name="Name"        Type="String">Sun</Attribute>
        <Attribute Name="RelatedTags" Type="reference">not resolved</Attribute>
      </DBObject>
    </Attribute>
  </DBObject>

  <Duration resolution="ms">2</Duration>

</QueryResult>


Func­tions on weighted graphs

The weighted edges come with server­aly func­tions to make use of this weights.

MAXWEIGHT($attribute)

FROM Tag t SELECT t.Name, t.RelatedTags, MAXWEIGHT(t.RelatedTags) AS 'MaxWeight' DEPTH 1

TOP($attribute, num­ber)

FROM Tag t SELECT t.Name, TOP(t.RelatedTags, 2) AS 'RelatedTags' WHERE t.Name = 'Summer' DEPTH 1
FROM Tag t SELECT t.Name, TOP(t.RelatedTags, 5) AS 'RelatedTags' WHERE t.RelatedTags.Name = 'Summer' DEPTH 1