PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vim4: <http://bipm.org/jcgm/vim4#>
PREFIX comp: <http://ua.be/sdo2l/vocabulary/component#>
PREFIX base: <http://ua.be/sdo2l/vocabulary/base#>


SELECT ?car ?wheel ?attribute ?attMass ?avg
WHERE {
	?car rdf:type comp:CarComponent .
	?wheel rdf:type comp:WheelComponent .
	?car base:contains ?wheel .
	?attribute rdf:type comp:Attribute .
	?attribute vim4:characterizes ?wheel .
	?attribute base:hasIdentifier ?attID .
    FILTER regex(?attID, "w[0-9]-mass") .
	?attribute vim4:hasDoubleNumber ?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) )

