cui-unige / outils-formels-modelisation

Cours de Bachelor : Outils Formels de Modélisation
20 stars 44 forks source link

Generic types... #23

Closed consolethinks closed 6 years ago

consolethinks commented 6 years ago

J'ai une probleme de conversion des types quand j'essaie d'appeler la fonction fireableBingings (c'est le nom definit dans PredicateNet au lieu de fireableBindings):

public func markingGraph(from marking: MarkingType) -> PredicateMarkingNode<T>? {

      var mark_init = PredicateMarkingNode<T>(marking: marking, successors: [:]) // initial marking
      var mark_todo = [mark_init] // Markings to do
      var mark_visited = [mark_init] // Markings already visited
      var curr_mark: PredicateMarkingNode<T> // The marking that is currently tested
      var curr_trans: MarkingType?
      var mark_next = mark_init // The resultant marking from curr_trans
      var curr_bindings = [Variable: T]()
      var if_end = true // For a single piece of code in the while loop, which should only be executed once

      while !(mark_todo.isEmpty) {
        curr_mark = mark_todo[0] // Test the first element for each iteration
        for transit in self.transitions {
          //print(transit)
          curr_bindings = transit.fireableBingings(from: curr_mark.marking)
          curr_trans = transit.fire(from: curr_mark.marking, with: curr_bindings) // Try the transition
          if (nil != curr_trans) { // If curr_trans is not nill then...
            //print(curr_trans!)
            mark_next = PredicateMarkingNode<T>(marking: curr_trans!, successors: [:]) // ...store the resulting marking
            if (mark_visited.contains(where: { PredicateNet.greater( $0.marking, mark_next.marking) })) {
              print("Unbounded model")
              return nil
            }
            if !(mark_visited.contains(where: { PredicateNet.equals($0.marking, mark_next.marking) })) { // if the marking is not visited yet
              curr_mark.successors.updateValue(mark_next, forKey: transit) // add marking as successor
              mark_todo.append(mark_next) // Add marking to the waiting list
              mark_visited.append(mark_next) // Add marking to the list of visited ones
            } else { // Otherwise...
              curr_mark.successors.updateValue(mark_next, forKey: transit) // ...add marking as successor
            }
          }
        }
        if (if_end) { // Only run this part of the code ONCE
          if_end = false
          mark_init = curr_mark // Modify the mark_init to contain the successors
        }
        mark_todo.remove(at: 0) // all tests finished for curr_mark, remove it from the waiting list
      }
      return mark_init // Returning initial marking with successors
        // Write your code here ...

        // Note that I created the two static methods `equals(_:_:)` and `greater(_:_:)` to help
        // you compare predicate markings. You can use them as the following:
        //
        //     PredicateNet.equals(someMarking, someOtherMarking)
        //     PredicateNet.greater(someMarking, someOtherMarking)
        //
        // You may use these methods to check if you've already visited a marking, or if the model
        // is unbounded.
    }

Comme c'est la premiere fois que j'utilise des "Generics", c'est probablement la raison pourquoi je n'arrive pas corriger les erreurs.

L'erreur donne par le compilateur (il y a des autres erreurs de meme type):

Compile Swift Module 'PhilosophersLib' (5 sources) /home/reedwolf/projects/outils-formels-modelisation/tp-04/Philosophers/Sources/PhilosophersLib/PredicateNet+MarkingGraph.swift:19:68: error: cannot convert value of type '[PredicateNet.PlaceType : [T]]' (aka 'Dictionary<String, Array>') to expected argument type '[PredicateNet.PlaceType : []]' (aka 'Dictionary<String, Array<>>') curr_bindings = transit.fireableBingings(from: curr_mark.marking)


as! [PredicateNet.PlaceType : [_]]
/home/reedwolf/projects/outils-formels-modelisation/tp-04/Philosophers/Sources/PhilosophersLib/PredicateNet+MarkingGraph.swift:29:48: error: cannot convert value of type 'PredicateMarkingNode<T>' to expected argument type 'PredicateBindingMap<_>'
curr_mark.successors.updateValue(mark_next, forKey: transit) // add marking as successor
^~~~~~~~~
/home/reedwolf/projects/outils-formels-modelisation/tp-04/Philosophers/Sources/PhilosophersLib/PredicateNet+MarkingGraph.swift:33:48: error: cannot convert value of type 'PredicateMarkingNode<T>' to expected argument type 'PredicateBindingMap<_>'
curr_mark.successors.updateValue(mark_next, forKey: transit) // ...add marking as successor
^~~~~~~~~
error: terminated(1): /opt/swift/swift-4.0-RELEASE-ubuntu16.10/usr/bin/swift-build-tool -f /home/reedwolf/projects/outils-formels-modelisation/tp-04/Philosophers/.build/debug.yaml main
kyouko-taiga commented 6 years ago

Le type de curr_bindings est incorrect, car PredicateTransition.fireableBingings(from:) retourne un tableau de bindings. Le type correct est [PredicateTransition<T>.Binding] (aka [[Variable: T]).

Il conviendra ensuite d'itérer sur ce tableau de bindings pour identifier les successeurs obtenus par le tir de la transition.