72. Database construction Create a graph database example import edu.upc.dama.dex.core.*; … DEX dex = new DEX(); GraphPoolgpool = dex.create(“C:/image.dex”); Session s = gpool.newSession(); … … s.close(); gpool.close(); dex.close();
73. Databaseconstruction Add nodes intGraph#newNodeType(String name) Creates a new node type with the given unique name. Returns the DEX node type identifier. long Graph#newNode(intnodeType) Creates a new node belonging to the given node type. Returns the DEX object identifier.
74. Databaseconstruction Add edges intGraph#newEdgeType(String name, bool directed) Creates a new edge type with the given unique name. Directed or undirected edge type. Returns the DEX edge type identifier. intGraph#newRestrictedEdgeType(String name, intsrcNodeType, intdstNodeType) Creates a new directed edge type with the given unique name. (Integrity restriction) Source and destionation of the edge instances are restricted to the given node types. Returns the DEX edge type identifier. long Graph#newEdge(long tail, long head, intedgeType) Creates a new edge belonging to the given edge type. Tail is the source and head is the target (iff directed). Returns the DEX edge identifier.
75. Databaseconstruction Add nodes and edges example … DbGraphdbg = s.getDbGraph(); intperson = dbg.newNodeType(“PERSON”); long p1 = dbg.newNode(person); long p2 = dbg.newNode(person); long p3 = dbg.newNode (person); intfriend = dbg.newUndirectedEdgeType(“FRIEND”); long e1 = dbg.newEdge(p1, p2, friend); long e2 = dbg.newEdge(p2, p3, friend); intloves = dbg.newEdgeType(“LOVES”); long e3 = dbg.newEdge(p1, p3, loves); … p1 p2 p3 p1 p2 p3
76. Databaseconstruction Manage attributes classValue Encapsulates a value and itsdomain (data type): String, Integer, Long, Double, Boolean, Timestamp. Use themto set and getattributevaluesfortheobjects. longGraph#newAttribute(inttype, Stringname, short dataType, short kind) Creates a new attributewiththegivenuniquenameforthegivennodeoredgetype. Returnsthe DEX attributeidentifier. “dataType” can be: Value#STRING, Value#INT, Value#LONG, Value#DOUBLE, Value#BOOL, Value#TIMESTAMP. “kind” can be: Graph#ATTR_KIND_BASIC. Basic attribute (just set and getvalues). Grahp#ATTR_KIND_INDEXED. Indexedattribute (set and getvalues as well as selectoperations) Graph#ATTR_KINDUNIQUE. Indexedattribute. Unique (PK).
77. Databaseconstruction Manage attributes Graph#setAttribute(longoid, longattr, Value v) Sets thegivenValueforthegivenattributetothegivenobjectidentifier. Givenattributemustbedefinedfortheobject’stype. Value ‘s data typemust match attribute’s data typeor NULL. Graph#getAttribute(longoid, longattr, Value v) GetstheValueforthegivenattribute and forthegivenobjectidentifier. Givenattributemustbedefinedfortheobject’stype.
78. Databaseconstruction Manage attributes example … longname = dbg.newAttribute(person, “NAME”, Value.STRING); longage= dbg.newAttribute(person, “AGE”, Value.INT); Value v = new Value(); dbg.setAttribute(p1, age, v.setInt(18)); dbg.setAttribute(p2, name, v.setString(“KELLY")); dbg.setAttribute(p3, name, v.setString(“MARY")); … longsince = dbg.newAttribute(friend, “SINCE”, Value.INT); dbg.setAttribute(e1, since, v.setInt(2000)); dbg.setAttribute(e2, since, v.setInt(1995)); … JOHN 18 KELLY MARY JOHN 18 2000 KELLY MARY 1995
79. Databaseconstruction Manage attributes example … int phones = dbg.newEdgeType("phones"); long when = dbg.newAttribute(phones, "when", Value.STRING); long e4 = dbg.newEdge(p1, p3, phones); dbg.setAttribute(e4, when, v.setString("4pm"))); long e5 = dbg.newEdge(p1, p3, phones); dbg.setAttribute(e5, when, v.setString("5pm")); long e6 = dbg.newEdge(p3, p2, phones); dbg.setAttribute(e6, when, v.setString("6pm")); … System.out.println(dbg.getAttribute(p1, name)); System.out.println(dbg.getAttribute(e4, when)); System.out.println(dbg.getAttribute(e5, when)); JOHN 18 2000 KELLY 4pm 5pm MARY 1995 6pm
88. Validateconstruction Validate construction example import java.io.PrintWriter; import java.io.FileWriter; … //Default Export is used in the example by stating null. //Personalized export can be created implementing class Export PrintWriterstdOut = new PrintWriter( new FileWriter("out.graphml")); dbg.export(stdOut, Type.YGRAPHML, null); pw.close(); //Dump data file gpool.dumpData(“out.data”); //Dump internal storage file gpool.dumpStorage(“out.storage”);
89. Validateconstruction Data file example Edgetypes -- 1:2 = Friend -- 2:3 = Loves -- 3:4 = phones edgetypes = 3 -- 4:1 = Person ------- #00000400 [Person] ATTR=1 //Person//age{18} TO [Friend] #00000401 [Person] ATTR=1 //Person//name{kelly} TO [Loves] #00000402 [Person] ATTR=1 //Person//name{mary} TO [phones] #00000402 [Person] ATTR=1 //Person//name{mary} #00000402 [Person] ATTR=1 //Person//name{mary} … ------- 3 nodes. node types = 4 # of edgetypes Node of typeperson Valuefor “age” attribute “Phones” out-goingedges # of nodetypes
90. Validateconstruction Internal storage file example Number of nodes 2011-02-09 12:12:45.330 ... SIZE: 262KB / 2688KB NODES: 3 EDGES: 6 ... - NODES Person : N=3 ... ATTR DATA: 4 ... - //Friend//since[INDEXED|INDEXED LINK] 2/2... [1995] - [2000] - //Person//age[INDEXED|INDEXED LINK] 1/1 [18] - [18] ... Number of edges Number of “Person”s Number of attributes Name and kind of theattribute Minimum and maximumvalue # differentvalues / # values
100. QueryDatabase Objects longObjects#union(Objectsobjs) this = this UNION objs Returnsthe new size of thecollection. longObjects#intersection(Objectsobjs) this = this INTERSECTION objs Returnsthe new size of thecollection. longObjects#difference(Objectsobjs) this = this DIFFERENCE objs Returnsthe new size of thecollection.
101. QueryDatabase Retrieve data ObjectsGraph#select(int t) Retrievesobjectidentifiersbelongingtothegivennodeoredgetype. ObjectsGraph#select(longattr, short op, Value v) Retrievesobjectidentifierswhichsatisfaythequery. “op” can be: Graph#OPERATION_{EQ|NE|GT|GE|LT|LE|LIKE|ERE} longGraph#findObj(longattr, Value v) Randomlyretrievesanobjectidentifierwhich has thegivenvalueforthegivenattribute (or INVALID_OID ifnotfound). Usefulforunique atributes.
102. QueryDatabase Navigation ObjectsGraph#explode(longoid, intedgeType, short direction) Retrievesout-goingor in-goingedges (orboth) fromortothegivennodeidentifier and forthegivenedgetype. “direction” can be: Graph#EDGES_IN, Graph#EDGES_OUT, Graph#EDGES_BOTH. ObjectsGraph#neighbors(longoid, intedgeType, short direction) Retrievesneighbornodestothegivennodeidentifierwhich can bereachedthroughthegivenedgetype and direction. “direction” can be: Graph#EDGES_IN, Graph#EDGES_OUT, Graph#EDGES_BOTH.
103. QueryDatabase Retrieve data example … DbGraphdbg = s.getDbGraph(); Objectspersons = dbg.select(person); Objects.Iteratorit = persons.iterator(); while (it.hasNext()) { long p = it.next(); Stringname = dbg.getAttribute(p, name).toString(); } it.close(); persons.close(); … JOHN 18 JOHN 18 2000 5pm KELLY KELLY 4pm 1995 MARY MARY 6pm
113. GraphAlgorithms Traversals example Graphgraph = … // graphinstance long idsource= … // sourcenode TraversalBFSbfs = new TraversalBFS(graph, idsource); // Addingtheallowededgetypes for traversingthegraph fs.addEdge(graph.findType("street"), Algorithm.NAVIGATION_FORWARD); bfs.addEdge(graph.findType("road"), Algorithm.NAVIGATION_FORWARD); // Addingtheallowed node types for traversingthegraph bfs.addAllNodes(); // Runningthealgorithm long idnode; while(bfs.hasNext()) { idnode= bfs.next(); } // Closingthetraversal bfs.close();
114. GraphAlgorithms Shortest paths classSinglePairShortestPath Findstheshortestpathbetweentwogivennodeidentifiers. Node and edgetypes can befilteredout. ClassSinglePairShortestPathBFS Uses a BFS traversaltofindtheshortestpath. For un-weightededges. ClassSinglePairShortestPathDijkstra Uses a Dijkstraalgorithmtofindtheshortestpath. Forweightededges.
116. GraphAlgorithms Connected components classConnectivity Retrievesconnectedcomponents of thegivengraph. Node and edgetypes can befilteredout. Connectedcomponents can bematerializedintoanattribute. classStrongConnectivityGabow Retrievesstrongconnectedcomponents in a directedgraph. Uses theGabowalgorithm. classWeakConnectivityDFS Retrievesweaklyconnectedcomponents in a undirectedgraphor in a directedgraph (ignoringdirections). Uses a DFS algorithm.