PageRenderTime 63ms CodeModel.GetById 19ms app.highlight 28ms RepoModel.GetById 14ms app.codeStats 0ms

/rdf/ntriple/ntriple.factor

http://github.com/x6j8x/factor-rdf
Unknown | 111 lines | 87 code | 24 blank | 0 comment | 0 complexity | 8b03393b2f7e79e002e56598efdad43c MD5 | raw file
  1USING: accessors arrays assocs byte-arrays combinators math math.parser
  2combinators.short-circuit io io.encodings io.streams.string
  3kernel namespaces peg peg.ebnf rdf rdf.util sequences strings ;
  4IN: rdf.ntriple
  5
  6SINGLETON: ntriple
  7
  8<PRIVATE
  9
 10: ?skip-whitespace ( -- char )
 11    read1 dup whitespace?
 12    [ drop ?skip-whitespace ] [  ] if ; inline recursive
 13
 14: read-literal ( -- literal )
 15    "\"" cs-read-until drop >string
 16    read1
 17    {
 18        { [ dup CHAR: @ = ] [ drop " ." cs-read-until drop >string <lang-literal> ] }
 19        { [ dup CHAR: ^ = ] [ drop 2 read drop read-uriref <type-literal> ] }
 20        [ drop <literal> ]
 21    } cond ;
 22
 23: read-subject ( -- subject )
 24    ?skip-whitespace
 25    {
 26        { [ dup CHAR: < = ] [ drop read-uriref ] }
 27        { [ dup CHAR: _ = ] [ drop read-bnode ] }
 28        [ drop f ]
 29    } cond ;
 30
 31: read-predicate ( -- predicate )
 32    ?skip-whitespace CHAR: < =
 33    [ read-uriref ] [ f ] if ;
 34
 35: read-object ( -- object )
 36    ?skip-whitespace
 37    {
 38        { [ dup CHAR: < = ] [ drop read-uriref ] }
 39        { [ dup CHAR: _ = ] [ drop read-bnode ] }
 40        { [ dup CHAR: " = ] [ drop read-literal ] }
 41        [ drop f ]
 42    } cond ;
 43
 44: (collect) ( acc value -- acc value )
 45    [ [ over push ] keep ] [ f ] if* ; inline
 46
 47: read-ntriple ( line -- triple )
 48    [
 49        V{ } clone {
 50            [ read-subject (collect) ]
 51            [ read-predicate (collect) ]
 52            [ read-object (collect) ]
 53        } 0&&
 54        [ >array ] [ drop f ] if
 55    ] with-string-reader ; inline
 56
 57: read-ntriples ( -- seq )
 58    V{ } clone [ read-ntriple [ over push ] when* ] each-line ;
 59
 60GENERIC: write-ntriple-part ( object -- )
 61
 62M: uriref write-ntriple-part ( object -- )
 63    CHAR: < write1 cs-output
 64    [ [ ns>> uri>> ] dip stream-write ]
 65    [ [ id>> ] dip stream-write ] 2bi
 66    B{ CHAR: > CHAR: space } write ;
 67
 68M: bnode write-ntriple-part ( object -- )
 69    B{ CHAR: _ CHAR: : } write
 70    id>> >byte-array write
 71    CHAR: space write1 ;
 72
 73: write-lang ( lang -- )
 74    CHAR: @ write1 >byte-array write ;
 75
 76: write-type ( uriref -- )
 77    B{ CHAR: ^ CHAR: ^ } write write-ntriple-part ;
 78
 79: write-string ( string -- )
 80    CHAR: " write1 cs-output stream-write CHAR: " write1 ; inline
 81
 82M: literal write-ntriple-part ( object -- )
 83    [ value>> write-string ]
 84    [
 85        dup lang>>
 86        [ nip write-lang ]
 87        [ type>> [ write-type ] when* ] if*
 88    ] bi
 89    CHAR: space write1 ;
 90
 91M: number write-ntriple-part ( object -- )
 92    number>string write-string CHAR: space write1 ;
 93
 94: write-triple ( object predicate subject -- )
 95    [ write-ntriple-part ] tri@
 96    B{ CHAR: space CHAR: . CHAR: \n } write ; inline 
 97
 98PRIVATE>
 99
100M: ntriple import-triples ( string graph format -- graph )
101    drop swap [ read-ntriples ] with-string-reader
102    [ add-triple-seq ] each ;
103
104M: ntriple serialize-graph ( graph format -- )
105    drop spo>>
106    [ [ keys [ [ 2dup ] dip write-triple ] each drop ] assoc-each drop ] assoc-each ;
107
108M: ntriple serialize-triples ( seq format -- )
109    drop [ [ s>> ] [ p>> ] [ o>> ] tri write-triple ] each ;
110
111M: ntriple mime-type drop "text/plain" ;