Java Code Examples for org.apache.jena.graph.Triple#getObject()

The following examples show how to use org.apache.jena.graph.Triple#getObject() . 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
/**
 * @return the spdxPackage
 * @throws InvalidSPDXAnalysisException 
 */
public SPDXPackage getSpdxPackage() throws InvalidSPDXAnalysisException {
	if (this.spdxPackage != null) {
		return this.spdxPackage;
	}
	Node spdxDocNode = getSpdxDocNode();
	if (spdxDocNode == null) {
		throw(new InvalidSPDXAnalysisException("Must set an SPDX doc before getting an SPDX package"));
	}
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_PACKAGE).asNode();
	Triple m = Triple.createMatch(spdxDocNode, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	SPDXPackage newSpdxPackage = null;
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		newSpdxPackage = new SPDXPackage(t.getObject(), this);
	}
	this.spdxPackage = newSpdxPackage;
	return newSpdxPackage;
}
 
Example 2
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the sha1
 * @throws InvalidSPDXAnalysisException
 */
public String getSha1() throws InvalidSPDXAnalysisException {

	String retval = null;
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_PACKAGE_CHECKSUM).asNode();
	Triple m = Triple.createMatch(this.node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		SPDXChecksum cksum = new SPDXChecksum(model, t.getObject());
		if (cksum.getAlgorithm().equals(SPDXChecksum.ALGORITHM_SHA1)) {
			retval = cksum.getValue();
		}
	}
	return retval;
}
 
Example 3
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 4
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 5
Source File: SPDXFile.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the sha1
 * @throws InvalidSPDXAnalysisException 
 */
public String getSha1()  {
	if (this.model != null && this.resource != null) {
		try {
			Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM).asNode();
			Triple m = Triple.createMatch(this.resource.asNode(), p, null);
			ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
			Triple t = null;
			while (tripleIter.hasNext()) {
				t = tripleIter.next();
				if (this.sha1 != null && nodesEquals(this.sha1.getResource().asNode(), t.getObject())) {
					break;
				}
			}
			if (this.sha1 == null || !nodesEquals(this.sha1.getResource().asNode(), t.getObject())) {
				this.sha1 = new SPDXChecksum(model, t.getObject());
			}
		} catch (InvalidSPDXAnalysisException e) {
			// just use the original sha1
		}
	}
	return this.sha1.getValue();
}
 
Example 6
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @return the spdxPackage
 * @throws InvalidSPDXAnalysisException
 */
public SPDXPackage getSpdxPackage() throws InvalidSPDXAnalysisException {
	if (this.spdxPackage != null) {
		return this.spdxPackage;
	}
	Node spdxDocNode = getSpdxDocNode();
	if (spdxDocNode == null) {
		throw(new InvalidSPDXAnalysisException("Must set an SPDX doc before getting an SPDX package"));
	}
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_PACKAGE).asNode();
	Triple m = Triple.createMatch(spdxDocNode, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
	SPDXPackage newSpdxPackage = null;
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		newSpdxPackage = new SPDXPackage(t.getObject());
	}
	this.spdxPackage = newSpdxPackage;
	return newSpdxPackage;
}
 
Example 7
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all SPDX files by the given name
 * @param fileName Name of SPDX file to be removed
 * @throws InvalidSPDXAnalysisException 
 */
public void removeFile(String fileName) throws InvalidSPDXAnalysisException {
	List<Node> filesToRemove = Lists.newArrayList();
	Node fileNameProperty = model.getProperty(SPDX_NAMESPACE, PROP_FILE_NAME).asNode();
	Property docFileProperty = model.getProperty(SPDX_NAMESPACE, PROP_SPDX_FILE_REFERENCE);
	Property pkgFileProperty = model.createProperty(SPDX_NAMESPACE, PROP_PACKAGE_FILE);
	Resource docResource = getResource(getSpdxDocNode());
	Resource pkgResource = getResource(this.node);
	Triple m = Triple.createMatch(getSpdxDocNode(), docFileProperty.asNode(), null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	//TODO: See if there is a more efficient search method for files
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		Node fileObject = t.getObject();
		Triple fileNameMatch = Triple.createMatch(fileObject, fileNameProperty, null);
		ExtendedIterator<Triple> fileNameIterator = model.getGraph().find(fileNameMatch);
		while (fileNameIterator.hasNext()) {
			Triple fileNameTriple = fileNameIterator.next();
			String searchFileName = fileNameTriple.getObject().toString(false); 
			if (searchFileName.equals(fileName)) {
				filesToRemove.add(fileObject);
			}
		}
	}
	for (int i = 0; i < filesToRemove.size(); i++) {
		// remove the references files
		RDFNode o = model.getRDFNode(filesToRemove.get(i));
		model.removeAll(docResource, docFileProperty, o);
		// remove the package files
		model.removeAll(pkgResource, pkgFileProperty, o);				
	}
}
 
