Java Code Examples for org.apache.jena.graph.Node#isVariable()

The following examples show how to use org.apache.jena.graph.Node#isVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: localname.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator execFixedSubject(Node nodeURI, Node nodeLocalname, Binding binding, ExecutionContext execCxt)
{
    if ( ! nodeURI.isURI() )
        // Subject bound but not a URI
        return QueryIterNullIterator.create(execCxt) ;

    // Subject is bound and a URI - get the localname as a Node 
    Node localname = NodeFactory.createLiteral(nodeURI.getLocalName()) ;
    
    // Object - unbound variable or a value? 
    if ( ! nodeLocalname.isVariable() )
    {
        // Object bound or a query constant.  Is it the same as the calculated value?
        if ( nodeLocalname.equals(localname) )
            // Same
            return QueryIterSingleton.create(binding, execCxt) ;
        // No - different - no match.
        return QueryIterNullIterator.create(execCxt) ;
    }
    
    // Object unbound variable - assign the localname to it.
    return QueryIterSingleton.create(binding, Var.alloc(nodeLocalname), localname, execCxt) ;
}
 
Example 2
Source File: StatementUnifierForJena.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/***
 * Returns null if both terms cant be unified, a neutralsubstitution if they
 * are already equal, or a singleton substitution otherwise.
 * 
 * @param term1
 * @param term2
 * @return
 */
public static SubstitutionForJena getUnifier(Node term1, Node term2) {

	if (term1.equals(term2))
		return new NeutralSubstitutionForJena();

	if (!term1.isVariable() && !term2.isVariable()) {
		if (term1.matches(term2)) {
			return new NeutralSubstitutionForJena();
		}
		return null;
	}
	else if (term1.isVariable() && !term2.isVariable())// left is always a variable
		return new SingletonSubstitutionForJena(term1, term2);
	else {
		// both not equal variables, substitution right for left (priority for
		// original names).
		return new SingletonSubstitutionForJena(term2, term1);  // n, x
	}
}
 
Example 3
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
protected String getKey(Triple t) {
	Node subj = t.getSubject();
	Node obj = t.getObject();
	Node pred = t.getPredicate();
	if (!pred.isURI()) {
		return null;
	}
	if (!subj.isVariable() && !obj.isVariable()) {
		return null;
	}
	if (pred.getURI().equals(RDFConstants.RDF_TYPE)) {
		return obj.isURI()? obj.getURI() : null;
	} else {
		return pred.getURI();
	}
	
}
 
Example 4
Source File: TriplesToRuleSystem.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * converts a {@link Node} into an {l@ink Expr}
 * @param n
 * @return
 */
protected Expr	toExpr(Node n) {
	if (n.isVariable()) {
		return new VariableExpr(n.getName());
	} else if (n.isURI()) {
		try {
			return new ConstantExpr(new URI(n.getURI()));
		} catch (URISyntaxException e) {
			throw new RuntimeException(e);
		}
	} else if (n.isLiteral()) {
		Literal l = (Literal) n;
		return new ConstantExpr(l.getValue());
	} else {
		throw new RuntimeException("Unsuported node type in query : "+n);
	}
}
 
Example 5
Source File: localname.java    From xcurator with Apache License 2.0 5 votes vote down vote up
@Override
public QueryIterator execEvaluated(Binding binding, Node nodeURI, Node predicate, Node nodeLocalname, ExecutionContext execCxt)
{
    if ( ! nodeURI.isVariable() )
        return execFixedSubject(nodeURI, nodeLocalname, binding, execCxt) ;
    else
        return execAllNodes(Var.alloc(nodeURI), nodeLocalname, binding, execCxt) ;
}
 
Example 6
Source File: QueryIterTripleStarPattern.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
static protected Node convertToBeUsedForFind( Node node )
{
    if ( node instanceof Node_Triple )
    	return Node_TripleStarPattern.asNode_TripleStarPattern( (Node_Triple) node );
    else if ( node.isVariable() )
        return Node.ANY;
    else
    	return node;
}
 
Example 7
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected Set<String> getAllVariables(Triple t) {
	Set<String> ret = new HashSet<String>();
	Node s = t.getSubject();
	Node o = t.getObject();
	if (s.isVariable()) {
		ret.add(s.getName());
	}
	if (o.isVariable()) {
		ret.add(o.getName());
	}
	return ret;
}
 
Example 8
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean unify(Node t1, Node t2, Map<Node, Node> old2New) {
	if (t1.isVariable()) {
		return unifyT1Variable(t1, t2, old2New);
	} else if (t2.isVariable()) {
		return unifyT1Variable(t2,t1, old2New);
	} else {
		// i.e., t1 and t2 are not variables
		return t1.equals(t2);
	}
}
 
Example 9
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected Node replace(Node old) {
	if (old!=null && old.isVariable()) {
		Var var = Var.alloc(old);
		Node newN = oldVar2NewValue.get(var);
		if (newN!=null) {
			return newN;
		}
	}
	return old;
}
 
