public function setUp() { $this->connection = getConnection(); $v = time(); $this->graphName = "graph" . $v; $this->v1 = "v1" . $v; $this->v2 = "v2" . $v; $this->v3 = "v3" . $v; $this->v4 = "v4" . $v; $this->v5 = "v5" . $v; $this->e1 = "e1" . $v; $this->e2 = "e2" . $v; $param1 = array(); $param1[] = $this->v1; $param1[] = $this->v2; $param2 = array(); $param2[] = $this->v3; $param2[] = $this->v4; $ed1 = EdgeDefinition::createDirectedRelation($this->e1, $param1, $param2); $ed2 = EdgeDefinition::createUndirectedRelation($this->e2, $this->v5); $this->graph = new Graph($this->graphName); $this->graph->addEdgeDefinition($ed1); $this->graph->addEdgeDefinition($ed2); $this->graph->addOrphanCollection("orphan"); $this->graphHandler = new GraphHandler($this->connection); $this->graphHandler->createGraph($this->graph); $this->graph = $this->graphHandler->getGraph($this->graphName); $this->vertex1Array = array('_key' => "vertex1", 'someKey1' => 'someValue1', 'sharedKey1' => 1); $this->vertex2Array = array('_key' => "vertex2", 'someKey2' => 'someValue2', 'sharedKey1' => 2); $this->vertex3Array = array('_key' => "vertex3", 'someKey3' => 'someValue3', 'sharedKey1' => 1); $this->vertex4Array = array('_key' => "vertex4", 'someKey4' => 'someValue4', 'sharedKey1' => 2); $this->vertex5Array = array('_key' => "vertex5", 'someKey5' => 'someValue5', 'a' => 3, 'sharedKey1' => 1); $this->vertex6Array = array('_key' => "vertex6", 'someKey6' => 'someValue6', 'sharedKey1' => 1); $this->vertex7Array = array('_key' => "vertex7", 'someKey7' => 'someValue7', 'a' => 3, 'sharedKey1' => 1); $this->edge1Array = array('_key' => "edge1", 'someEdgeKey1' => 'someEdgeValue1', 'sharedKey1' => 1, 'weight' => 10); $this->edge2Array = array('_key' => "edge2", 'someEdgeKey2' => 'someEdgeValue2', 'sharedKey2' => 2, 'weight' => 15); $this->edge3Array = array('_key' => "edge3", 'sharedKey3' => 2, 'weight' => 12); $this->edge4Array = array('_key' => "edge4", 'sharedKey4' => 1, 'weight' => 7); $this->edge5Array = array('_key' => "edge5", 'sharedKey5' => 1, 'weight' => 5); $this->edge6Array = array('_key' => "edge6", 'sharedKey6' => 1, 'weight' => 2); }
/** * replaces an edge definition of the graph. * * This will replace an edge definition in the graph.<br><br> * * * @throws Exception * * @param mixed $graph - graph name as a string or instance of Graph * @param EdgeDefinition $edgeDefinition - the edge definition. * * @return Graph * @since 2.2 */ public function replaceEdgeDefinition($graph, $edgeDefinition) { if ($graph instanceof Graph) { $graph = $graph->getKey(); } $url = UrlHelper::buildUrl(Urls::URL_GRAPH, array($graph, Urls::URLPART_EDGE, $edgeDefinition->getRelation())); $data = $edgeDefinition->transformToArray(); try { $response = $this->getConnection()->put($url, $this->json_encode_wrapper($data)); } catch (Exception $e) { throw new ClientException($e->getMessage()); } $data = $response->getJson(); $options['_isNew'] = false; $result = Graph::createFromArray($data['graph'], $options); $result->set(Graph::ENTRY_KEY, $data['graph'][self::OPTION_NAME]); return $result; }
/** * Test adding, getting and deleting of edgecollections */ public function testAddGetDeleteEdgeCollections() { $this->graph = new Graph('Graph1'); $ed1 = EdgeDefinition::createUndirectedRelation("undirected", "singleV"); $this->graph->addEdgeDefinition($ed1); $this->graphHandler = new GraphHandler($this->connection); $result = $this->graphHandler->createGraph($this->graph); $this->assertTrue($result['_key'] == 'Graph1', 'Did not return Graph1!'); $this->graph = $this->graphHandler->addEdgeDefinition($this->graph, EdgeDefinition::createUndirectedRelation("undirected2", "singleV2")); $this->assertTrue($this->graphHandler->getEdgeCollections($this->graph) === array(0 => 'undirected', 1 => 'undirected2')); $error = null; try { $this->graph = $this->graphHandler->addEdgeDefinition($this->graph, EdgeDefinition::createUndirectedRelation("undirected2", "singleV2")); } catch (\Exception $e) { $error = $e->getMessage(); } $this->assertTrue($error === "multi use of edge collection in edge def"); $error = null; try { $this->graph = $this->graphHandler->getEdgeCollections("bla"); } catch (\Exception $e) { $error = $e->getMessage(); } $this->assertTrue($error === "graph not found"); $this->graph = $this->graphHandler->deleteEdgeDefinition($this->graph, "undirected"); $this->assertTrue($this->graphHandler->getEdgeCollections($this->graph) === array(0 => 'undirected2')); $error = null; try { $this->graph = $this->graphHandler->deleteEdgeDefinition("bla", "undefined"); } catch (\Exception $e) { $error = $e->getMessage(); } $this->assertTrue($error === "graph not found"); $this->graph = $this->graphHandler->replaceEdgeDefinition($this->graph, EdgeDefinition::createUndirectedRelation("undirected2", "singleV3")); $ed = $this->graph->getEdgeDefinitions(); $ed = $ed[0]; $ed = $ed->getToCollections(); $this->assertTrue($ed[0] === "singleV3"); $error = null; try { $this->graph = $this->graphHandler->replaceEdgeDefinition($this->graph, EdgeDefinition::createUndirectedRelation("notExisting", "singleV3")); } catch (\Exception $e) { $error = $e->getMessage(); } $this->assertTrue($error === "edge collection not used in graph"); }
/** * Set a graph attribute * * The key (attribute name) must be a string. * This will validate the value of the attribute and might throw an * exception if the value is invalid. * * @throws ClientException * * @param string $key - attribute name * @param mixed $value - value for attribute * * @return void */ public function set($key, $value) { if ($key === self::ENTRY_EDGE_DEFINITIONS) { if ($this->_doValidate) { ValueValidator::validate($value); } foreach ($value as $ed) { $edgeDefinition = new EdgeDefinition(); foreach ($ed[self::ENTRY_FROM] as $from) { $edgeDefinition->addFromCollection($from); } foreach ($ed[self::ENTRY_TO] as $to) { $edgeDefinition->addToCollection($to); } $edgeDefinition->setRelation($ed[self::ENTRY_COLLECTION]); $this->addEdgeDefinition($edgeDefinition); } } else { if ($key === self::ENTRY_ORPHAN_COLLECTIONS) { if ($this->_doValidate) { ValueValidator::validate($value); } foreach ($value as $o) { $this->addOrphanCollection($o); } } else { parent::set($key, $value); } } }