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

The following examples show how to use org.apache.jena.graph.Node#equals() . 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: 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 3
Source File: SubstitutionImplForJena.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Accumulates the different subsititions for a triple and applies them intelligently
	if we have a substitution 
	x/y
	and in a next step we compute a new substitution
	y/z
	then the composition should be
	x/z, y/z
	and not
	x/y, y/z **/
@Override
public boolean compose(Node original, Node substitution) {
	SubstitutionForJena unifier = StatementUnifierForJena.getUnifier(original, substitution);
	Set<Node> trivialRemove = new HashSet<Node>();

	if (unifier == null)
		return false;
	if (unifier instanceof NeutralSubstitution)
		return true;
	for (Node key : map.keySet()) {
		Node value = map.get(key);
		if (value.equals(original))
			if (key.equals(substitution)) {
				// existing substitition now trivial, remove it.
				trivialRemove.add(key);
			} else {
				map.put(key, substitution);
			}
	}
	map.put(original, substitution);
	map.keySet().removeAll(trivialRemove);
	return true;
}
 
Example 4
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 5
Source File: LicenseException.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param id the id to set
 * @throws InvalidSPDXAnalysisException 
 */
public void setLicenseExceptionId(String id) throws InvalidSPDXAnalysisException {
	if (model != null) {
		Node duplicateNode = findException(model, this.licenseExceptionId);
		if (duplicateNode != null && !duplicateNode.equals(this.exceptionNode)) {
			throw(new InvalidSPDXAnalysisException("Can not set the License Exception ID to "+id+".  That ID is already in use."));
		}
	}
	this.licenseExceptionId = id;
	if (this.exceptionNode != null) {
		// delete any previous created
		Property p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_EXCEPTION_ID);
		model.removeAll(resource, p, null);
		// add the property
		if (id != null) {
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_EXCEPTION_ID);
			resource.addProperty(p, id);
		}
	}
}
 
Example 6
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private int printProperty(Node p) {
    String str = slotToString(p);
    if (p.equals(RDF.Nodes.type) && str.equals(RDFTYPE)) {
        out.print("a");
    } else {
        out.print(str);
    }
    out.pad(predicateWidth);
    return str.length();
}
 
Example 7
Source File: SHACLFunctionsCache.java    From shacl with Apache License 2.0 5 votes vote down vote up
private boolean argEquals(Node arg1, Node arg2) {
	if(arg1 == null) {
		return arg2 == null;
	}
	else if(arg2 == null) {
		return false;
	}
	else {
		return arg1.equals(arg2);
	}
}
 
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: DB2Dataset.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterator<Quad> findNG(Node g, Node s, Node p, Node o)
   {
   if ((g == null) || g.equals(Node.ANY))
      {
      // TODO: REVISIT: very inefficient way to get all the matching quads from named graphs only
      LinkedList<Quad> l = new LinkedList<Quad>();
      Iterator<Quad> res = find(g, s, p, o);
      while (res.hasNext())
         {
         Quad q = res.next();
         if (q.getGraph() != null
               && (!q.getGraph().isURI() || !q.getGraph().getURI().equalsIgnoreCase(Constants.DEFAULT_GRAPH_MONIKER)))
            {
            l.add(q);
            }
         }
      return l.iterator();

      }
   else if (g.getURI().equalsIgnoreCase(Constants.DEFAULT_GRAPH_MONIKER))
      {
      return new LinkedList<Quad>().iterator();
      }
   else
      {
      return find(g, s, p, o);
      }
   }
 
Example 10
Source File: DB2Dataset.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Iterator<Quad> find(Node g, final Node s, final Node p, final Node o)
{

ExtendedIterator<Triple> it;

if ((g == null) || g.equals(Node.ANY))
   {
   it = DB2Graph.find(store, new Triple(s, p, o), null, connection, false, /* not reification */
         true /* search all graphs */);
   }
else
   {

   if (g.getURI().equalsIgnoreCase(Constants.DEFAULT_GRAPH_MONIKER))
      {
      it = defaultModel.getGraph().find(s, p, o);
      }
   else
      {
      it = getGraph(g).find(s, p, o);
      }

   }

Set<Quad> sq = new HashSet<Quad>();
while (it.hasNext())
   {
   sq.add(new Quad(g, it.next()));
   }
it.close();

return sq.iterator();

}
 
Example 11
Source File: QueryIterTripleStarPattern.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
static protected boolean insert( Node inputNode,
                                 Node outputNode,
                                 BindingMap results,
                                 boolean inputNodeIsTripleWithVars )
{
	if ( inputNodeIsTripleWithVars )
    {
		if ( !(outputNode instanceof Node_Triple) )
			return false;

		final Triple outputTriple = ( (Node_Triple) outputNode ).get();
		final Triple inputTriple  = ( (Node_Triple) inputNode  ).get();

		return insert(outputTriple,
		              inputTriple.getSubject(),
		              inputTriple.getPredicate(),
		              inputTriple.getObject(),
		              results );
    }
    else if ( Var.isVar(inputNode) )
    {
    	final Var v = Var.alloc(inputNode);
        final Node x = results.get(v);
        if ( x != null )
            return outputNode.equals(x);

        results.add(v, outputNode);
        return true;
    }
    else
    	return true;
}
 
Example 12
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 13
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void doubleNestedObject()
{
	final String filename = "doubleNestedObject.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 s = t.getSubject();
       	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()) && s.getURI().contains("alice") )
       		cntReifiedStmt1++;
       	else if ( p.equals(DCTerms.created.asNode()) )
       		cntReifiedStmt2++;
       	else if ( p.equals(FOAF.knows.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 14
Source File: Match.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
public static boolean match(Node node, Node pattern) {
    return pattern == null || pattern == Node.ANY || pattern.equals(node);
}
 
Example 15
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void nestedSubjectAndObject()
{
	final String filename = "nestedSubjectAndObject.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(DCTerms.creator.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(DCTerms.creator.asNode()) )
       		cntReifiedStmt1++;
       	else if ( p.equals(DCTerms.created.asNode()) )
       		cntReifiedStmt2++;
       	else if ( p.equals(DCTerms.requires.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 16
Source File: SPDXFile.java    From tools with Apache License 2.0 4 votes vote down vote up
private boolean nodesEquals(Node n1, Node n2) {
	return n1.equals(n2);
}
 
Example 17
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 18
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void nestedSubject()
{
	final String filename = "nestedSubject.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 );
}