/androjena/src/com/hp/hpl/jena/reasoner/rulesys/builtins/StrConcat.java

http://androjena.googlecode.com/ · Java · 112 lines · 35 code · 10 blank · 67 comment · 8 complexity · 016cc9d829b230e57e6a03d5af54abf3 MD5 · raw file

  1. /******************************************************************
  2. * File: StrConcat.java
  3. * Created by: Dave Reynolds
  4. * Created on: 10 Jan 2007
  5. *
  6. * (c) Copyright 2007, Hewlett-Packard Development Company, LP
  7. * [See end of file]
  8. * $Id: StrConcat.java,v 1.1 2009/06/29 08:55:36 castagna Exp $
  9. *****************************************************************/
  10. package com.hp.hpl.jena.reasoner.rulesys.builtins;
  11. import com.hp.hpl.jena.graph.Node;
  12. import com.hp.hpl.jena.reasoner.rulesys.BuiltinException;
  13. import com.hp.hpl.jena.reasoner.rulesys.RuleContext;
  14. /**
  15. * Builtin which concatenates a set of strings. It binds the last argument to
  16. * a plain literal which is the concatenation of all the preceeding arguments.
  17. * For a literal argument we use its lexcical form, for a URI argument its URI,
  18. * for a bNode argument its internal ID.
  19. *
  20. * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
  21. * @version $Revision: 1.1 $
  22. */
  23. public class StrConcat extends BaseBuiltin {
  24. /**
  25. * Return a name for this builtin, normally this will be the name of the
  26. * functor that will be used to invoke it.
  27. */
  28. public String getName() {
  29. return "strConcat";
  30. }
  31. /**
  32. * Return the expected number of arguments for this functor or 0 if the number is flexible.
  33. */
  34. @Override
  35. public int getArgLength() {
  36. return 0;
  37. }
  38. /**
  39. * This method is invoked when the builtin is called in a rule body.
  40. * @param args the array of argument values for the builtin, this is an array
  41. * of Nodes, some of which may be Node_RuleVariables.
  42. * @param length the length of the argument list, may be less than the length of the args array
  43. * for some rule engines
  44. * @param context an execution context giving access to other relevant data
  45. * @return return true if the buildin predicate is deemed to have succeeded in
  46. * the current environment
  47. */
  48. @Override
  49. public boolean bodyCall(Node[] args, int length, RuleContext context) {
  50. if (length < 1)
  51. throw new BuiltinException(this, context, "Must have at least 1 argument to " + getName());
  52. StringBuffer buff = new StringBuffer();
  53. for (int i = 0; i < length-1; i++) {
  54. buff.append( lex(getArg(i, args, context), context) );
  55. }
  56. Node result = Node.createLiteral(buff.toString());
  57. return context.getEnv().bind(args[length-1], result);
  58. }
  59. /**
  60. * Return the appropriate lexical form of a node
  61. */
  62. protected String lex(Node n, RuleContext context) {
  63. if (n.isBlank()) {
  64. return n.getBlankNodeLabel();
  65. } else if (n.isURI()) {
  66. return n.getURI();
  67. } else if (n.isLiteral()) {
  68. return n.getLiteralLexicalForm();
  69. } else {
  70. throw new BuiltinException(this, context, "Illegal node type: " + n);
  71. }
  72. }
  73. }
  74. /*
  75. (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP
  76. All rights reserved.
  77. Redistribution and use in source and binary forms, with or without
  78. modification, are permitted provided that the following conditions
  79. are met:
  80. 1. Redistributions of source code must retain the above copyright
  81. notice, this list of conditions and the following disclaimer.
  82. 2. Redistributions in binary form must reproduce the above copyright
  83. notice, this list of conditions and the following disclaimer in the
  84. documentation and/or other materials provided with the distribution.
  85. 3. The name of the author may not be used to endorse or promote products
  86. derived from this software without specific prior written permission.
  87. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  88. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  89. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  90. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  91. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  92. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  93. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  94. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  95. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  96. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  97. */