% -*- prolog -*-

pcaSafe :-
    gpcaSR(Requirements),
    gpcaModel(Model),
    reflImpl(Implementation),
    intendedEnvironment(Environment),
    gpcaHazards(Hazards),
    hazardsMitigated(Requirements, Model, Implementation, Environment, Hazards).

hazardsMitigated(Requirements, Model, Implementation, Environment, Hazards) :-
    pcaParts(Requirements, Model, Implementation, Environment, Hazards).

pcaParts(Requirements, Model, Implementation, Environment, Hazards) :-
    pcaSW(Requirements, Model, Implementation, Environment, Hazards),
    pcaHW(Implementation, Environment, Hazards).

pcaSW(Requirements, Model, Implementation, Environment, Hazards) :-
    impSW(Model, Implementation),
    gpcaSafetyReq(Requirements, Model, Implementation, Environment),
    hazardsMitigated(Requirements, Hazards).

gpcaSafetyReq(Requirements, Model, Implementation, Environment) :-
    devProcess(Requirements, Model, Implementation),
    validation(Requirements, Implementation, Environment).

devProcess(Requirements, Model, Implementation) :-
    taModel(Model),
    taModelSR(Requirements, Model),
    devProcessSR(Model, Implementation).

taModelSR(Requirements, TAModel) :-
    formalizedRequirements(Requirements, FRequirements),
    verifyEach(FRequirements, TAModel).

verifyEach(FRequirements, TAModel) :-
    hd(FRequirements, Req),
    tl(FRequirements, Reqs),
    nil(Reqs),
    verify(Req, TAModel).
verifyEach(FRequirements, TAModel) :-
    hd(FRequirements, Req),
    tl(FRequirements, Reqs),
    cons(Reqs),
    verify(Req, TAModel),
    verifyEach(Reqs, TAModel).
    
devProcessSR(TAModel, Implementation) :-
    piSW(TAModel, ModelImplementation),
    glueCodeBeh(ModelImplementation, Implementation).

piSW(TAModel, ModelImplementation) :- synthesize(TAModel, ModelImplementation).

glueCodeBeh(ModelImplementation, Implementation) :-
    glueCode(ModelImplementation, GlueCode, Implementation),
    timeNotion(ModelImplementation, GlueCode, Implementation),
    envInterface(ModelImplementation, GlueCode, Implementation),
    platformInterface(ModelImplementation, GlueCode, Implementation).

%% validation(requirements, implementation, env).

validation(Requirements, Implementation, Environment) :-
    requirementCategories(Requirements, Categories),
    validateEach(Implementation, Environment, Categories).

validateEach(Implementation, Environment, Categories) :-
    hd(Categories, Category),
    tl(Categories, Cs),
    nil(Cs),
    validate(Implementation, Environment, Category).
validateEach(Implementation, Environment, Categories) :-
    hd(Categories, Category),
    tl(Categories, Cs),
    cons(Cs),
    validate(Implementation, Environment, Category),
    validateEach(Implementation, Environment, Cs).

% Define the data

gpcaSR(requirements).
gpcaModel(model).
reflImpl(implementation).
intendedEnvironment(env).
gpcaHazards(hazards).

% Stubs

impSW(model, implementation).

taModel(model).

formalizedRequirements(requirements, [fr1, fr2, fr3]).
verify(fr1, model).
verify(fr2, model).
verify(fr3, model).

synthesize(model, modelImpl).

glueCode(modelImpl, glueCode, implementation).
timeNotion(modelImpl, glueCode, implementation).
envInterface(modelImpl, glueCode, implementation).
platformInterface(modelImpl, glueCode, implementation).

requirementCategories(requirements, [sr_cat1, sr_cat2, sr_cat3, sr_cat4]).

validate(implementation, env, sr_cat1).
validate(implementation, env, sr_cat2).
validate(implementation, env, sr_cat3).
validate(implementation, env, sr_cat4).

hazardsMitigated(requirements, hazards).

pcaHW(implementation, env, hazards).