Example 8
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
public SpdxPackageVerificationCode getVerificationCode() throws InvalidSPDXAnalysisException {
	SpdxPackageVerificationCode retval = null;
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_PACKAGE_VERIFICATION_CODE).asNode();
	Triple m = Triple.createMatch(this.node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval = new SpdxPackageVerificationCode(model, t.getObject());
	}
	return retval;
}
 
Example 9
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected static Set<String> getAllVariables(Collection<Triple> triples) {
	Set<String> ret = new HashSet<String>();
	for (Triple t: triples) {
		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 10
Source File: NormalizedOWLQLTbox.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isTripleAbsentFromAbox(Triple qt) {
	Node pred = qt.getPredicate();
	if (pred.isURI() && isGeneratedRole(pred.getURI())) {
		return true;
	} else if (pred.isURI() && pred.getURI().equals(RDFConstants.RDF_TYPE)) {
		Node obj = qt.getObject();
		if (obj.isURI() && isGeneratedClass(obj.getURI())) {
			return true;
		}
	}
	return false;
}
 
Example 11
Source File: RdfModelObject.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException
 */
protected SpdxPackageVerificationCode findVerificationCodePropertyValue(
		String nameSpace,String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	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();
		return new SpdxPackageVerificationCode(model, t.getObject());
	}
	return null;
}
 
Example 12
Source File: RDFStarUtils.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
/**
 * If the given triple is a nested triple that has another triple in its
 * object position, return that object triple; otherwise return null.
 */
static public Triple getObjectTriple( Triple t )
{
	final Node o = t.getObject();
	if ( o instanceof Node_Triple )
		return ( (Node_Triple) o ).get();
	else
		return null;
}
 
Example 13
Source File: RdfModelObject.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected SPDXCreatorInformation findCreationInfoPropertyValue(
		String nameSpace, String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	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();
		return new SPDXCreatorInformation(model, t.getObject());
	}
	return null;
}
 
Example 14
Source File: SinkTripleStarOutput.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Override
public void send(Triple triple) {
    Node s = triple.getSubject() ;
    Node p = triple.getPredicate() ;
    Node o = triple.getObject() ;

    nodeFmt.format(writer, s) ;
    writer.print(" ") ;
    nodeFmt.format(writer, p) ;
    writer.print(" ") ;
    nodeFmt.format(writer, o) ;
    writer.print(" .\n") ;
}
 
Example 15
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 16
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void nestedObject()
{
	final String filename = "nestedObject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 6, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.creator.asNode()) )
       		cntPredicateStmt++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(DCTerms.creator.asNode()) )
       		cntReifiedStmt++;
       	else if ( p.equals(DCTerms.source.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 1, cntTypeStmt );
       assertEquals( 1, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt );
       assertEquals( 1, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt );
       assertEquals( 1, cntMetaStmt );
}
 
