org.apache.jena.util.iterator.ExtendedIterator Java Examples

The following examples show how to use org.apache.jena.util.iterator.ExtendedIterator. 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: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
public SPDXCreatorInformation getCreatorInfo() throws InvalidSPDXAnalysisException {
	Node spdxDocNode = getSpdxDocNode();
	if (spdxDocNode == null) {
		throw(new InvalidSPDXAnalysisException("No SPDX Document was found.  Can not access the creator information"));
	}
	List<SPDXCreatorInformation> als = Lists.newArrayList();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_CREATION_INFO).asNode();
	Triple m = Triple.createMatch(spdxDocNode, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		als.add(new SPDXCreatorInformation(model, t.getObject()));
	}
	if (als.size() > 1) {
		throw(new InvalidSPDXAnalysisException("Too many creation information for document.  Only one is allowed."));
	}
	if (als.size() > 0) {
		return als.get(0);
	} else {
		return null;
	}
}
 
Example #2
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the declaredLicenses
 * @throws InvalidSPDXAnalysisException
 */
public SPDXLicenseInfo getDeclaredLicense() throws InvalidSPDXAnalysisException {
	ArrayList<SPDXLicenseInfo> alLic = new ArrayList<SPDXLicenseInfo>();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_PACKAGE_DECLARED_LICENSE).asNode();
	Triple m = Triple.createMatch(this.node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		alLic.add(SPDXLicenseInfoFactory.getLicenseInfoFromModel(model, t.getObject()));
	}
	if (alLic.size() > 1) {
		throw(new InvalidSPDXAnalysisException("Too many declared licenses"));
	}
	if (alLic.size() == 0) {
		return null;
	}
	return alLic.get(0);
}
 
Example #3
Source File: TestSPDXFile.java    From tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoneCopyright() throws InvalidSPDXAnalysisException {
	model = ModelFactory.createDefaultModel();
	SPDXDocument doc = new SPDXDocument(model);
	doc.createSpdxAnalysis("http://somethingunique");
	SPDXFile file = new SPDXFile("filename", "BINARY", "sha1", COMPLEX_LICENSE, CONJUNCTIVE_LICENSES, "", SpdxRdfConstants.NONE_VALUE, new DOAPProject[0]);
	Resource fileResource = file.createResource(doc, doc.getDocumentNamespace() + doc.getNextSpdxElementRef());
	Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT).asNode();
	Triple m = Triple.createMatch(null, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		assertTrue(t.getObject().isURI());
		assertEquals(SpdxRdfConstants.URI_VALUE_NONE, t.getObject().getURI());
	}
	SPDXFile file2 = new SPDXFile(modelContainer, fileResource.asNode());
	assertEquals(SpdxRdfConstants.NONE_VALUE, file2.getCopyright());
}
 
Example #4
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Find all annotations with a subject of this object
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected Annotation[] findAnnotationPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	List<Annotation> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new Annotation(this.modelContainer, t.getObject()));
	}
	return retval.toArray(new Annotation[retval.size()]);
}
 
Example #5
Source File: SimpleLicensingInfo.java    From tools with Apache License 2.0 6 votes vote down vote up
@Override
public Resource findDuplicateResource(IModelContainer modelContainer, String uri) throws InvalidSPDXAnalysisException {
	Property idProperty = modelContainer.getModel().createProperty(SpdxRdfConstants.SPDX_NAMESPACE, 
			SpdxRdfConstants.PROP_LICENSE_ID);
	Property typeProperty = modelContainer.getModel().getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE);
	Triple m = Triple.createMatch(null, idProperty.asNode(), null);
	ExtendedIterator<Triple> tripleIter = modelContainer.getModel().getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		if (t.getObject().toString(false).equals(this.licenseId)) {
			Triple typeMatch = Triple.createMatch(t.getSubject(), typeProperty.asNode(), getType(modelContainer.getModel()).asNode());
			ExtendedIterator<Triple> typeTripleIter = modelContainer.getModel().getGraph().find(typeMatch);
			if (typeTripleIter.hasNext()) {
				// found it
				if (t.getSubject().isURI()) {
					return modelContainer.getModel().createResource(t.getSubject().getURI());
				} else if (t.getSubject().isBlank()) {
					return modelContainer.getModel().createResource(new AnonId(t.getSubject().getBlankNodeId()));
				}
			}
		}
	}
	return null;
}
 
