1 6 7 package com.hp.hpl.jena.graph.query; 8 9 import java.util.*; 10 11 import com.hp.hpl.jena.util.CollectionFactory; 12 13 33 public interface Expression 34 { 35 40 public Valuator prepare( VariableIndexes vi ); 41 42 45 public boolean isVariable(); 46 47 51 public String getName(); 52 53 56 public boolean isConstant(); 57 58 62 public Object getValue(); 63 64 68 public boolean isApply(); 69 70 74 public String getFun(); 75 76 80 public int argCount(); 81 82 86 public Expression getArg( int i ); 87 88 91 public static Expression TRUE = new BoolConstant( true ); 92 93 96 public static Expression FALSE = new BoolConstant( false ); 97 98 102 public static abstract class Base implements Expression 103 { 104 public boolean isVariable() { return false; } 105 public boolean isApply() { return false; } 106 public boolean isConstant() { return false; } 107 public String getName() { return null; } 108 public Object getValue() { return null; } 109 public int argCount() { return 0; } 110 public String getFun() { return null; } 111 public Expression getArg( int i ) { return null; } 112 113 public boolean equals( Object other ) 114 { return other instanceof Expression && Expression.Util.equals( this, (Expression) other ); } 115 } 116 117 120 public static abstract class Constant extends Base 121 { 122 public boolean isConstant() { return true; } 123 public abstract Object getValue(); 124 } 125 126 130 public static class Fixed extends Constant 131 { 132 protected Object value; 133 134 public Fixed( Object value ) 135 { this.value = value; } 136 137 public Object getValue() 138 { return value; } 139 140 public Valuator prepare( VariableIndexes vi ) 141 { return new FixedValuator( value ); } 142 143 public String toString() 144 { return value.toString(); } 145 } 146 147 150 public static abstract class Variable extends Base 151 { 152 public boolean isVariable() { return true; } 153 public abstract String getName(); 154 } 155 156 160 public static abstract class Application extends Base 161 { 162 public boolean isApply() { return true; } 163 public abstract int argCount(); 164 public abstract String getFun(); 165 public abstract Expression getArg( int i ); 166 } 167 168 172 public static class Util 173 { 174 178 public static Set variablesOf( Expression e ) 179 { return addVariablesOf( CollectionFactory.createHashedSet(), e ); } 180 181 185 public static Set addVariablesOf( Set s, Expression e ) 186 { 187 if (e.isVariable()) 188 s.add( e.getName() ); 189 else if (e.isApply()) 190 for (int i = 0; i < e.argCount(); i += 1) 191 addVariablesOf( s, e.getArg( i ) ); 192 return s; 193 } 194 195 public static boolean containsAllVariablesOf( Set variables, Expression e ) 196 { 197 if (e.isConstant()) 198 return true; 199 if (e.isVariable()) 200 return variables.contains( e.getName() ); 201 if (e.isApply()) 202 { 203 for (int i = 0; i < e.argCount(); i += 1) 204 if (containsAllVariablesOf( variables, e.getArg(i) ) == false) return false; 205 return true; 206 } 207 return false; 208 } 209 210 public static boolean equals( Expression L, Expression R ) 211 { 212 return 213 L.isConstant() ? R.isConstant() && L.getValue().equals( R.getValue() ) 214 : L.isVariable() ? R.isVariable() && R.getName().equals( R.getName() ) 215 : L.isApply() ? R.isApply() && sameApply( L, R ) 216 : false 217 ; 218 } 219 220 public static boolean sameApply( Expression L, Expression R ) 221 { 222 return 223 L.argCount() == R.argCount() && L.getFun().equals( R.getFun() ) 224 && sameArgs( L, R ) 225 ; 226 } 227 228 public static boolean sameArgs( Expression L, Expression R ) 229 { 230 for (int i = 0; i < L.argCount(); i += 1) 231 if (!equals( L.getArg( i ), R.getArg( i ) )) return false; 232 return true; 233 } 234 } 235 236 244 static class Valof implements VariableValues 245 { 246 private VariableIndexes map; 247 private IndexValues dom; 248 249 public Valof( VariableIndexes map ) { this.map = map; } 250 251 public final Object get( String name ) 252 { return dom.get( map.indexOf( name ) ); } 253 254 public final Valof setDomain( IndexValues d ) { dom = d; return this; } 255 } 256 257 260 public static class BoolConstant extends Base implements Expression, Valuator 261 { 262 private boolean value; 263 public BoolConstant( boolean value ) { this.value = value; } 264 public boolean isConstant() { return true; } 265 public Object getValue() { return value ? Boolean.TRUE : Boolean.FALSE; } 267 public Valuator prepare( VariableIndexes vi ) { return this; } 268 public boolean evalBool( VariableValues vv ) { return value; } 269 public boolean evalBool( IndexValues vv ) { return value; } 270 public Object evalObject( IndexValues iv ) { return getValue(); } 271 } 272 } 273 274 303 | Popular Tags |