To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / src / may / vamp / vamprdf.yeti @ 599:dc3632590d01

History | View | Annotate | Download (11.2 KB)

1

    
2
module may.vamp.vamprdf;
3

    
4
//!!! todo: Test. None of this has tests yet
5

    
6
read = load yertle.read;
7
{ newRdfStore } = load yertle.store;
8
{ textProperty, iriProperty, nodeProperty, 
9
  textsProperty, irisProperty, nodesProperty } = load yertle.property;
10

    
11
import java.io: File;
12

    
13
import org.vamp_plugins: PluginLoader;
14

    
15
import java.lang: UnsatisfiedLinkError;
16

    
17
getPluginPath () =
18
   (try map string PluginLoader#getInstance()#getPluginPath();
19
    catch UnsatisfiedLinkError e: 
20
        eprintln "WARNING: getPluginPath: Failed to load jVamp library: \(e#getMessage())";
21
        eprintln "(Looking up VAMP_PATH environment variable directly instead)";
22
        try
23
            path = System#getenv("VAMP_PATH");
24
            if nullptr? path then []
25
            else
26
                sep = System#getProperty("path.separator");
27
                list (strSplit sep path);
28
            fi
29
        catch Exception e:
30
            eprintln "(Caught exception: \(e#getMessage())";
31
            [];
32
        yrt;
33
    yrt);
34

    
35
systemVampRdfFiles () =
36
    concat
