動機
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
0 件のコメント:
コメントを投稿