Bonjour j'essaie d'implementer une turing machine qui est censee lire des strings et me donner un output: "le string est rejete" ou "le string est accepte" ou le "NE SAIS PAS". L'output le string est accepte doit etre produit au cas ou je suis dans le state "qa" accept state. L'output est ""REJETER" au case ou tous les stages finaux sont "qr" ou au cas ou il n y a plus de transitions a prendre a partir d'une node dans le graphe. l'output "JE NE SAIS PAS" est produit dans tous les autres cas. au fait, j'ai essaye d'implementer la Turing machine mais je ny arrive pas il faut aussi prendre en consideration le non determinisme soit le case ou, la TM a deux chois de transitions a partir de la meme node dans le graphe. Le input que j'ai est du type:
#q0,a->qa,a,R#q0,a->q1,a,R#q0,b->q1,b,R#q0,_->q1,_,R#q1,b->q1,b,R#q1,_->q1,_,R#q1,a->qr,a,R#q1,b->qr,b,R##bba#

ou les # delimitent les transitions, le INPUT STRING est identifie a la fin sous la forme de: #bba#

J'ai transforme mes transitions sous la forme suivante
q0,a==>qa,a,R
q0,b==>qr,b,R
q0,_==>qr,_,R

Aussim jai reussi a isoler mon input string du reste. Mais je ne sais pas ce qui ne va pas au niveau de mon implementation, je vous saurai gree de bien vouloir maider, je vous envoie mon code

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.util.Arrays;
 
 
public class Stage {
	String current_state; 
	int counter; 
	String[] myinput2;
	public String getCurrent_state() {
		return current_state;
	}
	public void setCurrent_state(String current_state) {
		this.current_state = current_state;
	}
	public int getCounter() {
		return counter;
	}
	public void setCounter(int counter) {
		this.counter = counter;
	}
	public String[] getMyinput2() {
		return myinput2;
	}
	public void setMyinput2(String[] myinput2) {
		this.myinput2 = myinput2;
	}
 
 
	public Stage(String current_state, int counter, String[] myinput2) {
		super();
		this.current_state = current_state;
		this.counter = counter;
		this.myinput2 = myinput2;
	}
	@Override
	public String toString() {
		return "Stage [current_state=" + current_state + ", counter=" + counter
				+ ", myinput2=" + Arrays.toString(myinput2) + "]";
	} 
 
 
 
 
}








Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
 
 
public class machine {
	//static List<Configuration> configlist= new ArrayList<Configuration>(); 
 
 
	int counter=0; 
	int total=1; 
	/**
         * @param args
         */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, List<String>> hash = new HashMap<String, List<String>>(); 
		machine m= new machine(); 
		List<String> mylist=m.Tokenize(); 
		String input=m.Input(mylist); 
 
		char[] myinput= input.toCharArray(); 
		String[] myinput2= new String[myinput.length]; 
 
		System.out.println("MY INPUT STRING"); 
		for(int i=0; i<myinput2.length; i++){			
			myinput2[i]="" +myinput[i];
			System.out.println(""+myinput2[i]); 
		}
 
 
		List<String> transitions=m.RemoveArrow(mylist); 
		List<String> KeyList=m.RightHandSide(transitions); 
		List<String> ValueList=m.LeftHandSide(transitions); 
		KeyList =m.RemoveBlank(KeyList); 
		Set<String> mm= m.findDuplicates(KeyList); 
 
 
 
 
 
		//System.out.println("  ");
		Object[] keys =KeyList.toArray(); 
		Object[] values =ValueList.toArray(); 
 
		//		for(Object obj: keys){
		//			System.out.println("key: "+obj); 
		//		}
		//		 
		//		for(Object obj: values){
		//			System.out.println("value: "+obj); 
		//		}
 
 
 
 
		System.out.println("MY RULES"); 
 
		String[]obj = new String[KeyList.size()];
		for (int i = 0; i <obj.length; i++){
			obj[i] = keys[i] + "==>" + values[i] ;
 
 
		}
		List<String> states=m.FindStates(KeyList); 
		Set<String> states2= m.findDuplicates(states);
		System.out.println("MY STATES"); 
		for(String s: states2){
 
			System.out.println(s); 
		}
		System.out.println("END OF STATES"); 
 
		for(int i = 0; i < obj.length; i++)
		{
 
			System.out.print(obj[i]);
			// System.out.println("MOUNA");
 
			System.out.println();
		}
 
		String current_state="q0";
		int counter=0; 
		int counter_loop=0; 
		for(int i=0; i<myinput2.length; i++){
			if(myinput2[i].equals("e")){
				myinput2[i]="_"; 
			}
		}
		String s2= "q0"+","+myinput2[0];
		System.out.println("INITIALLY:     "+s2); 
 
		String[] array =new String[myinput2.length]; 
		String[] mykeys= new String [3]; 
		int index=0; 
		boolean firsttime= false; 
		List<Stage> stagelist= new ArrayList<Stage>(); 
 
