Exemple #1
0
    $actors->add($kevin, 'name', $kevin->getProperty('name'));
    $matrix = $client->makeNode()->setProperty('title', 'The Matrix')->save();
    $higherLearning = $client->makeNode()->setProperty('title', 'Higher Learning')->save();
    $mysticRiver = $client->makeNode()->setProperty('title', 'Mystic River')->save();
    $keanu->relateTo($matrix, 'IN')->save();
    $laurence->relateTo($matrix, 'IN')->save();
    $laurence->relateTo($higherLearning, 'IN')->save();
    $jennifer->relateTo($higherLearning, 'IN')->save();
    $laurence->relateTo($mysticRiver, 'IN')->save();
    $kevin->relateTo($mysticRiver, 'IN')->save();
    // Find a path
} else {
    if ($cmd == 'path' && !empty($argv[2]) && !empty($argv[3])) {
        $from = $argv[2];
        $to = $argv[3];
        $fromNode = $actors->findOne('name', $from);
        if (!$fromNode) {
            echo "{$from} not found\n";
            exit(1);
        }
        $toNode = $actors->findOne('name', $to);
        if (!$toNode) {
            echo "{$to} not found\n";
            exit(1);
        }
        // Each degree is an actor and movie node
        $maxDegrees = 6;
        $depth = $maxDegrees * 2;
        $path = $fromNode->findPathsTo($toNode)->setmaxDepth($depth)->getSinglePath();
        if ($path) {
            foreach ($path as $i => $node) {
    }
    foreach ($streets as $info) {
        $start = $inters[$info[0]];
        $end = $inters[$info[1]];
        $properties = $info[2];
        $street = $start->relateTo($end, 'CONNECTS')->setProperties($properties);
        $street->save();
    }
    // Find a path
} else {
    if ($cmd == 'path' && !empty($argv[2]) && !empty($argv[3])) {
        $from = $argv[2];
        $to = $argv[3];
        $algorithm = null;
        $all = !empty($argv[4]) && $argv[4] == 'all';
        $fromNode = $intersections->findOne('name', $from);
        $toNode = $intersections->findOne('name', $to);
        $finder = $fromNode->findPathsTo($toNode, 'CONNECTS', Relationship::DirectionOut)->setMaxDepth(5);
        $algorithm = !empty($argv[4]) ? $argv[4] : null;
        // Find all paths regardless of complexity or distance
        if ($algorithm == 'all') {
            $finder->setAlgorithm(PathFinder::AlgoAllSimple);
            // Find paths with the smallest number of instructions
        } else {
            if ($algorithm == 'simple') {
                $finder->setAlgorithm(PathFinder::AlgoShortest);
                // Find the most optimal paths
            } else {
                $finder->setAlgorithm(PathFinder::AlgoDijkstra)->setCostProperty('distance');
            }
        }
Exemple #3
0
        }
        // Find stores where the part was sold
    } else {
        if ($cmd == 'stores' && !empty($argv[2])) {
            $partName = $argv[2];
            // Use the Cypher query language
            if (!empty($argv[3]) && $argv[3] == 'cypher') {
                $queryTemplate = "START part=node:parts3('name:{$partName}') " . "MATCH (store)-[:SOLD]->()-[:CONTAINS]->(part) " . "RETURN store, count(*)";
                $query = new Cypher\Query($client, $queryTemplate);
                $result = $query->getResultSet();
                echo "Found " . count($result) . " stores:\n";
                foreach ($result as $row) {
                    echo "* " . $row['store']->getProperty('name') . "\n";
                }
                // Use javascript traversal
            } else {
                $part = $partsIndex->findOne('name', $partName);
                if (!$part) {
                    die("{$partName} not found.\n");
                }
                $traversal = new Traversal($client);
                $traversal->addRelationship('CONTAINS', Relationship::DirectionIn)->addRelationship('SOLD', Relationship::DirectionIn)->setMaxDepth(4)->setReturnFilter('javascript', '(position.length() > 0 && position.lastRelationship().getType() == "SOLD");');
                $stores = $traversal->getResults($part, Traversal::ReturnTypeNode);
                echo "Found " . count($stores) . " stores:\n";
                foreach ($stores as $store) {
                    echo "* {$store->getProperty('name')}\n";
                }
            }
        }
    }
}