/** * Return the integer value of a literal node */ public static int getIntValue(Node n) { return ((Number)n.getLiteralValue()).intValue(); }
/** * Check whether a Node is an Instant (DateTime) value */ public static boolean isInstant(Node n) { if (n.isLiteral()) { Object o = n.getLiteralValue(); return (o instanceof XSDDateTime); } else { return false; } }
/** * Return the value of the literal. In the case of plain literals * this will return the literal string. In the case of typed literals * it will return a java object representing the value. In the case * of typed literals representing a java primitive then the appropriate * java wrapper class (Integer etc) will be returned. */ @Override public Object getValue() { return asNode().getLiteralValue(); }
/** * Check whether a Node is a numeric (integer) value */ public static boolean isNumeric(Node n) { return n.isLiteral() && n.getLiteralValue() instanceof Number; }
/** * Compare two time Instant nodes. * @param n1 the first time instant (XSDDateTime) valued literal node * @param n2 the second time instant (XSDDateTime) valued literal node * @return -1 if n1 is less than n2, 0 if n1 equals n2 and +1 if n1 greater than n2 * @throws ClassCastException if either not is not numeric */ public static int compareInstants(Node n1, Node n2) { if (n1.isLiteral() && n2.isLiteral()) { Object v1 = n1.getLiteralValue(); Object v2 = n2.getLiteralValue(); if (v1 instanceof XSDDateTime && v2 instanceof XSDDateTime) { XSDDateTime a = (XSDDateTime) v1; XSDDateTime b = (XSDDateTime) v2; return a.compare(b); } } throw new ClassCastException("Non-numeric literal in compareNumbers"); }
/** * A convenience version of getGlobalProperty which can only return * a boolean result. */ @Override public boolean testGlobalProperty(Node property) { Node resultNode = getGlobalProperty(property); if (resultNode.isLiteral()) { Object result = resultNode.getLiteralValue(); if (result instanceof Boolean) { return ((Boolean)result).booleanValue(); } } throw new ReasonerException("Global property test returned non-boolean value" + "\nTest was: " + property + "\nResult was: " + resultNode); }
@Override public boolean getBoolean() { Object value = asNode().getLiteralValue(); if (isPlainLiteral()) { // old style plain literal - try parsing the string if (value.equals("true")) { return true; } else if (value.equals("false")) { return false; } else { throw new BadBooleanException( value.toString() ); } } else { // typed literal if (value instanceof Boolean) { return ((Boolean)value).booleanValue(); } else { throw new DatatypeFormatException(this.toString() + " is not a Boolean"); } } }
/** * Close a single node. */ private Node cloneNode(Node nIn, Map<Node_RuleVariable, Node> vmap, BindingEnvironment env) { Node n = (env == null) ? nIn : env.getGroundVersion(nIn); if (n instanceof Node_RuleVariable) { Node_RuleVariable nv = (Node_RuleVariable)n; Node c = vmap.get(nv); if (c == null) { c = ((Node_RuleVariable)n).cloneNode(); vmap.put(nv, c); } return c; } else if (Functor.isFunctor(n)) { Functor f = (Functor)n.getLiteralValue(); return Functor.makeFunctorNode(cloneFunctor(f, vmap, env)); } else { return n; } }
public double getDouble() { if ( ! isNode() ) throw new ItemException("Not a node, can't be a double: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a double: "+this) ; return ((Number)(getNode().getLiteralValue())).doubleValue() ; }
public int getLong() { if ( ! isNode() ) throw new ItemException("Not a node, can't be an integer: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a integer: "+this) ; return ((Number)(getNode().getLiteralValue())).intValue() ; }
public int getLong() { if ( ! isNode() ) throw new ItemException("Not a node, can't be an integer: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a integer: "+this) ; return ((Number)(getNode().getLiteralValue())).intValue() ; }
public double getDouble() { if ( ! isNode() ) throw new ItemException("Not a node, can't be a double: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a double: "+this) ; return ((Number)(getNode().getLiteralValue())).doubleValue() ; }
public long getInteger() { if ( ! isNode() ) throw new ItemException("Not a node, can't be an integer: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a integer: "+this) ; //Integer.parseInt(getNode().getLiteralLexicalForm()) ; return ((Number)(getNode().getLiteralValue())).longValue() ; }
public int getInt() { if ( ! isNode() ) throw new ItemException("Not a node, can't be an integer: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a integer: "+this) ; return ((Number)(getNode().getLiteralValue())).intValue() ; }
public int getInt() { if ( ! isNode() ) throw new ItemException("Not a node, can't be an integer: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a integer: "+this) ; return ((Number)(getNode().getLiteralValue())).intValue() ; }
public long getInteger() { if ( ! isNode() ) throw new ItemException("Not a node, can't be an integer: "+this) ; if ( ! getNode().isLiteral() ) throw new ItemException("Not a literal, can't be a integer: "+this) ; //Integer.parseInt(getNode().getLiteralLexicalForm()) ; return ((Number)(getNode().getLiteralValue())).longValue() ; }
/** * Test if the pattern is ground, contains no variables. */ public boolean isGround() { if (subject.isVariable() || predicate.isVariable() || object.isVariable()) return false; if (Functor.isFunctor(object)) { return ((Functor)object.getLiteralValue()).isGround(); } return true; }
/** * Check a pattern to see if it is legal, used to exclude backchaining goals that * could never be satisfied. A legal pattern cannot have literals in the subject or * predicate positions and is not allowed nested functors in the object. */ public boolean isLegal() { if (subject.isLiteral() || predicate.isLiteral()) return false; if (Functor.isFunctor(subject)) return false; if (Functor.isFunctor(object)) { Node[] args = ((Functor)object.getLiteralValue()).getArgs(); for (int i = 0; i < args.length; i++) { if (Functor.isFunctor(args[i])) return false; } } return true; }
/** * Check that the object of a triple match corresponds to the given functor pattern. * Side effects the variable bindings. */ public boolean functorMatch(Triple t, LPInterpreter interpreter) { Node o = t.getObject(); if (!Functor.isFunctor(o)) return false; Functor f = (Functor)o.getLiteralValue(); if ( ! f.getName().equals(objectFunctor.getName())) return false; if ( f.getArgLength() != objectFunctor.getArgLength()) return false; Node[] fargs = f.getArgs(); Node[] oFargs = objectFunctor.getArgs(); for (int i = 0; i < fargs.length; i++) { if (!interpreter.unify(oFargs[i], fargs[i])) return false; } return true; }
/** * Find all the variables in a TriplePattern. */ private int findVars(TriplePattern t, int maxIn) { int max = maxIn; max = maxVarIndex(t.getSubject(), max); max = maxVarIndex(t.getPredicate(), max); Node obj = t.getObject(); if (obj instanceof Node_RuleVariable) { max = maxVarIndex(obj, max); } else if (Functor.isFunctor(obj)) { max = findVars((Functor)obj.getLiteralValue(), max); } return max; }