Example #6
Source File: SPDXLicenseInfoFactory.java    From tools with Apache License 2.0 6 votes vote down vote up
static void loadStdLicenseIDs() {
	STANDARD_LICENSES = Maps.newHashMap();
	try {
		Model stdLicenseModel = getStandardLicenseModel();
		Node p = stdLicenseModel.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_ID).asNode();
		Triple m = Triple.createMatch(null, p, null);
		ExtendedIterator<Triple> tripleIter = stdLicenseModel.getGraph().find(m);	
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			STANDARD_LICENSE_ID_SET.add(t.getObject().toString(false));
		}
	} catch (Exception ex) {
		
		for (int i = 0; i < STANDARD_LICENSE_IDS.length; i++) {
			STANDARD_LICENSE_ID_SET.add(STANDARD_LICENSE_IDS[i]);
		}	
	}
}
 
Example #7
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
public SpdxListedLicense getDataLicense() throws InvalidSPDXAnalysisException {
	List<AnyLicenseInfo> alLic = Lists.newArrayList();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_DATA_LICENSE).asNode();
	Triple m = Triple.createMatch(getSpdxDocNode(), p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		alLic.add(LicenseInfoFactory.getLicenseInfoFromModel(this, t.getObject()));
	}
	if (alLic.size() > 1) {
		throw(new InvalidSPDXAnalysisException("Too many data licenses"));
	}
	if (alLic.size() == 0) {
		return null;
	}
	if (!(alLic.get(0) instanceof SpdxListedLicense)) {
		throw(new InvalidSPDXAnalysisException("Incorrect license for datalicense - must be a standard SPDX license type"));
	}
	return (SpdxListedLicense)(alLic.get(0));
}
 
Example #8
Source File: TestDOAPProject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param resource
 * @param resource2
 */
private void copyProperties(Resource resource, Resource resource2) {
	Triple m = Triple.createMatch(resource.asNode(), null, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		Property p = model.createProperty(t.getPredicate().getURI());
		Node value = t.getObject();
		if (value instanceof RDFNode) {
			RDFNode valuen = (RDFNode)value;
			resource2.addProperty(p, valuen);
		} else {
			resource2.addProperty(p, value.toString(false));
		}
	}
}
 
Example #9
Source File: DiffGraph.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public boolean contains(Triple t) {
	if(addedGraph.contains(t)) {
		return true;
	}
	else {
		ExtendedIterator<Triple> it = base.find(t);
		while(it.hasNext()) {
			Triple n = it.next();
			if(!deletedTriples.contains(n)) {
				it.close();
				return true;
			}
		}
		return false;
	}
}
 
Example #10
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the reviewers
 * @throws InvalidSPDXAnalysisException 
 */
public SPDXReview[] getReviewers() throws InvalidSPDXAnalysisException {
	Node spdxDocNode = getSpdxDocNode();
	if (spdxDocNode == null) {
		throw(new InvalidSPDXAnalysisException("Must have an SPDX document to get reviewers"));
	}
	List<SPDXReview> als = Lists.newArrayList();
	als.clear();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_REVIEWED_BY).asNode();
	Triple m = Triple.createMatch(spdxDocNode, p, null);
	ExtendedIterator<Triple >tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		als.add(new SPDXReview(model, t.getObject()));
	}
	SPDXReview[] reviewers = new SPDXReview[als.size()];
	reviewers = als.toArray(reviewers);
	return reviewers;
}
 
Example #11
Source File: OrLaterOperator.java    From tools with Apache License 2.0 6 votes vote down vote up
@Override
public void getPropertiesFromModel() throws InvalidSPDXAnalysisException {
	Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_SET_MEMEBER).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		if (this.license != null) {
			throw (new InvalidSPDXAnalysisException("More than one license for a license WITH expression"));
		}
		AnyLicenseInfo anyLicense = LicenseInfoFactory.getLicenseInfoFromModel(modelContainer, t.getObject());
		if (!(anyLicense instanceof SimpleLicensingInfo)) {
			throw (new InvalidSPDXAnalysisException("The license for a WITH expression must be of type SimpleLicensingInfo"));
		}
		this.license = (SimpleLicensingInfo)anyLicense;
	}
}
 
Example #12
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Find all property string values belonging to the subject
 * @param subject
 * @param nameSpace
 * @param propertyName
 * @return string values of the properties or null if the subject or propertyName is null
 */
private String[] findDocPropertieStringValues(Node subject, String nameSpace, String propertyName) {
	if (subject == null || propertyName == null) {
		return null;
	}
	List<String> alResult = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(subject, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		if (t.getObject().isURI()) {
			if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NONE)) {
				alResult.add(SpdxRdfConstants.NONE_VALUE);
			} else if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NOASSERTION)) {
				alResult.add(SpdxRdfConstants.NOASSERTION_VALUE);
			} else {
				alResult.add(t.getObject().toString(false));
			}
		} else {
			alResult.add(t.getObject().toString(false));
		}
	}
	String[] retval = new String[alResult.size()];
	return alResult.toArray(retval);
}
 
