library(swiplr)
swipl <- swiplr::swiplR()

Tests databases

Test 1 for upper case and spécial caracters in atoms

```{prolog test1_data}

test1(foo, bar). test1(foo, 'Bar'). test1(foo, 'Bar of joe.'). test1('foo', 'Bar of Joé.'). test1(fée, 'Bar of Joé.').

test1(with, more, columns, in, order, 'to hide', some, of, them). test1(with, less, columns, in, order, 'to show', many, of, them).

### Test 2 complex data in results

```{prolog test2_data}

test2(some(foo_bar),other(with,things)).
test2(some(of(lists,number)),[1,2,3]).
test2(some(of(lists,things)),[some(thing),2,foo(bar)]).

Query test databases

Test 1 for special characters

swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(Foo, Bar)")

Warning: #€$ and () are not allowed in Variable names

swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(FoéWithÀccent, Bar_With_Ümlletter)")
swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(FoéWithÀccent, Bar_With_Ümlletter)")

Hiding columns after query

swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(Visible, Hidden_)")
swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(Hidden_, Visible, A, B, C, D, E, F, G)")

swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(Hidden_, Visible, A_, B_, C_, D, E_, F_, G_)")

swipl$query(.swiplr_chunks$test1_data, 
                    query = "test1(Hidden_, Visible, _, _, _, D_, _, _, _)")

Test 2 : Multi level query

Automatic type unlist for columns

swipl$query(.swiplr_chunks$test2_data, 
                    query = "test2(What, Content)")


swipl$query(.swiplr_chunks$test2_data, 
                    query = "test2(some(foo_bar), Content)")
l_out <- swipl$query(.swiplr_chunks$test2_data, 
                    query = "test2(some(of(lists, What)), List)")

str(l_out$List)

Test 3 : Inject data in body

body <- "
test3(some({{{key}}}_always_visible),other({{{value}}})).

{{{#visible}}}
test3(some({{{key}}}_may_hide),other({{{value}}})).
{{{/visible}}}
"


swipl$query(body, query = "test3(Show, Other)",
  data = list(key="foo1", value="bar1é", visible=T))

swipl$query(body, query = "test3(Hide, Other)",
  data = list(key="foo2", value="bar2é", visible=F))
body <- "
{{{#row}}}
test3(some({{{key}}}),other({{{value}}})).
{{{/row}}}
"

swipl$query(body,
  query =   "test3(Key, Value)",
  data = list(row=list(
              list(key="foo1", value="bar1"),
              list(key="foo2", value="bar2")
              ) )
  )

This is how to convert a data.frame into a prolog predicate.

body <- "
{{{#row}}}
co2('{{{Plant}}}', '{{{Type}}}', {{{conc}}}, {{{uptake}}}).
{{{/row}}}
"
data("CO2")
swipl$query(body,
  query =   "co2(Plant, Type, Conc, Uptake)",
  data = list(row=unname(apply(CO2[1:5,], 1, as.list) ) )
  )

Broken files and error handling

Test 4: bad syntax in file

swipl$restart()

swipl$query("missing_point(at,end,of,line)", "X=1")
swipl$query("missing_operator ponctuation.", "X=1")
swipl$query(":- my_undefined_procedure.", "X=1")

swipl$query("foo(bar).", "foo(X)")

Test 5: bad syntax in query

swipl$restart()

swipl$query("foo(bar).", "foo(X ")
swipl$query("foo(bar).", "foo2(x) ")

Performances

full call duration

# usually  less than 100ms
system.time({out <- swiplr::swiplR()$query("foo(bar).", "foo(X).")})
out

pl <- swiplr::swiplR()
# usually  less than 10 ms
system.time({out <- pl$query("foo(bar).", "foo(X).")})
out
data <- as.pl_data.data.frame(CO2)
pl <- swiplr::swiplR()
# usually  less than 80 ms
system.time({out <- pl$query(
  "\n{{{#co2}}}co2('{{{Plant}}}','{{{Type}}}','{{{Treatment}}}','{{{conc}}}',{{{uptake}}}).\n{{{/co2}}}",
  "co2(Plant, Type, Treatment, Conc, Uptake).", 
  data = data, nsol = 1000)})

out

data <- as.pl_data.data.frame(EuStockMarkets)
# usually 1500 ms
system.time({out <- pl$query(
  "\n{{{#eustockmarkets}}}eustockmarkets('{{{DAX}}}','{{{SMI}}}','{{{CAC}}}','{{{FTSE}}}').\n{{{/eustockmarkets}}}",
  "eustockmarkets(DAX, SMI, CAC, FTSE).", 
  data = data, nsol = 2000)})
out


battmanux/swiplr documentation built on July 29, 2021, 12:58 a.m.