Repository: marmotta Updated Branches: refs/heads/develop fabf99090 -> 1d58fa6cb
towards MARMOTTA-543 (modular function support): - started with service registry for native functions Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/1d58fa6c Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/1d58fa6c Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/1d58fa6c Branch: refs/heads/develop Commit: 1d58fa6cbfbfaea13f77de5aa0225a62d6faf80b Parents: fabf990 Author: Sebastian Schaffert <[email protected]> Authored: Fri Sep 19 16:56:39 2014 +0200 Committer: Sebastian Schaffert <[email protected]> Committed: Fri Sep 19 16:56:39 2014 +0200 ---------------------------------------------------------------------- .../sparql/function/FulltextQueryFunction.java | 63 --------- .../sparql/function/FulltextSearchFunction.java | 62 --------- .../function/custom/FulltextQueryFunction.java | 139 +++++++++++++++++++ .../function/custom/FulltextSearchFunction.java | 139 +++++++++++++++++++ ...marmotta.kiwi.sparql.function.NativeFunction | 61 ++++++++ ...f.query.algebra.evaluation.function.Function | 4 +- 6 files changed, 341 insertions(+), 127 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/1d58fa6c/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextQueryFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextQueryFunction.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextQueryFunction.java deleted file mode 100644 index 1490132..0000000 --- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextQueryFunction.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.marmotta.kiwi.sparql.function; - -import org.apache.marmotta.kiwi.vocabulary.FN_MARMOTTA; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; -import org.openrdf.query.algebra.evaluation.function.Function; -import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; - -/** - * A SPARQL function for doing a full-text search on the content of a string using a query language with boolean operators. - * The query syntax is the syntax of PostgreSQL (see http://www.postgresql.org/docs/9.1/static/datatype-textsearch.html) - * Should be implemented directly in the database, as the in-memory implementation is non-functional. - * <p/> - * The function can be called either as: - * <ul> - * <li>fn:fulltext-query(?var, 'query') - using a generic stemmer and dictionary</li> - * <li> - * fn:fulltext-query(?var, 'query', 'language') - using a language-specific stemmer and dictionary - * (currently only supported by PostgreSQL with the language values 'english', 'german', 'french', 'italian', 'spanish' - * and some other languages as supported by PostgreSQL). - * </li>* - * </ul> - * Note that for performance reasons it might be preferrable to create a full-text index for your database. Please - * consult your database documentation on how to do this. - * - * @author Sebastian Schaffert ([email protected]) - */ -public class FulltextQueryFunction implements Function { - - // auto-register for SPARQL environment - static { - if(!FunctionRegistry.getInstance().has(FN_MARMOTTA.QUERY_FULLTEXT.toString())) { - FunctionRegistry.getInstance().add(new FulltextQueryFunction()); - } - } - - @Override - public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { - throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); - } - - @Override - public String getURI() { - return FN_MARMOTTA.QUERY_FULLTEXT.toString(); - } -} http://git-wip-us.apache.org/repos/asf/marmotta/blob/1d58fa6c/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextSearchFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextSearchFunction.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextSearchFunction.java deleted file mode 100644 index 4386496..0000000 --- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/FulltextSearchFunction.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.marmotta.kiwi.sparql.function; - -import org.apache.marmotta.kiwi.vocabulary.FN_MARMOTTA; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; -import org.openrdf.query.algebra.evaluation.function.Function; -import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; - -/** - * A SPARQL function for doing a full-text search on the content of a string. Should be implemented directly in - * the database, as the in-memory implementation is non-functional. - * <p/> - * The function can be called either as: - * <ul> - * <li>fn:fulltext-search(?var, 'query') - using a generic stemmer and dictionary</li> - * <li> - * fn:fulltext-search(?var, 'query', 'language') - using a language-specific stemmer and dictionary - * (currently only supported by PostgreSQL with the language values 'english', 'german', 'french', 'italian', 'spanish' - * and some other languages as supported by PostgreSQL). - * </li>* - * </ul> - * Note that for performance reasons it might be preferrable to create a full-text index for your database. Please - * consult your database documentation on how to do this. - * - * @author Sebastian Schaffert ([email protected]) - */ -public class FulltextSearchFunction implements Function { - - // auto-register for SPARQL environment - static { - if(!FunctionRegistry.getInstance().has(FN_MARMOTTA.SEARCH_FULLTEXT.toString())) { - FunctionRegistry.getInstance().add(new FulltextSearchFunction()); - } - } - - @Override - public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { - throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); - } - - @Override - public String getURI() { - return FN_MARMOTTA.SEARCH_FULLTEXT.toString(); - } -} http://git-wip-us.apache.org/repos/asf/marmotta/blob/1d58fa6c/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextQueryFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextQueryFunction.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextQueryFunction.java new file mode 100644 index 0000000..5376475 --- /dev/null +++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextQueryFunction.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.function.custom; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.OPTypes; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_MARMOTTA; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a full-text search on the content of a string using a query language with boolean operators. + * The query syntax is the syntax of PostgreSQL (see http://www.postgresql.org/docs/9.1/static/datatype-textsearch.html) + * Should be implemented directly in the database, as the in-memory implementation is non-functional. + * <p/> + * The function can be called either as: + * <ul> + * <li>fn:fulltext-query(?var, 'query') - using a generic stemmer and dictionary</li> + * <li> + * fn:fulltext-query(?var, 'query', 'language') - using a language-specific stemmer and dictionary + * (currently only supported by PostgreSQL with the language values 'english', 'german', 'french', 'italian', 'spanish' + * and some other languages as supported by PostgreSQL). + * </li>* + * </ul> + * Note that for performance reasons it might be preferrable to create a full-text index for your database. Please + * consult your database documentation on how to do this. + * + * @author Sebastian Schaffert ([email protected]) + */ +public class FulltextQueryFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if(!FunctionRegistry.getInstance().has(FN_MARMOTTA.QUERY_FULLTEXT.toString())) { + FunctionRegistry.getInstance().add(new FulltextQueryFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_MARMOTTA.QUERY_FULLTEXT.toString(); + } + + + /** + * Return true if this function has available native support for the given dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this function is translated into SQL in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if(dialect instanceof PostgreSQLDialect) { + if(args.length == 2) { + return String.format("(to_tsvector('simple' :: regconfig,%1$s) @@ to_tsquery('simple' :: regconfig,%2$s))", args[0], args[1]); + } else if(args.length == 3) { + return String.format("(to_tsvector(kiwi_ft_lang(%3$s) :: regconfig, %1$s) @@ to_tsquery(kiwi_ft_lang(%3$s) :: regconfig, %2$s))", args[0], args[1], args[2]); + } + } + throw new UnsupportedOperationException("fulltext search not supported by dialect "+dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting inside KiWi. + * + * @return + */ + @Override + public OPTypes getReturnType() { + return OPTypes.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting to count at 0). + * This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public OPTypes getArgumentType(int arg) { + return OPTypes.STRING; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 3; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/1d58fa6c/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextSearchFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextSearchFunction.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextSearchFunction.java new file mode 100644 index 0000000..81459cc --- /dev/null +++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/custom/FulltextSearchFunction.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.function.custom; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.OPTypes; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_MARMOTTA; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a full-text search on the content of a string. Should be implemented directly in + * the database, as the in-memory implementation is non-functional. + * <p/> + * The function can be called either as: + * <ul> + * <li>fn:fulltext-search(?var, 'query') - using a generic stemmer and dictionary</li> + * <li> + * fn:fulltext-search(?var, 'query', 'language') - using a language-specific stemmer and dictionary + * (currently only supported by PostgreSQL with the language values 'english', 'german', 'french', 'italian', 'spanish' + * and some other languages as supported by PostgreSQL). + * </li>* + * </ul> + * Note that for performance reasons it might be preferrable to create a full-text index for your database. Please + * consult your database documentation on how to do this. + * + * @author Sebastian Schaffert ([email protected]) + */ +public class FulltextSearchFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if(!FunctionRegistry.getInstance().has(FN_MARMOTTA.SEARCH_FULLTEXT.toString())) { + FunctionRegistry.getInstance().add(new FulltextSearchFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_MARMOTTA.SEARCH_FULLTEXT.toString(); + } + + + /** + * Return true if this function has available native support for the given dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this function is translated into SQL in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if(dialect instanceof PostgreSQLDialect) { + if(args.length == 2) { + return String.format("(to_tsvector('simple' :: regconfig,%1$s) @@ plainto_tsquery('simple' :: regconfig,%2$s))", args[0], args[1]); + } else if(args.length == 3) { + return String.format("(to_tsvector(kiwi_ft_lang(%3$s) :: regconfig, %1$s) @@ plainto_tsquery(kiwi_ft_lang(%3$s) :: regconfig, %2$s))", args[0], args[1], args[2]); + } + + } + throw new UnsupportedOperationException("fulltext search not supported by dialect "+dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting inside KiWi. + * + * @return + */ + @Override + public OPTypes getReturnType() { + return OPTypes.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting to count at 0). + * This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public OPTypes getArgumentType(int arg) { + return OPTypes.STRING; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 3; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/1d58fa6c/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction new file mode 100644 index 0000000..94b3db8 --- /dev/null +++ b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction @@ -0,0 +1,61 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +org.apache.marmotta.kiwi.sparql.function.cast.NBooleanCast +org.apache.marmotta.kiwi.sparql.function.cast.NDateTimeCast +org.apache.marmotta.kiwi.sparql.function.cast.NDecimalCast +org.apache.marmotta.kiwi.sparql.function.cast.NDoubleCast +org.apache.marmotta.kiwi.sparql.function.cast.NFloatCast +org.apache.marmotta.kiwi.sparql.function.cast.NIntegerCast +org.apache.marmotta.kiwi.sparql.function.cast.NStringCast + +org.apache.marmotta.kiwi.sparql.function.custom.FulltextQueryFunction +org.apache.marmotta.kiwi.sparql.function.custom.FulltextSearchFunction + +org.apache.marmotta.kiwi.sparql.function.datetime.NDay +org.apache.marmotta.kiwi.sparql.function.datetime.NHours +org.apache.marmotta.kiwi.sparql.function.datetime.NMinutes +org.apache.marmotta.kiwi.sparql.function.datetime.NMonth +org.apache.marmotta.kiwi.sparql.function.datetime.NNow +org.apache.marmotta.kiwi.sparql.function.datetime.NSeconds +org.apache.marmotta.kiwi.sparql.function.datetime.NYear + +org.apache.marmotta.kiwi.sparql.function.hash.NMD5 +org.apache.marmotta.kiwi.sparql.function.hash.NSHA1 +org.apache.marmotta.kiwi.sparql.function.hash.NSHA256 +org.apache.marmotta.kiwi.sparql.function.hash.NSHA384 +org.apache.marmotta.kiwi.sparql.function.hash.NSHA512 + +org.apache.marmotta.kiwi.sparql.function.numeric.NAbs +org.apache.marmotta.kiwi.sparql.function.numeric.NCeil +org.apache.marmotta.kiwi.sparql.function.numeric.NFloor +org.apache.marmotta.kiwi.sparql.function.numeric.NRand +org.apache.marmotta.kiwi.sparql.function.numeric.NRound + +org.apache.marmotta.kiwi.sparql.function.rdfterm.NSTRUUID +org.apache.marmotta.kiwi.sparql.function.rdfterm.NUUID + +org.apache.marmotta.kiwi.sparql.function.string.NConcat +org.apache.marmotta.kiwi.sparql.function.string.NContains +org.apache.marmotta.kiwi.sparql.function.string.NLowerCase +org.apache.marmotta.kiwi.sparql.function.string.NReplace +org.apache.marmotta.kiwi.sparql.function.string.NStrAfter +org.apache.marmotta.kiwi.sparql.function.string.NStrBefore +org.apache.marmotta.kiwi.sparql.function.string.NStrEnds +org.apache.marmotta.kiwi.sparql.function.string.NStrLen +org.apache.marmotta.kiwi.sparql.function.string.NStrStarts +org.apache.marmotta.kiwi.sparql.function.string.NUpperCase \ No newline at end of file http://git-wip-us.apache.org/repos/asf/marmotta/blob/1d58fa6c/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function index 2686bd2..dad77ca 100644 --- a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function +++ b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function @@ -1,2 +1,2 @@ -org.apache.marmotta.kiwi.sparql.function.FulltextSearchFunction -org.apache.marmotta.kiwi.sparql.function.FulltextQueryFunction \ No newline at end of file +org.apache.marmotta.kiwi.sparql.function.custom.FulltextSearchFunction +org.apache.marmotta.kiwi.sparql.function.custom.FulltextQueryFunction \ No newline at end of file
