#Function generate provenance graph of the system dataset. First step in creating provenance definition. 

sunset_provenance_dataset_system<-function(recipe,recipe_file, data) {

#Check if the user requested provenance
if (recipe$Analysis$Provenance) {
ref<-showDatasources()
Dataset.name = recipe$Analysis$Datasets$System$name #System name
isKnownDatasource <- ifelse(Dataset.name %in% ref$Name, TRUE, FALSE)
#Type.name<-tolower(recipe$Analysis$Horizon)

} else {
    stop("No provenance description requested for System")
         }

graph<-make_empty_graph(directed=TRUE)

#Check if the requested data is in the "SUNSET's repository. If it is, the provenance information is automatically appended"
if(isKnownDatasource) {
message("The Forecast System Dataset is in the internal reference table: all datasource metadata will be automatically appended")
metadata<-ref[ref$Name == Dataset.name, ]
Dataset.nodename=paste0("Dataset.",randomName())
Dataset.subclass<-metadata$Type
DataProvider<-metadata$Name
ModellingCenter<-metadata$Name
Project<-recipe$Description

# Dataset node ------------------
        graph <- my_add_vertices(graph,
                                 nv = 1,
                                 name = Dataset.nodename,
                                 label = Dataset.name,
                                 className = paste0("ds:", "Dataset")) #Need to define it according to metaclip ontology!!!!
                                 #className = paste0("ds:",metadata$Range,metadata$Type))
# Data Provider ------------------
DataProvider.nodename<-paste0("DataProvider.",randomName())
        if (length(DataProvider) > 1) stop("Only one DataProvider per Dataset is allowed", call. = FALSE)
        graph <- my_add_vertices(graph,
                                    name = DataProvider.nodename,
                                    label = DataProvider,
                                    className = "ds:DataProvider"
                                    #attr = list("ds:hasMainURL" = DataProvider.URL))
                                    )
        graph <- add_edges(graph, 
                            c(getNodeIndexbyName(graph, Dataset.nodename),
                                getNodeIndexbyName(graph, DataProvider.nodename)),
                            label = "ds:hadDataProvider")


# Modelling Center ------------------
#For now we assume only one Modelling Center 
ModellingCenter.nodename <-paste0("ModellingCenter.",randomName())
        graph <- my_add_vertices(graph,
                                        nv = 1,
                                        name = ModellingCenter.nodename,
                                        label = ModellingCenter,
                                        className = "ds:ModellingCenter")
        graph <- add_edges(graph, 
                            c(getNodeIndexbyName(graph, Dataset.nodename),
                            getNodeIndexbyName(graph, ModellingCenter.nodename)),
                            label = "ds:hadModellingCenter")
# Project ------------------
Project.nodename<- paste0("Project.",randomName())
        graph <- my_add_vertices(graph,
                                                nv = 1,
                                                name = Project.nodename ,
                                                label = "Project",
                                                className = "ds:Project",
                                                attr = list("dc:description" = Project))

        graph <- add_edges(graph, 
                            c(getNodeIndexbyName(graph, Dataset.nodename),
                            getNodeIndexbyName(graph, Project.nodename)),
                            label = "ds:hadProject")

# Command Call -----------
#if(!is.null(recipe_file)){
#graph <- metaclip.graph.Command(graph, "Loading", "2.0.0", "Loading.R", arg.list=list("Recipe", Recipe_path=recipe_file), Dataset.nodename)
#}

}
 else {
 print("Dataset not registered in the repository")
 stop()
     }

return(list("graph" = graph, "parentnodename" = Dataset.nodename))

}