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

The following examples show how to use org.apache.jena.graph.Node#getURI() . 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: SourcePlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param binding -
 * @return the actual URI that represents the location of the query to be
 * fetched.
 */
private String getActualSource(
        final Binding binding) {
    if (node.isVariable()) {
        Node actualSource = binding.get((Var) node);
        if (actualSource == null) {
            return null;
        }
        if (!actualSource.isURI()) {
            throw new SPARQLExtException("Variable " + node.getName()
                    + " must be bound to a IRI that represents the location"
                    + " of the query to be fetched.");
        }
        return actualSource.getURI();
    } else {
        if (!node.isURI()) {
            throw new SPARQLExtException("The source must be a IRI"
                    + " that represents the location of the query to be"
                    + " fetched. Got " + node.getURI());
        }
        return node.getURI();
    }
}
 
Example 2
Source File: EvalUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 * The query name may be an expression, which evaluates differently
 * depending on the input bindings. This method groups the bindings for
 * which the query name evaluation is the same.
 *
 * @param expr the expression for the query name
 * @param bindings
 * @param env
 * @return
 */
public static Map<String, List<Binding>> splitBindingsForQuery(
        final Expr expr,
        final List<Binding> bindings,
        final FunctionEnv env) {
    final Map<String, List<Binding>> calls = new HashMap<>();
    for (Binding binding : bindings) {
        final Node n = eval(expr, binding, env);
        if (n == null) {
            continue;
        }
        if (!n.isURI()) {
            LOG.warn("Name of sub query resolved to something else than a"
                    + " URI: " + n);
            continue;
        }
        String queryName = n.getURI();
        List<Binding> call = calls.get(queryName);
        if (call == null) {
            call = new ArrayList<>();
            calls.put(queryName, call);
        }
        call.add(binding);
    }
    return calls;
}
 
Example 3
Source File: TraversalBuilder.java    From sparql-gremlin with Apache License 2.0 6 votes vote down vote up
public static GraphTraversal<?, ?> transform(final Triple triple) {
    final GraphTraversal<Vertex, ?> matchTraversal = __.as(triple.getSubject().getName());
    final Node predicate = triple.getPredicate();
    final String uri = predicate.getURI();
    final String uriValue = Prefixes.getURIValue(uri);
    final String prefix = Prefixes.getPrefix(uri);
    switch (prefix) {
        case "edge":
            return matchTraversal.out(uriValue).as(triple.getObject().getName());
        case "property":
            return matchProperty(matchTraversal, uriValue, PropertyType.PROPERTY, triple.getObject());
        case "value":
            return matchProperty(matchTraversal, uriValue, PropertyType.VALUE, triple.getObject());
        default:
            throw new IllegalStateException(String.format("Unexpected predicate: %s", predicate));
    }
}
 
Example 4
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the next license ref and next element reference variables
 * @return any verification errors encountered
 * @throws InvalidSPDXAnalysisException 
 */
private List<String> initialize() throws InvalidSPDXAnalysisException {
	Node spdxDocNode = getSpdxDocNode();
	if (spdxDocNode != null) {	// not empty - we should verify
		if (!spdxDocNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("SPDX Documents must have a unique URI"));
		}
		String docUri = spdxDocNode.getURI();
		this.documentNamespace = this.formDocNamespace(docUri);
		List<String> errors = verify();
		initializeNextLicenseRef();
		initializeNextElementRef();
		return errors;
	} else {
		return Lists.newArrayList();
	}
}
 
Example 5
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 6
Source File: TraversalBuilder.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
static GraphTraversal<?, ?> transform(final Triple triple) {
    final GraphTraversal<Vertex, ?> matchTraversal = __.as(triple.getSubject().getName());
    
    final Node predicate = triple.getPredicate();
    final String uri = predicate.getURI();
    final String uriValue = Prefixes.getURIValue(uri);
    final String prefix = Prefixes.getPrefix(uri);

    switch (prefix) {
        case "edge":
            return matchTraversal.out(uriValue).as(triple.getObject().getName());
        case "property":
            return matchProperty(matchTraversal, uriValue, PropertyType.PROPERTY, triple.getObject());
        case "value":
            return matchProperty(matchTraversal, uriValue, PropertyType.VALUE, triple.getObject());
        default:
            throw new IllegalStateException(String.format("Unexpected predicate: %s", predicate));
    }
}
 
