Highly flexible manufacturing systems require continuous run-time (self-) optimization of processes with respect to various parameters, e.g. efficiency, availability, energy consumption etc. A promising approach for achieving (self-) optimization in manufacturing systems is the usage of the context sensitivity approach. Thereby the Cyber-Physical Systems play an important role as sources of information to achieve context sensitivity. In this paper it is demonstrated how context sensitivity can be used to realize a holistic solution for (self-) optimization of discrete flexible manufacturing systems, by making use of Cyber-Physical System integrated in manufacturing systems/processes. A generic approach for context sensitivity, based on self-learning algorithms, is proposed aiming at a various manufacturing systems. The new solution is propos encompassing run-time context extractor and optimizer. Based on the self-learning module both context extraction and optimizer are continuously learning and improving their performance. The solution is following Service Oriented Architecture principles. The generic solution is developed and then applied to two very different manufacturing processes. This paper proposes a holistic solution to achieve context sensitivity for Flexible Manufacturing Systems, whereby the knowledge created by applying the context sensitivity approach can be used for (self-) optimization of manufacturing processes.