;; This is an SMTLib specification to verify that a subgraph of
;; the avionics architecture specification (written in AADL) is
;; subgraph isomorphic to the solid model (written in Creo).  For
;; example, every logical processor should have a mapping to
;; a physical card in the solid model.

(FUSED-subscribe AIRCRAFT)

(FUSED-subscribe UAV)

;; ASSERT INTER-MODEL CONSISTENCY CONDITION

;; The following specifies that there be a subgraph isomorphism of the
;; processor and bus subgraph in the logical avionics model into a
;; solid model subgraph of cards and cables.  This would be written by
;; a system engineer, or more probably someone skilled in FUSED and
;; SMT-LIB who would help write such complex consistency relations.
;; Note that, once specified, this consistency specification should be
;; fairly robust to changes in the models.  It would not need to be
;; tweaked as engineers tweak the specs, in fact it would hopefully be
;; fairly reusable across projects.

;; Specify the subgraph in the avionics model to be mapped.  This
;; is done here by specifying the types of objects that must have a
;; mapping to solid model objects.
(define-fun avionicsSubgraphObject ((ae UAV.Object)) Bool
   (or
       (UAV.ofType ae UAV.HardwarePlatform.PfpCPU.Fast)
       (UAV.ofType ae UAV.Communications.Radio)
       (UAV.ofType ae UAV.MotorControl.Motor)
       (UAV.ofType ae UAV.SensorData.IMU)
       (UAV.ofType ae UAV.SensorData.GPS)
       (UAV.ofType ae UAV.PayloadControl.Payload)
       (UAV.ofType ae UAV.ActuatorControl.Servo)   ;; Uncomment to introduce an inconsistency                                  
))

;; Specify allowed mappings from avionics objects to solid objects.
;; This is done here by specifying which types of avionics objects
;; are compatible with (may be mapped to) which types of solid objects.
(define-fun typeCompatible ((ae UAV.Object) (se AIRCRAFT.Object)) Bool
    (or 
       (and (UAV.ofType ae UAV.HardwarePlatform.PfpCPU.Fast) (AIRCRAFT.ofType se AIRCRAFT.Board))
       (and (UAV.ofType ae UAV.Communications.Radio) (AIRCRAFT.ofType se AIRCRAFT.Board))
       (and (UAV.ofType ae UAV.MotorControl.Motor) (AIRCRAFT.ofType se AIRCRAFT.Motor))
       (and (UAV.ofType ae UAV.SensorData.IMU) (AIRCRAFT.ofType se AIRCRAFT.Board))
       (and (UAV.ofType ae UAV.SensorData.GPS) (AIRCRAFT.ofType se AIRCRAFT.Board))
       (and (UAV.ofType ae UAV.PayloadControl.Payload) (AIRCRAFT.ofType se AIRCRAFT.Camera))                            
    ))

;; The following asserts that there exists at least one subgraph
;; isomorphism from the avionics subgraph selected by avionicsSubgraphObject
;; into the solid model graph.  This is probably a general-purpose assertion
;; that can be reused, the model-specific conditions are captured in
;; the above functions.

;; One condition that should be (easily) added is the ability for the
;; system engineer to explicitly specify a small number of seed
;; mappings, e.g. "avionics.mainProcessor maps to solid.mainCircuitCard".

;; The mapping from nodes in the above-specified avionics subgraph
;; to a solid model subgraph is defined by an array "map" whose index
;; is avionics objects and whose values are solid model objects.
;; The following condition essentially says there exists such a map
;; array that defines a subgraph isomorphism and satisfies the
;; above-defined object-to-object compatibility condition.
(assert
   (exists ((map (Array UAV.Object AIRCRAFT.Object)))
       (forall ((ae UAV.Object))
	   (=>  (avionicsSubgraphObject ae)
		(and
		   ; the map is 1-to-1, namely:
           ; if A maps to A' and B maps to A' then A = B
		   (forall ((aeB UAV.Object))
		       (=> (and (avionicsSubgraphObject aeB) (= (select map ae) (select map aeB)))
			   (= ae aeB)))
		   ; if A maps to A' and A attaches to B and B is in the subgraph
           ; then B maps to B' and A' attaches to B'
		   (let ((se (select map ae)))
		       (forall ((aeB UAV.Object))
			   (=> (and (UAV.attached ae aeB UAV.connection) (avionicsSubgraphObject aeB))  ;; HARD-CODED EDGE TYPE
			       (AIRCRAFT.attached se (select map aeB) AIRCRAFT.Containment))))   ;; HARD-CODED EDGE TYPE
		   ; if A maps to A' 
           ; then class of A is compatible with class of A'
		   (typeCompatible ae (select map ae)))))))

(check-sat)
(model)