Example #13
Source File: SPDXLicenseInfoFactory.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param model
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SPDXLicenseInfo getLicenseInfoById(Model model, Node node) throws InvalidSPDXAnalysisException {
	Node licenseIdPredicate = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_ID).asNode();
	Triple m = Triple.createMatch(node, licenseIdPredicate, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		String id = triple.getObject().toString(false);
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one ID associated with license "+id));
		}
		if (isStandardLicenseID(id)) {
			return new SPDXStandardLicense(model, node);
		} else if (id.startsWith(SpdxRdfConstants.NON_STD_LICENSE_ID_PRENUM)) {
			return new SPDXNonStandardLicense(model, node);
		} else {
			// could not determine the type from the ID
			// could be a conjunctive or disjunctive license ID
			return null;
		}
	} else {
		throw(new InvalidSPDXAnalysisException("No ID associated with a license"));
	}
}
 
Example #14
Source File: TestSpdxFile.java    From tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoassertionCopyright() throws InvalidSPDXAnalysisException {
	model = ModelFactory.createDefaultModel();
	IModelContainer modelContainer = new ModelContainerForTest(model, "http://somethingunique.com/something");
	SpdxFile file = new SpdxFile("filename", null, null, null, 
			COMPLEX_LICENSE, CONJUNCTIVE_LICENSES, SpdxRdfConstants.NOASSERTION_VALUE, null,
			null, new Checksum[] {new Checksum(ChecksumAlgorithm.checksumAlgorithm_sha1,
					"1123456789abcdef0123456789abcdef01234567")}, null, null, null);
	Resource fileResource = file.createResource(modelContainer);
	Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT).asNode();
	Triple m = Triple.createMatch(null, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		assertTrue(t.getObject().isURI());
		assertEquals(SpdxRdfConstants.URI_VALUE_NOASSERTION, t.getObject().getURI());
	}
	SpdxFile file2 = new SpdxFile(modelContainer, fileResource.asNode());
	assertEquals(SpdxRdfConstants.NOASSERTION_VALUE, file2.getCopyrightText());
}
 
Example #15
Source File: WithExceptionOperator.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the exception
 */
public LicenseException getException() {
	if (this.resource != null && this.refreshOnGet) {
		Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_EXCEPTION).asNode();
		Triple m = Triple.createMatch(node, p, null);
		ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			try {
				this.exception = new LicenseException(modelContainer, t.getObject());
			} catch (InvalidSPDXAnalysisException e) {
				logger.warn("Error getting exception, using stored exception value", e);
			}
		}
	}
	return exception;
}
 
Example #16
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected Checksum[] findMultipleChecksumPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return new Checksum[0];
	}
	List<Checksum> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new Checksum(modelContainer, t.getObject()));
	}
	return retval.toArray(new Checksum[retval.size()]);
}
 
Example #17
Source File: LicenseException.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Searches the model for a exception with the ID
 * @param model
 * @param id
 * @return Node containing the exception or Null if none found
 */
public static Node findException(Model model, String id) {
	Property idProperty = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, 
			SpdxRdfConstants.PROP_LICENSE_EXCEPTION_ID);
	Property typeProperty = model.getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE);
	Property exceptionTypeProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE,
			SpdxRdfConstants.CLASS_SPDX_LICENSE_EXCEPTION);
	Triple m = Triple.createMatch(null, idProperty.asNode(), null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		if (t.getObject().toString(false).equals(id)) {
			Triple typeMatch = Triple.createMatch(t.getSubject(), typeProperty.asNode(), exceptionTypeProperty.asNode());
			ExtendedIterator<Triple> typeTripleIter = model.getGraph().find(typeMatch);
			if (typeTripleIter.hasNext()) {
				return t.getSubject();
			}
		}
	}
	return null;
}
 
Example #18
Source File: PathEvaluator.java    From shacl with Apache License 2.0 6 votes vote down vote up
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	if(input == null) {
		ExtendedIterator<RDFNode> asserted = evalFocusNode(focusNode, context);
		return withDefaultValues(withInferences(asserted, focusNode, context), focusNode, context);
	}
	else {
		Iterator<RDFNode> it = input.eval(focusNode, context);
		if(it.hasNext()) {
			RDFNode first = it.next();
			ExtendedIterator<RDFNode> result = withDefaultValues(withInferences(evalFocusNode(first, context), first, context), first, context);
			while(it.hasNext()) {
				RDFNode n = it.next();
				result = result.andThen(withDefaultValues(withInferences(evalFocusNode(n, context), n, context), first, context));
			}
			return result;
		}
		else {
			return WrappedIterator.emptyIterator();
		}
	}
}
 