Example 17
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void doubleNestedSubject()
{
	final String filename = "doubleNestedSubject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 11, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt1 = 0;
       int cntPredicateStmt2 = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt1 = 0;
       int cntReifiedStmt2 = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(FOAF.knows.asNode()) )
       		cntPredicateStmt1++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.created.asNode()) )
       		cntPredicateStmt2++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(FOAF.knows.asNode()) )
       		cntReifiedStmt1++;
       	else if ( p.equals(DCTerms.created.asNode()) )
       		cntReifiedStmt2++;
       	else if ( p.equals(DCTerms.source.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 2, cntTypeStmt );
       assertEquals( 2, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt1 );
       assertEquals( 1, cntPredicateStmt2 );
       assertEquals( 2, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt1 );
       assertEquals( 1, cntReifiedStmt2 );
       assertEquals( 1, cntMetaStmt );
}
 
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: ExternalRef.java    From tools with Apache License 2.0 4 votes vote down vote up
@Override
public Resource findDuplicateResource(IModelContainer modelContainer, String uri) throws InvalidSPDXAnalysisException {
	if (referenceCategory == null || referenceType == null || referenceLocator == null) {
		return null;
	}
	Node referenceLocatorProperty = modelContainer.getModel().getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_REFERENCE_LOCATOR).asNode();
	Triple referenceLocatorMatch = Triple.createMatch(null, referenceLocatorProperty, NodeFactory.createLiteral(this.referenceLocator));
	ExtendedIterator<Triple> referenceMatchIter = modelContainer.getModel().getGraph().find(referenceLocatorMatch);	
	while (referenceMatchIter.hasNext()) {
		Triple referenceMatchTriple = referenceMatchIter.next();
		Node referenceNode = referenceMatchTriple.getSubject();
		// Check the type and category
		Node typeProperty = modelContainer.getModel().getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_REFERENCE_TYPE).asNode();
		Node categoryProperty = modelContainer.getModel().getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_REFERENCE_CATEGORY).asNode();
		Triple typeMatch = Triple.createMatch(referenceNode, typeProperty, null);
		Triple categoryMatch = Triple.createMatch(referenceNode, categoryProperty, null);
		ExtendedIterator<Triple> typeMatchIterator = modelContainer.getModel().getGraph().find(typeMatch);
		ExtendedIterator<Triple> categoryMatchIterator = modelContainer.getModel().getGraph().find(categoryMatch);
		if (typeMatchIterator.hasNext()) {
			Triple typeMatchTriple = typeMatchIterator.next();
			if (typeMatchTriple.getObject() != null && typeMatchTriple.getObject().isURI()) {
				if (this.referenceType.getReferenceTypeUri().toString().equals(typeMatchTriple.getObject().getURI())) {
					// check category
					if (categoryMatchIterator.hasNext()) {
						Triple categoryMatchTriple = categoryMatchIterator.next();
						if (categoryMatchTriple.getObject() != null && categoryMatchTriple.getObject().isURI()) {
							String categoryUri = categoryMatchTriple.getObject().getURI();
							if (categoryUri != null && categoryUri.length() > SpdxRdfConstants.SPDX_NAMESPACE.length()) {
								if (this.referenceCategory.toString().equals(categoryUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length()))) {
									return RdfParserHelper.convertToResource(modelContainer.getModel(), referenceNode);
								}
							}
						}
					}
				}
			}
		}
	}
	// if we get to here, we did not find a match
	return null;
}
 
Example 20
Source File: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
protected Node unNestTriplePattern( Triple tp, ElementPathBlock epb, boolean hasParent )
{	
	Node s = tp.getSubject();
	Node p = tp.getPredicate();
	Node o = tp.getObject();

	if ( s instanceof Node_Triple )
	{
		final Triple sTP = ( (Node_Triple) s ).get();
		s = unNestTriplePattern(sTP, epb, true);
	}

	if ( o instanceof Node_Triple )
	{
		final Triple oTP = ( (Node_Triple) o ).get();
		o = unNestTriplePattern(oTP, epb, true);
	}

	final Triple nonnestedTP = new Triple(s, p, o);
	final boolean seenBefore = doneNested.containsKey(nonnestedTP);

	final Node var;
	if ( seenBefore ) {
		var = doneNested.get(nonnestedTP);
	}
	else
	{
		var = createFreshAnonVarForReifiedTriple();
		epb.addTriple(nonnestedTP);

		if ( hasParent ) {
			epb.addTriple( new Triple(var, RDF.Nodes.type,      RDF.Nodes.Statement) );
			epb.addTriple( new Triple(var, RDF.Nodes.subject,   s) );
			epb.addTriple( new Triple(var, RDF.Nodes.predicate, p) );
			epb.addTriple( new Triple(var, RDF.Nodes.object,    o) );
			doneNested.put(nonnestedTP, var);
		}
	}

	return var;
}