From 0c76b05638c1f12ec7faa0ae1c68ff886941d106 Mon Sep 17 00:00:00 2001 From: Zachary Whitley Date: Sat, 4 Jun 2022 10:43:27 -0400 Subject: [PATCH] Add back property functions --- pom.xml | 4 +- .../kibble/webfunctions/CacheClear.java | 1 - .../kibble/webfunctions/CacheLoad.java | 1 - .../kibble/webfunctions/CacheRefresh.java | 1 - .../stardog/kibble/webfunctions/Call.java | 11 +- .../stardog/kibble/webfunctions/Compose.java | 12 +- .../stardog/kibble/webfunctions/Filter.java | 2 - .../stardog/kibble/webfunctions/Get.java | 1 - .../stardog/kibble/webfunctions/Map.java | 3 - .../stardog/kibble/webfunctions/Memoize.java | 3 - .../stardog/kibble/webfunctions/Partial.java | 1 - .../kibble/webfunctions/PluginVersion.java | 7 - .../kibble/webfunctions/PluginVersions.java | 2 - .../stardog/kibble/webfunctions/Reduce.java | 1 - .../webfunctions/StardogWasmInstance.java | 20 +- .../webfunctions/WebFunctionService.java | 6 +- .../WebFunctionServiceModule.java | 1 - .../WebFunctionServiceOperator.java | 12 -- .../webfunctions/WebFunctionServiceQuery.java | 4 - .../WebFunctionInCallOutPropertyFunction.java | 106 +++++++++ ...tionInCallOutPropertyFunctionOperator.java | 201 ++++++++++++++++++ ...tionInCallOutPropertyFunctionPlanNode.java | 111 ++++++++++ ...allOutPropertyFunctionPlanNodeBuilder.java | 69 ++++++ .../WebFunctionOutCallInPropertyFunction.java | 106 +++++++++ ...tionOutCallInPropertyFunctionOperator.java | 201 ++++++++++++++++++ ...tionOutCallInPropertyFunctionPlanNode.java | 111 ++++++++++ ...CallInPropertyFunctionPlanNodeBuilder.java | 85 ++++++++ .../stardog/kibble/AbstractStardogTest.java | 12 +- .../kibble/StardogWebfunctionsTestSuite.java | 69 ------ .../TestInCallOutPropertyFunction.java | 122 +++++++++++ .../TestOutCallInPropertyFunction.java | 141 ++++++++++++ .../kibble/webfunctions/WasmTestSuite.java | 13 +- src/test/rust/function/array_of/src/lib.rs | 13 +- src/test/rust/function/to_upper/src/lib.rs | 13 +- 34 files changed, 1314 insertions(+), 152 deletions(-) create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunction.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionOperator.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNode.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNodeBuilder.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunction.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionOperator.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNode.java create mode 100644 src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNodeBuilder.java delete mode 100644 src/test/java/com/semantalytics/stardog/kibble/StardogWebfunctionsTestSuite.java create mode 100644 src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestInCallOutPropertyFunction.java create mode 100644 src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestOutCallInPropertyFunction.java diff --git a/pom.xml b/pom.xml index 0eb3838..acc8c44 100644 --- a/pom.xml +++ b/pom.xml @@ -45,8 +45,8 @@ - 7 - ${stardog.major.version}.9.1 + 8 + ${stardog.major.version}.0.0 4.12 1.7.21 3.15.0 diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheClear.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheClear.java index e8ce5ec..3e4fca5 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheClear.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheClear.java @@ -5,7 +5,6 @@ import com.complexible.stardog.plan.filter.functions.AbstractFunction; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.google.common.collect.Range; -import com.stardog.stark.Literal; import com.stardog.stark.Value; import java.net.MalformedURLException; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheLoad.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheLoad.java index 85ce3dc..f2b6252 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheLoad.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheLoad.java @@ -5,7 +5,6 @@ import com.complexible.stardog.plan.filter.functions.AbstractFunction; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.google.common.collect.Range; -import com.stardog.stark.Literal; import com.stardog.stark.Value; import java.net.MalformedURLException; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheRefresh.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheRefresh.java index c3a73dc..169abd8 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheRefresh.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/CacheRefresh.java @@ -5,7 +5,6 @@ import com.complexible.stardog.plan.filter.functions.AbstractFunction; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.google.common.collect.Range; -import com.stardog.stark.Literal; import com.stardog.stark.Value; import java.net.MalformedURLException; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Call.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Call.java index 50e1e81..bb055a6 100755 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Call.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Call.java @@ -1,10 +1,17 @@ package com.semantalytics.stardog.kibble.webfunctions; -import com.complexible.stardog.plan.filter.*; +import com.complexible.stardog.plan.filter.AbstractExpression; +import com.complexible.stardog.plan.filter.Expression; +import com.complexible.stardog.plan.filter.ExpressionVisitor; +import com.complexible.stardog.plan.filter.Expressions; +import com.complexible.stardog.plan.filter.ValueSolution; import com.complexible.stardog.plan.filter.expr.ValueOrError; import com.complexible.stardog.plan.filter.functions.FunctionRegistry; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; -import com.stardog.stark.*; +import com.stardog.stark.BNode; +import com.stardog.stark.IRI; +import com.stardog.stark.Literal; +import com.stardog.stark.Value; import com.stardog.stark.query.SelectQueryResult; import java.io.IOException; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Compose.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Compose.java index bde61c0..2887146 100755 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Compose.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Compose.java @@ -1,15 +1,17 @@ package com.semantalytics.stardog.kibble.webfunctions; -import com.complexible.stardog.plan.filter.*; +import com.complexible.stardog.plan.filter.AbstractExpression; +import com.complexible.stardog.plan.filter.EvalUtil; +import com.complexible.stardog.plan.filter.Expression; +import com.complexible.stardog.plan.filter.ExpressionVisitor; +import com.complexible.stardog.plan.filter.ValueSolution; import com.complexible.stardog.plan.filter.expr.ValueOrError; -import com.complexible.stardog.plan.filter.functions.FunctionRegistry; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.stardog.stark.*; -import org.apache.commons.math3.analysis.function.Exp; -import java.util.*; +import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import static java.util.stream.Collectors.*; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Filter.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Filter.java index c692515..23d8092 100755 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Filter.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Filter.java @@ -7,7 +7,6 @@ import com.complexible.stardog.plan.filter.functions.FunctionRegistry; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.google.common.collect.Lists; -import com.stardog.stark.IRI; import com.stardog.stark.Literal; import com.stardog.stark.Value; @@ -16,7 +15,6 @@ import java.util.stream.IntStream; import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertArrayLiteral; -import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertLiteral; import static java.util.stream.Collectors.toList; public final class Filter extends AbstractExpression implements UserDefinedFunction { diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Get.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Get.java index f660483..d302b9a 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Get.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Get.java @@ -10,7 +10,6 @@ import com.stardog.stark.impl.IntegerLiteral; import com.stardog.stark.impl.StringLiteral; import org.apache.commons.io.IOUtils; -import org.apache.james.mime4j.util.MimeUtil; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Map.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Map.java index 1904946..cc3116b 100755 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Map.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Map.java @@ -7,15 +7,12 @@ import com.complexible.stardog.plan.filter.functions.FunctionRegistry; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.google.common.collect.Lists; -import com.stardog.stark.IRI; -import com.stardog.stark.Literal; import com.stardog.stark.Value; import java.util.Arrays; import java.util.List; import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertArrayLiteral; -import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertLiteral; import static java.util.stream.Collectors.toList; public final class Map extends AbstractExpression implements UserDefinedFunction { diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Memoize.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Memoize.java index a641e0c..3703d78 100755 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Memoize.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Memoize.java @@ -4,10 +4,8 @@ import com.complexible.stardog.plan.filter.expr.ValueOrError; import com.complexible.stardog.plan.filter.functions.FunctionRegistry; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; -import com.google.api.client.util.Lists; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import com.stardog.stark.IRI; import com.stardog.stark.Literal; import com.stardog.stark.Value; @@ -17,7 +15,6 @@ import java.util.stream.Stream; import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertIntegerLiteral; -import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertLiteral; import static java.util.stream.Collectors.toList; public final class Memoize extends AbstractExpression implements UserDefinedFunction { diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Partial.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Partial.java index 12019ce..7a5c00a 100755 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Partial.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Partial.java @@ -6,7 +6,6 @@ import com.complexible.stardog.plan.filter.ValueSolution; import com.complexible.stardog.plan.filter.expr.ValueOrError; import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; -import com.google.common.collect.Lists; import com.stardog.stark.BNode; import com.stardog.stark.Value; import com.stardog.stark.Values; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersion.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersion.java index 695fe6c..f6cd363 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersion.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersion.java @@ -6,13 +6,6 @@ import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; import com.stardog.stark.Value; -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.security.CodeSource; -import java.util.jar.JarFile; -import java.util.jar.Manifest; - import static com.stardog.stark.Values.*; public class PluginVersion extends AbstractFunction implements UserDefinedFunction { diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersions.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersions.java index f0f5720..0734568 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersions.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/PluginVersions.java @@ -12,9 +12,7 @@ import com.vdurmont.semver4j.Semver; import java.util.List; -import java.util.Optional; import java.util.ServiceLoader; -import java.util.stream.Collectors; public class PluginVersions extends AbstractExpression implements UserDefinedFunction { diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Reduce.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Reduce.java index 6924e96..491541e 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Reduce.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/Reduce.java @@ -12,7 +12,6 @@ import java.util.Arrays; import java.util.List; -import java.util.stream.Stream; import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertArrayLiteral; import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertLiteral; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/StardogWasmInstance.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/StardogWasmInstance.java index 87e55dd..aaf4cb5 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/StardogWasmInstance.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/StardogWasmInstance.java @@ -6,12 +6,10 @@ import com.complexible.stardog.index.statistics.Accuracy; import com.complexible.stardog.index.statistics.Cardinality; import com.complexible.stardog.plan.PlanException; -import com.complexible.stardog.plan.QueryTerm; import com.complexible.stardog.plan.filter.expr.ValueOrError; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import com.google.common.cache.RemovalListener; import com.google.common.collect.Lists; import com.google.common.collect.Streams; import com.stardog.stark.Literal; @@ -25,7 +23,16 @@ import com.stardog.stark.query.io.QueryResultFormats; import com.stardog.stark.query.io.QueryResultParsers; import com.stardog.stark.query.io.QueryResultWriters; -import io.github.kawamuray.wasmtime.*; +import io.github.kawamuray.wasmtime.Engine; +import io.github.kawamuray.wasmtime.Extern; +import io.github.kawamuray.wasmtime.Func; +import io.github.kawamuray.wasmtime.Instance; +import io.github.kawamuray.wasmtime.Linker; +import io.github.kawamuray.wasmtime.Memory; +import io.github.kawamuray.wasmtime.Module; +import io.github.kawamuray.wasmtime.Store; +import io.github.kawamuray.wasmtime.Val; +import io.github.kawamuray.wasmtime.WasmFunctions; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -82,9 +89,10 @@ private static byte[] getWasm(final URL wasmUrl) throws IOException { conn.setReadTimeout(240000); conn.connect(); - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - IOUtils.copy(conn.getInputStream(), baos); - return baos.toByteArray(); + try(final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + IOUtils.copy(conn.getInputStream(), baos); + return baos.toByteArray(); + } } private Store store = Store.withoutData(); diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionService.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionService.java index d1905de..85ce24e 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionService.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionService.java @@ -10,8 +10,12 @@ import com.stardog.stark.Value; import com.stardog.stark.Values; -import java.util.*; +import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import static java.util.stream.Collectors.toList; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceModule.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceModule.java index 7d78163..b65d3ae 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceModule.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceModule.java @@ -1,7 +1,6 @@ package com.semantalytics.stardog.kibble.webfunctions; import com.complexible.stardog.AbstractStardogModule; -import com.complexible.stardog.metadata.MetaProperties; import com.complexible.stardog.plan.eval.service.Service; import com.google.inject.Singleton; import com.google.inject.multibindings.Multibinder; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceOperator.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceOperator.java index d0f9a91..020acac 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceOperator.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceOperator.java @@ -1,9 +1,6 @@ package com.semantalytics.stardog.kibble.webfunctions; -import com.complexible.common.base.Pair; import com.complexible.common.rdf.model.ArrayLiteral; -import com.complexible.stardog.StardogException; -import com.complexible.stardog.plan.PlanNode; import com.complexible.stardog.plan.QueryTerm; import com.complexible.stardog.plan.SortType; import com.complexible.stardog.plan.eval.ExecutionContext; @@ -11,24 +8,15 @@ import com.complexible.stardog.plan.eval.operator.impl.AbstractOperator; import com.complexible.stardog.plan.eval.operator.impl.Solutions; import com.complexible.stardog.plan.filter.expr.ValueOrError; -import com.google.common.base.Preconditions; import com.google.common.collect.Iterators; import com.google.common.collect.Sets; import com.stardog.stark.Literal; import com.stardog.stark.Value; import com.stardog.stark.query.BindingSet; import com.stardog.stark.query.SelectQueryResult; -import io.github.kawamuray.wasmtime.Func; -import io.github.kawamuray.wasmtime.Instance; -import io.github.kawamuray.wasmtime.Val; -import org.apache.jena.rdfconnection.SparqlQueryConnection; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; import java.util.*; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.IntStream; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceQuery.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceQuery.java index 7cf3170..070c3d3 100644 --- a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceQuery.java +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/WebFunctionServiceQuery.java @@ -11,7 +11,6 @@ import com.complexible.stardog.plan.eval.service.ServiceQuery; import com.complexible.stardog.plan.filter.expr.Constant; import com.complexible.stardog.plan.filter.expr.ValueOrError; -import com.google.api.client.util.Lists; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; @@ -21,13 +20,10 @@ import com.stardog.stark.query.io.QueryResultFormats; import java.net.MalformedURLException; -import java.util.Collections; import java.util.List; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.function.UnaryOperator; -import java.util.stream.IntStream; -import java.util.stream.Stream; import static com.stardog.stark.Values.iri; import static java.util.stream.Collectors.*; diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunction.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunction.java new file mode 100644 index 0000000..3a9f986 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunction.java @@ -0,0 +1,106 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.incallout; + +import com.complexible.stardog.index.statistics.Accuracy; +import com.complexible.stardog.index.statistics.Cardinality; +import com.complexible.stardog.plan.*; +import com.complexible.stardog.plan.eval.ExecutionContext; +import com.complexible.stardog.plan.eval.TranslateException; +import com.complexible.stardog.plan.eval.operator.*; +import com.complexible.stardog.plan.filter.expr.ValueOrError; +import com.complexible.stardog.plan.util.QueryTermRenderer; +import com.google.common.base.Preconditions; +import com.semantalytics.stardog.kibble.webfunctions.StardogWasmInstance; +import com.semantalytics.stardog.kibble.webfunctions.WebFunctionVocabulary; +import com.stardog.stark.IRI; +import com.stardog.stark.Values; + +import java.net.MalformedURLException; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import static java.util.stream.Collectors.toList; + +public class WebFunctionInCallOutPropertyFunction implements PropertyFunction { + + private static final WebFunctionVocabulary names = WebFunctionVocabulary.IN_call_OUT; + + /** + * {@inheritDoc} + */ + @Override + public List getURIs() { + return names.getNames().stream().map(Values::iri).collect(toList()); + } + + /** + * {@inheritDoc} + */ + @Override + public WebFunctionInCallOutPropertyFunctionPlanNodeBuilder newBuilder() { + return new WebFunctionInCallOutPropertyFunctionPlanNodeBuilder(); + } + + /** + * {@inheritDoc} + */ + @Override + public Operator translate(final ExecutionContext theExecutionContext, + final PropertyFunctionPlanNode thePropertyFunctionPlanNode, + final Operator theOperator) throws TranslateException { + + if (thePropertyFunctionPlanNode instanceof WebFunctionInCallOutPropertyFunctionPlanNode) { + return new WebFunctionInCallOutPropertyFunctionOperator(theExecutionContext, + (WebFunctionInCallOutPropertyFunctionPlanNode) thePropertyFunctionPlanNode, + theOperator); + } else { + throw new TranslateException("Invalid node type, cannot translate"); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void estimate(final PropertyFunctionPlanNode theNode, + Costs.CostingContext theContext) throws PlanException { + + Preconditions.checkArgument(theNode instanceof WebFunctionInCallOutPropertyFunctionPlanNode); + + List argsValueOrError = ((WebFunctionInCallOutPropertyFunctionPlanNode) theNode).getInput().stream().map(queryTerm -> { + if(queryTerm.isVariable()) { + return ValueOrError.General.of(Values.iri(WebFunctionVocabulary.var.getImmutableName())); + } else { + return queryTerm.getValue(); + } + }).collect(toList()); + if(argsValueOrError.stream().anyMatch(ValueOrError::isError)) { + throw new PlanException("Unable to generate cardinalty estimates"); + } + + try { + StardogWasmInstance instance = StardogWasmInstance.from(((WebFunctionInCallOutPropertyFunctionPlanNode) theNode).getWasm().value()); + Cardinality cardinality = instance.getCardinality(theNode.getArg().getCardinality(), argsValueOrError.stream().map(ValueOrError::value).collect(toList())); + + theNode.setCardinality(Cardinality.of(cardinality.value(), Accuracy.takeLessAccurate(cardinality.accuracy(), theNode.getArg().getCardinality().accuracy()))); + } catch(ExecutionException | MalformedURLException e) { + throw new PlanException(e); + } + + // assume a flat cost of 1 per iteration + the cost of our child + theNode.setCost(theNode.getCardinality().value() + theNode.getArg().getCost()); + } + + /** + * {@inheritDoc} + */ + @Override + public String explain(final PropertyFunctionPlanNode theNode, final QueryTermRenderer theTermRenderer) { + Preconditions.checkArgument(theNode instanceof WebFunctionInCallOutPropertyFunctionPlanNode); + Preconditions.checkNotNull(theTermRenderer); + //TODO should this be added to the webassembly as well? + + final WebFunctionInCallOutPropertyFunctionPlanNode aNode = (WebFunctionInCallOutPropertyFunctionPlanNode) theNode; + + return String.format("Call WebFunction {}", aNode.getWasm()); + } +} diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionOperator.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionOperator.java new file mode 100644 index 0000000..b4f9609 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionOperator.java @@ -0,0 +1,201 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.incallout; + +import com.complexible.stardog.StardogException; +import com.complexible.stardog.plan.QueryTerm; +import com.complexible.stardog.plan.SortType; +import com.complexible.stardog.plan.eval.ExecutionContext; +import com.complexible.stardog.plan.eval.operator.*; +import com.complexible.stardog.plan.eval.operator.impl.AbstractOperator; +import com.complexible.stardog.plan.eval.operator.impl.Solutions; +import com.complexible.stardog.plan.filter.expr.ValueOrError; +import com.google.common.base.Preconditions; +import com.google.common.collect.Iterators; +import com.google.common.collect.Sets; +import com.semantalytics.stardog.kibble.webfunctions.StardogWasmInstance; +import com.stardog.stark.Value; +import com.stardog.stark.query.BindingSet; +import com.stardog.stark.query.SelectQueryResult; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Executable operator for the repeat function + * + * @author Michael Grove + */ +public final class WebFunctionInCallOutPropertyFunctionOperator extends AbstractOperator implements PropertyFunctionOperator { + + private ValueOrError wasmIRI ; + + /** + * The current solution + */ + private Solution solution; + + /** + * The child argument + */ + private final Optional mArg; + + /** + * The original node + */ + private final WebFunctionInCallOutPropertyFunctionPlanNode mNode; + + /** + * An iterator over the child solutions of this operator + */ + private Iterator mInputs = null; + + private StardogWasmInstance instance; + + private SelectQueryResult selectQueryResult; + + public WebFunctionInCallOutPropertyFunctionOperator(final ExecutionContext theExecutionContext, + final WebFunctionInCallOutPropertyFunctionPlanNode theNode, + final Operator theOperator) { + super(theExecutionContext, SortType.UNSORTED); + + mNode = Preconditions.checkNotNull(theNode); + mArg = Optional.of(theOperator); + wasmIRI = theNode.getWasm(); + + try { + //TODO should check if it's an error before assumign it's a value + instance = StardogWasmInstance.from(wasmIRI.value(), getMappings()); + } catch (ExecutionException | MalformedURLException e) { + throw new StardogException(e); + } + } + + @Override + protected Solution computeNext() { + if (mInputs == null) { + // first call to compute results, perform some init + // either use our child's solutions, or if we don't have a child, create a single solution to use + if (mArg.filter(theOp -> !(theOp instanceof EmptyOperator)).isPresent()) { + // these are the variables the child arg will bind + Set aVars = Sets.newHashSet(mArg.get().getVars()); + + // and these are the ones that the pf will bind + aVars.addAll(mNode.getResultVars().stream().map(QueryTerm::getName).collect(Collectors.toList())); + + // now we create a solution that contains room for bindings for these variables + final Solution aSoln = mExecutionContext.getSolutionFactory() + .variables(aVars) + .newSolution(); + + // and transform the child solutions to this one large enough to accomodate our vars + mInputs = Iterators.transform(mArg.get(), theSoln -> { + Solutions.copy(aSoln, theSoln); + return aSoln; + }); + } else if (mNode.getInput().stream().allMatch(QueryTerm::isVariable)) { + // no arg or empty operator and the input is a variable, there's nothing to repeat + return endOfData(); + } else { + final Set aVars = Sets.newHashSet(); + + aVars.addAll(mNode.getResultVars().stream().map(QueryTerm::getName).collect(Collectors.toList())); + + // we only want to create solutions with the minimum number of variables + mInputs = Iterators.singletonIterator(mExecutionContext.getSolutionFactory() + .variables(aVars) + .newSolution()); + } + } + + while (mInputs.hasNext()) { + if (solution == null) { + solution = mInputs.next(); + } + if (selectQueryResult == null) { + try { + ValueOrError[] valueOrErrors = mNode.getSubjects().stream().map(queryTerm -> { + if (queryTerm.isVariable()) { + return solution.getValue(queryTerm.getName(), getMappings()); + } else { + return queryTerm.getValue(); + } + }).toArray(ValueOrError[]::new); + + if (Arrays.stream(valueOrErrors).noneMatch(ValueOrError::isError)) { + selectQueryResult = instance.evaluate(Arrays.stream(valueOrErrors).skip(1).map(ValueOrError::value).toArray(Value[]::new)); + } else { + instance.close(); + instance = null; + return endOfData(); + } + } catch (IOException e) { + instance.close(); + instance = null; + return endOfData(); + } + } + if(selectQueryResult.hasNext()) { + BindingSet bindingSet = selectQueryResult.next(); + if (mNode.getResultVars().size() <= bindingSet.size()) { + IntStream.range(0, mNode.getResultVars().size()).forEach(i -> + solution.setValue(mNode.getResultVars().get(i).getName(), bindingSet.get(String.format("value_%d", i)), getMappings())); + } else { + instance.close(); + instance = null; + return endOfData(); + } + return solution; + } else { + selectQueryResult = null; + solution = null; + } + } + instance.close(); + instance = null; + return endOfData(); + } + /* + + private long getValue() { + return mNode.getInput().isConstant() + ? mNode.getInput().getIndex() + : mValue.get(mNode.getInput().getName()); + } + + */ + + /** + * {@inheritDoc} + */ + @Override + protected void performReset() { + mArg.ifPresent(Operator::reset); + instance.close(); + instance = null; + } + + /** + * {@inheritDoc} + */ + @Override + public Set getVars() { + return mNode.getSubjects().stream() + .filter(QueryTerm::isVariable) + .map(QueryTerm::getName) + .collect(Collectors.toSet()); + } + + /** + * {@inheritDoc} + */ + @Override + public void accept(final OperatorVisitor theOperatorVisitor) { + theOperatorVisitor.visit(this); + } +} diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNode.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNode.java new file mode 100644 index 0000000..f7e07b3 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNode.java @@ -0,0 +1,111 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.incallout; + +import com.complexible.stardog.index.statistics.Cardinality; +import com.complexible.stardog.plan.*; +import com.complexible.stardog.plan.filter.expr.ValueOrError; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.semantalytics.stardog.kibble.webfunctions.WebFunctionVocabulary; +import com.stardog.stark.IRI; + +import java.util.List; + +import static com.stardog.stark.Values.iri; +import static java.util.stream.Collectors.toList; + +public final class WebFunctionInCallOutPropertyFunctionPlanNode extends AbstractPropertyFunctionPlanNode { + + private static final WebFunctionVocabulary names = WebFunctionVocabulary.IN_call_OUT; + + public WebFunctionInCallOutPropertyFunctionPlanNode(final PlanNode theArg, + final List theSubjects, + final List theObjects, + final QueryTerm theContext, + final QueryDataset.Scope theScope, + final double theCost, + final Cardinality theCardinality, + final ImmutableSet theSubjVars, + final ImmutableSet thePredVars, + final ImmutableSet theObjVars, + final ImmutableSet theContextVars, + final ImmutableSet theAssuredVars, + final ImmutableSet theAllVars) { + super(theArg, + theSubjects, + theObjects, + theContext, + theScope, + theCost, + theCardinality, + theSubjVars, + thePredVars, + theObjVars, + theContextVars, + theAssuredVars, + theAllVars); + } + + public ValueOrError getWasm() { + return getSubjects().get(0).getValue(); } + + + public List getInput() { + return getSubjects().stream().collect(toList()); + } + + public List getResultVars() { + return getObjects(); + } + + /** + * {@inheritDoc} + */ + @Override + public ImmutableList getInputs() { + return getSubjects(); + } + + /** + * {@inheritDoc} + */ + @Override + public IRI getURI() { + return iri(names.getImmutableName()); + } + + /** + * {@inheritDoc} + */ + @Override + public WebFunctionInCallOutPropertyFunctionPlanNode copy() { + return new WebFunctionInCallOutPropertyFunctionPlanNode(getArg().copy(), + getSubjects(), + getObjects(), + getContext(), + getScope(), + getCost(), + getCardinality(), + getSubjectVars(), + getPredicateVars(), + getObjectVars(), + getContextVars(), + getAssuredVars(), + getAllVars()); + } + + /** + * {@inheritDoc} + */ + @Override + protected PropertyFunctionNodeBuilder createBuilder() { + return new WebFunctionInCallOutPropertyFunctionPlanNodeBuilder(); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean canEquals(final Object theObj) { + return theObj instanceof WebFunctionInCallOutPropertyFunctionPlanNode; + } +} \ No newline at end of file diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNodeBuilder.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNodeBuilder.java new file mode 100644 index 0000000..8d6e869 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/incallout/WebFunctionInCallOutPropertyFunctionPlanNodeBuilder.java @@ -0,0 +1,69 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.incallout; + +import com.complexible.stardog.plan.AbstractPropertyFunctionNodeBuilder; +import com.complexible.stardog.plan.PlanNodes; +import com.complexible.stardog.plan.QueryTerm; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; + +import java.util.List; + +public class WebFunctionInCallOutPropertyFunctionPlanNodeBuilder extends AbstractPropertyFunctionNodeBuilder { + + public WebFunctionInCallOutPropertyFunctionPlanNodeBuilder() { + arg(PlanNodes.empty()); + } + + /** + * {@inheritDoc} + */ + @Override + protected void validate() { + super.validate(); + + Preconditions.checkState(mObjects.stream().allMatch(QueryTerm::isVariable)); + Preconditions.checkState(!mSubjects.isEmpty() && mSubjects.get(0).isConstant()); + } + + /** + * {@inheritDoc} + */ + @Override + protected WebFunctionInCallOutPropertyFunctionPlanNode createNode(final ImmutableSet theSubjVars, + final ImmutableSet theObjVars, + final ImmutableSet theContextVars, + final ImmutableSet theAllVars) { + + return new WebFunctionInCallOutPropertyFunctionPlanNode(mArg, + mSubjects, + mObjects, + mContext, + mScope, + mCost, + mCardinality, + theSubjVars, + ImmutableSet.of(), + theObjVars, + theContextVars, + Sets.union(theSubjVars, theObjVars).immutableCopy(), + theAllVars); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean hasInputs() { + return mSubjects.stream().skip(1).anyMatch(QueryTerm::isVariable); + } + + /** + * {@inheritDoc} + */ + @Override + public List getInputs() { + return mSubjects.stream().filter(QueryTerm::isVariable).collect(ImmutableList.toImmutableList()); + } +} diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunction.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunction.java new file mode 100644 index 0000000..06cab03 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunction.java @@ -0,0 +1,106 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.outcallin; + +import com.complexible.stardog.index.statistics.Accuracy; +import com.complexible.stardog.index.statistics.Cardinality; +import com.complexible.stardog.plan.*; +import com.complexible.stardog.plan.eval.ExecutionContext; +import com.complexible.stardog.plan.eval.TranslateException; +import com.complexible.stardog.plan.eval.operator.*; +import com.complexible.stardog.plan.filter.expr.ValueOrError; +import com.complexible.stardog.plan.util.QueryTermRenderer; +import com.google.common.base.Preconditions; +import com.semantalytics.stardog.kibble.webfunctions.StardogWasmInstance; +import com.semantalytics.stardog.kibble.webfunctions.WebFunctionVocabulary; +import com.stardog.stark.IRI; +import com.stardog.stark.Values; + +import java.net.MalformedURLException; +import java.util.*; +import java.util.concurrent.ExecutionException; + +import static java.util.stream.Collectors.*; + +public class WebFunctionOutCallInPropertyFunction implements PropertyFunction { + + private static final WebFunctionVocabulary names = WebFunctionVocabulary.OUT_call_IN; + + /** + * {@inheritDoc} + */ + @Override + public List getURIs() { + return names.getNames().stream().map(Values::iri).collect(toList()); + } + + /** + * {@inheritDoc} + */ + @Override + public WebFunctionOutCallInPropertyFunctionPlanNodeBuilder newBuilder() { + return new WebFunctionOutCallInPropertyFunctionPlanNodeBuilder(); + } + + /** + * {@inheritDoc} + */ + @Override + public Operator translate(final ExecutionContext theExecutionContext, + final PropertyFunctionPlanNode thePropertyFunctionPlanNode, + final Operator theOperator) throws TranslateException { + + if (thePropertyFunctionPlanNode instanceof WebFunctionOutCallInPropertyFunctionPlanNode) { + return new WebFunctionOutCallInPropertyFunctionOperator(theExecutionContext, + (WebFunctionOutCallInPropertyFunctionPlanNode) thePropertyFunctionPlanNode, + theOperator); + } else { + throw new TranslateException("Invalid node type, cannot translate"); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void estimate(final PropertyFunctionPlanNode theNode, + Costs.CostingContext theContext) throws PlanException { + Preconditions.checkArgument(theNode instanceof WebFunctionOutCallInPropertyFunctionPlanNode); + + List argsValueOrError = ((WebFunctionOutCallInPropertyFunctionPlanNode) theNode).getInput().stream().map(queryTerm -> { + if(queryTerm.isVariable()) { + return ValueOrError.General.of(Values.iri(WebFunctionVocabulary.var.getImmutableName())); + } else { + return queryTerm.getValue(); + } + }).collect(toList()); + if(argsValueOrError.stream().anyMatch(ValueOrError::isError)) { + throw new PlanException("Unable to generate cardinalty estimates"); + } + + try { + StardogWasmInstance instance = StardogWasmInstance.from(((WebFunctionOutCallInPropertyFunctionPlanNode) theNode).getWasm().value()); + Cardinality cardinality = instance.getCardinality(theNode.getArg().getCardinality(), argsValueOrError.stream().map(ValueOrError::value).collect(toList())); + + theNode.setCardinality(Cardinality.of(cardinality.value(), Accuracy.takeLessAccurate(cardinality.accuracy(), theNode.getArg().getCardinality().accuracy()))); + } catch(ExecutionException | MalformedURLException e) { + throw new PlanException(e); + } + + // assume a flat cost of 1 per iteration + the cost of our child + theNode.setCost(theNode.getCardinality().value() + theNode.getArg().getCost()); + } + + /** + * {@inheritDoc} + */ + @Override + public String explain(final PropertyFunctionPlanNode theNode, final QueryTermRenderer theTermRenderer) { + Preconditions.checkArgument(theNode instanceof WebFunctionOutCallInPropertyFunctionPlanNode); + Preconditions.checkNotNull(theTermRenderer); + //TODO should this be added to the webassembly as well? + + final WebFunctionOutCallInPropertyFunctionPlanNode aNode = (WebFunctionOutCallInPropertyFunctionPlanNode) theNode; + + return String.format("Call WebFunction %s", aNode.getWasm()); + } + +} diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionOperator.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionOperator.java new file mode 100644 index 0000000..ec2d6b9 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionOperator.java @@ -0,0 +1,201 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.outcallin; + +import com.complexible.stardog.StardogException; +import com.complexible.stardog.plan.QueryTerm; +import com.complexible.stardog.plan.SortType; +import com.complexible.stardog.plan.eval.ExecutionContext; +import com.complexible.stardog.plan.eval.operator.*; +import com.complexible.stardog.plan.eval.operator.impl.AbstractOperator; +import com.complexible.stardog.plan.eval.operator.impl.Solutions; +import com.complexible.stardog.plan.filter.expr.ValueOrError; +import com.google.common.base.Preconditions; +import com.google.common.collect.Iterators; +import com.google.common.collect.Sets; +import com.semantalytics.stardog.kibble.webfunctions.StardogWasmInstance; +import com.stardog.stark.Value; +import com.stardog.stark.query.BindingSet; +import com.stardog.stark.query.SelectQueryResult; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Executable operator for the repeat function + * + * @author Michael Grove + */ +public final class WebFunctionOutCallInPropertyFunctionOperator extends AbstractOperator implements PropertyFunctionOperator { + + private ValueOrError wasmIRI ; + + /** + * The current solution + */ + private Solution solution; + + /** + * The child argument + */ + private final Optional mArg; + + /** + * The original node + */ + private final WebFunctionOutCallInPropertyFunctionPlanNode mNode; + + /** + * An iterator over the child solutions of this operator + */ + private Iterator mInputs = null; + + private StardogWasmInstance instance; + + private SelectQueryResult selectQueryResult; + + public WebFunctionOutCallInPropertyFunctionOperator(final ExecutionContext theExecutionContext, + final WebFunctionOutCallInPropertyFunctionPlanNode theNode, + final Operator theOperator) { + super(theExecutionContext, SortType.UNSORTED); + + mNode = Preconditions.checkNotNull(theNode); + mArg = Optional.of(theOperator); + wasmIRI = theNode.getWasm(); + + try { + //TODO should check if it's an error before assumign it's a value + instance = StardogWasmInstance.from(wasmIRI.value(), getMappings()); + } catch (ExecutionException | MalformedURLException e) { + throw new StardogException(e); + } + } + + @Override + protected Solution computeNext() { + if (mInputs == null) { + // first call to compute results, perform some init + // either use our child's solutions, or if we don't have a child, create a single solution to use + if (mArg.filter(theOp -> !(theOp instanceof EmptyOperator)).isPresent()) { + // these are the variables the child arg will bind + Set aVars = Sets.newHashSet(mArg.get().getVars()); + + // and these are the ones that the pf will bind + aVars.addAll(mNode.getResultVars().stream().map(QueryTerm::getName).collect(Collectors.toList())); + + // now we create a solution that contains room for bindings for these variables + final Solution aSoln = mExecutionContext.getSolutionFactory() + .variables(aVars) + .newSolution(); + + // and transform the child solutions to this one large enough to accomodate our vars + mInputs = Iterators.transform(mArg.get(), theSoln -> { + Solutions.copy(aSoln, theSoln); + return aSoln; + }); + } else if (mNode.getInput().stream().allMatch(QueryTerm::isVariable)) { + // no arg or empty operator and the input is a variable, there's nothing to repeat + return endOfData(); + } else { + final Set aVars = Sets.newHashSet(); + + aVars.addAll(mNode.getResultVars().stream().map(QueryTerm::getName).collect(Collectors.toList())); + + // we only want to create solutions with the minimum number of variables + mInputs = Iterators.singletonIterator(mExecutionContext.getSolutionFactory() + .variables(aVars) + .newSolution()); + } + } + + while (mInputs.hasNext()) { + if (solution == null) { + solution = mInputs.next(); + } + if (selectQueryResult == null) { + try { + ValueOrError[] valueOrErrors = mNode.getObjects().stream().map(queryTerm -> { + if (queryTerm.isVariable()) { + return solution.getValue(queryTerm.getName(), getMappings()); + } else { + return queryTerm.getValue(); + } + }).toArray(ValueOrError[]::new); + + if (Arrays.stream(valueOrErrors).noneMatch(ValueOrError::isError)) { + selectQueryResult = instance.evaluate(Arrays.stream(valueOrErrors).skip(1).map(ValueOrError::value).toArray(Value[]::new)); + } else { + instance.close(); + instance = null; + return endOfData(); + } + } catch (IOException e) { + instance.close(); + instance = null; + return endOfData(); + } + } + if(selectQueryResult.hasNext()) { + BindingSet bindingSet = selectQueryResult.next(); + if (mNode.getResultVars().size() <= bindingSet.size()) { + IntStream.range(0, mNode.getResultVars().size()).forEach(i -> + solution.setValue(mNode.getResultVars().get(i).getName(), bindingSet.get(String.format("value_%d", i)), getMappings())); + } else { + instance.close(); + instance = null; + return endOfData(); + } + return solution; + } else { + selectQueryResult = null; + solution = null; + } + } + instance.close(); + instance = null; + return endOfData(); + } + /* + + private long getValue() { + return mNode.getInput().isConstant() + ? mNode.getInput().getIndex() + : mValue.get(mNode.getInput().getName()); + } + + */ + + /** + * {@inheritDoc} + */ + @Override + protected void performReset() { + mArg.ifPresent(Operator::reset); + instance.close(); + instance = null; + } + + /** + * {@inheritDoc} + */ + @Override + public Set getVars() { + return mNode.getSubjects().stream() + .filter(QueryTerm::isVariable) + .map(QueryTerm::getName) + .collect(Collectors.toSet()); + } + + /** + * {@inheritDoc} + */ + @Override + public void accept(final OperatorVisitor theOperatorVisitor) { + theOperatorVisitor.visit(this); + } +} diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNode.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNode.java new file mode 100644 index 0000000..e36e1a4 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNode.java @@ -0,0 +1,111 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.outcallin; + +import com.complexible.stardog.index.statistics.Cardinality; +import com.complexible.stardog.plan.*; +import com.complexible.stardog.plan.filter.expr.ValueOrError; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.semantalytics.stardog.kibble.webfunctions.WebFunctionVocabulary; +import com.stardog.stark.IRI; + +import java.util.List; + +import static com.stardog.stark.Values.iri; +import static java.util.stream.Collectors.toList; + +public final class WebFunctionOutCallInPropertyFunctionPlanNode extends AbstractPropertyFunctionPlanNode { + + private static final WebFunctionVocabulary names = WebFunctionVocabulary.OUT_call_IN; + + public WebFunctionOutCallInPropertyFunctionPlanNode(final PlanNode theArg, + final List theSubjects, + final List theObjects, + final QueryTerm theContext, + final QueryDataset.Scope theScope, + final double theCost, + final Cardinality theCardinality, + final ImmutableSet theSubjVars, + final ImmutableSet thePredVars, + final ImmutableSet theObjVars, + final ImmutableSet theContextVars, + final ImmutableSet theAssuredVars, + final ImmutableSet theAllVars) { + super(theArg, + theSubjects, + theObjects, + theContext, + theScope, + theCost, + theCardinality, + theSubjVars, + thePredVars, + theObjVars, + theContextVars, + theAssuredVars, + theAllVars); + } + + public ValueOrError getWasm() { + return getObjects().get(0).getValue(); } + + + public List getInput() { + return getObjects().stream().collect(toList()); + } + + public List getResultVars() { + return getSubjects(); + } + + /** + * {@inheritDoc} + */ + @Override + public ImmutableList getInputs() { + return getObjects(); + } + + /** + * {@inheritDoc} + */ + @Override + public IRI getURI() { + return iri(names.getImmutableName()); + } + + /** + * {@inheritDoc} + */ + @Override + public WebFunctionOutCallInPropertyFunctionPlanNode copy() { + return new WebFunctionOutCallInPropertyFunctionPlanNode(getArg().copy(), + getSubjects(), + getObjects(), + getContext(), + getScope(), + getCost(), + getCardinality(), + getSubjectVars(), + getPredicateVars(), + getObjectVars(), + getContextVars(), + getAssuredVars(), + getAllVars()); + } + + /** + * {@inheritDoc} + */ + @Override + protected PropertyFunctionNodeBuilder createBuilder() { + return new WebFunctionOutCallInPropertyFunctionPlanNodeBuilder(); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean canEquals(final Object theObj) { + return theObj instanceof WebFunctionOutCallInPropertyFunctionPlanNode; + } +} \ No newline at end of file diff --git a/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNodeBuilder.java b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNodeBuilder.java new file mode 100644 index 0000000..a75f3e9 --- /dev/null +++ b/src/main/java/com/semantalytics/stardog/kibble/webfunctions/propertyfunction/outcallin/WebFunctionOutCallInPropertyFunctionPlanNodeBuilder.java @@ -0,0 +1,85 @@ +package com.semantalytics.stardog.kibble.webfunctions.propertyfunction.outcallin; + +import com.complexible.stardog.plan.AbstractPropertyFunctionNodeBuilder; +import com.complexible.stardog.plan.PlanNodes; +import com.complexible.stardog.plan.QueryTerm; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; + +import java.util.List; + +import static com.google.common.collect.ImmutableList.toImmutableList; + +/** + * Representation of the property function as a `PlanNode`. This is used to represent the function within a query plan. + * + * @author Michael Grove + */ + + +/** + * Basic builder for creating a {@link WebFunctionOutCallInPropertyFunctionPlanNode} + * + * @author Michael Grove + */ +public final class WebFunctionOutCallInPropertyFunctionPlanNodeBuilder extends AbstractPropertyFunctionNodeBuilder { + + public WebFunctionOutCallInPropertyFunctionPlanNodeBuilder() { + arg(PlanNodes.empty()); + } + + /** + * {@inheritDoc} + */ + @Override + protected void validate() { + super.validate(); + + Preconditions.checkState(mSubjects.stream().allMatch(QueryTerm::isVariable)); + Preconditions.checkState(!mObjects.isEmpty() && mObjects.get(0).isConstant()); + //TODO possibly relax constraint on constant with warning that not using a constant might kill performance + } + + /** + * {@inheritDoc} + */ + @Override + protected WebFunctionOutCallInPropertyFunctionPlanNode createNode(final ImmutableSet theSubjVars, + final ImmutableSet theObjVars, + final ImmutableSet theContextVars, + final ImmutableSet theAllVars) { + + return new WebFunctionOutCallInPropertyFunctionPlanNode(mArg, + mSubjects, + mObjects, + mContext, + mScope, + mCost, + mCardinality, + theSubjVars, + ImmutableSet.of(), + theObjVars, + theContextVars, + Sets.union(theSubjVars, theObjVars).immutableCopy(), + theAllVars); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean hasInputs() { + return mObjects.stream().skip(1).anyMatch(QueryTerm::isVariable); + } + + /** + * {@inheritDoc} + */ + @Override + public List getInputs() { + return mObjects.stream().filter(QueryTerm::isVariable).collect(toImmutableList()); + } +} + + diff --git a/src/test/java/com/semantalytics/stardog/kibble/AbstractStardogTest.java b/src/test/java/com/semantalytics/stardog/kibble/AbstractStardogTest.java index 9c8d2c9..ab61d3f 100644 --- a/src/test/java/com/semantalytics/stardog/kibble/AbstractStardogTest.java +++ b/src/test/java/com/semantalytics/stardog/kibble/AbstractStardogTest.java @@ -12,15 +12,20 @@ import com.complexible.stardog.api.admin.AdminConnectionConfiguration; import com.google.common.io.Files; import com.google.common.io.Resources; +import io.undertow.server.handlers.cache.CachedHttpRequest; +import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.net.URISyntaxException; +import java.nio.charset.Charset; public abstract class AbstractStardogTest { @@ -34,16 +39,19 @@ public abstract class AbstractStardogTest { @BeforeClass public static void beforeClass() throws IOException, ServerException { + AdminConnection adminConnection; try{ adminConnection = AdminConnectionConfiguration.toEmbeddedServer() .credentials("admin", "admin") .connect(); - adminConnection.setProperty("ipfs.gateway", "http://did.this.get.set"); } catch(StardogException e) { + Process process = new ProcessBuilder("cargo", "make", "build").directory(new File("./src/test/rust")).start(); + System.out.println(IOUtils.toString(process.getErrorStream(), Charset.defaultCharset())); + System.out.println(IOUtils.toString(process.getErrorStream(), Charset.defaultCharset())); - final File TEST_HOME = Files.createTempDir(); + final File TEST_HOME = Files.createTempDir(); TEST_HOME.deleteOnExit(); try { diff --git a/src/test/java/com/semantalytics/stardog/kibble/StardogWebfunctionsTestSuite.java b/src/test/java/com/semantalytics/stardog/kibble/StardogWebfunctionsTestSuite.java deleted file mode 100644 index d1163fa..0000000 --- a/src/test/java/com/semantalytics/stardog/kibble/StardogWebfunctionsTestSuite.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.semantalytics.stardog.kibble; - -import com.complexible.common.protocols.server.Server; -import com.complexible.common.protocols.server.ServerException; -import com.complexible.stardog.Stardog; -import com.complexible.stardog.StardogConfiguration; -import com.complexible.stardog.api.Connection; -import com.complexible.stardog.api.admin.AdminConnection; -import com.complexible.stardog.api.admin.AdminConnectionConfiguration; -import com.google.common.io.Files; -import junit.framework.TestCase; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -import java.io.File; -import java.io.IOException; -import java.net.InetSocketAddress; - -@RunWith(Suite.class) -@SuiteClasses({ - }) - -public class StardogWebfunctionsTestSuite extends TestCase { - - private static Stardog STARDOG; - private static Server SERVER; - public static final String DB = "test"; - public static final int TEST_PORT = 5888; - protected Connection connection; - private static final String STARDOG_LICENSE_PATH = System.getenv("STARDOG_LICENSE_PATH"); - - @BeforeClass - public static void beforeClass() throws IOException, ServerException { - - final File TEST_HOME; - - TEST_HOME = Files.createTempDir(); - TEST_HOME.deleteOnExit(); - - STARDOG = Stardog.builder() - .set(StardogConfiguration.LICENSE_LOCATION, STARDOG_LICENSE_PATH) - .home(TEST_HOME).create(); - - SERVER = STARDOG.newServer() - .bind(new InetSocketAddress("localhost", TEST_PORT)) - .start(); - - final AdminConnection adminConnection = AdminConnectionConfiguration.toEmbeddedServer() - .credentials("admin", "admin") - .connect(); - - if (adminConnection.list().contains(DB)) { - adminConnection.drop(DB); - } - - adminConnection.newDatabase(DB).create(); - } - - @AfterClass - public static void afterClass() { - if (SERVER != null) { - SERVER.stop(); - } - STARDOG.shutdown(); - } -} diff --git a/src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestInCallOutPropertyFunction.java b/src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestInCallOutPropertyFunction.java new file mode 100644 index 0000000..1f091fe --- /dev/null +++ b/src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestInCallOutPropertyFunction.java @@ -0,0 +1,122 @@ +package com.semantalytics.stardog.kibble.webfunctions; + +import com.semantalytics.stardog.kibble.AbstractStardogTest; +import com.stardog.stark.Literal; +import com.stardog.stark.Value; +import com.stardog.stark.query.SelectQueryResult; +import org.junit.Test; + +import java.util.Optional; + +import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertStringLiteral; +import static org.assertj.core.api.Assertions.assertThat; + +public class TestInCallOutPropertyFunction extends AbstractStardogTest { + + @Test + public void testToUpperpf() { + + final String aQuery = WebFunctionVocabulary.sparqlPrefix("wf", "0.0.0") + + "prefix f: " + + " select ?result where {" + + "(\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" \"stardog\") wf:IN_call_OUT ?result" + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + final Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + final Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + final Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("STARDOG"); + assertThat(aResult).isExhausted(); + } + } + + @Test + public void testToUpperpfInCallOut() { + + final String aQuery = WebFunctionVocabulary.sparqlPrefix("wf", "0.0.0") + + "prefix f: " + + " select ?result where {" + + "(\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" \"stardog\") wf:IN_call_OUT ?result" + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + final Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + final Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + final Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("STARDOG"); + assertThat(aResult).isExhausted(); + } + } + + + @Test + public void testToUpperPfVar() { + + final String aQuery = WebFunctionVocabulary.sparqlPrefix("wf", "0.0.0") + + "prefix f: " + + " select ?result where {" + + "BIND(\"stardog\" as ?str)" + + "(\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" ?str) wf:IN_call_OUT ?result" + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + final Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + final Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + final Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("STARDOG"); + assertThat(aResult).isExhausted(); + } + } + + @Test + public void testToUpperPfMultiple() { + + final String aQuery = WebFunctionVocabulary.sparqlPrefix("wf", "0.0.0") + + "prefix f: " + + " select ?result where {" + + "(\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" ?str) wf:IN_call_OUT ?result" + + " values ?str { \"what language do you think this is\" \"como estas\" \"bonjour\"}}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("WHAT LANGUAGE DO YOU THINK THIS IS"); + + assertThat(aResult).hasNext(); + aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("COMO ESTAS"); + + assertThat(aResult).hasNext(); + aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("BONJOUR"); + + assertThat(aResult).isExhausted(); + } + } +} diff --git a/src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestOutCallInPropertyFunction.java b/src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestOutCallInPropertyFunction.java new file mode 100644 index 0000000..9f94ecb --- /dev/null +++ b/src/test/java/com/semantalytics/stardog/kibble/webfunctions/TestOutCallInPropertyFunction.java @@ -0,0 +1,141 @@ +package com.semantalytics.stardog.kibble.webfunctions; + +import com.semantalytics.stardog.kibble.AbstractStardogTest; +import com.stardog.stark.Literal; +import com.stardog.stark.Value; +import com.stardog.stark.query.BindingSet; +import com.stardog.stark.query.SelectQueryResult; +import org.junit.Test; + +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +import static com.complexible.stardog.plan.filter.functions.AbstractFunction.assertStringLiteral; +import static org.assertj.core.api.Assertions.assertThat; + +public class TestOutCallInPropertyFunction extends AbstractStardogTest { + + final String queryHeader = WebFunctionVocabulary.sparqlPrefix("wf", "0.0.0") + + " prefix f: "; + + @Test + public void testToUpperpfOutCallIn() { + + final String aQuery = queryHeader + + " SELECT ?result WHERE {" + + "?result wf:OUT_call_IN (\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" \"stardog\")" + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + final Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + final Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + final Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("STARDOG"); + assertThat(aResult).isExhausted(); + } + } + + @Test(expected = ExecutionException.class) + public void testConstantOutputShouldFail() { + + final String aQuery = queryHeader + + " select ?result where {" + + "\"stardog\" wf:OUT_call_IN (\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" \"stardog\")" + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + assertThat(aResult.hasNext()).isTrue(); + + final BindingSet aBindingSet = aResult.next(); + + assertThat(aBindingSet.size()).isZero(); + assertThat(aResult.hasNext()).isFalse(); + } + } + + @Test + public void testToUpperpfInCallOut() { + + final String aQuery = queryHeader + + " select ?result where {" + + "(\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" \"stardog\") wf:IN_call_OUT ?result" + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + final Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + final Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + final Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("STARDOG"); + assertThat(aResult).isExhausted(); + } + } + + + @Test + public void testToUpperPfVar() { + + final String aQuery = queryHeader + + " select ?result where {" + + "BIND(\"stardog\" as ?str)" + + "?result wf:OUT_call_IN (\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" ?str) " + + "}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + final Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + final Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + final Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("STARDOG"); + assertThat(aResult).isExhausted(); + } + } + + @Test + public void testToUpperPfMultiple() { + + final String aQuery = queryHeader + + " select ?result where {" + + "?result wf:OUT_call_IN (\"file:src/test/rust/target/wasm32-unknown-unknown/release/to_upper.wasm\" ?str)" + + " values ?str { \"what language do you think this is\" \"como estas\" \"bonjour\"}}"; + + try (final SelectQueryResult aResult = connection.select(aQuery).execute()) { + + assertThat(aResult).hasNext(); + Optional aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + Value aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + Literal aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("WHAT LANGUAGE DO YOU THINK THIS IS"); + + assertThat(aResult).hasNext(); + aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("COMO ESTAS"); + + assertThat(aResult).hasNext(); + aPossibleValue = aResult.next().value("result"); + assertThat(aPossibleValue).isPresent(); + aValue = aPossibleValue.get(); + assertThat(assertStringLiteral(aValue)); + aLiteral = ((Literal)aValue); + assertThat(aLiteral.label()).isEqualTo("BONJOUR"); + + assertThat(aResult).isExhausted(); + } + } +} diff --git a/src/test/java/com/semantalytics/stardog/kibble/webfunctions/WasmTestSuite.java b/src/test/java/com/semantalytics/stardog/kibble/webfunctions/WasmTestSuite.java index 58c2cae..5ed577d 100644 --- a/src/test/java/com/semantalytics/stardog/kibble/webfunctions/WasmTestSuite.java +++ b/src/test/java/com/semantalytics/stardog/kibble/webfunctions/WasmTestSuite.java @@ -11,29 +11,36 @@ import com.google.common.io.Files; import com.semantalytics.stardog.kibble.webfunctions.*; import junit.framework.TestCase; +import org.apache.commons.io.IOUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.net.InetSocketAddress; +import java.nio.charset.Charset; @RunWith(Suite.class) @SuiteClasses({ TestAggregate.class, + TestArrayOf.class, TestCache.class, TestCall.class, TestCompose.class, TestDoc.class, TestFilter.class, + TestInCallOutPropertyFunction.class, TestIpfsGet.class, TestMap.class, - TestMappingDictionaryAdd.class, TestMappingDictionaryGet.class, + TestMappingDictionaryAdd.class, TestMemoize.class, + TestOutCallInPropertyFunction.class, TestPartial.class, TestPluginVersion.class, TestReduce.class, @@ -54,6 +61,10 @@ public class WasmTestSuite extends TestCase { @BeforeClass public static void beforeClass() throws IOException, ServerException { + Process process = new ProcessBuilder("cargo", "make", "build").directory(new File("./src/test/rust")).start(); + System.out.println(IOUtils.toString(process.getErrorStream(), Charset.defaultCharset())); + System.out.println(IOUtils.toString(process.getErrorStream(), Charset.defaultCharset())); + try{ AdminConnectionConfiguration.toEmbeddedServer() .credentials("admin", "admin") diff --git a/src/test/rust/function/array_of/src/lib.rs b/src/test/rust/function/array_of/src/lib.rs index 847eb02..cdaa52a 100644 --- a/src/test/rust/function/array_of/src/lib.rs +++ b/src/test/rust/function/array_of/src/lib.rs @@ -42,16 +42,5 @@ arguemnts: #[no_mangle] pub extern fn cardinality_estimate(subject: *mut c_char) -> *mut c_char { - let subject = unsafe { CStr::from_ptr(subject).to_str().unwrap() }; - - let values: Value = serde_json::from_str(subject).unwrap(); - let estimate = values["results"]["bindings"][0]["value_0"]["value"].as_str().unwrap(); - - let result = json!({ - "head": {"vars":["value_0", "value_1"]}, "results":{"bindings":[ - {"value_0":{"type":"literal","value": estimate}, "value_1":{"type":"literal","value": "ACCURATE"}} - ]} - }).to_string().into_bytes(); - - unsafe { CString::from_vec_unchecked(result) }.into_raw() + return stardog_function::cardinality_estimate(subject); } \ No newline at end of file diff --git a/src/test/rust/function/to_upper/src/lib.rs b/src/test/rust/function/to_upper/src/lib.rs index 24a9995..98237b1 100644 --- a/src/test/rust/function/to_upper/src/lib.rs +++ b/src/test/rust/function/to_upper/src/lib.rs @@ -24,16 +24,5 @@ pub extern fn evaluate(subject: *mut c_char) -> *mut c_char { #[no_mangle] pub extern fn cardinality_estimate(subject: *mut c_char) -> *mut c_char { - let subject = unsafe { CStr::from_ptr(subject).to_str().unwrap() }; - - let values: Value = serde_json::from_str(subject).unwrap(); - let estimate = values["results"]["bindings"][0]["value_0"]["value"].as_str().unwrap(); - - let result = json!({ - "head": {"vars":["value_0", "value_1"]}, "results":{"bindings":[ - {"value_0":{"type":"literal","value": estimate}, "value_1":{"type":"literal","value": "ACCURATE"}} - ]} - }).to_string().into_bytes(); - - unsafe { CString::from_vec_unchecked(result) }.into_raw() + return stardog_function::cardinality_estimate(subject); } \ No newline at end of file