Example #19
Source File: PathEvaluator.java    From shacl with Apache License 2.0 6 votes vote down vote up
private ExtendedIterator<RDFNode> evalFocusNode(RDFNode focusNode, NodeExpressionContext context) {
	if(jenaPath == null) {
		if(focusNode.isLiteral()) {
			return WrappedIterator.emptyIterator();
		}
		else {
			return context.getDataset().getDefaultModel().listObjectsOfProperty((Resource)focusNode, predicate);
		}
	}
	else if(isInverse) {
		return context.getDataset().getDefaultModel().listSubjectsWithProperty(predicate, focusNode).mapWith(r -> (RDFNode)r);
	}
	else {
		// This ought to do lazy evaluation too
		List<RDFNode> results = new LinkedList<>();
		SHACLPaths.addValueNodes(focusNode.inModel(context.getDataset().getDefaultModel()), jenaPath, results);
		return WrappedIterator.create(results.iterator());
	}
}
 
Example #20
Source File: SpdxSnippet.java    From tools with Apache License 2.0 6 votes vote down vote up
@Override
public Resource findDuplicateResource(IModelContainer modelContainer, String uri) throws InvalidSPDXAnalysisException {
	if (this.snippetFromFile == null) {
		return null;
	}
	if (this.byteRange == null) {
		return null;
	}
	Resource snippetFromFileResource = SpdxFile.findFileResource(modelContainer, this.snippetFromFile);
	if (snippetFromFileResource == null) {
		return null;
	}
	Model model = modelContainer.getModel();
	Node snippetFromFileProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_SNIPPET_FROM_FILE).asNode();
	Triple fileMatch = Triple.createMatch(null, snippetFromFileProperty, snippetFromFileResource.asNode());
	
	ExtendedIterator<Triple> fileMatchIter = model.getGraph().find(fileMatch);	
	while (fileMatchIter.hasNext()) {
		Triple fileMatchTriple = fileMatchIter.next();
		SpdxSnippet localSnippet = new SpdxSnippet(modelContainer, fileMatchTriple.getSubject());
		if (this.byteRange.equivalent(localSnippet.getByteRange())) {
			return model.asRDFNode(fileMatchTriple.getSubject()).asResource();
		}
	}
	return null;
}
 
Example #21
Source File: SPDXFile.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the resource for an existing file in the model
 * @param spdxFile
 * @return resource of an SPDX file with the same name and checksum.  Null if none found
 * @throws InvalidSPDXAnalysisException 
 */
static protected Resource findFileResource(Model model, SPDXFile spdxFile) throws InvalidSPDXAnalysisException {
	// find any matching file names
	Node fileNameProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME).asNode();
	Triple fileNameMatch = Triple.createMatch(null, fileNameProperty, NodeFactory.createLiteral(spdxFile.getName()));
	
	ExtendedIterator<Triple> filenameMatchIter = model.getGraph().find(fileNameMatch);	
	if (filenameMatchIter.hasNext()) {
		Triple fileMatchTriple = filenameMatchIter.next();
		Node fileNode = fileMatchTriple.getSubject();
		// check the checksum
		Node checksumProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM).asNode();
		Triple checksumMatch = Triple.createMatch(fileNode, checksumProperty, null);
		ExtendedIterator<Triple> checksumMatchIterator = model.getGraph().find(checksumMatch);
		if (checksumMatchIterator.hasNext()) {
			Triple checksumMatchTriple = checksumMatchIterator.next();
			SPDXChecksum cksum = new SPDXChecksum(model, checksumMatchTriple.getObject());
			if (cksum.getValue().compareToIgnoreCase(spdxFile.sha1.getValue()) == 0) {
				return RdfParserHelper.convertToResource(model, fileNode);
			}
		}
	}
	// if we get to here, we did not find a match
	return null;
}
 
