Custom SWRL built-in function

68 views
Skip to first unread message

yves teissier

unread,
Apr 10, 2018, 9:26:07 AM4/10/18
to Pellet Users
Hi all,

I'm trying to implement a custom SWRL function in Pellet.
I'm using the current Github head version ( https://github.com/stardog-union/pellet )

This is the rule:
TYPE1(?o) ^ TYPE2(?p) ^ isAlignedTo(?o, ?p) ^ hasValue1(?o, ?in) ^ translateTS(?in, ?out) -> hasValue2(?p, ?out)

The new function (translateTS(?in, ?out)) is dedicated to get the parameter 1 (input) then produce the parameter 2 (output).
In the rule, the parameter 1 come from the Data Property Assertion "hasValue1" applied on the individual of type TYPE1.

All seems to be good except that the "?in" is null when the "apply" method is invoked in the function code.
Normally the "?in" should contains the value of the Data Property "hasValue".

This is the code:

package org.mindswap.pellet.examples;
import org.mindswap.pellet.jena.PelletReasonerFactory;
import static org.mindswap.pellet.utils.Namespaces.XSD;
import org.mindswap.pellet.ABox;
import org.mindswap.pellet.Literal;
import org.mindswap.pellet.utils.ATermUtils;
import com.clarkparsia.pellet.rules.builtins.BuiltInRegistry;
import com.clarkparsia.pellet.rules.builtins.GeneralFunction;
import com.clarkparsia.pellet.rules.builtins.GeneralFunctionBuiltIn;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.ValidityReport;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class RulesExample {
 
 
 
private static class TranslateTS implements GeneralFunction {


 
public boolean apply(ABox abox, Literal[] args) {
           
// Some code ...

 
return true;

 
}




 
public boolean isApplicable(boolean[] boundPositions) {

 
//the built-in is applicable for two arguments only

 
return boundPositions.length == 1;
 
}


 
}
 
 
public static void main(String[] args) {
 
       
BuiltInRegistry.instance.registerBuiltIn("urn:makub:builtIn#translateTS", new GeneralFunctionBuiltIn(new TranslateTS()));
       

 
String ns = "http://www.semanticweb.org/sesa188919/ontologies/2018/1/untitled-ontology-444";
 
String ont = "file:/c:/Users/SESA188919/Documents/SCHNEIDER/XDI/ONTOLOGY/SWRL/GIT/swrl4m2m/SWRL4M2M.owl";
 
 
OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC, null );
 model
.read( ont );
 
//ValidityReport report = model.validate();








 model
.prepare();




 printPropertiesValues
(I_ATTR01);
 
 
}


 

 
public static void printPropertiesValues(Individual ind) {

 
System.out.print( ind.getLocalName() + " ... ");
 
StmtIterator i = ind.listProperties();
 
while( i.hasNext() ) {
 
Statement prop = (Statement) i.next();
 
System.out.print( prop.toString() );
 
if( i.hasNext() )
 
System.out.print( "\n" );
 
// printIterator( ind.listPropertyValues( prop ) );
 
}
 
}
 

}



And this is the rule in RDF/XML format


<rdf:Description>
       
<swrla:isRuleEnabled rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</swrla:isRuleEnabled>
       
<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"></rdfs:comment>
       
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">FRM2COMM_TimeStamp</rdfs:label>
       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Imp"/>
       
<swrl:body>
           
<rdf:Description>
               
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
               
<rdf:first>
                   
<rdf:Description>
                       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
                       
<swrl:classPredicate rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#TYPE1"/>
                       
<swrl:argument1 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#o"/>
                   
</rdf:Description>
               
</rdf:first>
               
<rdf:rest>
                   
<rdf:Description>
                       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
                       
<rdf:first>
                           
<rdf:Description>
                               
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
                               
<swrl:classPredicate rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#TYPE2"/>
                               
<swrl:argument1 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#p"/>
                           
</rdf:Description>
                       
</rdf:first>
                       
<rdf:rest>
                           
<rdf:Description>
                               
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
                               
<rdf:first>
                                   
<rdf:Description>
                                       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
                                       
<swrl:propertyPredicate rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#isAlignedTo"/>
                                       
<swrl:argument1 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#o"/>
                                       
<swrl:argument2 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#p"/>
                                   
</rdf:Description>
                               
</rdf:first>
                               
<rdf:rest>
                                   
<rdf:Description>
                                       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
                                       
<rdf:first>
                                           
<rdf:Description>
                                               
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#DatavaluedPropertyAtom"/>
                                               
<swrl:propertyPredicate rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#hasValue1"/>
                                               
<swrl:argument1 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#o"/>
                                               
<swrl:argument2 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#in"/>
                                           
</rdf:Description>
                                       
</rdf:first>
                                       
<rdf:rest>
                                           
<rdf:Description>
                                               
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
                                               
<rdf:first>
                                                   
<rdf:Description>
                                                       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#BuiltinAtom"/>
                                                       
<swrl:builtin rdf:resource="urn:makub:builtIn#translateTS"/>
                                                       
<swrl:arguments>
                                                           
<rdf:Description>
                                                               
<rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"/>
                                                               
<rdf:first rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#in"/>
 
<rdf:rest>
                                                                   
<rdf:Description>
                                                                       
<rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"/>
 
<rdf:first rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#out"/>
                                                                   
</rdf:Description>
                                                               
</rdf:rest>
                                                           
</rdf:Description>
                                                       
</swrl:arguments>
                                                   
</rdf:Description>
                                               
</rdf:first>
                                               
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
                                           
</rdf:Description>
                                       
</rdf:rest>
                                   
</rdf:Description>
                               
</rdf:rest>
                           
</rdf:Description>
                       
</rdf:rest>
                   
</rdf:Description>
               
</rdf:rest>
           
</rdf:Description>
       
</swrl:body>
       
<swrl:head>
           
<rdf:Description>
               
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
               
<rdf:first>
                   
<rdf:Description>
                       
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#DatavaluedPropertyAtom"/>
                       
<swrl:propertyPredicate rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#hasValue2"/>
                       
<swrl:argument1 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#p"/>
                       
<swrl:argument2 rdf:resource="http://www.semanticweb.org/tchekov/ontologies/2018/1/untitled-ontology-444#out"/>
                   
</rdf:Description>
               
</rdf:first>
               
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
           
</rdf:Description>
       
</swrl:head>
   
</rdf:Description>





If you have some subjection to understand the issue :-)

Regards,
Yves Teissier


Reply all
Reply to author
Forward
0 new messages