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

The following examples show how to use org.apache.jena.graph.Node#isBlank() . 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: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
public boolean canBeIsomorphic(Node n1, Node n2) {
    if (    (n1.isBlank() || Var.isBlankNodeVar(n1))
         && (n2.isBlank() || Var.isBlankNodeVar(n2)) ) {
        final Node other = map.get(n1);
        if ( other == null ) {
        	if ( valueSet.contains(n2) )
        		return false;

            map.put(n1, n2);
            valueSet.add(n2);
            return true;
        }
        return other.equals(n2);
    }
    return n1.equals(n2);
}
 
Example 2
Source File: ParserProfileTurtleStarExtImpl.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Override
protected void checkTriple(Node subject, Node predicate, Node object, long line, long col) {
	if ( subject == null
	     || (!subject.isURI() && 
	         !subject.isBlank() &&
	         !(subject instanceof Node_Triple)) ) {
    	getErrorHandler().error("Subject is not a URI, blank node, or triple", line, col);
        throw new RiotException("Bad subject: " + subject);
    }
    if ( predicate == null || (!predicate.isURI()) ) {
    	getErrorHandler().error("Predicate not a URI", line, col);
        throw new RiotException("Bad predicate: " + predicate);
    }
    if ( object == null
         || (!object.isURI() &&
             !object.isBlank() &&
             !object.isLiteral() &&
             !(object instanceof Node_Triple)) ) {
    	getErrorHandler().error("Object is not a URI, blank node, literal, or triple", line, col);
        throw new RiotException("Bad object: " + object);
    }
}
 
Example 3
Source File: PointerFactory.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Get the pointer from the model determining the subclass from the information in the
 * model.
 * @param modelContainer
 * @param object
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
public static SinglePointer getSinglePointerFromModel(
		IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	if (!node.isURI() && !node.isBlank()) {
		throw(new InvalidSPDXAnalysisException("Can not create a SinglePointer from a literal node"));
	}
	if (node.isURI() && !node.getURI().startsWith(modelContainer.getDocumentNamespace())) {
		throw(new InvalidSPDXAnalysisException("Unable to access SinglePointer snippet information outside of the SPDX document"));
	}
	SinglePointer retval = getElementByType(modelContainer, node);
	if (retval == null) {
		retval = guessElementByProperties(modelContainer, node);
		if (retval == null) {
			throw(new InvalidSPDXAnalysisException("Unable to determine the SinglePointer type from the model"));
		}
	}
	return retval;
}
 
Example 4
Source File: QueryBNodeNormalizer.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private static Node replace(Node node, Set<String> assignNeeded, Map<String, Var> assignSuper, Map<String, Var> assign) {
    if (!node.isBlank()) {
        return node;
    }
    String label = node.getBlankNodeLabel();
    if (!assignNeeded.contains(label)) {
        return node;
    }
    if (assignSuper.containsKey(label)) {
        return assignSuper.get(label);
    }
    if (assign.containsKey(label)) {
        return assign.get(label);
    }
    return node;
}
 
Example 5
Source File: LicenseInfoFactory.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Create the appropriate SPDXLicenseInfo from the model and node provided.
 * The appropriate SPDXLicenseInfo subclass object will be chosen based on
 * the class (rdf type) of the node.  If there is no rdf type, then the
 * license ID is parsed to determine the type
 * @param modelContainer
 * @param node
 * @return
 */
public static AnyLicenseInfo getLicenseInfoFromModel(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	if (!node.isURI() && !node.isBlank()) {
		throw(new InvalidSPDXAnalysisException("Can not create a LicenseInfo from a literal node"));
	}
	AnyLicenseInfo retval = null;
	// check to see if it is a "simple" type of license (NONESEEN, NONE, NOTANALYZED, or SPDX_LISTED_LICENSE)
	if (node.isURI()) {
		retval = getLicenseInfoByUri(modelContainer, node);
	}
	if (retval == null) {	// try by type
		retval = getLicenseInfoByType(modelContainer, node);
	}
	if (retval == null) {	// try by ID
		retval = getLicenseInfoById(modelContainer, node);
	}
	if (retval == null) {	// OK, we give up
		logger.error("Could not determine the type for a license");
		throw(new InvalidSPDXAnalysisException("Could not determine the type for a license"));
	}
	return retval;
}
 
Example 6
Source File: SpdxDocumentContainer.java    From tools with Apache License 2.0 5 votes vote down vote up
private Resource getResource(Node node) throws InvalidSPDXAnalysisException {
	Resource s;
	if (node.isURI()) {
		s = model.createResource(node.getURI());
	} else if (node.isBlank()) {
		s = model.createResource(new AnonId(node.getBlankNodeId()));
	} else {
		throw(new InvalidSPDXAnalysisException("Node can not be a literal"));
	}
	return s;
}
 