Example 7
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 8
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * returns the accept header computed from ACCEPT clause.
 *
 * @param binding -
 * @return the actual accept header to use.
 */
private String getAcceptHeader(
        final Binding binding) {
    Node actualAccept = accept;
    if (accept == null) {
        return "*/*";
    }
    if (accept.isVariable()) {
        actualAccept = binding.get((Var) accept);
        if (accept == null) {
            return "*/*";
        }
    }
    if (!actualAccept.isURI()) {
        throw new SPARQLExtException("Variable " + node.getName()
                + " must be bound to a IRI that represents the internet"
                + " media type of the source to be fetched. For"
                + " instance, <http://www.iana.org/assignments/media-types/application/xml>.");
    }
    if (!actualAccept.getURI().startsWith("http://www.iana.org/assignments/media-types/")) {
        throw new SPARQLExtException("Variable " + node.getName()
                + " must be bound to a IANA MIME URN (RFC to be"
                + " written). For instance,"
                + " <http://www.iana.org/assignments/media-types/application/xml>.");
    }
    return actualAccept.getURI();

}
 
Example 9
Source File: SPDXLicenseInfoFactory.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @param model
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SPDXLicenseInfo getLicenseInfoByType(Model model, Node node) throws InvalidSPDXAnalysisException {
	// find the subclass
	Node rdfTypePredicate = model.getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with a licenseInfo"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.SPDX_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not an SPDX type"));
		}
		String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_SPDX_CONJUNCTIVE_LICENSE_SET)) {
			return new SPDXConjunctiveLicenseSet(model, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DISJUNCTIVE_LICENSE_SET)) {
			return new SPDXDisjunctiveLicenseSet(model, node);
		}else if (type.equals(SpdxRdfConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO)) {
			return new SPDXNonStandardLicense(model, node);
		}else if (type.equals(SpdxRdfConstants.CLASS_SPDX_STANDARD_LICENSE)) {
			return new SPDXStandardLicense(model, node);
		} else {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo '"+type+"'"));
		}
	} else {
		return null;
	}
}
 
Example 10
Source File: Id.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a {@link Node} to an {@code Id}. The {@link Node} can be a URI or
 * a string literal. The preferred form is a {@code <uuid:...>}.
 * <p>
 * An argument of {@code null} returns {@code null}.
 *
 * @param node
 * @return Id
 */
public static Id fromNode(Node node) {
    if ( node == null )
        return null ;

    String s = null ;

    if ( node.isURI() )
        s = node.getURI() ;
    else if ( Util.isSimpleString(node) )
        s = node.getLiteralLexicalForm() ;
    if ( s == null )
        throw new IllegalArgumentException("Id input is not a URI or a string") ;
    return fromString$(s) ;
}
 
Example 11
Source File: PointerFactory.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Get the single pointer class based on the element type specified in the model
 * @param modelContainer
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SinglePointer getElementByType(
		IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	Node rdfTypePredicate = modelContainer.getModel().getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = modelContainer.getModel().getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with a SinglePointer"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for a SinglePointer - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.RDF_POINTER_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for a SinglePointer - not an RDF Pointer type (namespace must begin with "+
						SpdxRdfConstants.RDF_POINTER_NAMESPACE));
		}
		String type = typeUri.substring(SpdxRdfConstants.RDF_POINTER_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_POINTER_BYTE_OFFSET_POINTER)) {
			return new ByteOffsetPointer(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_POINTER_LINE_CHAR_POINTER)) {
			return new LineCharPointer(modelContainer, node);
		} else {
			throw(new InvalidSPDXAnalysisException("Unsupported type for SinglePointer '"+type+"'"));
		}
	} else {
		return null;
	}
}
 
Example 12
Source File: labelSearch.java    From xcurator with Apache License 2.0 4 votes vote down vote up
@Override
public void build(PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt)
{
    if ( argSubject.isList() || argObject.isList() )
        throw new QueryBuildException("List arguments to "+predicate.getURI()) ;
}
 
Example 13
Source File: DB2Dataset.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public Graph getGraph(Node graphNode)
{
return new DB2Graph(store, connection, graphNode.getURI());
}
 
