1 10 package com.hp.hpl.jena.reasoner.rulesys.builtins; 11 12 import com.hp.hpl.jena.reasoner.rulesys.*; 13 import com.hp.hpl.jena.vocabulary.RDF; 14 import com.hp.hpl.jena.graph.*; 15 16 23 public class ListLength extends BaseBuiltin { 24 25 29 public String getName() { 30 return "listLength"; 31 } 32 33 36 public int getArgLength() { 37 return 2; 38 } 39 40 50 public boolean bodyCall(Node[] args, int length, RuleContext context) { 51 checkArgs(length, context); 52 BindingEnvironment env = context.getEnv(); 53 int len = getLength(getArg(0, args, context), context); 54 if (len == -1) { 55 return false; 56 } else { 57 env.bind(args[1], Util.makeIntNode(len)); 58 return true; 59 } 60 } 61 62 68 protected static int getLength(Node node, RuleContext context ) { 69 if (node.equals(RDF.Nodes.nil)) { 70 return 0; 71 } else { 72 Node next = Util.getPropValue(node, RDF.Nodes.rest, context); 73 if (next == null) { 74 return -1; 75 } else { 76 int sublen = getLength(next, context); 77 if (sublen == -1) { 78 return -1; 79 } else { 80 return 1 + sublen; 81 } 82 } 83 } 84 } 85 } 86 87 88 89 | Popular Tags |