ラベル SPARQL の投稿を表示しています。 すべての投稿を表示
ラベル SPARQL の投稿を表示しています。 すべての投稿を表示

2011年9月14日水曜日

メモ的: Twitterのhome_timelineの情報を無理矢理4storeに入れてみる(いろんな意味でいろいろ間違ってるとは思う)

  • OAuthConsumerフレームワーク('#import <OAuthConsumer/OAuthConsumer.h>')についてはこちら.
  • 4storeのクライアントライブラリ(っぽいなにか)('#import "FourStore.h"')についてはこちら.

    #import "TwTurtleAppDelegate.h"
    #import <OAuthConsumer/OAuthConsumer.h>
    #import "FourStore.h"

    @implementation TwTurtleAppDelegate

    @synthesize window;

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
    OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"XXXXXXXXXXXXXXXXXXXXXX"
    secret:@"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"];

    OAToken *accessToken= [[OAToken alloc] initWithKey:@"XXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    secret:@"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"];

    NSURL *url= [NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.xml"];
    OAMutableURLRequest *requestWithBodyParams = [[[OAMutableURLRequest alloc] initWithURL:url
    consumer:consumer
    token:accessToken
    realm:nil
    signatureProvider:nil] autorelease];

    OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];

    [fetcher fetchDataWithRequest:requestWithBodyParams
    delegate:self
    didFinishSelector:@selector(apiTicket:didFinishWithData:)
    didFailSelector:@selector(apiTicket:didFailWithError:)];

    }
    - (void)fourstoreWithTurtle:(NSString *)turtle
    {
    FourStore *store= [[FourStore alloc] initWithEndpoint:@"http://localhost:8000/sparql/"];
    [store addPrefix:@"tsp" uri:@"http://example.org/twitter/status/predicate/"];
    [store addPrefix:@"tu" uri:@"http://example.org/twitter/users/"];

    NSString *message0= [store deleteWithGraph:@"http://example.org/twitter/status/"];
    NSLog(@"message0 %@", message0);


    NSString *message1= [store addTurtle:turtle graph:@"http://example.org/twitter/status/"];
    NSLog(@"message1 %@", message1);

    NSString *message2= [store queryWithString:
    @"SELECT ?s ?p ?o \nWHERE {\nGRAPH <http://example.org/twitter/status/> {\n?s ?p ?o .\n}\n}\n"];
    NSLog(@"message2 \n%@", message2);

    }
    - (NSString *)createTurtleWithXMLData:(NSData *)data
    {
    NSMutableString *turtle= [NSMutableString string];

    NSXMLDocument *document = [[[NSXMLDocument alloc] initWithData:data
    options:NSXMLDocumentTidyHTML
    error:NULL] autorelease];
    NSError *error= nil;
    NSArray *nodes = [document nodesForXPath:@"//status" error:&error];
    NSEnumerator *nodesEnum= [nodes objectEnumerator];
    NSXMLNode *node= nil;
    while(node= [nodesEnum nextObject]) {
    NSArray *idnodes= [node nodesForXPath:@"id" error:&error];
    if ([idnodes count]==0) continue;
    NSString *idString= [[idnodes objectAtIndex:0] stringValue];
    NSEnumerator *childrenEnum= [[node children] objectEnumerator];
    NSXMLNode *child=nil;
    while(child= [childrenEnum nextObject]) {
    NSString *name= [child name];
    if ([name isEqualToString:@"id"])
    continue;
    NSString *value= nil;
    if ([name isEqualToString:@"retweeted_status"]){
    NSArray *cidnodes= [child nodesForXPath:@"id" error:&error];
    if ([cidnodes count]==0) continue;
    NSString *cidString= [[cidnodes objectAtIndex:0] stringValue];
    value= [NSString stringWithFormat:@"<%@>", cidString];
    } else if ([name isEqualToString:@"user"]){
    NSArray *cidnodes= [child nodesForXPath:@"id" error:&error];
    if ([cidnodes count]==0) continue;
    NSString *cidString= [[cidnodes objectAtIndex:0] stringValue];
    value= [NSString stringWithFormat:@"<tu:%@>", cidString];
    } else {
    if ([[child stringValue] length]==0) continue;
    value= [child stringValue];
    value= [value stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    value= [value stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
    value= [NSString stringWithFormat:@"\"%@\"", value];
    }

    [turtle appendFormat:@"<%@> tsp:%@ %@ .\n", idString, name, value];
    }
    }


    return turtle;
    }
    - (void)apiTicket:(OAServiceTicket *)ticket
    didFinishWithData:(NSData *)data
    {

    if (0)
    NSLog(@"didFinish %@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);

    NSString *turtle= [self createTurtleWithXMLData:data];

    if (0)
    NSLog(@"\n%@", turtle);

    [self fourstoreWithTurtle:turtle];
    }
    - (void)apiTicket:(OAServiceTicket *)ticket
    didFailWithError:(NSError *)error
    {

    NSLog(@"%@", error);
    }
    @end



    やってみてアレコレ悩んだ足跡.
  • 2011年9月12日月曜日

    Cocoa ( Objective-C )での 4store の Client Library (?)

    動機
    4storeを入れてみた.rubyのclient用ライブラリをみて,似たものをcocoaでやってみようと思った(たぶん既にどこかにあるだろうけど).
    入れる際(環境: MacOSX10.7)にハマった点
  • MacPortsでraptorとrasqalを入れたのだがraptorに1.x(www/raptor)と2.x(www/raptor2)があり, rasqal0.9.26がraptor2.xを利用して入っているのにraptor 1.xが入ったままだと4storeのmake時にエラーがでたこと.
  • "git clone https://github.com/garlik/4store.git"でとってきたらconfigureがなかったので(そういうものなのかな?), "sh autogen.sh"から始めたこと.



  • やってみた(rubyにあるload作ってないしsetもマネの途中)
    FourStore.h

    @interface FourStore : NSObject
    {
    NSURL *_urlSPARQL;
    NSURL *_urlDATA;
    NSMutableArray *_prefixs;
    }
    - (id)initWithEndpoint:(NSString *)endpoint;
    - (void)addPrefix:(NSString *)prefix uri:(NSString *)uri;
    - (NSString *)addTurtle:(NSString *)turtle graph:(NSString *)graph;
    //- (NSString *)setTurtle:(NSString *)turtle graph:(NSString *)graph;
    - (NSString *)queryWithString:(NSString *)query;
    - (NSString *)deleteWithGraph:(NSString *)graph;
    @end

    FourStore.m

    #import "FourStore.h"

    @implementation FourStore

    - (id)init
    {
    self = [super init];
    if (self) {
    }
    return self;
    }
    - (id)initWithEndpoint:(NSString *)endpoint
    {
    if (![endpoint hasSuffix:@"/sparql/"]) {
    return nil;
    }
    self = [self init];
    if (self) {
    _urlSPARQL= [[NSURL URLWithString:endpoint] retain];
    NSArray *ary= [endpoint componentsSeparatedByString:@"/sparql/"];
    _urlDATA= [[NSURL URLWithString:[NSString stringWithFormat:@"%@/data/", [ary objectAtIndex:0]]] retain];
    _prefixs= [[NSMutableArray array] retain];
    }
    return self;
    }
    - (void)dealloc
    {
    [_urlSPARQL release];
    [_urlDATA release];
    [super dealloc];
    }
    + (NSString *)sendRequest:(NSURLRequest *)request
    {
    NSURLResponse *response=nil;
    NSError *error= nil;
    NSData *data= [NSURLConnection sendSynchronousRequest:request
    returningResponse:&response
    error:&error];
    if (error) {
    NSLog(@"statusCode %ld", [response statusCode]);
    NSLog(@"error code %ld", [error code]);
    }

    NSString *message= [[NSString alloc] initWithData:data
    encoding:NSUTF8StringEncoding];
    return [message autorelease];
    }
    + (NSString *)send:(NSString *)request url:(NSURL *)url method:(NSString *)method
    {
    NSMutableURLRequest *requestWithBodyParams = [[NSMutableURLRequest alloc] initWithURL:url];
    NSData *requestData = [request dataUsingEncoding:NSASCIIStringEncoding
    allowLossyConversion:YES];
    [requestWithBodyParams setHTTPBody:requestData];
    [requestWithBodyParams setValue:[NSString stringWithFormat:@"%d", [requestData length]]
    forHTTPHeaderField:@"Content-Length"];
    [requestWithBodyParams setValue:@"application/x-www-form-urlencoded"
    forHTTPHeaderField:@"Content-Type"];
    [requestWithBodyParams setHTTPMethod:method];

    NSString *msg= [FourStore sendRequest:requestWithBodyParams];
    [requestWithBodyParams release];

    return msg;
    }

    - (void)addPrefix:(NSString *)prefix uri:(NSString *)uri
    {
    [_prefixs addObject:[NSDictionary dictionaryWithObjectsAndKeys:
    prefix, @"p", uri, @"u", nil]];
    }
    - (NSString *)prefixsOfQuery
    {
    NSMutableString *ret= [NSMutableString string];
    NSEnumerator *prefixsEnum= [_prefixs objectEnumerator];
    NSDictionary *dic;
    while(dic= [prefixsEnum nextObject]) {
    [ret appendFormat:@"PREFIX %@: <%@>\n", [dic objectForKey:@"p"], [dic objectForKey:@"u"]];
    }
    return [[ret retain] autorelease];
    }
    - (NSString *)prefixsOfTurtle
    {
    NSMutableString *ret= [NSMutableString string];
    NSEnumerator *prefixsEnum= [_prefixs objectEnumerator];
    NSDictionary *dic;
    while(dic= [prefixsEnum nextObject]) {
    [ret appendFormat:@"@prefix %@: <%@> .\n", [dic objectForKey:@"p"], [dic objectForKey:@"u"]];
    }
    return [[ret retain] autorelease];
    }
    - (NSString *)queryWithString:(NSString *)query
    {
    NSString *fullquery= [NSString stringWithFormat:@"%@%@", [self prefixsOfQuery], query];

    NSString *request= [NSString stringWithFormat:@"query=%@&soft-limit=%@",
    [fullquery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], @""];

    return [FourStore send:request url:_urlSPARQL method:@"POST"];
    }
    - (NSString *)addTurtle:(NSString *)turtle graph:(NSString *)graph
    {
    NSString *fullturtle= [NSString stringWithFormat:@"%@%@", [self prefixsOfTurtle], turtle];

    NSString *request= [NSString stringWithFormat:@"mime-type=%@&data=%@&graph=%@",
    [@"application/x-turtle" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
    [fullturtle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
    [graph stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    return [FourStore send:request url:_urlDATA method:@"POST"];
    }
    //- (NSString *)setTurtle:(NSString *)turtle graph:(NSString *)graph
    //{
    // NSString *fullturtle= [NSString stringWithFormat:@"%@%@", [self prefixsOfTurtle], turtle];
    //
    // NSString *request= [fullturtle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //
    // NSURL *url= [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [_urlDATA absoluteString], graph]];
    //
    // return [FourStore send:request url:url method:@"PUT"];
    //}
    - (NSString *)deleteWithGraph:(NSString *)graph
    {
    NSURL *url= [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [_urlDATA absoluteString], graph]];

    return [FourStore send:@"" url:url method:@"DELETE"];
    }
    @end



    使い方(?)

    FourStore *store= [[FourStore alloc] initWithEndpoint:@"http://localhost:8000/sparql/"];
    [store addPrefix:@"dc" uri:@"http://purl.org/dc/elements/1.1/"];

    if (1) {
    NSString *msg= [store addTurtle:@"<http://example.org/book/book1> dc:title \"SPARQL Tutorial\" ."
    graph:@"http://example.org/book"];
    NSLog(@"%@", msg);
    }
    if (1) {
    NSString *msg= [store queryWithString:
    @"SELECT ?title \nWHERE {\nGRAPH <http://example.org/book> {\n<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .\n}\n}\n"];
    NSLog(@"%@", msg);

    }
    if (1) {
    NSString *msg= [store queryWithString:
    @"SELECT ?title \nWHERE {\nGRAPH <http://example.org/book> {\n<http://example.org/book/book1> dc:title ?title .\n}\n}\n"];
    NSLog(@"%@", msg);

    }
    if (1) {
    NSString *msg= [store deleteWithGraph:@"http://example.org/book"];
    NSLog(@"%@", msg);
    }

    [store release];


    結果

    (> 4s-backend-setup [KB])
    > 4s-backend [KB]
    > 4s-httpd -p 8000 -U [KB]


    > 200 added successfully
    This is a 4store SPARQL server v1.1.3-54-gd8008fc

    > <?xml version="1.0"?>
    <sparql xmlns="http://www.w3.org/2005/sparql-results#">
    <head>
    <variable name="title"/>
    </head>
    <results>
    <result>
    <binding name="title"><literal>SPARQL Tutorial</literal></binding>
    </result>
    </results>
    </sparql>

    > <?xml version="1.0"?>
    <sparql xmlns="http://www.w3.org/2005/sparql-results#">
    <head>
    <variable name="title"/>
    </head>
    <results>
    <result>
    <binding name="title"><literal>SPARQL Tutorial</literal></binding>
    </result>
    </results>
    </sparql>

    > 200 deleted successfully
    This is a 4store SPARQL server v1.1.3-54-gd8008fc

    2011年6月19日日曜日

    ARQを使ってSPARQLをやってみた2(FROMとFROM NAMEDの結合)

    これ('RDF用クエリ言語SPARQL/8.2.3 FROMとFROM NAMEDの結合')をやってみた.


    やってみた
    0.環境
    前回環境を参考

    1.データファイル
    data_8.2.1_1.ttl
    # Default graph (stored at http://example.org/dft.ttl)
    @prefix dc: <http://purl.org/dc/elements/1.1/> .

    <http://example.org/bob> dc:publisher "Bob Hacker" .
    <http://example.org/alice> dc:publisher "Alice Hacker" .


    data_8.2.1_2.ttl
    # Named graph: http://example.org/bob
    @prefix foaf: <http://xmlns.com/foaf/0.1/> .

    _:a foaf:name "Bob" .
    _:a foaf:mbox <mailto:bob@oldcorp.example.org> .


    data_8.2.1_3.ttl
    # Named graph: http://example.org/alice
    @prefix foaf: <http://xmlns.com/foaf/0.1/> .

    _:a foaf:name "Alice" .
    _:a foaf:mbox <mailto:alice@work.example.org> .

    各ファイルの'#'行はコメントなのでこれでナニカを定義しているわけではない.

    2.コード
    Test_8_2_3.java
    package sample;

    import org.openjena.atlas.lib.StrUtils;

    import com.hp.hpl.jena.query.DataSource;
    import com.hp.hpl.jena.query.DatasetFactory;
    import com.hp.hpl.jena.query.Query;
    import com.hp.hpl.jena.query.QueryExecution;
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.QueryFactory;
    import com.hp.hpl.jena.query.ResultSetFormatter;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.util.FileManager;

    public class Test_8_2_3 {
    public static void main(String[] args) {
    Model model1 = ModelFactory.createDefaultModel();
    FileManager.get().readModel(model1, "data_8.2.3_1.ttl");
    Model model2 = ModelFactory.createDefaultModel();
    FileManager.get().readModel(model2, "data_8.2.3_2.ttl");
    Model model3 = ModelFactory.createDefaultModel();
    FileManager.get().readModel(model3, "data_8.2.3_3.ttl");

    DataSource dataSource= DatasetFactory.create();
    dataSource.setDefaultModel(model1);
    dataSource.addNamedModel("http://example.org/bob", model2);
    dataSource.addNamedModel("http://example.org/alice", model3);
    System.out.println(dataSource.asDatasetGraph().toString());
    System.out.println();


    String queryString = StrUtils.strjoin("\n",
    "PREFIX foaf: <http://xmlns.com/foaf/0.1/>",
    "PREFIX dc: <http://purl.org/dc/elements/1.1/>",
    "",
    "SELECT ?who ?g ?mbox",
    "FROM <http://example.org/dft.ttl>",
    "FROM NAMED <http://example.org/alice>",
    "FROM NAMED <http://example.org/bob>",
    "WHERE",
    "{",
    " ?g dc:publisher ?who .",
    " GRAPH ?g { ?x foaf:mbox ?mbox }",
    "}");
    Query query = QueryFactory.create(queryString);

    QueryExecution qExec = QueryExecutionFactory.create(query, dataSource);
    ResultSetFormatter.out(System.out, qExec.execSelect(), query);
    qExec.close();
    }
    }

    ('addNamedModelの第一引数の値で関連づけている)

    3.実行
    (dataset
    (graph
    (triple <http://example.org/alice> <http://purl.org/dc/elements/1.1/publisher> "Alice Hacker")
    (triple <http://example.org/bob> <http://purl.org/dc/elements/1.1/publisher> "Bob Hacker")
    )
    (graph <http://example.org/bob>
    (triple _:-335a33de:130a7151814:-7fff <http://xmlns.com/foaf/0.1/mbox> <mailto:bob@oldcorp.example.org>)
    (triple _:-335a33de:130a7151814:-7fff <http://xmlns.com/foaf/0.1/name> "Bob")
    )
    (graph <http://example.org/alice>
    (triple _:-335a33de:130a7151814:-7ffe <http://xmlns.com/foaf/0.1/mbox> <mailto:alice@work.example.org>)
    (triple _:-335a33de:130a7151814:-7ffe <http://xmlns.com/foaf/0.1/name> "Alice")
    ))



    ----------------------------------------------------------------------------------
    | who | g | mbox |
    ==================================================================================
    | "Alice Hacker" | <http://example.org/alice> | <mailto:alice@work.example.org> |
    | "Bob Hacker" | <http://example.org/bob> | <mailto:bob@oldcorp.example.org> |
    ----------------------------------------------------------------------------------



    ARQを使ってSPARQLをやってみた

    これ('RDF用クエリ言語SPARQL/2.1 シンプルなクエリの記述')をやってみた.

    やってみた.
    0.ARQ
    ARQはこちらのページから取得(ARQ-2.8.8).

    1.Eclipseプロジェクト
    Eclipseのプロジェクトにまるごと入れてlib中のjar上のコンテクストメニューにてBuild Path/Add to Build Pathにてパスを通す.

    2.コードを書く
    package sample;

    import org.openjena.atlas.lib.StrUtils;

    import com.hp.hpl.jena.query.Query;
    import com.hp.hpl.jena.query.QueryExecution;
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.QueryFactory;
    import com.hp.hpl.jena.query.ResultSetFormatter;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.Resource;
    import com.hp.hpl.jena.vocabulary.DC;

    public class Test {
    public static void main(String[] args) {
    Model model = ModelFactory.createDefaultModel() ;
    Resource resource = model.createResource("http://example.org/book/book1");
    resource.addProperty(DC.title, "SPARQL Tutorial");

    String queryString = StrUtils.strjoin("\n",
    "SELECT ?title",
    "WHERE{",
    " <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .",
    "}");
    Query query = QueryFactory.create(queryString);

    QueryExecution qExec = QueryExecutionFactory.create(query, model);
    ResultSetFormatter.out(System.out, qExec.execSelect(), query);
    qExec.close();
    }
    }


    データをファイルで与える場合:
    package sample;

    import org.openjena.atlas.lib.StrUtils;

    import com.hp.hpl.jena.query.Query;
    import com.hp.hpl.jena.query.QueryExecution;
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.QueryFactory;
    import com.hp.hpl.jena.query.ResultSetFormatter;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.util.FileManager;

    public class Test {
    public static void main(String[] args) {
    Model model = ModelFactory.createDefaultModel();
    FileManager.get().readModel(model, "data.ttl");


    String queryString = StrUtils.strjoin("\n",
    "SELECT ?title",
    "WHERE{",
    " <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .",
    "}");
    Query query = QueryFactory.create(queryString);

    QueryExecution qExec = QueryExecutionFactory.create(query, model);
    ResultSetFormatter.out(System.out, qExec.execSelect(), query);
    qExec.close();
    }
    }

    'data.ttl'ファイル.拡張子はttlである必要があるみたい(?).
    <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" .


    3.実行(画像をクリックで拡大)


    追記
    'RDF用クエリ言語SPARQL/2.2 複数マッチ'の実行(画像をクリックで拡大)


    追記2
    'RDF用クエリ言語SPARQL/2.3 RDFリテラルのマッチング'の実行(画像をクリックで拡大)