1 10 package com.hp.hpl.jena.reasoner.rulesys; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.util.PrintUtil; 14 import com.hp.hpl.jena.util.iterator.Filter; 15 import com.hp.hpl.jena.datatypes.*; 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import java.util.*; 20 21 33 public class Functor implements ClauseEntry { 34 35 protected String name; 36 37 38 protected Node[] args; 39 40 41 protected Builtin implementor; 42 43 44 public static final Filter acceptFilter = new Filter() { 45 public boolean accept(Object t) { 46 if (((Triple)t).getSubject().isLiteral()) return true; 47 Node n = ((Triple)t).getObject(); 48 return n.isLiteral() && n.getLiteral().getDatatype() == FunctorDatatype.theFunctorDatatype; 49 } 50 }; 51 52 protected static Log logger = LogFactory.getLog(Functor.class); 53 54 59 public Functor(String name, List args) { 60 this.name = name; 61 this.args = (Node[]) args.toArray(new Node[]{}); 62 } 63 64 70 public Functor(String name, Node[] args) { 71 this.name = name; 72 this.args = args; 73 } 74 75 82 public Functor(String name, List args, BuiltinRegistry registry) { 83 this.name = name; 84 this.args = (Node[]) args.toArray(new Node[]{}); 85 this.implementor = registry.getImplementation(name); 86 } 87 88 91 public String getName() { 92 return name; 93 } 94 95 98 public Node[] getArgs() { 99 return args; 100 } 101 102 105 public int getArgLength() { 106 return args.length; 107 } 108 109 112 public boolean isGround() { 113 for (int i = 0; i < args.length; i++) { 114 Node n = args[i]; 115 if (n instanceof Node_RuleVariable || n instanceof Node_ANY) { 116 return false; 117 } 118 } 119 return true; 120 } 121 122 125 public boolean isGround(BindingEnvironment env) { 126 for (int i = 0; i < args.length; i++) { 127 Node n = args[i]; 128 if (env.getGroundVersion(args[i]).isVariable()) return false; 129 } 130 return true; 131 } 132 133 138 public boolean evalAsBodyClause(RuleContext context) { 139 if (getImplementor() == null) { 140 logger.warn("Invoking undefined functor " + getName() + " in " + context.getRule().toShortString()); 141 return false; 142 } 143 return implementor.bodyCall(getBoundArgs(context.getEnv()), args.length, context); 144 } 145 146 151 public boolean safeEvalAsBodyClause(RuleContext context) { 152 if (getImplementor() == null) { 153 logger.warn("Invoking undefined functor " + getName() + " in " + context.getRule().toShortString()); 154 return false; 155 } 156 if (implementor.isSafe()) { 157 return implementor.bodyCall(getBoundArgs(context.getEnv()), args.length, context); 158 } else { 159 return false; 160 } 161 } 162 163 166 public Node[] getBoundArgs(BindingEnvironment env) { 167 Node[] boundargs = new Node[args.length]; 168 for (int i = 0; i < args.length; i++) { 169 boundargs[i] = env.getGroundVersion(args[i]); 170 } 171 return boundargs; 172 } 173 174 178 public Builtin getImplementor() { 179 if (implementor == null) { 180 implementor = BuiltinRegistry.theRegistry.getImplementation(name); 181 } 182 return implementor; 183 } 184 185 188 public void setImplementor(Builtin implementor) { 189 this.implementor = implementor; 190 } 191 192 195 public String toString() { 196 StringBuffer buff = new StringBuffer (name); 197 buff.append("("); 198 for (int i = 0; i < args.length; i++) { 199 buff.append(PrintUtil.print(args[i])); 200 if (i < args.length - 1) { 201 buff.append(" "); 202 } 203 } 204 buff.append(")"); 205 return buff.toString(); 206 } 207 208 211 public static boolean isFunctor(Node n) { 212 if (n == null) return false; 213 return n.isLiteral() && n.getLiteral().getDatatype() == FunctorDatatype.theFunctorDatatype; 214 } 215 216 219 public boolean equals(Object obj) { 220 if (obj instanceof Functor) { 221 Functor f2 = (Functor)obj; 222 if (name.equals(f2.name) && args.length == f2.args.length) { 223 for (int i = 0; i < args.length; i++) { 224 if (!args[i].sameValueAs(f2.args[i])) return false; 225 } 226 return true; 227 } 228 } 229 return false; 230 } 231 232 233 public int hashCode() { 234 return (name.hashCode()) ^ (args.length << 2); 235 } 236 237 241 public boolean sameAs(Object o) { 242 if (o instanceof Functor) { 243 Functor f2 = (Functor)o; 244 if (name.equals(f2.name) && args.length == f2.args.length) { 245 for (int i = 0; i < args.length; i++) { 246 if (! Node_RuleVariable.sameNodeAs(args[i], f2.args[i])) return false; 247 } 248 return true; 249 } 250 } 251 return false; 252 } 253 254 260 public static Node makeFunctorNode(String name, Node[] args) { 261 return makeFunctorNode( new Functor( name, args ) ); 262 } 263 264 268 public static Node makeFunctorNode(Functor f) { 269 return Node.createUncachedLiteral(f, null, FunctorDatatype.theFunctorDatatype); 270 } 271 272 276 public static class FunctorDatatype extends BaseDatatype { 277 278 public FunctorDatatype() { 279 super("urn:x-hp-jena:Functor"); 280 } 281 282 public static final RDFDatatype theFunctorDatatype = new FunctorDatatype(); 283 } 284 285 } 286 287 316 | Popular Tags |