37
       (map do p:
38
            map ((p ^ File#separator ^) . (.name))
39
               (filter do entry:
40
                    entry.file? and
41
                       (lc = strLower entry.name;
42
                        (strEnds? lc ".ttl") or
43
                        (strEnds? lc ".n3") or
44
                        (strEnds? lc ".nt"))
45
                    done (listDirectory false p))
46
            done (getPluginPath ()));
47

    
48
addVampPrefixes store =
49
   (store.addPrefix "vamp" "http://purl.org/ontology/vamp/";
50
    store.addPrefix "dc" "http://purl.org/dc/elements/1.1/";
51
    store.addPrefix "foaf" "http://xmlns.com/foaf/0.1/";
52
    store.addPrefix "owl" "http://www.w3.org/2002/07/owl#";
53
    store.addPrefix "af" "http://purl.org/ontology/af/");
54

    
55
loadSystemVampRdf store =
56
   (addVampPrefixes store;
57
    for (systemVampRdfFiles ()) do file:
58
        case read.loadTurtleFile store ("file://" ^ file) file of
59
        OK (): ();
60
        Error e: eprintln
61
            "WARNING: Failed to load Vamp plugin RDF file \"\(file)\": \(e)";
62
        esac
63
    done);
64

    
65
getGlobalPluginIndex () =
66
    list (strSplit "\n" (fetchURL [ Timeout 10 ] (Handle getContents)
67
                         "http://www.vamp-plugins.org/rdf/plugins/index.txt"));
68

    
69
//!!! need to cache these retrievals
70
parseGlobalVampRdf () =
71
   (parse urls =
72
        case urls of
73
        url::rest:
74
           (doc = fetchURL [ Timeout 10 ] (Handle getContents) url;
75
            parsed = read.parseTurtleString url doc;
76
            { url, parsed } :. \(parse rest));
77
         _: [];
78
        esac;
79
    parse (getGlobalPluginIndex ()));
80

    
81
loadGlobalVampRdf store =
82
    for (parseGlobalVampRdf ()) do { url, parsed }:
83
        case read.loadParsedTriples store parsed of
84
        OK (): ();
85
        Error e: eprintln "WARNING: Failed to load Vamp RDF from URL \(url): \(e)";
86
        esac;
87
    done;
88

    
89
subjects = map (.s);
90

    
91
iriTypes =
92
    map do t:
93
        case t of
94
        IRI iri: IRI iri;
95
        Blank n: Blank n;
96
        Literal lit: failWith "Unexpected literal \"\(lit)\" in subject!";
97
        esac done;
98

    
99
iriSubjects = iriTypes . subjects;
100

    
101
allLibraryNodes store =
102
    iriSubjects
103
       (store.match {
104
            s = Wildcard (),
105
            p = Known (store.expand "a"),
106
            o = Known (store.expand "vamp:PluginLibrary")
107
            });
108

    
109
allPluginNodes store =
110
    iriSubjects
111
       (store.match {
112
            s = Wildcard (),
113
            p = Known (store.expand "a"),
114
            o = Known (store.expand "vamp:Plugin")
115
            });
116

    
117
pluginsWithId store id = 
118
    iriTypes
119
       (filter do pnode:
120
        store.contains {
121
            s = pnode,
122
            p = store.expand "vamp:identifier",
123
            o = Literal { value = id, type = "", language = "" }
124
            }
125
        done (allPluginNodes store));
126

    
127
librariesWithId store id =
128
    iriTypes
129
       (filter do lnode:
130
        store.contains {
131
            s = lnode,
132
            p = store.expand "vamp:identifier",
133
            o = Literal { value = id, type = "", language = "" }
134
            }
135
        done (allLibraryNodes store));
136

    
137
splitPluginKey key =
138
   (bits = strSplit ":" key;
139
    reversed = reverse bits;
140
    soname = strJoin ":" (reverse (tail reversed));
141
    identifier = head reversed;
142
    { soname, identifier });
143

    
144
pluginNodesByKey store key =
145
   (case splitPluginKey key of { soname, identifier }:
146
        candidatePlugins = pluginsWithId store identifier;
147
        candidateLibraries = librariesWithId store soname;
148
        filter do pnode:
149
            any do lnode:
150
                store.contains {
151
                    s = lnode,
152
                    p = store.expand "vamp:available_plugin",
153
                    o = pnode
154
                    }
155
                done candidateLibraries
156
            done candidatePlugins
157
    esac);
158

    
159
libraryNodesFor store pluginNode =
160
    map (.s) 
161
       (store.match {
162
            s = Wildcard (), 
163
            p = Known (store.expand "vamp:available_plugin"), 
164
            o = Known pluginNode
165
        });
166

    
167
libraryTextProperty store pluginNode abbr =
168
    case concatMap do n:
169
        textsProperty store n abbr
170
        done (libraryNodesFor store pluginNode) of
171
    t::_: t;
172
    _: "";
173
    esac;
174

    
175
libraryNodeProperty store pluginNode abbr =
176
    case concatMap do n:
177
        nodesProperty store n abbr
178
        done (libraryNodesFor store pluginNode) of
179
    (Some n)::_: Some n;
180
    _: None ();
181
    esac;
182

    
183
makerNodesFor store pluginNode =
184
    concatMap do n:
185
        nodesProperty store n "foaf:maker"
186
        done (pluginNode::(libraryNodesFor store pluginNode));
187

    
188
inputDomainOf store pluginNode =
189
   case store.match {
190
        s = Known pluginNode, p = Known (store.expand "vamp:input_domain"), o = Wildcard ()
191
        } of
192
    { o = IRI iri }::others:
193
        if IRI iri == store.expand "vamp:FrequencyDomain"
194
        then FrequencyDomain ()
195
        else TimeDomain ()
196
        fi;
197
     _: TimeDomain ();
198
    esac;
199

    
200
outputDescriptor store outputNode =
201
   (tprop abbr = textProperty store outputNode abbr;
202
    iprop abbr = iriProperty store outputNode abbr;
203
    bprop abbr deflt =
204
       (b = strLower (textProperty store outputNode abbr);
205
        if b == "true" then true elif b == "false" then false else deflt fi);
206
    nprop abbr =
207
        try number (textProperty store outputNode abbr); catch Exception _: 0 yrt;
208
    {
209
        identifier = tprop "vamp:identifier",
210
        name = tprop "dc:title",
211
        description = tprop "dc:description",
212
        rdfType = case iprop "a" of IRI iri: iri; _: "" esac,
213
        valueUnit = tprop "vamp:unit",
214
        binCount = 
215
            if bprop "vamp:fixed_bin_count" false
216
            then Known (nprop "vamp:bin_count")
217
            else Unknown ()
218
            fi,
219
        sampleType = iprop "vamp:sample_type",
220
        sampleRate =
221
            case tprop "vamp:sample_rate" of
222
            "": Unknown();
223
            sr: try Known (number sr)
224
                catch Exception _: Unknown ()
225
                yrt;
226
            esac,
227
        computes =
228
            case iprop "vamp:computes_event_type" of
229
            IRI iri: Event iri;
230
             _: case iprop "vamp:computes_signal_type" of
231
                IRI iri: Signal iri;
232
                 _: case iprop "vamp:computes_feature_type" of
233
                    IRI iri: Feature iri;
234
                     _: Unknown ();
235
                    esac
236
                esac
237
            esac,
238
        //!!! and some other properties
239
    });
240

    
241
pluginDataByNode store pluginNode =
242
   (makerNodes = makerNodesFor store pluginNode;
243
    libtextprop abbr = libraryTextProperty store pluginNode abbr;
244
    libnodeprop abbr = libraryNodeProperty store pluginNode abbr;
245
    textprop abbr = 
246
        case textProperty store pluginNode abbr of
247
        "": libtextprop abbr;
248
        text: text;
249
        esac;
250
    nodeprop abbr = 
251
        case nodeProperty store pluginNode abbr of
252
        Some s: Some s;
253
        _: libnodeprop abbr;
254
        esac;
255
    numprop abbr = try
256
        number (textProperty store pluginNode abbr);
257
        catch Exception _: 0 
258
        yrt;
259
    soname = libtextprop "vamp:identifier";
260
    {
261
        pluginNode,
262
        pluginKey = soname ^ ":" ^ 
263
           (textProperty store pluginNode "vamp:identifier"),
264
        library = { 
265
            soname, 
266
            name = libtextprop "dc:title",
267
            description = libtextprop "dc:description",
268
            platforms = concatMap do n:
269
                textsProperty store n "vamp:has_binary"
270
                done (libraryNodesFor store pluginNode),
271
            source = strLower (libtextprop "vamp:has_source") == "true",
272
            isVamPy = strLower (libtextprop "vamp:is_vampy_plugin") == "true",
273
            downloadURL = 
274
                case concatMap do n:
275
                    irisProperty store n "doap:download-page"
276
                    done (libraryNodesFor store pluginNode) of
277
                IRI iri::_: iri;
278
                _: "";
279
                esac,
280
            infoURL = 
281
                case concatMap do n:
282
                    irisProperty store n "foaf:page"
283
                    done (libraryNodesFor store pluginNode) of
284
                IRI iri::_: iri;
285
                _: "";
286
                esac,
287
        },
288
        apiVersion = numprop "vamp:vamp_API_version",
289
        identifier = textprop "vamp:identifier",
290
        name = textprop "dc:title",
291
        description = textprop "dc:description",
292
        maker =
293
            case textprop "foaf:maker" of
294
            "": case concatMap do n:
295
                    textsProperty store n "foaf:name"
296
                    done makerNodes of
297
                name::_: name;
298
                _: "";
299
                esac;
300
            name: name;
301
            esac,
302
        makerLogoURL =
303
            case concatMap do n:
304
                irisProperty store n "foaf:logo"
305
                done makerNodes of
306
            IRI iri::_: iri;
307
            _: "";
308
            esac,
309
        makerInfoURL =
310
            case concatMap do n:
311
                irisProperty store n "foaf:page"
312
                done makerNodes of
313
            IRI iri::_: iri;
314
            _: "";
315
            esac,
316
        copyright = textprop "dc:rights",
317
        version = textprop "owl:versionInfo",
318
        category = 
319
           (cat = textprop "vamp:category";
320
            if cat == "" then []
321
            else list (strSplit " > " cat)
322
            fi),
323
        inputDomain = inputDomainOf store pluginNode,
324
        infoURL =
325
            case nodeprop "foaf:page" of
326
            Some (IRI iri): iri;
327
            Some _: "";
328
            None (): ""
329
            esac,
330
        outputs = 
331
           (matches = store.match { s = Known pluginNode,
332
                                    p = Known (store.expand "vamp:output"), 
333
                                    o = Wildcard () };
334
            array (map do t:
335
                       outputDescriptor store t.o;
336
                       done matches)),
337
    });
338

    
339
pluginKeyByNode store node =
340
    case libraryNodesFor store node of
341
    libnode::_:
342
        Some
343
           (textProperty store libnode "vamp:identifier" ^
344
            ":" ^
345
            textProperty store node "vamp:identifier");
346
    _: None ();
347
    esac;
348

    
349
pluginDataByKey store key =
350
    case pluginNodesByKey store key of
351
    node::_: Some (pluginDataByNode store node);
352
    _: None ()
353
    esac;
354

    
355
{
356
loadSystemVampRdf,
357
loadGlobalVampRdf,
358
allPluginNodes,
359
allLibraryNodes,
360
pluginNodesByKey,
361
pluginKeyByNode,
362
pluginDataByNode,
363
pluginDataByKey,
364
}
365