		while(!current_state.equals("qa") && counter_loop<20){
		List<String> myletters= new ArrayList<String>(); 
		List<String> mine= new ArrayList<String>(); 
		List<String> yours= new ArrayList<String>(); 
		List<Integer> indexList= new ArrayList<Integer>(); 
		for(String s: KeyList){
				if(firsttime==false){
					if(s.equals(s2) && !mine.contains(s)){
						 index=KeyList.indexOf(s); 
						 indexList.add(index); 
						 String key=KeyList.get(index); 
						 mine.add(key); 
						 mykeys= key.split(","); 
						 System.out.println("MY INDEX IS:  "+KeyList.indexOf(s)); }	
				}
				else{
					for(String s3: myletters){
						if(s3.equals(s2) && !mine.contains(s3)){
							 index=KeyList.indexOf(s); 
							 indexList.add(index); 
							 String key=KeyList.get(index); 
							 mine.add(key); 
							 mykeys= key.split(","); 
						}
 
				}
				}}
 
				for(int i: indexList){
					String value=ValueList.get(i); 
					System.out.println("MY VALUEs ARE:  "+value); 
						if(!yours.contains(value)){
							yours.add(value); 
							array = value.split(","); 
 
							System.out.println("MY VALUE IS:  "+ counter +" my key  "+mykeys[1]); 
 
							if(array[2].equals("R") && counter<myinput2.length && myinput2[counter].equals(mykeys[1])){
								current_state= array[0]; 
								System.out.println("CURRENT STATE   "+current_state); 
								myinput2[counter]= array[1]; 
								counter++; 
								System.out.println("CURRENT STATE   "+current_state+"counter"+counter); 
								s2= current_state+","+myinput2[counter-1];
								myletters.add(s2); 
 
							}
							else if(  counter> 0 && counter<myinput2.length  && myinput2[counter].equals(mykeys[1])){
								current_state= array[0]; 
								System.out.println("CURRENT STATE   "+current_state); 
								myinput2[counter]= array[1]; 
								counter--; 
								s2= current_state+","+myinput2[counter];
								myletters.add(s2); 
 
 
							}
 
							Stage stage= new Stage(current_state, counter, myinput2 ); 
							stagelist.add(stage); 
							System.out.println("MY STAGE:     "+stage.toString());
 
							counter_loop++; 
 
						}
 
 
 
 
 
 
 
				  }
 
			}
 
 
 
			//Initialize  the head, the current symbol, the current state, the whole input
 
		if(current_state.equals("qa")){
			System.out.println("ACCEPT!!!!"); 
			return; 
		}
 
		List<Stage> myl= m.decide(stagelist, myinput2); 
 
		if(myl==null){
			System.out.println("DO NOT KNOW"); 
 
		}
		else{
			System.out.println("STOP AND REJECT"); 
		}
		}	
 
 
	public List<Stage> decide(List<Stage> stagelist, String[] myinput2){
	List<Stage> newlist= null; 	
	if(stagelist.size()>0){
		newlist= new ArrayList<Stage>(); 
		for(Stage st: stagelist){
				if(st.counter==myinput2.length-1){
					newlist.add(st); 
 
			}
			}
 
 
 
 
		}
	return newlist;
 
	}
	/**********************************************************************/
	public List<String> FindStates(List<String> ls){
		List<String> states= new ArrayList<String>(); 
		for(String s: ls){
			String[] transition = s.split(",");
			states.add(transition[0]); 
		}
 
 
		return states;
 
	}
 
	public List<String>Tokenize(){
 
		List<String> transitions =null; 
		try {
			File f = new File("/Users/mouna/Documents/workspace/mouna/input.txt");
			Scanner sc = new Scanner(f);
 
			transitions = new ArrayList<String>();
 
			while(sc.hasNextLine()){
				String line = sc.nextLine();
				String[] transition = line.split("#");
				for(String i: transition){
					transitions.add(i);           	  
				}
			}
 
 
		} catch (FileNotFoundException e) {         
			e.printStackTrace();
		}
 
		return transitions; 
	}
	/**********************************************************************/
 
	public List<String>RemoveBlank(List<String> KeyList){
		List<String> KeyList2= new ArrayList<String>(); 
		for(String s:KeyList){
			if(s.equals("")==false){
				KeyList2.add(s); 
			}
		}
 
		return KeyList2; 
	}
	/**********************************************************************/
	//Returns the Input String 
	public String Input(List<String> mylist){
		String input= null; 
		int size= mylist.size(); 
		input= mylist.get(mylist.size() - 1); 
		mylist.remove(mylist.size() - 1); 
		//mylist.remove(mylist.size() - 2); 
		return input; 
	}
 
	/**********************************************************************/
	//Removes the arrow
	public List<String> RemoveArrow(List<String> mylist){
		List<String> transitions =new ArrayList<String>(); 
 
		for(String s: mylist){
 
			String[] transition = s.split("->");
			for(String i: transition){
				transitions.add(i);           	  
			}
		}
 
		return transitions; 
 
	}
 
	/**********************************************************************/
	//Removes the arrow
	public List<String> RightHandSide(List<String> mylist){
		List<String> KeyList =new ArrayList<String>(); 
		for(String s: mylist){
			if(s.contains("R")==false && s.contains("L")==false){
				KeyList.add(s); 
			}
 
		}
 
		return KeyList; 
 
	}
	/**********************************************************************/
	//Removes the arrow
	public List<String> LeftHandSide(List<String> mylist){
		List<String> KeyList =new ArrayList<String>(); 
		for(String s: mylist){
			if(s.contains("R")==true || s.contains("L")==true){
				KeyList.add(s); 
			}
 
		}
 
		return KeyList; 
 
	}
 
	/**********************************************************************/
 
	public  Set<String> findDuplicates(List<String> KeyList) {
 
		final Set<String> setToReturn = new HashSet<String>();
		final Set<String> set1 = new HashSet<String>();
 
		for (String yourInt : KeyList) {
			if (!set1.add(yourInt)) {
				setToReturn.add(yourInt);
			}
		}
		return setToReturn;
	}
}