Re: [topbraid-users] Executing user defined SPIN Function

132 views
Skip to first unread message

Holger Knublauch

unread,
May 14, 2013, 8:38:38 PM5/14/13
to topbrai...@googlegroups.com
Hi Leo,

On 5/14/2013 21:17, Leo Anbarasan wrote:
I have tried many options to execute user defined SPIN functions. But i could not succeed.

SPIN Function:

SELECT ?users
WHERE {
    {
        SELECT ((GROUP_CONCAT(DISTINCT ?user; SEPARATOR=',')) AS ?users)
        WHERE {
            ?user a :Person .
            ?user :accountAge ?age .
            FILTER (?age <= 10) .
        }
    } .
}



It works fine in TopBraid Composer. But doesn't work in coding.

I have tried in 4 methods and everything returns empty result.  They are,

        // TYPE 1
        registry.registerAll(model, null);
        String str = "SELECT ?x WHERE { BIND (<http://topbraid.org/sparqlmotionfunctions#parseDate>('12/3/11','MM/dd/
yy') as ?x) }";
        QueryExecution exec = QueryExecutionFactory.create(str, model);
       
        ResultSet resultSet = exec.execSelect();
        ResultSetFormatter.out(resultSet);

The smf:parseDate function is not part of the SPIN API, and only works inside of TopBraid products.

       
       
        // TYPE 2
        Resource queryResource = model.listStatements(null, RDF.type, SP.Select).next().getSubject();
        Select selectQuery = (Select) SPINFactory.asQuery(queryResource);
       
        List<Resource> res = selectQuery.getResultVariables();
        System.out.println(res.get(0).toString());

What is wrong with this example? The code above is just iterating over all Select query declarations in SPIN RDF syntax, but doesn't execute them. Note that SELECT * may not have any result variables, so res.get(0) seems unsafe.

       
        // TYPE 3
        Function function = registry.getFunction("http://www.semanticweb.org/socure.me/activityOntology#InvalidUser", model);
        registry.registerFunctions(model, function);

I assume you are calling something like in the OWLRLExample:

        // Register any new functions defined in OWL RL
        SPINModuleRegistry.get().registerAll(owlrlModel, null);

on the correct input model (with imports etc)?

        String query = "SELECT (<http://www.semanticweb.org/socure.me/activityOntology#InvalidUser>() AS ?u) {}";
       
        QueryExecution exec = QueryExecutionFactory.create(query, model);
       
        ResultSet resultSet = exec.execSelect();
       
        System.out.println(resultSet.getRowNumber());

Are you getting an error here? Something like missing function registry for your function? If the function is registered correctly then this should work. If not, maybe walk through the code in debugging mode to see why the SPIN API doesn't recognize your function.

HTH
Holger


       
       
        // TYPE 4
        Query arqQuery = new ARQFactory().createQuery(spinQuery);
       
        QueryExecution exec = QueryExecutionFactory.create(arqQuery, model);
       
        ResultSet resultSet = exec.execSelect();
       
        System.out.println(resultSet.getRowNumber());


Type 1 is predefined function. Even that is returning empty result. I am not sure whether this is the correct methods.

If someone could help me resolve this problem.

Awaiting for your reply.

Thanks
---
Leo Anbarasan M
India
--
-- You received this message because you are subscribed to the Google
Group "TopBraid Suite Users", the topics of which include Enterprise Vocabulary Network (EVN), TopBraid Composer, TopBraid Live,
TopBraid Ensemble, SPARQLMotion, SPARQL Web Pages and SPIN.
To post to this group, send email to
topbrai...@googlegroups.com
To unsubscribe from this group, send email to
topbraid-user...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/topbraid-users?hl=en
---
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Leo Anbarasan

unread,
May 15, 2013, 3:32:26 AM5/15/13
to topbrai...@googlegroups.com
Hi Holger,

Thanks for your reply.

I need some calcification to execute SPIN function, please refer TYPE 3.

This method return empty result. I have run this in topbraid composer. It works fine and return a result. I am not sure about it in API. Could give an example to execute SPIN function.

Awaiting for your reply.

Thanks,
Leo

 

Holger Knublauch

unread,
May 15, 2013, 6:21:25 PM5/15/13
to topbrai...@googlegroups.com
On 5/15/2013 17:32, Leo Anbarasan wrote:
>
> This method return empty result. I have run this in topbraid composer.
> It works fine and return a result. I am not sure about it in API.
> Could give an example to execute SPIN function.

Your SPARQL syntax looks correct, and I don't have enough background to
give more useful feedback here. Calling and registering SPIN functions
is done by the OWLRL and the Kennedys Java code examples that come with
the SPIN API download. Please take a look at those. I have attached the
kennedys example snippet below.

Holger

---

package org.topbraid.spin.examples;

import java.util.List;

import org.topbraid.spin.constraints.ConstraintViolation;
import org.topbraid.spin.constraints.SPINConstraints;
import org.topbraid.spin.inference.SPINInferences;
import org.topbraid.spin.system.SPINLabels;
import org.topbraid.spin.system.SPINModuleRegistry;
import org.topbraid.spin.util.JenaUtil;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.shared.ReificationStyle;


/**
* Loads the Kennedys SPIN ontology and runs inferences and then
* constraint checks on it.
*
* @author Holger Knublauch
*/
public class KennedysInferencingAndConstraintsExample {

public static void main(String[] args) {

// Initialize system functions and templates
SPINModuleRegistry.get().init();

// Load main file
Model baseModel =
ModelFactory.createDefaultModel(ReificationStyle.Minimal);
baseModel.read("http://topbraid.org/examples/kennedysSPIN");

// Create OntModel with imports
OntModel ontModel =
JenaUtil.createOntologyModel(OntModelSpec.OWL_MEM,baseModel);

// Create and add Model for inferred triples
Model newTriples =
ModelFactory.createDefaultModel(ReificationStyle.Minimal);
ontModel.addSubModel(newTriples);

// Register locally defined functions
SPINModuleRegistry.get().registerAll(ontModel, null);

// Run all inferences
SPINInferences.run(ontModel, newTriples, null, null, false, null);
System.out.println("Inferred triples: " + newTriples.size());

// Run all constraints
List<ConstraintViolation> cvs = SPINConstraints.check(ontModel,
null);
System.out.println("Constraint violations:");
for(ConstraintViolation cv : cvs) {
System.out.println(" - at " +
SPINLabels.get().getLabel(cv.getRoot()) + ": " + cv.getMessage());
}

// Run constraints on a single instance only
Resource person = cvs.get(0).getRoot();
List<ConstraintViolation> localCVS =
SPINConstraints.check(person, null);
System.out.println("Constraint violations for " +
SPINLabels.get().getLabel(person) + ": " + localCVS.size());
}
}

Reply all
Reply to author
Forward
0 new messages