0

I created a function of extension sparql! how I can compare the subject,object and proopriété of rdf file with the parameter of the function which are (subject, property, object)?

This is my function but it does not display any result!

import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.String;
import java.*;
import java.io.*;**strong text**
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.function.Function;
import org.apache.jena.sparql.function.FunctionBase2;
import org.apache.jena.sparql.function.FunctionFactory;
import static org.apache.jena.vocabulary.RDF.Nodes.object;
import static org.apache.jena.vocabulary.RDF.Nodes.predicate;
import static org.apache.jena.vocabulary.RDF.Nodes.subject;

public  class haschild extends FunctionBase2 implements FunctionFactory{
private Map _cache;

public haschild()
{
    _cache = new HashMap();
}



@Override
public NodeValue exec(NodeValue v1, NodeValue v2) {
    String value = v2.asString();
    String value2 = v1.asString();
   Model model = ModelFactory.createDefaultModel();
InputStream is = null;
    try {
        is = new BufferedInputStream(
                new FileInputStream( "C:\\\\fichier     rdf/journal.webscience.org-vivo.rdf"));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(haschild.class.getName()).log(Level.SEVERE, null, ex);
    }
 model.read(new InputStreamReader(is), "");
 StmtIterator iter = model.listStatements();

  // affiche l'objet, le prédicat et le sujet de chaque déclaration
  while (iter.hasNext()) {
  Statement stmt      = iter.nextStatement();  // obtenir la prochaine     déclaration
Resource  subject   = stmt.getSubject();     // obtenir le sujet
Property  predicate = stmt.getPredicate();   // obtenir le prédicat
RDFNode   object    = stmt.getObject();      // obtenir l'objet

if((value2.equals(subject.toString()))&&(object.toString().equals(value))&&(predicate.toString().equals("http://www.w3.org/2000/01/rdf-schema#HasChild")))

                         return NodeValue.TRUE;


                            return NodeValue.FALSE;}

                           return null;
                                 }

                                @Override
                public Function create(String uri) {
                     throw new UnsupportedOperationException("Not supported   yet."); //To change body of generated methods, choose Tools | Templates.}



}
Andrea
  • 11,801
  • 17
  • 65
  • 72
  • Use RDFDataMgr to read the file. Be aware that functions can be called many times in a single query execution even with the same arguments (they are assumed to be quite cheap to call). – AndyS Oct 21 '15 at 15:36

1 Answers1

0

Compare the Nodes that back the various items and .equals those.

See NodeValue#asNode and RDFNode.asNode.

If you want value-equality (e.g. 1 equals +1.0e0), convert the RDFNode.asNode to a NodeValue and use NodeValue.sameAs.

NodeValue.asString returns the SPARQL syntax version of the value. Not for use in comparisons.

AndyS
  • 16,345
  • 17
  • 21