Example 7
Source File: RdfModelObject.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Create an RDF Model Object based on an existing Node
 * @param modelContainer Container containing the RDF Model
 * @param node Node describing this object
 * @throws InvalidSPDXAnalysisException
 */
public RdfModelObject(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	this.modelContainer = modelContainer;
	this.model = modelContainer.getModel();
	this.node = node;
	this.refreshOnGet = modelContainer.addCheckNodeObject(node, this);
	if (node.isBlank()) {
		resource = model.createResource(new AnonId(node.getBlankNodeId()));
	} else if (node.isURI()) {
		resource = model.createResource(node.getURI());
	} else {
		throw(new InvalidSPDXAnalysisException("Can not have an model node as a literal"));
	}
}
 
Example 8
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
private Resource getResource(Node node) throws InvalidSPDXAnalysisException {
	Resource s;
	if (node.isURI()) {
		s = model.createResource(node.getURI());
	} else if (node.isBlank()) {
		s = model.createResource(new AnonId(node.getBlankNodeId()));
	} else {
		throw(new InvalidSPDXAnalysisException("Node can not be a literal"));
	}
	return s;
}
 
Example 9
Source File: RdfParserHelper.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a node to a resource
 * @param cmodel
 * @param cnode
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
public static Resource convertToResource(Model cmodel, Node cnode) throws InvalidSPDXAnalysisException {
	if (cnode.isBlank()) {
		return cmodel.createResource(new AnonId(cnode.getBlankNodeId()));
	} else if (cnode.isURI()) {
		return cmodel.createResource(cnode.getURI());
	} else {
		throw(new InvalidSPDXAnalysisException("Can not create a resource from a literal"));
	}
}
 
Example 10
Source File: SPDXLicenseInfo.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a node to a resource
 * @param cmodel
 * @param cnode
 * @return
 * @throws InvalidSPDXAnalysisException
 */
private Resource convertToResource(Model cmodel, Node cnode) throws InvalidSPDXAnalysisException {
	if (cnode.isBlank()) {
		return cmodel.createResource(cnode.getBlankNodeId());
	} else if (cnode.isURI()) {
		return cmodel.createResource(cnode.getURI());
	} else {
		throw(new InvalidSPDXAnalysisException("Can not create a license from a literal"));
	}
}
 
Example 11
Source File: SPDXLicenseInfoFactory.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Create the appropriate SPDXLicenseInfo from the model and node provided.
 * The appropriate SPDXLicenseInfo subclass object will be chosen based on
 * the class (rdf type) of the node.  If there is no rdf type, then the
 * license ID is parsed to determine the type
 * @param model
 * @param node
 * @return
 */
public static SPDXLicenseInfo getLicenseInfoFromModel(Model model, Node node) throws InvalidSPDXAnalysisException {
	if (!node.isURI() && !node.isBlank()) {
		throw(new InvalidSPDXAnalysisException("Can not create a LicenseInfo from a literal node"));
	}
	// check to see if it is a "standard" type of license (NONESEEN, NONE, NOTANALYZED, or STANDARD_LICENSE)
	if (node.isURI()) {
		if (node.getURI().equals(SpdxRdfConstants.SPDX_NAMESPACE+SpdxRdfConstants.TERM_LICENSE_NONE)) {
			return new SPDXNoneLicense(model, node);
		} else if (node.getURI().equals(SpdxRdfConstants.SPDX_NAMESPACE+SpdxRdfConstants.TERM_LICENSE_NOASSERTION)) {
			return new SpdxNoAssertionLicense(model, node);
		} else if (node.getURI().startsWith(STANDARD_LICENSE_URI_PREFIX)) {
			// try to fetch the standard license from the model
			try {
				return getLicenseFromStdLicModel(node.getURI());
			} catch (Exception ex) {
				// ignore for now - we'll try to get the standard license from the information in the model itself if it exists
			}
		}
	}
	SPDXLicenseInfo retval = getLicenseInfoByType(model, node);
	if (retval == null) {
		retval = getLicenseInfoById(model, node);
	}
	if (retval == null) {
		throw(new InvalidSPDXAnalysisException("Could not determine the type for a license"));
	}
	return retval;
}
 
Example 12
Source File: Node2String.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static short getType(Node n)
{
if (n.isURI())
   {
   return TypeMap.IRI_ID;
   }
if (n.isBlank())
   {
   return TypeMap.BLANK_NODE_ID;
   }

if (n.isLiteral())
   {

   if (n.getLiteral().getDatatype() != null)
      {
      return TypeMap.getDatatypeType(n.getLiteralDatatypeURI());
      }
   else
      {
      String lang = n.getLiteralLanguage();
      if (lang != null && lang.length() > 0)
         {
         return TypeMap.getLanguageType(lang);
         }
      else
         {
         return TypeMap.SIMPLE_LITERAL_ID;
         }
      }
   }

throw new RdfStoreException("Unknown RDFterm");
}
 
