Stanbol Home

The RESTful Semantic Engine

Apache Stanbol Rules

Rules tutorial

The rule pattern (modus ponens) used in Stanbol is the following
     if conditioon then consequent
For example the statement "every person has a father", i.e. ∀x.∃y. Person(x) ⇒ hasFather(x, y), becomes
     if X is a person then X has a father
Or "the brother of the father is the uncle", i.e. ∀xyz. hasFather(x,y) ∧ hasBrother(y,z) ⇒ hasUncle(x,z), becomes
     if Y is the father of X and Z the brother of Y then Z is the uncle of X
in Stanbol a rule is defined using the following syntax
     ruleName[body -> head]
where
  • the ruleName identifies the rule
  • the body is a set of atoms that must be satisfied by evaluating the rule
  • the head or consequent is a set of atoms that must be true if the condition is evaluated to be true
  • both body and head consist of a list of conjunctive atoms
    • head = atom1 . atom2 . ... . atomM
    • body = atom1 . atom2 . ... . atomN
  • the conjunction ∧ in Stanbol Rules is expressed with the symbol " . "
An atom is the smallest unit of the interpretation of a rule, e.g. the following predicate calculus formula
     Person(x) ⇒ hasFather(x, y)
has two atoms
  • Person(·)
  • hasFather(·, ·)
In Stanbol basic atoms are The atoms may contain
  • constants: they consist of URI (we are in Web context) or Literal (values), e.g. e.g. <http//dbpedia.org/resource/Bob_Marley> is a constant, but "Bob Marley"^^xsd:string is a constant too
  • variables: any identifier preceded by ?, e.g. ?x is a variable, but also ?y is a variable
A class assertion atom is identified by the operator
     is(classPredicate, argument)
where
  • classPredicate is a URI that identified a class
  • argument is the resource that has to be proved as typed with the classPredicate. It can be both a constant (a URI) or a variable