Example 10
Source File: RuleSystemToQueries.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected static E_NotEquals toIRINotEqualsFilter(Expr left, Expr right) {
	Node ln = toNode(left);
	org.apache.jena.sparql.expr.Expr le = ln.isVariable()? new ExprVar(ln.getName()) : new NodeValueNode(ln);
	Node rn = toNode(right);
	org.apache.jena.sparql.expr.Expr re = rn.isVariable()? new ExprVar(rn.getName()) : new NodeValueNode(rn);
	return new E_NotEquals(new E_Str(le), new E_Str(re));
}
 
Example 11
Source File: RuleSystemToQueries.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected static E_NotEquals toNotEqualsFilter(Expr left, Expr right) {
	Node ln = toNode(left);
	org.apache.jena.sparql.expr.Expr le = ln.isVariable()? new ExprVar(ln.getName()) : new NodeValueNode(ln);
	Node rn = toNode(right);
	org.apache.jena.sparql.expr.Expr re = rn.isVariable()? new ExprVar(rn.getName()) : new NodeValueNode(rn);
	return new E_NotEquals(le, re);
}
 
Example 12
Source File: RuleSystemToQueries.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected static E_Equals toIRIEqualsFilter(Expr left, Expr right) {
	Node ln = toNode(left);
	org.apache.jena.sparql.expr.Expr le = ln.isVariable()? new ExprVar(ln.getName()) : new NodeValueNode(ln);
	Node rn = toNode(right);
	org.apache.jena.sparql.expr.Expr re = rn.isVariable()? new ExprVar(rn.getName()) : new NodeValueNode(rn);
	return new E_Equals(new E_Str(le), new E_Str(re));
}
 
Example 13
Source File: RuleSystemToQueries.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected static E_Equals toEqualsFilter(Expr left, Expr right) {
	Node ln = toNode(left);
	org.apache.jena.sparql.expr.Expr le = ln.isVariable()? new ExprVar(ln.getName()) : new NodeValueNode(ln);
	Node rn = toNode(right);
	org.apache.jena.sparql.expr.Expr re = rn.isVariable()? new ExprVar(rn.getName()) : new NodeValueNode(rn);
	return new E_Equals(le, re);
}
 
Example 14
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * The generation plan of a <code>{@code SOURCE <node> ACCEPT <mime> AS
 * <var>}</code> clause.
 *
 * @param node The IRI or the Variable node where a GET must be operated.
 * Must not be null.
 * @param accept The IRI or the Variable node that represent the accepted
 * Internet Media Type. May be null.
 * @param var The variable to bound the potentially retrieved document.
 * Must not be null.
 */
public SourcePlan(
        final Node node,
        final Node accept,
        final Var var) {
    super(var);
    Objects.requireNonNull(node, "Node must not be null");
    Objects.requireNonNull(var, "Var must not be null");
    if (!node.isURI() && !node.isVariable()) {
        throw new IllegalArgumentException("Source node must be a IRI or a"
                + " Variable. got " + node);
    }
    this.node = node;
    this.accept = accept;
}
 
Example 15
Source File: OpVariableVistor.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private void process(Node n) {
	if (n.isVariable()) {
		add(processVar(n));
	}
}
 
Example 16
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isUnbound(Node t) {
	return t.isVariable() && isValidNameForUnboundVariable(t.getName());
}
 
Example 17
Source File: OpVariableVistor.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private void process(Node n) {
	if (n.isVariable()) {
		add(processVar(n));
	}
}
 
Example 18
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementPathBlock el) {
    // Write path block - don't put in a final trailing "." 
    if (el.isEmpty()) {
        out.println("# Empty BGP");
        return;
    }

    // Split into BGP-path-BGP-...
    // where the BGPs may be empty.
    PathBlock pBlk = el.getPattern();
    BasicPattern bgp = new BasicPattern();
    boolean first = true;      // Has anything been output?
    for (TriplePath tp : pBlk) {
        if (tp.isTriple()) {
            Triple t = tp.asTriple();
            Node s = t.getSubject();
            if(s.isVariable()) {
                s = Var.alloc(Var.canonical(((Var)s).getVarName()));
            }
            Node p = t.getPredicate();
            Node o = t.getObject();
            if(o.isVariable()) {
                o = Var.alloc(Var.canonical(((Var)o).getVarName()));
            }
            bgp.add(new Triple(s, p, o));
            continue;
        }

        if (!bgp.isEmpty()) {
            if (!first) {
                out.println(" .");
            }
            flush(bgp);
            first = false;
        }
        if (!first) {
            out.println(" .");
        }
        // Path
        printSubject(tp.getSubject());
        out.print(" ");
        SPARQLExtPathWriter.write(out, tp.getPath(), context);
        out.print(" ");
        printObject(tp.getObject());
        first = false;
    }
    // Flush any stored triple patterns.
    if (!bgp.isEmpty()) {
        if (!first) {
            out.println(" .");
        }
        flush(bgp);
        first = false;
    }
}
 
Example 19
Source File: FindAllVariables.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected void addVar(Node n) {
	if (n!=null && n.isVariable()) {
		vars.add(Var.alloc(n));
	}
}
 
Example 20
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected Expr getExpr(Node n) {
	return n.isVariable()? new ExprVar(n.getName()) : new NodeValueNode(n);
}