मैं असहमत हूं कि एफएसएम लागू करने के लिए तुच्छ हैं। यह बहुत छोटा दिखता है, और या तो दिखाता है विकल्प के साथ परिचित की कमी, या जटिल राज्य मशीनों के साथ अनुभव की कमी।
मौलिक समस्या यह है कि एक राज्य मशीन ग्राफ स्पष्ट है, लेकिन FSM कोड नहीं है। एक बार जब आप एक दर्जन से अधिक राज्यों से परे मिल और संक्रमण का स्कोर, एफएसएम कोड बदसूरत हो जाता है और पालन करना मुश्किल होता है।
थर ई उपकरण हैं जिनके द्वारा आप ड्रा राज्य मशीन, और इसके लिए जावा कोड उत्पन्न करते हैं। हालांकि, इसके लिए मुझे किसी भी ओपन सोर्स टूल के बारे में पता नहीं है।
अब, एरलांग/स्कैला वापस लौटने के बाद, स्कैला में अभिनेता और संदेश भी पास हो रहा है, और यह जेवीएम पर आधारित है, इसलिए यह आपकी बाधाओं के कारण एरलांग से बेहतर विकल्प हो सकता है।
स्कैला पर भी एक डीएफए/एनएफए लाइब्रेरी है, हालांकि यह विशेष रूप से एक अच्छा नहीं है। यह डीएफए/एनएफए में मनमाने ढंग से नियमित अभिव्यक्तियों (यानी, शाब्दिक पात्रों की आवश्यकता नहीं है) से रूपांतरण का समर्थन करता है।
मैं इसका उपयोग कर नीचे कुछ कोड पोस्ट करूंगा।इस कोड में, विचार एक एफएसएम बना रहा है जो शब्दों की सूची के लिए मनमाने ढंग से उपसर्गों के अनुक्रमिक संयोजन को स्वीकार करेगा, विचार पूर्वनिर्धारित कीबाइंड के बिना मेनू विकल्पों को देख रहा है।
import scala.util.regexp._
import scala.util.automata._
// The goal of this object below is to create a class, MyChar, which will
// be the domain of the tokens used for transitions in the DFA. They could
// be integers, enumerations or even a set of case classes and objects. For
// this particular code, it's just Char.
object MyLang extends WordExp {
type _regexpT = RegExp
type _labelT = MyChar
case class MyChar(c:Char) extends Label
}
// We now need to import the types we defined, as well as any classes we
// created extending Label.
import MyLang._
// We also need an instance (singleton, in this case) of WordBerrySethi,
// which will convert the regular expression into an automatum. Notice the
// language being used is MyLang.
object MyBerrySethi extends WordBerrySethi {
override val lang = MyLang
}
// Last, a function which takes an input in the language we defined,
// and traverses the DFA, returning whether we are at a sink state or
// not. For other uses it will probably make more sense to test against
// both sink states and final states.
def matchDet(pat: DetWordAutom[MyChar], seq: Seq[Char]): Boolean =
!pat.isSink((0 /: seq) ((state, c) => pat.next(state, MyChar(c))))
// This converts a regular expression to a DFA, with using an intermediary NFA
def compile(pat: MyLang._regexpT) =
new SubsetConstruction(MyBerrySethi.automatonFrom(pat, 100000)).determinize
// Defines a "?" function, since it isn't provided by the library
def Quest(rs: _regexpT*) = Alt(Eps, Sequ(rs: _*)) // Quest(pat) = Eps|pat = (pat)?
// And now, the algorithm proper. It splits the string into words
// converts each character into Letter[MyChar[Char]],
// produce the regular expression desired for each word using Quest and Sequ,
// then the final regular expression by using Sequ with each subexpression.
def words(s : String) = s.split("\\W+")
def wordToRegex(w : String) : Seq[MyLang._regexpT] = w.map(c => Letter(MyChar(c)))
def wordRegex(w : String) = Quest(wordToRegex(w) reduceRight ((a,b) => Sequ(a, Quest(b))))
def phraseRegex(s : String) = Sequ(words(s).map(w => wordRegex(w)) : _*)
// This takes a list of strings, produce a DFA for each, and returns a list of
// of tuples formed by DFA and string.
def regexList(l : List[String]) = l.map(s => compile(phraseRegex(s)) -> s)
// The main function takes a list of strings, and returns a function that will
// traverse each DFA, and return all strings associated with DFAs that did not
// end up in a sink state.
def regexSearcher(l : List[String]) = {
val r = regexList(l)
(s : String) => r.filter(t => matchDet(t._1, s)).map(_._2)
}
अपने आवश्यक वस्तुएँ क्या _are_? और आपकी बाधाएं? –
अब मैं फ्लाइंग स्पेगेटी राक्षस की मानसिक छवि के साथ अटक गया हूं। संदेश पासिंग को नूडली एपेंडेज में लागू किया जा सकता है। –