For example
     is(http://xmlns.com/foaf/0.1/Person, ?x) is evaluated to be
                    true if the concrete value associated to ?x is typed as http://xmlns.com/foaf/0.1/Person 	
                    false otherwise
An individual assertion atom is useful to evaluate or assert facts between individuals and is identified by the operator
     has(propertyPredicate, individualArgument1, individualArgument2)
where
  • propertyPredicate is the object property that has to be evaluated. It can be a constant (URI) or a variable (?x)
  • individualArgument1 and individualArgument2 are the two arguments of the property. They can be either constants (URI) or variables (?x)
An datavalued assertion atom is useful to evaluate or assert facts between individuals and values and is identified by the operator
     values(propertyPredicate, individualArgument, valueArgument)
where
  • propertyPredicate is the object property that has to be evaluated. It can be a constant (URI) or a variable (?x)
  • individualArgumentis the subject of the statement. It can be either constants (i.e. URI) or variables (e.g. ?x)
  • valueArgumentis the object of the statement. It can be either constants (i.e. a literal) or variables (e.g. ?x)

Hands-on about refactoring RDF graphs

Stanbol refactor coverts RDF graphs given a set of mapping rules i.e. transformation patterns. Stanbol rule syntax is described in detail in the Stanbol rules documentation page.

Below you can see an example of transformation patterns defined for the GRS patterns

RDF graph in input to which the refactoring should be applied.

Transformation patterns defined as Stanbol Rules

Click on the button below and view results!


See how to access the refactor service through cURL command as well as Javascript.

cURL command for refactor
Example of access to Stanbol refactor from a Javascript application

Rules syntax in BNF

TOKEN

 
<DEFAULT> SKIP : {
" "
}
 
<DEFAULT> SKIP : {
"\r"
| "\t"
| "\n"
}
   
 
<DEFAULT> TOKEN : {
<LARROW: "->">
| <COLON: ":">
| <EQUAL: "=">
| <AND: ".">
| <COMMA: ",">
| <REFLEXIVE: "+">
| <SAME: "same">
| <DIFFERENT: "different">
| <LESSTHAN: "lt">
| <GREATERTHAN: "gt">
| <IS: "is">
| <NEW_NODE: "newNode">
| <LENGTH: "length">
| <SUBSTRING: "substring">
| <UPPERCASE: "upperCase">
| <LOWERCASE: "lowerCase">
| <STARTS_WITH: "startsWith">
| <ENDS_WITH: "endsWith">
| <LET: "let">
| <CONCAT: "concat">
| <HAS: "has">
| <VALUES: "values">
| <NOTEX: "notex">
| <PLUS: "sum">
| <MINUS: "sub">
| <NOT: "not">
| <NAMESPACE: "namespace">
| <LOCALNAME: "localname">
| <STR: "str">
| <APOX: "^">
| <UNION: "union">
| <CREATE_LABEL: "createLabel">
| <SPARQL_C: "sparql-c">
| <SPARQL_D: "sparql-d">
| <SPARQL_DD: "sparql-dd">
| <PROP: "prop">
| <IS_BLANK: "isBlank">
| <FORWARD_CHAIN: "!">
}
   
 
<DEFAULT> TOKEN : {
<LPAR: "(">
| <RPAR: ")">
| <DQUOT: "\"">
| <LQUAD: "[">
| <RQUAD: "]">
}
   
 
<DEFAULT> TOKEN : {
<NUM: (["0"-"9"])+>
| <VAR: (["0"-"9","a"-"z","A"-"Z","-","_","."])+>
| <VARIABLE: "?" (["0"-"9","a"-"z","A"-"Z","-","_"])+>
| <URI: "<" (["0"-"9","a"-"z","A"-"Z","-","_",".","#",":","/","(",")"])+ ">">
| <STRING: "\"" (["0"-"9","a"-"z","A"-"Z","-","_",".",":","/","#","\\","?"," ","!","$","%"])+ "\"">
| <SPARQL_STRING: "%" (["0"-"9","a"-"z","A"-"Z","-","_",".",":","/","#","\\","?"," ","!","$","%","{","}","(",")","\"","<",">","=","+","\n","\t","&","|",","])+ "%">
| <BNODE: "_:" (["0"-"9","a"-"z","A"-"Z","-","_","."])+>
}
   

NON-TERMINALS

start ::= expression expressionCont
expressionCont ::= ( <AND> expression )
|
expression ::= prefix expressionCont
prefix ::= getVariable ( equality | rule )
| <FORWARD_CHAIN> getVariable rule
| <REFLEXIVE> getVariable rule
equality ::= <EQUAL> ( getURI )
rule ::= <LQUAD> ruleDefinition <RQUAD>
ruleDefinition ::= atomList <LARROW> atomList
| <SPARQL_C> <LPAR> <SPARQL_STRING> <RPAR>
| <SPARQL_D> <LPAR> <SPARQL_STRING> <RPAR>
| <SPARQL_DD> <LPAR> <SPARQL_STRING> <RPAR>
atomList ::= atom atomListRest
|
atomListRest ::= <AND> atomList
|
atom ::= classAtom
| individualPropertyAtom
| datavaluedPropertyAtom
| letAtom
| newNodeAtom
| comparisonAtom
| unionAtom
unionAtom ::= <UNION> <LPAR> atomList <COMMA> atomList <RPAR>
createLabelAtom ::= <CREATE_LABEL> <LPAR> stringFunctionAtom <RPAR>
propStringAtom ::= <PROP> <LPAR> stringFunctionAtom <COMMA> stringFunctionAtom <RPAR>
endsWithAtom ::= <ENDS_WITH> <LPAR> stringFunctionAtom <COMMA> stringFunctionAtom <RPAR>
startsWithAtom ::= <STARTS_WITH> <LPAR> stringFunctionAtom <COMMA> stringFunctionAtom <RPAR>
stringFunctionAtom ::= ( concatAtom | upperCaseAtom | lowerCaseAtom | substringAtom | namespaceAtom | localnameAtom | strAtom | stringAtom | propStringAtom | createLabelAtom )
strAtom ::= <STR> <LPAR> iObject <RPAR>
namespaceAtom ::= <NAMESPACE> <LPAR> iObject <RPAR>
localnameAtom ::= <LOCALNAME> <LPAR> iObject <RPAR>
stringAtom ::= uObject
concatAtom ::= <CONCAT> <LPAR> stringFunctionAtom <COMMA> stringFunctionAtom <RPAR>
upperCaseAtom ::= <UPPERCASE> <LPAR> stringFunctionAtom <RPAR>
lowerCaseAtom ::= <LOWERCASE> <LPAR> stringFunctionAtom <RPAR>
substringAtom ::= <SUBSTRING> <LPAR> stringFunctionAtom <COMMA> numericFunctionAtom <COMMA> numericFunctionAtom <RPAR>
numericFunctionAtom ::= ( sumAtom | subtractionAtom | lengthAtom | numberAtom )
lengthAtom ::= <LENGTH> <LPAR> stringFunctionAtom <RPAR>
sumAtom ::= <PLUS> <LPAR> numericFunctionAtom <COMMA> numericFunctionAtom <RPAR>
subtractionAtom ::= <MINUS> <LPAR> numericFunctionAtom <COMMA> numericFunctionAtom <RPAR>
numberAtom ::= ( <NUM> | <VARIABLE> )
classAtom ::= <IS> <LPAR> iObject <COMMA> iObject <RPAR>
newNodeAtom ::= <NEW_NODE> <LPAR> iObject <COMMA> dObject <RPAR>
letAtom ::= <LET> <LPAR> iObject <COMMA> stringFunctionAtom <RPAR>
individualPropertyAtom ::= <HAS> <LPAR> iObject <COMMA> iObject <COMMA> iObject <RPAR>
datavaluedPropertyAtom ::= <VALUES> <LPAR> iObject <COMMA> iObject <COMMA> dObject <RPAR>
sameAsAtom ::= <SAME> <LPAR> stringFunctionAtom <COMMA> stringFunctionAtom <RPAR>
lessThanAtom ::= <LESSTHAN> <LPAR> iObject <COMMA> iObject <RPAR>
greaterThanAtom ::= <GREATERTHAN> <LPAR> iObject <COMMA> iObject <RPAR>
differentFromAtom ::= <DIFFERENT> <LPAR> stringFunctionAtom <COMMA> stringFunctionAtom <RPAR>
reference ::= getURI
| getVariable <COLON> getVariable
varReference ::= getURI
| getVariable <COLON> getVariable
getURI ::= <URI>
getVariable ::= <VAR>
getString ::= <STRING>
getInt ::= <NUM>
uObject ::= ( variable | reference | getString | getInt )
iObject ::= variable
| reference
dObject ::= ( literal | variable )
literal ::= ( getString typedLiteral | getInt typedLiteral )
typedLiteral ::= ( <APOX> <APOX> reference | )
variable ::= <NOTEX> <LPAR> <VARIABLE> <RPAR>
| <VARIABLE>
| <BNODE>
notAtom ::= <NOT> <LPAR> comparisonAtom <RPAR>
isBlankAtom ::= <IS_BLANK> <LPAR> iObject <RPAR>
comparisonAtom ::= ( sameAsAtom | lessThanAtom | greaterThanAtom | differentFromAtom | notAtom | startsWithAtom | endsWithAtom | isBlankAtom )