@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix vim4: <http://bipm.org/jcgm/vim4#> .

@prefix comp: <http://ua.be/sdo2l/vocabulary/component#> .
@prefix base: <http://ua.be/sdo2l/vocabulary/base#> .

# The namespace for fse shacl shapes.
@prefix comp-shapes: <http://ua.be/sdo2l/vocabulary/component-shapes#> .

# The following defines `fse-shapes` as the ontology for sh:declare prefixes to be added to each SPARQL query.
comp-shapes:
	a owl:Ontology ;
	owl:imports sh: ;
	sh:declare [
		sh:prefix "comp" ;
		sh:namespace "http://ua.be/sdo2l/vocabulary/component#"^^xsd:anyURI ;
	]  ;
	sh:declare [
		sh:prefix "base" ;
		sh:namespace "http://ua.be/sdo2l/vocabulary/base#"^^xsd:anyURI ;
	]  ;
	sh:declare [
		sh:prefix "rdf" ;
		sh:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#"^^xsd:anyURI ;
	] ;
	sh:declare [
		sh:prefix "vim4" ;
		sh:namespace "http://bipm.org/jcgm/vim4#"^^xsd:anyURI ;
	] .

comp-shapes:Car_contains_4_Wheels_shacl
	a sh:NodeShape ;
	sh:targetClass comp:CarComponent ;
	sh:property [
		sh:path base:contains ;
		sh:class comp:WheelComponent ;
		sh:nodeKind sh:IRI ;
		sh:minCount 4 ;
		sh:maxCount 4 ;
	] .
	
comp-shapes:Unbalanced_Wheel_Mass
	a sh:NodeShape ;
	sh:targetClass comp:Attribute ;
	sh:property [
    	sh:path vim4:hasDoubleNumber ;
    	sh:severity sh:Warning ;
    	sh:sparql [
      	a sh:SPARQLConstraint ;
      	sh:message "Wheel mass difference from the average should be less than 5%" ;
      	sh:prefixes [
      		sh:declare [
				sh:prefix "comp" ;
				sh:namespace "http://ua.be/sdo2l/vocabulary/component#"^^xsd:anyURI ;
				] ] ,
	 		[ sh:declare [
				sh:prefix "base" ;
				sh:namespace "http://ua.be/sdo2l/vocabulary/base#"^^xsd:anyURI ;
			] ]  ,
			[ sh:declare [
				sh:prefix "rdf" ;
				sh:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#"^^xsd:anyURI ;
			] ] ,
			[ sh:declare [
				sh:prefix "vim4" ;
				sh:namespace "http://bipm.org/jcgm/vim4#"^^xsd:anyURI ;
			] ];
      	sh:select """
       		SELECT ?car ?wheel $this ?attMass ?avg
			WHERE {
				?car rdf:type comp:CarComponent .
				?wheel rdf:type comp:WheelComponent .
				?car base:contains ?wheel .
				$this rdf:type comp:Attribute .
				$this vim4:characterizes ?wheel .
				$this base:hasIdentifier ?attID .
			    FILTER regex(?attID, "w[0-9]-mass") .
				$this $PATH ?attMass .
				{
				SELECT ?car (avg(?attMass2) as ?avg)
				WHERE {
					?car rdf:type comp:CarComponent .
					?wheel2 rdf:type comp:WheelComponent .
					?car base:contains ?wheel2 .
					?attribute2 rdf:type comp:Attribute .
					?attribute2 vim4:characterizes ?wheel2 .
					?attribute2 base:hasIdentifier ?attID2 .
			    	FILTER regex(?attID2, "w[0-9]-mass") .
					?attribute2 vim4:hasDoubleNumber ?attMass2 .
				}
				GROUP BY ?car
				}
			}
			HAVING (abs(?attMass-?avg) >= (?avg*0.05) )
        	"""
    	]
  	] ;
	sh:closed false .
