Bonsoir,
J'ai le même problème que ALIAS200 : je n'arrive pas à programmer en Java un automate à états finis avec des règles XML.
Merci d'avance pour votre aide.
Version imprimable
Bonsoir,
J'ai le même problème que ALIAS200 : je n'arrive pas à programmer en Java un automate à états finis avec des règles XML.
Merci d'avance pour votre aide.
Salut,
Il va falloir nous en dire plus. Qu'as-tu déjà fait (code...) ? Qu'est-ce qui te bloque exactement ?
j'explique mon automate est une boite noir a pour entre des tag xml et doit fournir en sortie une regle.
donc il recupere les tag xml qui sont les composants de la regle et enfin selon sa trajectoire il fournit une regle. j'espere que j'etais un peu clair.
j'ai pense qe ses transitions sont tag xml mais comment le programmer en java pour dire pour une telle transition je ferais les actions telle et telle
Merci pour vos aides .
Ce n'est pas ce qu'est un automate fini que tu dois nous expliquer, mais ce que, toi, tu n'arrives pas à faire (C'est la lecture du xml qui te pose problème ?) et ce que tu as déjà fait (quel est ton modèle par exemple).
En tout cas, on ne te donnera pas ici un code tout fait qui fait ce que tu cherches à faire.
je n'arrive pas à faire l'automate à partir des règles xml puisque je suit débutant, et même le programme jusque un coup de main.
Déjà, commence par lire les règles et les charger en mémoire. Quel est exactement le xml ? Regarde ce xml, détermine les informations qu'il contient, créé une classe qui permet de les représenter en mémoire (modélise...). A partir du moment où tu auras une liste de règles en mémoire, tu pourras déjà déterminer la liste des états. Ensuite, le principe est de générer les transitions qui passent d'un état à un autre en fonction de ces règles : donc, à commencer par sélectionner la règle en fonction d'un état connu. Le reste devrait venir assez naturellement.
Salut,
J'ai fait un petit effort , est-ce que je suis sur la bonne voie ou non ? Merci à vous.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 Bouton sur la réponse aux incidents */ package com.kw.lex; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JFileChooser; import com.kw.lex.Mainframe; import com.kw.lex.WordAnaly; import com.kw.lex.ReadFile; public class Action implements ActionListener{ public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("start")) { new WordAnaly(Mainframe.getString(),"src/document.xml").analy(); //Lancer analyse lexicale } else if(e.getActionCommand().equals("open")) { JFileChooser showFile = new JFileChooser(); //Ouvrez la boîte de sélection de fichier showFile.showOpenDialog(null); if(showFile.getSelectedFile()!=null) { String textword=new ReadFile().startRead(showFile.getSelectedFile()); //Le contenu du fichier est écrit dans une zone de saisie de texte Texte1 Mainframe.setTa1Text(textword); } } } *************************************************************************************************
*****************************************************************************************************************************Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 package com.kw.lex; import java.awt.Font; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import com.kw.lex.Action; public class Mainframe extends JFrame{ private static final long serialVersionUID = 1L; private static JTextArea text1; private static JTextArea text2; private JButton jb=new JButton("Analysis"); private JButton jb1=new JButton("open"); private JLabel jl1=new JLabel("input:"); private JLabel jl2=new JLabel("result:"); public Mainframe() { super("analyse lexicale"); this.setSize(600,448); this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight())/2); //Définir l'emplacement dans le milieu de l'écran this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(null); JPanel jp =new JPanel(); JPanel jp1 =new JPanel(); // Initialisation à jp et jp1 jp.setSize(300, 400); jp1.setSize(300, 400); jp.setLocation(0, 0); jp1.setLocation(300, 0); jp.setLayout(null); jp1.setLayout(null); //Zone de texte et text1 text2 initialisé et barre de défilement ajouté text1=new JTextArea(20,30); text2=new JTextArea(20,30); text2.setEditable(false); JScrollPane scrol1=new JScrollPane(text1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); JScrollPane scrol2=new JScrollPane(text2,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrol1.setSize(260, 310); scrol1.setLocation(18, 40); scrol2.setSize(260, 310); scrol2.setLocation(18, 40); jp.add(scrol1); jp1.add(scrol2); //Paire de boutons jb , jb1 et ballons réglé jb.setBounds(80, 370, 100, 25); jb1.setBounds(80, 370, 100, 25); jl1.setFont(new Font("Kaishu",Font.BOLD,12)); jl1.setSize(140, 25); jl1.setLocation(0, 0); jl2.setFont(new Font("Kaishu",Font.BOLD,12)); jl2.setSize(140, 25); jl2.setLocation(0, 0); //Ajouter au panneau JPanel jp.add(jb1); jp.add(jl1); jp1.add(jb); jp1.add(jl2); this.add(jp); this.add(jp1); //Paire de boutons jb , jb1 , les paramètres du moniteur de jb2 jb.setActionCommand("start"); jb1.setActionCommand("open"); jb.addActionListener(new Action()); jb1.addActionListener(new Action()); this.setResizable(true); this.setVisible(true); } public static String getString() { String temp=text1.getText(); return temp; } public static void setText(String text) { text2.setText(text); } public static void setTa1Text(String text) { text1.setText(text); } public static void main(String[] args) { new Mainframe(); } } }
******************************************************************************************************************************Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.kw.lex; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ReadFile { private File f; public String startRead(File path) { String outPut = ""; f = path; try { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String line = null; while ((line = br.readLine()) != null) { outPut += line + "\n"; } } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return outPut; } }
******************************************************************************************************************Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 package com.kw.lex; import com.kw.lex.XMLReader; import com.kw.lex.Mainframe; public class WordAnaly { private int line = 1; private String word; private char[] Input; private String[] Defind; // les identifiants de tableaux private String[] Operator; // Opérateurs de tableaux private String[] Exception; // Tableau exceptions de caractère private String analy = ""; private String showString = ""; private int j = 0; private boolean illegal = true; // caractère illégal private boolean isstring = false; // Si une chaîne private boolean ischar = false; // Si le caractère public WordAnaly(String word, String XMLPath) { int start_comment; // `//` `/*` int end_comment; // `*/`et puis`//`après la`\n` int flag; String test = ""; String head, tail; while (true) { start_comment = word.indexOf("//"); if (start_comment == -1) break; end_comment = word.indexOf("\n", start_comment); head = word.substring(0, start_comment); tail = word.substring(end_comment, word.length()); word = new String(head + tail); } while (true) { start_comment = word.indexOf("/*"); if (start_comment == -1) break; flag = word.indexOf("\n", start_comment); System.out.println(flag); end_comment = word.indexOf("*/", start_comment); while (flag < end_comment) { test = test + '\n'; flag = word.indexOf("\n", flag + 1); // System.out.println("trou Wei"); // System.out.println(flag); } head = word.substring(0, start_comment); tail = word.substring(end_comment + 2, word.length()); word = new String(head + test + tail); } this.word = word; Input = new char[word.length()]; Defind = new XMLReader().startprase(XMLPath, "sign"); // Set Identifier Operator = new XMLReader().startprase(XMLPath, "opeator"); // Opérateur de réglage Exception = new XMLReader().startprase(XMLPath, "special"); //Définir des exceptions de caractère for (int i = 0; i < word.length(); i++) // La chaîne d'entrée dans un tableau de char { Input[i] = word.charAt(i); } } /** *opérateurs */ private boolean checkSpecialWord(char word) { boolean isspecial = false; String testword = word + ""; for (int i = 0; i < Operator.length; i++) { if (testword.equals(Operator[i])) { isspecial = true; } else if (testword.equals(",")) { isspecial = true; } } return isspecial; } /** * _,$ */ private boolean checkException(char word) { boolean isexception = false; String testword = word + ""; for (int i = 0; i < Exception.length; i++) { if (testword.equals(Exception[i])) { isexception = true; } } return isexception; } /** * Déterminer si la chaîne ou la chaîne de caractères d' identité temporaire f i identité temporaire de caractères */ private void checkStringOrChar(int i, int f) { while (j < word.length() && i == 1) { analy += Input[j] + ""; j++; if (j < word.length() && Input[j] == '"') { analy += Input[j] + ""; j++; i = 2; isstring = true; } } while (j < word.length() && f == 1) { analy += Input[j] + ""; j++; if (j < word.length() && Input[j] == '\'') { analy += Input[j] + ""; j++; f = 2; ischar = true; } } } public void analy() { while (j < word.length()) { if (Character.isWhitespace(Input[j])) // est- espaces { if (Input[j] == '\n') { line++; } j++; } else if (Character.isLetter(Input[j]) || this.checkException(Input[j])) // Si une lettre ou _ , $ { illegal = true; while (j < word.length() && Character.isLetter(Input[j]) || j < word.length() && Character.isDigit(Input[j]) || j < word.length() && this.checkException(Input[j])) // Déterminer si les lettres ou des chiffres , ou _ , $ { analy += Input[j] + ""; j++; } while (j < word.length() && !Character.isWhitespace(Input[j]) && !Character.isDigit(Input[j]) && !Character.isLetter(Input[j]) // Si celle-ci est pas vide, des chiffres, des lettres, des opérateurs && !this.checkSpecialWord(Input[j])) { analy += Input[j] + ""; j++; illegal = false; } for (int z = 0; z < Defind.length; z++) // Est de mots-clés { if (analy.equals(Defind[z])) { System.out.println(analy + "mot-clé"); showString += line + " " + analy + " \ T est un mot-clé" + "\n"; analy = ""; } } if (!analy.equals("")) // Déterminer si elle est identifiant valide { if (illegal) { System.out.println(analy + "il correspond à l'identificateur"); showString += line + " " + analy + "\t est l'identificateur" + "\n"; } if (!illegal) { System.out.println(analy + "Caractère illégal"); showString += line + " " + analy + "\t est les opérateurs illégaux" + "\n"; } analy = ""; } } else if (Character.isDigit(Input[j])) // Déterminer si elle est un appareil photo numérique { illegal = true; while (j < word.length() && !Character.isWhitespace(Input[j]) && Character.isDigit(Input[j]) || j < word.length() && Input[j] == '.' && !Character.isWhitespace(Input[j])) // flotteur { analy += Input[j] + ""; j++; } while (j < word.length() && !Character.isWhitespace(Input[j]) // Déterminer si elle est variable illégale && !this.checkSpecialWord(Input[j])) { analy += Input[j] + ""; j++; illegal = false; } if (illegal) { System.out.println(analy + "Est la valeur"); showString += line + " " + analy + "\ T est le numérique" + "\n"; analy = ""; } if (!illegal) { System.out.println(analy + "Caractère illégal"); showString += line + " " + analy + "\ T est les opérateurs illégaux" + "\n"; analy = ""; } } else if (!Character.isDigit(Input[j]) && !Character.isLetter(Input[j]) && !Character.isWhitespace(Input[j])) { illegal = true; int i = 0; int f = 0; while (j < word.length() && !Character.isWhitespace(Input[j]) && !Character.isDigit(Input[j]) && !Character.isLetter(Input[j])) { analy += Input[j] + ""; if (Input[j] == '"') { i = 1; } else if (Input[j] == '\'') { f = 1; } j++; this.checkStringOrChar(i, f); // Que ce soit un caractère ou une chaîne if (isstring) { System.out.println(analy + "Une chaîne"); showString += line + " " + analy + "\t est une chaîne" + "\n"; isstring = false; i = 0; analy = ""; } if (ischar) { System.out.println(analy + "caractère"); showString += line + " " + analy + "\t est le caractère" + "\n"; ischar = false; f = 0; analy = ""; } } for (int z = 0; z < Operator.length; z++) { if (analy.equals(Operator[z])) { illegal = true; System.out.println(analy + "Est un opérateur"); showString += line + " " + analy + "\t est l'opérateur" + "\n"; analy = ""; } else if (analy.equals(",")) { illegal = true; System.out.println(analy + "Est un opérateur"); showString += line + " " + analy + "\t est l'opérateur" + "\n";// opérateurs analy = ""; } } } } Mainframe.setText(showString); // Les résultats montrent que la zone d'édition de texte } }
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 package com.kw.lex; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XMLReader { private Element init(String Path) { Element elmtInfo = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.parse(new File(Path)); elmtInfo = doc.getDocumentElement(); }catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return elmtInfo; } private String praseNode(Node result,String NodeName) { String text=""; NodeList ns = result.getChildNodes(); for (int j = 0; j < ns.getLength(); j++) { Node record = ns.item(j); if (record.getNodeType() == Node.ELEMENT_NODE && record.getNodeName().equals("value")) { text=record.getTextContent(); } } return text; } public String[] startprase(String Path,String Nodename) { NodeList nodes = this.init(Path).getChildNodes(); String[] Content=null; for (int i = 0; i < nodes.getLength(); i++) { Node result = nodes.item(i); //Obtenez noeud if (result.getNodeType() == Node.ELEMENT_NODE && result.getNodeName().equals(Nodename)) { Content=this.praseNode(result, Nodename).split(","); } } return Content; } public static void main(String[] args) { new XMLReader(); } }