Example 14
Source File: JenaUtil.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
static URI toURI(Node n) throws URISyntaxException {
	return new URI(n.getURI());
}
 
Example 15
Source File: SpdxElementFactory.java    From tools with Apache License 2.0 4 votes vote down vote up
/**
 * @param modelContainer
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SpdxElement getElementByType(IModelContainer modelContainer,
		Node node) throws InvalidSPDXAnalysisException {
	Node rdfTypePredicate = modelContainer.getModel().getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = modelContainer.getModel().getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with an SPDX Element"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for an SPDX Element - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.SPDX_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for an SPDX Element - not an SPDX type"));
		}
		String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_SPDX_FILE)) {
			return new SpdxFile(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_PACKAGE)) {
			return new SpdxPackage(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_SNIPPET)) {
			return new SpdxSnippet(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_ITEM)) {
			return new SpdxItem(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_ELEMENT)) {
			return new SpdxElement(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DOCUMENT)) {
			return new SpdxDocumentContainer(modelContainer.getModel()).getSpdxDocument();
		} else {
			throw(new InvalidSPDXAnalysisException("Invalid type for element '"+type+"'"));
		}
	} else {
		return null;
	}
}
 
Example 16
Source File: SpdxElementFactory.java    From tools with Apache License 2.0 4 votes vote down vote up
public static synchronized SpdxElement createElementFromModel(IModelContainer modelContainer,
		Node node) throws InvalidSPDXAnalysisException {
	Map<Node, SpdxElement> containerNodes = createdElements.get(modelContainer);
	if (containerNodes == null) {
		containerNodes = Maps.newHashMap();
		createdElements.put(modelContainer, containerNodes);
	}
	SpdxElement retval = containerNodes.get(node);
	if (retval != null) {
		return retval;
	}
	if (node.isBlank() ||
			(node.isURI() && node.getURI().startsWith(modelContainer.getDocumentNamespace()))) {
		// SPDX element local to this document
		retval = getElementByType(modelContainer, node);
		if (retval == null) {
			retval = guessElementByProperties(modelContainer, node);
			if (retval == null) {
				throw(new InvalidSPDXAnalysisException("Unable to determine the SPDX element type from the model"));
			}
		}
		containerNodes.put(node, retval);
		return retval;
	} else if (node.isURI()) {
		if (SpdxNoneElement.NONE_ELEMENT_URI.equals(node.getURI())) {
			return new SpdxNoneElement();
		} else if (SpdxNoAssertionElement.NOASSERTION_ELEMENT_URI.equals(node.getURI())) {
			return new SpdxNoAssertionElement();
		} else {
			// assume this is an external document reference
			String[] uriParts = node.getURI().split("#");
			if (uriParts.length != 2) {
				throw(new InvalidSPDXAnalysisException("Invalid element URI: "+node.getURI()));
			}
			String docId = modelContainer.documentNamespaceToId(uriParts[0]);
			if (docId == null) {
				throw(new InvalidSPDXAnalysisException("No external document reference was found for URI "+node.getURI()));
			}
			String externalId = docId + ":" + uriParts[1];
			return new ExternalSpdxElement(externalId);
		}
	} else {
		throw(new InvalidSPDXAnalysisException("Can not create an SPDX Element from a literal node"));
	}
}
 
Example 17
Source File: LicenseInfoFactory.java    From tools with Apache License 2.0 4 votes vote down vote up
/**
 * @param modelContainer
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static AnyLicenseInfo getLicenseInfoByType(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	// find the subclass
	Node rdfTypePredicate = modelContainer.getModel().getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = modelContainer.getModel().getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with a licenseInfo"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.SPDX_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not an SPDX type"));
		}
		String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_SPDX_CONJUNCTIVE_LICENSE_SET)) {
			return new ConjunctiveLicenseSet(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DISJUNCTIVE_LICENSE_SET)) {
			return new DisjunctiveLicenseSet(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO)) {
			return new ExtractedLicenseInfo(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_LICENSE)) {
			return new SpdxListedLicense(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_OR_LATER_OPERATOR)) {
			return new OrLaterOperator(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_WITH_EXCEPTION_OPERATOR)) {
			return new WithExceptionOperator(modelContainer, node);
		} else {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo '"+type+"'"));
		}
	} else {
		return null;
	}
}