How can I insert a vertex between two currently connected vertexes? - titan

I am trying to to grips with Gremlin. Having thoroughly read the documentation I still appear to be struggling with it conceptually.
I am creating a basic newsfeed, following the model found in the Neo4j documentation here:
http://neo4j.com/docs/snapshot/cypher-cookbook-newsfeed.html
I am actually using titandb, but am following the same sort of principles/schema as shown above.
So far I have created a graph of user vertexes, which are connected via friend edges.
And I am able to add a new post vertex and connect it via a posted edge to a user vertex like so:
def activity = graph.addVertex(T.label, "post");
activity.property("post_id", post_id);
activity.property("time", time);
activity.property("body", body);
def g = graph.traversal();
def user = g.V().hasLabel("user").has("userid", userid).next();
user.addEdge("posted", activity, "time", time);
However, I need to be able to do the following in a single Gremlin script:
Create the new post vertex, as above.
Remove the old posted edge between the user and any currently connected post vertex. But only if, a post currently exists.
Attach the new post vertex to the user with a new posted edge.
Then finally, if there was a previous post vertex, attach it to the newly added post vertex via a next edge. Ultimately creating long streams of posts for each user.
I've been playing around, using trial and error, for what seems like hours now, and just cannot seem to get my head around it.
Any help would be greatly appreciated.

Another way (using a single traversal):
Create the initial graph with a single user:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("user").property("userid", 123)
==>v[0]
Add the first post:
gremlin> g.V().has("user", "userid", 123).as("user"). /* find the user */
addV("post").as("p").property("post_id", 1). /* add a new post */
property("time", System.currentTimeMillis()).
property("body", "bla bla").
addE("posted").from("user").as("e"). /* connect user and post */
property("time", System.currentTimeMillis()).
outV(). /* traverse to user */
outE("posted").where(neq("e")).as("o"). /* traverse to any pre-existing posted edge */
inV(). /* traverse to pre-existing post */
addE("next").to("p"). /* connect it with the new post */
select("o").drop() /* drop the old posted edge */
gremlin> // check
gremlin> g.V().not(inE()).repeat(union(outE("posted").inV(), inE("next").outV())).until(__.not(union(outE("posted"), inE("next")))).path().by(label)
==>[user, posted, post]
Add another post (same query):
gremlin> g.V().has("user", "userid", 123).as("user").
addV("post").as("p").property("post_id", 1).
property("time", System.currentTimeMillis()).
property("body", "bla bla").
addE("posted").from("user").as("e").
property("time", System.currentTimeMillis()).
outV().
outE("posted").where(neq("e")).as("o").
inV().
addE("next").to("p").
select("o").drop()
gremlin> // check
gremlin> g.V().not(inE()).repeat(union(outE("posted").inV(), inE("next").outV())).until(__.not(union(outE("posted"), inE("next")))).path().by(label)
==>[user, posted, post, next, post]

Here's one way you might do it:
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> createPost = { user, post ->
......1> u = g.V().has('userId', user).next()
......2> previous = g.V(u).outE('posted').inV().tryNext().orElse(null)
......3> g.V(u).outE('posted').drop().iterate()
......4> activity = graph.addVertex(T.label, "post", "postId", post)
......5> u.addEdge("posted", activity)
......6>
......6> if (null != previous) previous.addEdge('next', activity)
......7>
......7> // with titan you would want to commit the transaction
......8> // graph.tx().commit()
......9> }
==>groovysh_evaluate$_run_closure1#522b2631
Then add a user and call the createPost() function a few times:
gremlin> user = g.addV(label,"user", "userId", "me").next()
==>v[0]
gremlin> createPost("me",1)
gremlin> createPost("me",2)
==>e[8][2-next->5]
gremlin> createPost("me",3)
==>e[12][5-next->9]
gremlin> createPost("me",4)
==>e[16][9-next->13]
gremlin> createPost("me",5)
==>e[20][13-next->17]
You can see the "posted" edge aimed at the newest "post" vertex:
gremlin> g.V().has('userId','me').outE()
==>e[19][0-posted->17]
If we traverse out from there along the incoming "next" edge you can get to the previous post:
gremlin> g.V().has('userId','me').out('posted').in('next')
==>v[13]
The following shows three previous posts:
gremlin> g.V().has('userId','me').out('posted').in('next').in('next')
==>v[9]
You can also traverse to arbitrary depth along next:
gremlin> g.V().has('userId','me').
out('posted').
repeat(__.in('next')).
until(inE().count().is(0)).emit().
path().
by(choose(label().is('post'),
values('postId'),
values('userId')))
==>[me,5,4]
==>[me,5,4,3]
==>[me,5,4,3,2]
==>[me,5,4,3,2,1]