Example #22
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
public SPDXCreatorInformation getCreatorInfo() throws InvalidSPDXAnalysisException {
	Node spdxDocNode = getSpdxDocNode();
	if (spdxDocNode == null) {
		throw(new InvalidSPDXAnalysisException("No SPDX Document was found.  Can not access the creator information"));
	}
	ArrayList<SPDXCreatorInformation> als = new ArrayList<SPDXCreatorInformation>();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_CREATION_INFO).asNode();
	Triple m = Triple.createMatch(spdxDocNode, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		als.add(new SPDXCreatorInformation(model, t.getObject()));
	}
	if (als.size() > 1) {
		throw(new InvalidSPDXAnalysisException("Too many creation infos for document.  Only one is allowed."));
	}
	if (als.size() > 0) {
		return als.get(0);
	} else {
		return null;
	}
}
 
Example #23
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Find all annotations with a subject of this object
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected Relationship[] findRelationshipPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	List<Relationship> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new Relationship(this.modelContainer, t.getObject()));
	}
	return retval.toArray(new Relationship[retval.size()]);
}
 
Example #24
Source File: SumExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	NodeValue total = NodeValue.nvZERO;
	while(it.hasNext()) {
		RDFNode n = it.next();
		NodeValue nv = NodeValue.makeNode(n.asNode());
		if (nv.isNumber()) {
			total = XSDFuncOp.numAdd(nv, total);
		}
		else {
			it.close();
			return WrappedIterator.emptyIterator();
		}
	}
	RDFNode result = focusNode.getModel().asRDFNode(total.asNode());
	List<RDFNode> results = Collections.singletonList(result);
	return WrappedIterator.create(results.iterator());
}
 
Example #25
Source File: MinExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	RDFNode min = null;
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	while(it.hasNext()) {
		RDFNode next = it.next();
		if(min == null || NodeValue.compareAlways(NodeValue.makeNode(min.asNode()), NodeValue.makeNode(next.asNode())) > 0) {
			min = next;
		}
	}
	if(min == null) {
		return WrappedIterator.emptyIterator();
	}
	else {
		return WrappedIterator.create(Collections.singletonList(min).iterator());
	}
}
 
Example #26
Source File: GroupConcatExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	StringBuffer sb = new StringBuffer();
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	while(it.hasNext()) {
		RDFNode node = it.next();
		if(node.isLiteral() && XSDDatatype.XSDstring.getURI().equals(node.asNode().getLiteralDatatypeURI())) {
			sb.append(node.asNode().getLiteralLexicalForm());
		}
		else {
			String label = RDFLabels.get().getNodeLabel(node);
			if(label != null) {
				sb.append(label);
			}
		}
		if(separator != null && it.hasNext()) {
			sb.append(separator);
		}
	}
	List<RDFNode> results = Collections.singletonList(ResourceFactory.createTypedLiteral(sb.toString()));
	return WrappedIterator.create(results.iterator());
}
 
Example #27
Source File: MaxExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	RDFNode max = null;
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	while(it.hasNext()) {
		RDFNode next = it.next();
		if(max == null || NodeValue.compareAlways(NodeValue.makeNode(max.asNode()), NodeValue.makeNode(next.asNode())) < 0) {
			max = next;
		}
	}
	if(max == null) {
		return WrappedIterator.emptyIterator();
	}
	else {
		return WrappedIterator.create(Collections.singletonList(max).iterator());
	}
}
 
Example #28
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the detectedLicenses
 * @throws InvalidSPDXAnalysisException 
 */
public AnyLicenseInfo getConcludedLicenses() throws InvalidSPDXAnalysisException {
	List<AnyLicenseInfo> alLic = Lists.newArrayList();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_PACKAGE_CONCLUDED_LICENSE).asNode();
	Triple m = Triple.createMatch(this.node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		alLic.add(LicenseInfoFactory.getLicenseInfoFromModel(enclosingSpdxDocument, t.getObject()));
	}
	if (alLic.size() > 1) {
		throw(new InvalidSPDXAnalysisException("Too many concluded licenses"));
	}
	if (alLic.size() == 0) {
		return null;
	}
	return alLic.get(0);
}
 
Example #29
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @param checksumValue
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected DoapProject[] findMultipleDoapPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return new DoapProject[0];
	}
	List<DoapProject> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new DoapProject(modelContainer, t.getObject()));
	}
	return retval.toArray(new DoapProject[retval.size()]);
}
 
Example #30
Source File: DistinctExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static <T> ExtendedIterator<T> recording(ClosableIterator<T> i, Set<T> seen) {
	return new NiceIterator<T>() {
		
		@Override
		public void remove() {
			i.remove();
		}

		@Override
		public boolean hasNext() {
			return i.hasNext(); 
		}    

		@Override
		public T next() { 
			T x = i.next(); 
			seen.add(x);
			return x;
		}  

		@Override
		public void close() {
			i.close(); 
		}
	};
}