Commit 5ac7c0b1 authored by David Gnabasik's avatar David Gnabasik
Browse files

Worked on ShaclValidationManager.

parent b8d673f1
Loading
Loading
Loading
Loading
+3 −11
Original line number Diff line number Diff line
@@ -122,20 +122,12 @@ public class Clause_9_4_6_Checker extends AbstractShaclChecker {
	 If the ontology in the ontology document of a SAREF project version imports an {Ontology | Vocabulary | Pattern} Version IRI defined in
	 another SAREF project version, either directly or transitively, then it shall conform to the SHACL specification of each
	 reference ontology pattern defined in this other SAREF project version.
	 This method calls the configured docker image that runs the common shacl files.
	 This method calls the generic docker image that runs a specific shacl files.
	 * */
	protected final void validateOntologyWithShacl(String ontologyToValidate) {
		try {
			HttpResponse<String> response = new ShaclValidationManager(ontologyToValidate).validateOntologyWithShacl();
		} catch (IOException | InterruptedException e) {
			logError(getMessage(Clause_9_4_6_Checker.MESSAGE.ioexception));
		}
	}

	// This method calls the generic docker image that runs a specific shacl files.
	protected final void validateOntologyPerShacl(String ontologyToValidate, String shaclFile) {
		//<<< process every file
		try {
			HttpResponse<String> response = new ShaclValidationManager(ontologyToValidate).validateOntologyWithShacl();
			HttpResponse<String> response = new ShaclValidationManager(ontologyToValidate).validateOntologyWithShacl(shaclFile);
		} catch (IOException | InterruptedException e) {
			logError(getMessage(Clause_9_4_6_Checker.MESSAGE.ioexception));
		}
+45 −76
Original line number Diff line number Diff line
@@ -38,10 +38,17 @@ import com.google.gson.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
//import fr.mines_stetienne.ci.saref.SAREFErrorLogger;

/*
	Docs for RDF validator: https://www.itb.ec.europa.eu/docs/guides/latest/validatingRDF/index.html
	Base docker image: https://hub.docker.com/r/isaitb/shacl-validator
	Use Swagger UI for testing: http://localhost:8080/shacl/swagger-ui/index.html#/{domain}/api/validate
	BASE64 Encoder/Decoder: https://www.base64encode.org/
    docker run -d --name SAREFvalidator -p 8080:8080 local/ontologyvalidator    # this runs the general validator.
* */
public class ShaclValidationManager {

	private final static String BASE_URL = "http://localhost:8080/shacl/";  // <<<
	private String ontologyToValidate;
	private String ontologyToValidate;	// file:///home/davidgnabasik/dev/real_saref4auto/ontology/saref4auto.ttl or URL
	private final HttpClient client;
	private final ObjectMapper objectMapper;

@@ -56,14 +63,21 @@ public class ShaclValidationManager {
	// The full list of available tags: https://www.itb.ec.europa.eu/docs/guides/latest/validatingRDF/
	// Replace default values for {contentToValidate, validationType}
	private String shaclRequestBody = "{ "+	// generic validator option
		"'contentToValidate': 'string', "+	// entire content of ontology file (or URI)
		"'embeddingMethod': 'STRING', "+  	// {STRING,URL,BASE64}
		"'contentToValidate': 'RDF_CONTENT_AS_BASE64_ENCODED_STRING', "+	// entire content of ontology file (or URI)
		"'embeddingMethod': 'BASE64', "+  	// {STRING,URL,BASE64}
		"'contentSyntax': 'text/turtle', "+
		"'validationType': 'string', "+	// 	Clause_9_4_3_1-3.ttl, Clause_9_4_4_2.ttl, Clause_9_6_3.ttl (for configured validator)
		"'reportSyntax': 'text/turtle', "+
		"'validationType': 'string', "+
		"'reportSyntax': 'application/json', "+
		"'locale': 'en', "+
		"'rdfReportSyntax': 'application/json' "+
		"}";
					/* "contentToValidate": "",
				"contentSyntax": "text/turtle",
				"embeddingMethod": "BASE64",
				"validationType": "Clause_9_4_3_1",
				"reportSyntax": "application/json",
				"locale": "en" */

	JsonObject jsonObject = new JsonParser().parse(shaclRequestBody).getAsJsonObject();

	/**
@@ -72,7 +86,7 @@ public class ShaclValidationManager {
	public ShaclValidationManager(String ontologyToValidate) throws IOException {
		client = HttpClient.newHttpClient();
		objectMapper = new ObjectMapper();
		this.ontologyToValidate = ontologyToValidate;	//<<< test for file presence.
		this.ontologyToValidate = ontologyToValidate;	//<<< test for ontology file presence.
	}

	public static HttpRequest.BodyPublisher ofForm(Map<String, String> data) {
@@ -90,26 +104,41 @@ public class ShaclValidationManager {

	/**
	 * POST request to Validate a single RDF instance: {domain}/api/validate/{requestBody}
	 <<< This method uses the validator configured to read the Clause_9_4_3_1-3.ttl, Clause_9_4_4_2.ttl, Clause_9_6_3.ttl shacl files.
	 * Response: HTTP code 200 for successful validation as JSON.
	 <<< This method uses the validator configured to read the Clause_9_4_3_1_3.ttl, Clause_9_4_4_2.ttl, Clause_9_6_3.ttl shacl files.
	 * Response: HTTP code 200 for successful validation. POST JSON return report format:
	 {
	 "date": "2024-04-05T10:18:24.197+0000",
	 "result": "SUCCESS",
	 "overview": {
	 "profileID": "Clause_9_4_3_1"
	 },
	 "counters": {
	 "nrOfAssertions": 0,
	 "nrOfErrors": 0,
	 "nrOfWarnings": 0
	 },
	 "context": {},
	 "reports": {},
	 "name": "SHACL Validation"
	 }
	 */
	public HttpResponse<String> validateOntologyWithShacl()
	public HttpResponse<String> validateOntologyWithShacl(String shaclFile)
			throws IOException, InterruptedException {
		Map<String, String> data = new HashMap<>();
		data = objectMapper.readValue(shaclRequestBody, HashMap.class);
		data.put("contentToValidate", ontologyToValidate);	// file:///home/davidgnabasik/dev/real_saref4auto/ontology/saref4auto.ttl or URL
		data.put("validationType", validationType);
		data.put("contentToValidate", ontologyToValidate);
		//data.put("validationType", validationType);
		System.out.println("Map is: "+data);//<<<
				/* "contentToValidate": "RDF_CONTENT_AS_BASE64_ENCODED_STRING",
				"contentSyntax": "text/turtle",
				"embeddingMethod": "STRING",
				"embeddingMethod": "BASE64",
				"validationType": "Clause_9_4_3_1",
				"reportSyntax": "application/json",
				"locale": "en" */

		HttpRequest request = HttpRequest.newBuilder()
				.header("Content-Type", "text/turtle")	// application/x-www-form-urlencoded
				.uri(URI.create(BASE_URL + validationType + "/api/validate" ))
				.uri(URI.create(BASE_URL + "/api/validate" ))
				.POST(ofForm(data))
				.build();

@@ -121,17 +150,16 @@ public class ShaclValidationManager {
	}

	/**
	 * If shaclDomain is empty, return info for all defined domains.
	 * If shaclDomain is empty, return info for all defined domains. http://localhost:8080/shacl/api/info
	 * Not needed since a configured validator is not used -- the Clause_9_*.ttl files are augmented (parameterized) so they are only partially useful.
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public HttpResponse<String> getShaclDomainInfo(String shaclDomain) throws IOException, InterruptedException {
	public HttpResponse<String> getShaclDomainInfo() throws IOException, InterruptedException {
		Map<String, String> data = new HashMap<>();
		data = objectMapper.readValue(shaclRequestBody, HashMap.class);
		data.put("validationType", shaclDomain);	// {Ontology,Commodity}}

		String url = BASE_URL + "shacl/api/info";
		if (shaclDomain.length() > 0)	 url = BASE_URL + "shacl/" + shaclDomain + "/api/info";

		HttpRequest request = HttpRequest.newBuilder()
				.header("Accept", "text/turtle")
@@ -147,62 +175,3 @@ public class ShaclValidationManager {
		return response;
	}
}
/*
[
		{
		"domain": "Commodity",
		"validationTypes": [
		{
		"type": "Commodity",
		"description": "Commodity"
		}
		]
		},
		{
		"domain": "Ontology",
		"validationTypes": [
		{
		"type": "Clause_9_4_3_1",
		"description": "Clause_9_4_3_1"
		},
		{
		"type": "Clause_9_4_3_2",
		"description": "Clause_9_4_3_2"
		},
		{
		"type": "Clause_9_4_3_3",
		"description": "Clause_9_4_3_3"
		},
		{
		"type": "Clause_9_4_3_4",
		"description": "Clause_9_4_3_4"
		},
		{
		"type": "Clause_9_6_3",
		"description": "Clause_9_6_3"
		},
		{
		"type": "ontologyPerson",
		"description": "ontologyPerson"
		}
		]
		}
		]
		*/
/* POST JSON report format:
{
  "date": "2024-04-05T10:18:24.197+0000",
  "result": "SUCCESS",
  "overview": {
    "profileID": "Clause_9_4_3_1"
  },
  "counters": {
    "nrOfAssertions": 0,
    "nrOfErrors": 0,
    "nrOfWarnings": 0
  },
  "context": {},
  "reports": {},
  "name": "SHACL Validation"
}
 */