Related

Connect to OrientDB with the Gremlin javascript driver

I'm struggling to find an example using the current gremlin javascript driver with OrientDB. I can't get it to connect to OrientDB (already using the tinkerpop enabled version).
My sample code looks like this:
const gremlin = require("gremlin")
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection
const graph = new gremlin.structure.Graph()
const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/demodb'))
g.V().toList().then(function(data) {
console.log(data)
}).catch(function(err) {
console.log(err)
})
Does someone have any experience using those together? Thanks
If you want to connect OrientDB trough Gremlin try this:
Local -> gremlin> g = new OrientGraph("plocal:db_path/nomeDB")
In-Memory -> gremlin> g = new OrientGraph("memory:nomeDB")
Remote -> gremlin> g = new OrientGraph("remote:localhost/nomeDB")
Hope it helps
Regards
I dug a little bit. Gremlin Javascript driver does not support GraphSON3
https://github.com/jbmusso/gremlin-javascript/issues/109
Add this to your server.yaml serializer configuration in order to add support for v2
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { ioRegistries: [org.apache.tinkerpop.gremlin.orientdb.io.OrientIoRegistry] }}
Then it should work
Try this:
import * as gremlin from 'gremlin';
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(<db username>, <db password>);
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', {
authenticator: authenticator
}));
Notice the /gremlin in the url, not /demodb.
To point to demodb or another database modify the demodb.properties file in the config folder from OrientDB.
If you want you can create another properties file, just remember to point to it in the gremlin-server.yaml file.

Issue with posting a score to Fb from iOS (flash, air) app