Example 13
Source File: JSFactory.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static JSTerm asJSTerm(Node node) {
	if(node.isURI()) {
		return new JSNamedNode(node);
	}
	else if(node.isBlank()) {
		return new JSBlankNode(node);
	}
	else if(node.isLiteral()) {
		return new JSLiteral(node);
	}
	else {
		throw new IllegalArgumentException("Unsupported node type " + node);
	}
}
 
Example 14
Source File: ClassPropertyMetadata.java    From shacl with Apache License 2.0 5 votes vote down vote up
public boolean hasMatchingPath(Node propertyShape, Graph graph) {
	if(inverse) {
		Node path = JenaNodeUtil.getObject(propertyShape, SH.path.asNode(), graph);
		if(path != null && path.isBlank()) {
			return predicate.equals(JenaNodeUtil.getObject(path, SH.inversePath.asNode(), graph));
		}
		else {
			return false;
		}
	}
	else {
		return graph.contains(propertyShape, SH.path.asNode(), predicate);
	}
}
 
Example 15
Source File: OWLClassPropertyMetadataPlugin.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ClassPropertyMetadata cpm, Node classNode, Graph graph) {
	ExtendedIterator<Triple> it = graph.find(classNode, RDFS.subClassOf.asNode(), Node.ANY);
	while(it.hasNext()) {
		Node superClass = it.next().getObject();
		if(superClass.isBlank() && graph.contains(superClass, OWL.onProperty.asNode(), cpm.getPredicate())) {
			if(cpm.getLocalRange() == null) {
				Node localRange = JenaNodeUtil.getObject(superClass, OWL.allValuesFrom.asNode(), graph);
				if(localRange != null) {
					cpm.setLocalRange(localRange);
					it.close();
					break;
				}
			}
			if(cpm.getMaxCount() == null) {
				Node maxCountNode = JenaNodeUtil.getObject(superClass, OWL.maxCardinality.asNode(), graph);
				if(maxCountNode == null) {
					maxCountNode = JenaNodeUtil.getObject(superClass, OWL.cardinality.asNode(), graph);
				}
				if(maxCountNode != null && maxCountNode.isLiteral()) {
					Object value = maxCountNode.getLiteralValue();
					if(value instanceof Number) {
						cpm.setMaxCount(((Number) value).intValue());
					}
				}
			}
		}
	}
}
 
Example 16
Source File: GenerateFormPlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private Node subst(Node n, Map<Node, Node> bNodeMap) {
    if (n.isBlank() || Var.isBlankNodeVar(n)) {
        if (!bNodeMap.containsKey(n)) {
            bNodeMap.put(n, NodeFactory.createBlankNode());
        }
        return bNodeMap.get(n);
    }
    return n;
}
 
Example 17
Source File: QueryBNodeNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private static void processBNode(Node node, Set<String> assignNeeded, Map<String, Var> assignSuper, Map<String, Var> assign) {
    if (!node.isBlank()) {
        return;
    }
    String label = node.getBlankNodeLabel();
    if (assignSuper.containsKey(label)) {
        assignNeeded.add(label);
        return;
    }
    if (!assign.containsKey(label)) {
        String uuid = UUID.randomUUID().toString().substring(0, 8);
        Var var = VarUtils.allocVar(uuid);
        assign.put(label, var);
    }
}
 
Example 18
Source File: TokenWriterText.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
public void format(AWriter w, Node n) {
    if ( n.isBlank() )
        formatBNode(w, n);
    else
        super.format(w, n);
}
 
Example 19
Source File: Node2String.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public static String getString(Node n)
{
if (n.isURI())
   {
   return n.toString(false);
   }
else if (n.isBlank())
   {
   return Constants.PREFIX_BLANK_NODE + n.toString();
   }
else if (n.isLiteral())
   {
   String object;
   if (n.getLiteral().getDatatype() != null)
      {
      // mdb types handling //uri = n.getLiteralDatatypeURI();
      // object = n.getLiteral().toString(false);
      object = n.getLiteralLexicalForm();
      // object = n.getLiteralValue().toString();
      }
   else
      {
      String lang = n.getLiteralLanguage();
      if (lang != null && lang.length() > 0)
         {
         // mdb types
         // object = n.getLiteralValue() + Constants.LITERAL_LANGUAGE
         // + lang;
         object = n.getLiteralValue().toString();
         }
      else
         {
         // mdb datatype change
         // object = "\"" + n.getLiteralValue() + "\"";
         object = n.getLiteralValue().toString();
         }
      }
   return object;
   }
else
   {
   return "\"" + n.toString(false) + "\"";
   }
}
 
Example 20
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"));
	}
}