From c595b5d368ae87c0bcd3163f15eb9f773cc3c2f1 Mon Sep 17 00:00:00 2001
From: selhattech <salma.elhattech@hotmail.com>
Date: Mon, 8 Mar 2021 11:17:51 +0100
Subject: [PATCH] Modification of model.ml and model.mli files (actually
 recognize nnet format)

---
 model.ml  | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 model.mli |  17 +++++-
 2 files changed, 168 insertions(+), 6 deletions(-)

diff --git a/model.ml b/model.ml
index e13dbcc..bc17b95 100644
--- a/model.ml
+++ b/model.ml
@@ -4,6 +4,13 @@
 (*                                                                        *)
 (**************************************************************************)
 
+(******************************)
+(*This program verifies whether a file is a nnet file or not*)
+(******************************)
+
+
+(*This section verifies if the file has an nnet extension before verifying its content*)
+
 type format = Onnx | Nnet [@@deriving show { with_path = false}]
 
 type t = {
@@ -11,14 +18,154 @@ type t = {
   filename: string;
 }
 
+let is_nnet = ref 0;;
+
 let build ~filename =
   if Sys.file_exists filename
   then
     let format =
       if Filename.check_suffix filename "nnet"
-      then Nnet
-      else Onnx
-    in
-    Ok { format; filename }
+      then begin
+       is_nnet := 1;
+       Nnet
+      end 
+      else begin
+       is_nnet := 0;
+       print_endline "This is not an nnet file -extension";
+       Onnx
+      end
+    in 
+    print_string "";
+
   else
-    Error (Format.sprintf "No such file `%s'." filename)
+    Format.printf "The file: %s doesn't exist\n" filename;
+;;
+
+let flag = ref 0;;
+(*The type "record" is created to collect some parameters of the nnet file *)
+type record = {
+    number_of_layers : int;
+    number_of_inputs: int;
+    number_of_outputs:int; 
+    maximum_layer_size:int
+    };;
+
+
+(******************************)
+(*This function helps verify whether some conditions are satisfied to determine 
+whether the file has a nnet format.
+The conditions are : 
+- The file starts with header lines, it can be any number of lines so long as they begin with "//"
+- The following line contains four values: Number of layers, number of inputs, number of outputs, and maximum layer size
+- The number of inputs in that line (second element) is equal to that of the following line (first element) 
+- The flag line contains only one element
+- The minimum values line contains as many elements as there are inputs
+- The maximum values line contains as many elements as there are inputs
+- The mean values line contains as many elements as there are inputs plus an extra value for all outputs
+- The range values line contains as many elements as there are inputs plus an extra value for all outputs
+*)
+(******************************)
+
+
+let content_file filename = 
+    if !is_nnet = 1 then begin 
+    let in_channel = open_in filename in 
+    let c = ref 0 in 
+    let flag1 = ref 0 in 
+    let flag2 = ref 0 in 
+    let flag3 = ref 0 in 
+    let flag4 = ref 0 in 
+    let flag5 = ref 0 in 
+    let element1 = ref "" in
+    let element2 = ref "" in
+    let a = ref 0 in  
+    let cnt = ref 0 in 
+    try
+        while true do 
+            let line = input_line in_channel in 
+                cnt := !cnt+1;
+                (*Verifying the first condition*)
+                let cmp = Str.string_match (Str.regexp "//") line 0 in
+                    if cmp then begin
+                        flag := !flag +1;
+                        a := !flag;
+                    end
+                    else 
+                        try 
+                             (*Verifying the second condition*)
+                            let b = Str.split (Str.regexp ",") line in 
+                            let tmp= List.nth b 1 in 
+                            let num_layers = List.nth b 0 in 
+                            let num_outputs = List.nth b 2 in 
+                            let max_layer_size = List.nth b 3 in 
+                            element2 := tmp;
+                            let record2 = {number_of_layers = int_of_string(num_layers); number_of_inputs=int_of_string !element2;number_of_outputs=int_of_string num_outputs;maximum_layer_size=int_of_string max_layer_size} in 
+                            Format.printf "Number of layers: %d\n" record2.number_of_layers;
+                            Format.printf "Number of inputs: %d\n" record2.number_of_inputs;
+                            Format.printf "Number of outputs: %d\n" record2.number_of_outputs;
+                            Format.printf "Maximum layer size : %d\n" record2.maximum_layer_size ;
+                            c := List.length b; 
+                            raise Exit;
+                        with Exit -> 
+                            let line2 = input_line in_channel in 
+                            let d = Str.split (Str.regexp ",") line2 in 
+                            let tmp2 = List.nth d 0 in
+                            element1 := tmp2; 
+                        try
+                           (*Verifying the fourth condition using variable flag1*)
+                           let line3 = input_line in_channel in 
+                           let e = Str.split (Str.regexp ",") line3 in 
+                           flag1 := List.length e; 
+                           raise Exit;
+                        with Exit ->
+                            print_newline();
+                        try
+                           (*Verifying the fifth condition using variable flag2*)
+                           let line4 = input_line in_channel in 
+                           let f = Str.split (Str.regexp ",") line4 in 
+                           flag2 := List.length f; 
+                           raise Exit;
+                        with Exit ->
+                            (*Verifying the sixth condition using variable flag3*)
+                            let line5 = input_line in_channel in 
+                            let g = Str.split (Str.regexp ",") line5 in 
+                            flag3 := List.length g; 
+                        try
+                           (*Verifying the seventh condition using variable flag4*)
+                           let line6 = input_line in_channel in 
+                           let h = Str.split (Str.regexp ",") line6 in 
+                           flag4 := List.length h; 
+                           raise Exit;
+                        with Exit ->
+                            (*Verifying the eighth condition using variable flag5*)
+                            let line7 = input_line in_channel in 
+                            let i = Str.split (Str.regexp ",") line7 in 
+                            flag5 := List.length i; 
+                            raise End_of_file;
+        done
+    with End_of_file ->
+        if !flag = 0 then print_endline "not an nnet file"
+        else 
+            (*Verifying the third condition using the variable t*)
+            let t = compare element1 element2 in 
+            (*Verifying all of the conditions using the if branch*)
+            if (!c = 4 && t=0 && !flag1 = 1 && flag2=ref(int_of_string !element2) && flag3=ref(int_of_string !element2) && flag4=ref((int_of_string !element2)+1) && flag5=ref((int_of_string !element2)+1)) then
+                print_endline "nnet file"
+            else print_endline "it is not an nnet file";
+    close_in in_channel;
+    end 
+;;
+
+let my_fun filename = 
+        build filename;
+        content_file filename;    
+;;
+
+(*This command is used to create an interface with the user such that they can insert the name of the file
+to be tested.
+In order to compile the code, use : 
+ocamlfind ocamlopt -package str -o (name_of_the_executable) (name_of_the_program.ml) -linkpkg 
+To run the executable, use : 
+./(name_of_the_executable) (name_of_the_nnet_file.nnet) *)
+
+my_fun Sys.argv.(1);;
diff --git a/model.mli b/model.mli
index 45687d8..f7294cc 100644
--- a/model.mli
+++ b/model.mli
@@ -11,8 +11,23 @@ type t = private {
   filename: string;
 }
 
+type record = {
+    number_of_layers : int;
+    number_of_inputs: int;
+    number_of_outputs:int; 
+    maximum_layer_size:int;
+    }
+
 (** Builds a model out of the given [filename], if possible.
 
     The model is inferred from the [filename] extension.
 *)
-val build: filename:string -> (t, string) Result.t
+val build: filename:string -> t -> unit
+
+(** Verifies the content of the given [filename] and checks 
+    if the conditions are satisfied.
+*)
+val content_file: filename:string -> t -> unit
+
+(** The main function of the program *)
+val my_fun: filename:string -> t -> unit
-- 
GitLab