The game is written on flash/as3, I'm using the Facebook ANE from freshplanet.
The call is done like that:
var data:Object = new Object();
data.score = 300;
data.access_token = _facebook.accessToken;
_facebook.requestWithGraphPath("/me/scores", data, "POST", onScoreSubmitted);
The result is:
{"error":{"code":100,"message":"(#100) The parameter score is required","type":"OAuthException"},"accessToken":"CAAGN..."}
Other requests like _facebook.requestWithGraphPath("/me"...), _facebook.requestWithGraphPath("/me/picture.type(large)"...) works well (return the desired data.
So I'm a bit blocked with the score posting (especially with the error type). Thanks for help!
I found the solution, here it is: _facebook.requestWithGraphPath("/me/scores?score=123"...
You can read the score for people playing your game by issuing an HTTP
GET request to /USER_ID/scores with a user access_token.
var data:Object = new Object();
data.score = 300;
data.access_token = _facebook.accessToken;
_facebook.requestWithGraphPath("/me/scores", data, "GET", onScoreSubmitted);
Just replace POST with GET

AngularJS $resource creating new object instead of updating object

// Set up the $resource
$scope.Users = $resource("http://localhost/users/:id");
// Retrieve the user who has id=1
$scope.user = $scope.Users.get({ id : 1 }); // returns existing user
// Results
{"created_at":"2013-03-03T06:32:30Z","id":1,"name":"testing","updated_at":"2013-03-03T06:32:30Z"}
// Change this user's name
$scope.user.name = "New name";
// Attempt to save the change
$scope.user.$save();
// Results calls POST /users and not PUT /users/1
{"created_at":"2013-03-03T23:25:03Z","id":2,"name":"New name","updated_at":"2013-03-03T23:25:03Z"}
I would expect that this would result in a PUT to /users/1 with the changed attribute. But it's instead POST to /users and it creates a new user (with a new id along with the new name).
Is there something that I'm doing wrong?
AngularJS v1.0.5
you just need to tell the $resource, how he has to bind the route-parameter ":id" with the JSON Object:
$scope.Users = $resource("http://localhost/users/:id",{id:'#id'});
This should work,
regards
thanks for the answer. It worked in the end for me too.
As a side note, I was using .NET WEB API and my entity has an Id property (UPPER CASE "I").
The PUT and DELETE worked only after I used the following:
$scope.Users = $resource("http://localhost/users/:Id",{Id:'#Id'});
Hope it helps.

Post on Friends' Wall(s) via Facebook Actionscript 3 SDK

I'm absolutely new to programming and just managed to learn the basics of ActionScript 3. Now, I would like to learn how to post on my Friends' Walls via the as3 SDK using the UI class (taken from a nice Tutorial):
This is how I post on my own Wall:
protected function newsFeed ():void
{
// define your caption text
var theCaption:String = "CaptionText";
// define the descrition text
var theDescription:String = "Text for game Achievement";
// We need to follow the FB docs to tell it what sort of input we are sending to FB
// We are trying to set the 'feed'
var methodInput:String = 'feed';
var thePicture:String = "mylink/picture.png";
var theLink:String = "mylink";
var theName:String = "Name of FB Status Setter";
// Create an object that we'll call 'data' and fill it with the actual data we're sending to Facebook
var data:Object = {
caption:theCaption,
description:theDescription,
picture:thePicture,
name:theName,
link:theLink
};
Facebook.ui(methodInput, data, onUICallback);
}
protected function onUICallback(result:Object):void
{
// do something
}
This works perfectly fine. I know that I have to integrate the parameter "to" somewhere. But I don't know where and how. Sorry I'm very very new to this. This is from Facebook Docs
Properties
from: The ID or username of the user posting the message. If this is unspecified, it defaults to the current user. If specified, it must be the ID of the user or of a page >that the user administers.
to: The ID or username of the profile that this story will be published to. If this >is unspecified, it defaults to the the value of from.
Hopefully someone can help me out.
Best Regards,
Amir
P.S.: Is there a way to post only one friend's wall and another way to post on several friends' walls?
I believe you want to use Facebook.api() rather than 'ui'. According to the documentation for the AS3 FB API, 'ui' just opens the share dialog. If you want to create a post on a friends wall, then you'll want to use 'api'.
I haven't tested this in Flash, but I think you can set the method as /PROFILE_ID/feed ... of course replacing "PROFILE_ID" with the FB uid of the friend. Then, include the arguments; message, picture, link, name, caption, description and source in your data object.
So your code would look something like:
var method:String = "/friend_id/feed";
var data:Object = {};
data.message = "Your message";
data.picture = "http://www.google.com/kittens.jpg";
data.link = "http://www.mysite.com/link";
data.caption = "Your caption";
data.description = "Your description";
data.source = "http://www.mysite.com/video.swf";//(optional) source is a video or Flash SWF
Facebook.api(method, yourCallback, data, "POST");
function yourCallback(result:Object, fail:Object):void {
if (result) {
trace(result)
} else if (fail) {
trace(fail);
}
}
If you have multiple friends, you could probably just put the uid's in an array and loop through the method above. The AS3 API has a batch request method that I haven't tried, but you can check out the Documentation.
Facebook has some pretty helpful tools that are somewhat hidden.
Checkout their Debugger and their Graph API Explorer
Hope that's helpful.

Retrieve Facebook Post Comments Using Graph API

I tried to get Facebook comments using:
http://graph.facebook.com/[post_id]/comments
It results only 2 of 15 comments, and without count info.
{
"data": [
{
"id": "[post_id]",
"from": {
"name": "[name]",
"id": "[id]"
},
"message": "[message]",
"created_time": "2011-01-23T02:36:23+0000"
},
{
"id": "[id]",
"from": {
"name": "[name]",
"id": "[id]"
},
"message": "[message]",
"created_time": "2011-01-23T05:16:56+0000"
}
]
}
Anyone know why only 2 comments?
Also, I want to retrieve comments (default number) or retrieve comments with my limit number, and get its comments count. Any idea? (Please use Graph API).
You need to call it from a secure request https and provide an access_token:
https://graph.facebook.com/19292868552_118464504835613/comments?access_token=XXX
EDIT:
Added the object from the post document. try clicking the comments connection and then remove the access_token and try and see the difference.
In order to get the Like count and the comment count then you need to use a combination of the PostOwnerID and PostID not just the PostID
So for your example it would be:
https://graph.facebook.com/153125724720582_184234384932460/comments
Again, as mentioned in some of the other answers you need to use the https method along with an auth_token
I experienced the same problem with comments. The issue was that I was using an access token for a test user. Because test users don't have access to other FB users information, only the comments from pages were shown.
There is a word JUGAAR in Urdu that means, finding a way out, just to get the job done. So for like purpose I made this JUGAAR, I hope it helps.
$contents = file_get_contents("http://graph.facebook.com/" . $_GET['id'] . "/likes");
if (substr_count($contents, 'name')>0) {
echo substr_count($contents, 'name') . " people like this album";
}
By the way I am also new to this Fb stuff, I am looking for help to post comments. When I try to use graph.api./id/comments?access_token=sdfsfsdf&message="D" it still returns comments for the id instead of posting.
As a sanity check, do you have "read_stream" permission? I can see the full comments with my access token that uses "read_stream". As mentioned by other people, you have to use https and access token as well...
Try to authenticate via App Login (http://developers.facebook.com/docs/authentication) and then to call GraphAPI with access_token prarameter.
You can do something like this to avoid the whole count of comments issues:
Get the object's (a post is considered an object in the Graph API) ID-as I understand from your question, you already have it?
Create a Comments Social Plugin with this ID, and get the code for it.
Embed the code in your site.
This will result in all the comments for this object.
To get the count of comments per object, you can execute an fql query, something like this:
SELECT comments FROM stream WHERE post_id = [yourpostid]
This will return in the comments array under the count parameter the number of counts for this object.
SELECT comments FROM stream WHERE post_id = [yourpostid] shall not work in this case ..
the id which is returned after making a graph call successfully to post on a user's wall (using access_token of an application ) is of the form abcdef_qwerty ( underscore seperated id )
where as the post id which is mapped in the post_id of the comments table is of the form "lmnop" ..
to get the counts of like and comments on this post id of form "abcdef_qwerty" making a graph call withh app generated access token seems to be the only solution ..
something like:
https://graph.facebook.com/100002619172565_117323155031656?access_token=xxxxxxxxxxxxx
After Successfully Login call this method facebookComments()
parameters.putString("fields", "message"); .............// Its Important
AccessToken accessToken = AccessToken.getCurrentAccessToken();
public void facebookComments() {
try {
getFriends(accessToken, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.e("keshav", "one" + response);
CommonMethod.showAlert("res --> " + response, MainActivity.this);
}
}
);
} catch (Exception e) {
CommonMethod.showAlert("Exception is -> " + e, MainActivity.this);
}
}
public void getFriends(AccessToken token, GraphRequest.Callback callback)
{
// TODO Comments Working but id return only
GraphRequest requestt = new GraphRequest(token, "744511125731315_751199848395776/comments",
null, HttpMethod.GET, callback);
Bundle parameters = new Bundle();
parameters.putString("fields", "id"); // todo in use imp
parameters.putString("fields", "name"); // todo in use imp
parameters.putString("fields", "from"); // todo in use imp
parameters.putString("fields", "message"); // todo in use imp
requestt.setParameters(parameters);
requestt.executeAsync();
}
It results only 2 of 15 comments
Add a limit parameter to the URL:
http://graph.facebook.com/[post_id]/comments?limit=1000&access_token=XXX
This should show all the comments.