@@ -2,9 +2,6 @@ import { expect } from 'chai';
22import { FunctionHandler } from "./FunctionHandler" ;
33import { } from 'mocha' ;
44import { JavaScriptHandler } from './handlers/JavaScriptHandler' ;
5- import { Argument , Function , Implementation } from './models' ;
6- import { namedNode } from 'rdflib' ;
7- import { Term } from 'rdf-js' ;
85import prefixes from './prefixes' ;
96
107const fnTtl = `@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@@ -30,22 +27,13 @@ fns:bParameter rdf:type fno:Parameter ;
3027
3128fns:sumOutput rdf:type fno:Output ;
3229 rdfs:label "sum output"@en ;
33- fno:predicate fno:Result .
30+ fno:predicate fns:out .
3431
3532fns:sum rdf:type fno:Function ;
3633 dcterms:description "Description of the sum function"@en ;
3734 rdfs:label "sum"@en ;
38- fno:expects _:g0 ;
39- fno:returns _:g2 .
40-
41- _:g0 rdf:first fns:aParameter ;
42- rdf:rest _:g1 .
43-
44- _:g1 rdf:first fns:bParameter ;
45- rdf:rest rdf:nil .
46-
47- _:g2 rdf:first fns:sumOutput ;
48- rdf:rest rdf:nil .
35+ fno:expects ( fns:aParameter fns:bParameter ) ;
36+ fno:returns ( fns:sumOutput ) .
4937
5038fns:sumImplementation rdf:type fno:Implementation, fnoi:JavaScriptImplementation, fnoi:JavaScriptFunction ;
5139 doap:release fns:sumImplementationRelease .
@@ -69,11 +57,91 @@ fns:bParametermapping rdf:type fno:ParameterMapping, fnom:PositionParameterMappi
6957 fnom:functionParameter fns:bParameter ;
7058 fnom:implementationParameterPosition "1"^^xsd:integer .
7159
72- fns:sumReturnMapping rdf:type fno:ReturnMapping, fnom:DefaultReturnMapping .
60+ fns:sumReturnMapping rdf:type fno:ReturnMapping, fnom:DefaultReturnMapping ;
61+ fnom:functionOutput fns:sumOutput .
7362
7463fns:sumMethodMapping rdf:type fno:MethodMapping, fnom:StringMethodMapping ;
7564 fnom:method-name "sum" .
7665` ;
66+ const fnTtlComposition = `
67+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
68+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
69+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
70+ @prefix ex: <http://www.example.com#> .
71+ @prefix dcterms: <http://purl.org/dc/terms/> .
72+ @prefix doap: <http://usefulinc.com/ns/doap#> .
73+ @prefix fno: <https://w3id.org/function/ontology#> .
74+ @prefix fnoi: <https://w3id.org/function/vocabulary/implementation#> .
75+ @prefix fnom: <https://w3id.org/function/vocabulary/mapping#> .
76+ @prefix fns: <http://example.com/functions#> .
77+ @prefix fnoc: <https://w3id.org/function/vocabulary/composition#> .
78+
79+ fns:sum3 rdf:type fno:Function ;
80+ dcterms:description "Description of the sum3 function"@en ;
81+ rdfs:label "sum3"@en ;
82+ fno:expects ( fns:aParameter fns:bParameter fns:cParameter ) ;
83+ fno:returns ( fns:sumOutput ) .
84+
85+ fns:cParameter rdf:type fno:Parameter ;
86+ rdfs:label "c"@en ;
87+ fno:predicate fns:c ;
88+ fno:required "true"^^xsd:boolean .
89+
90+ fns:sum3_1 fnoc:applies fns:sum .
91+ fns:sum3_2 fnoc:applies fns:sum .
92+
93+ fns:sum3Composition rdf:type fnoc:Composition ;
94+ fnoc:composedOf [
95+ fnoc:mapFrom [
96+ fnoc:constituentFunction fns:sum3;
97+ fnoc:functionParameter fns:aParameter
98+ ] ;
99+ fnoc:mapTo [
100+ fnoc:constituentFunction fns:sum3_1;
101+ fnoc:functionParameter fns:aParameter
102+ ]
103+ ],
104+ [
105+ fnoc:mapFrom [
106+ fnoc:constituentFunction fns:sum3;
107+ fnoc:functionParameter fns:bParameter
108+ ] ;
109+ fnoc:mapTo [
110+ fnoc:constituentFunction fns:sum3_1;
111+ fnoc:functionParameter fns:bParameter
112+ ]
113+ ],
114+ [
115+ fnoc:mapFrom [
116+ fnoc:constituentFunction fns:sum3_1;
117+ fnoc:functionOutput fns:sumOutput
118+ ] ;
119+ fnoc:mapTo [
120+ fnoc:constituentFunction fns:sum3_2;
121+ fnoc:functionParameter fns:aParameter
122+ ]
123+ ],
124+ [
125+ fnoc:mapFrom [
126+ fnoc:constituentFunction fns:sum3;
127+ fnoc:functionParameter fns:cParameter
128+ ] ;
129+ fnoc:mapTo [
130+ fnoc:constituentFunction fns:sum3_2;
131+ fnoc:functionParameter fns:bParameter
132+ ]
133+ ],
134+ [
135+ fnoc:mapFrom [
136+ fnoc:constituentFunction fns:sum3_2;
137+ fnoc:functionOutput fns:sumOutput
138+ ] ;
139+ fnoc:mapTo [
140+ fnoc:constituentFunction fns:sum3;
141+ fnoc:functionOutput fns:sumOutput
142+ ]
143+ ] .
144+ ` ;
77145
78146describe ( 'FunctionHandler tests' , ( ) => { // the tests container
79147 it . skip ( 'can parse a function file' , async ( ) => { // the single test
@@ -99,15 +167,38 @@ describe('FunctionHandler tests', () => { // the tests container
99167 expect ( fn . id ) . to . equal ( `${ prefixes . fns } sum` )
100168
101169 const jsHandler = new JavaScriptHandler ( ) ;
102- jsHandler . loadFunction ( new Implementation ( namedNode ( `${ prefixes . fns } sumImplementation` ) as Term ) , ( a , b ) => a + b )
170+ handler . implementationHandler . loadImplementation ( `${ prefixes . fns } sumImplementation` , jsHandler , { fn : ( a , b ) => a + b } )
171+ const result = await handler . executeFunction ( fn , {
172+ [ `${ prefixes . fns } a` ] : 1 ,
173+ [ `${ prefixes . fns } b` ] : 2
174+ } )
175+
176+ expect ( result [ `${ prefixes . fns } out` ] ) . to . equal ( 3 ) ;
177+ return ;
178+ } )
179+
180+ it ( 'can load a local file, add a handler, compose, and execute a function' , async ( ) => {
181+ const handler = new FunctionHandler ( ) ;
182+ await handler . addFunctionResource ( `${ prefixes . fns } sum3` , {
183+ type : 'string' ,
184+ contents : fnTtl + fnTtlComposition ,
185+ contentType : "text/turtle"
186+ } ) ;
187+ const fn = await handler . getFunction ( `${ prefixes . fns } sum3` ) ;
103188
104- handler . addHandler ( jsHandler ) ;
105- const result = await handler . executeFunction ( fn , [
106- new Argument ( `${ prefixes . fns } aParameter` , 1 ) ,
107- new Argument ( `${ prefixes . fns } bParameter` , 1 ) ,
108- ] )
189+ expect ( fn ) . to . be . not . null ;
190+
191+ expect ( fn . id ) . to . equal ( `${ prefixes . fns } sum3` )
109192
110- expect ( result ) . to . equal ( 2 ) ;
193+ const jsHandler = new JavaScriptHandler ( ) ;
194+ handler . implementationHandler . loadImplementation ( `${ prefixes . fns } sumImplementation` , jsHandler , { fn : ( a , b ) => a + b } )
195+ const result = await handler . executeFunction ( fn , {
196+ [ `${ prefixes . fns } a` ] : 1 ,
197+ [ `${ prefixes . fns } b` ] : 2 ,
198+ [ `${ prefixes . fns } c` ] : 3 ,
199+ } )
200+
201+ expect ( result [ `${ prefixes . fns } out` ] ) . to . equal ( 6 ) ;
111202 return ;
112203 } )
113204} ) ;
0 commit comments