Commit ed45a194 authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Add more actions to XML parser and add demo scenario loaded from xml

parent b4402faa
......@@ -2,26 +2,15 @@ package com.example.qrquest;
import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import com.example.qrquest.actions.Action;
import com.example.qrquest.actions.AddAtom;
import com.example.qrquest.actions.AddNode;
import com.example.qrquest.actions.CaptureQR;
import com.example.qrquest.actions.CaptureSpeech;
import com.example.qrquest.actions.ClearNodes;
import com.example.qrquest.actions.Checker;
import com.example.qrquest.actions.RemoveAtom;
import com.example.qrquest.actions.TTSReading;
import com.example.qrquest.atom.Atom;
import com.example.qrquest.atom.ItemAtom;
import com.example.qrquest.atom.QRAtom;
import com.example.qrquest.atom.SpeechAtom;
import com.example.qrquest.node.Node;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
......@@ -38,15 +27,10 @@ import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import org.xmlpull.v1.XmlPullParserException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.StringWriter;
import java.nio.file.Paths;
import java.util.ArrayList;
......
package com.example.qrquest;
import android.os.Build;
import android.util.Log;
import android.util.Xml;
import androidx.annotation.RequiresApi;
import com.example.qrquest.actions.Action;
import com.example.qrquest.actions.AddAtom;
import com.example.qrquest.actions.AddNode;
import com.example.qrquest.actions.CaptureQR;
import com.example.qrquest.actions.CaptureSpeech;
import com.example.qrquest.actions.Checker;
import com.example.qrquest.actions.AbsoluteChecker;
import com.example.qrquest.actions.ClearNodes;
import com.example.qrquest.actions.RemoveAtom;
import com.example.qrquest.actions.RemoveNode;
import com.example.qrquest.actions.TTSReading;
import com.example.qrquest.atom.Atom;
import com.example.qrquest.atom.ItemAtom;
......@@ -23,13 +21,8 @@ import com.example.qrquest.node.Node;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.nio.file.Paths;
import java.util.ArrayList;
public class ScenarioLoader {
......@@ -102,7 +95,6 @@ public class ScenarioLoader {
int ID = 0;
while (parser.next() != XmlPullParser.END_TAG) {
Log.d("Debug2", "Coucou");
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
......@@ -121,7 +113,6 @@ public class ScenarioLoader {
Log.d("Debug2", "Juste avant le throw error");
throw new Error("An unexpected tag was found in node " + Integer.toString(ID) + " XML definition");
}
Log.d("Debug2", "Coucou fin");
}
parser.require(XmlPullParser.END_TAG, ns, "node");
Log.d("Debug2", "finished reading node " + Integer.toString(ID) + ": " + Integer.toString(Conditions.size()) + " atoms and " + Actions.size() + " actions");
......@@ -141,34 +132,42 @@ public class ScenarioLoader {
String name = parser.getName();
Log.d("Debug2", "action name read in xml: " + name);
if (name.equals("atom")) {
if (name.equals("atom")) { // Add atom
Log.d("Debug2", "AddAtom action tag detected");
Actions.add(new AddAtom(mainActivity, nodeID, readAtom(parser)));
Log.d("Debug2", "AddAtom action added");
// parser.next();
}
else if (name.equals("TTSReading")) {
Log.d("Debug2", "TTSReading action tag detected");
Actions.add(new TTSReading(mainActivity, nodeID, readText(parser, "TTSReading")));
Log.d("Debug2", "TTSReading action added");
// parser.next();
}
else if (name.equals("ClearNodes")) {
Log.d("Debug2", "ClearNodes action tag detected");
Actions.add(new ClearNodes(mainActivity, nodeID));
while (parser.next() != XmlPullParser.END_TAG);
Log.d("Debug2", "ClearNodes action added");
}
else if (name.equals("AddNode")) {
Log.d("Debug2", "AddNode action tag detected");
Actions.add(new AddNode(mainActivity, nodeID, readInt(parser, "AddNode")));
Log.d("Debug2", "AddNode action added");
// parser.next();
}
else if (name.equals("RemoveNode")) {
Actions.add(new RemoveNode(mainActivity, nodeID, readInt(parser, "RemoveNode")));
}
else if (name.equals("AbsoluteChecker")) {
Actions.add(readAbsoluteChecker(parser, nodeID));
}
else if (name.equals("CaptureQR")) {
Actions.add(new CaptureQR(mainActivity, nodeID));
while (parser.next() != XmlPullParser.END_TAG);
}
else if (name.equals("CaptureSpeech")) {
Actions.add(new CaptureSpeech(mainActivity, nodeID));
while (parser.next() != XmlPullParser.END_TAG);
} else {
throw new Error("Unexpected action tag found in node " + Integer.toString(nodeID) + " XML definition");
// skip(parser);
}
}
Log.d("Debug2", "end of readAction reading");
......@@ -229,6 +228,47 @@ public class ScenarioLoader {
else return new ItemAtom(Integer.parseInt(Content));
}
private AbsoluteChecker readAbsoluteChecker(XmlPullParser parser, int nodeID) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "AbsoluteChecker");
String waited_value = "";
Class CheckedClass = Atom.class;
int SuccessNode = 0, FailNode = 0;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("waited_value")) {
waited_value = readText(parser, "waited_value");
} else if (name.equals("atom_type")) {
String CheckedClassString = readText(parser, "atom_type");
CheckedClass = getClassFromName(CheckedClassString);
Log.d("Debug2","AbsoluteChecker setting up with class " + CheckedClass.getName());
}
else if (name.equals("success_node_id")) {
SuccessNode = readInt(parser, "success_node_id");
}
else if (name.equals("fail_node_id")) {
FailNode = readInt(parser, "fail_node_id");
}
else
{
throw new Error("An unexpected tag was found in an AbsoluteChecker definition (other than type or content)");
}
}
parser.require(XmlPullParser.END_TAG, ns, "AbsoluteChecker");
return new AbsoluteChecker(mainActivity, nodeID, waited_value, CheckedClass, SuccessNode, FailNode);
}
private Class getClassFromName(String checkedClassString) {
if(checkedClassString.equals("QRAtom")) return QRAtom.class;
else if(checkedClassString.equals("SpeechAtom")) return SpeechAtom.class;
else return ItemAtom.class;
}
private String readText(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, tag);
String result = "";
......@@ -298,7 +338,7 @@ public class ScenarioLoader {
A3.add(new ClearNodes(mainActivity,3));
A3.add(new TTSReading(mainActivity,3, "Cherchons un QR"));
A3.add(new CaptureQR(mainActivity,3));
A3.add(new Checker(mainActivity,3, "36271", QRAtom.class, 4,2));
A3.add(new AbsoluteChecker(mainActivity,3, "36271", QRAtom.class, 4,2));
ArrayList<Atom> C3 = new ArrayList<Atom>();
C3.add(new ItemAtom(1));
......@@ -318,7 +358,7 @@ public class ScenarioLoader {
A6.add(new ClearNodes(mainActivity,6));
A6.add(new TTSReading(mainActivity,6, "Combien font 4 fois 2 ?"));
A6.add(new CaptureSpeech(mainActivity,6));
A6.add(new Checker(mainActivity,6, "8", SpeechAtom.class, 7,5));
A6.add(new AbsoluteChecker(mainActivity,6, "8", SpeechAtom.class, 7,5));
ArrayList<Atom> C6 = new ArrayList<Atom>();
C6.add(new ItemAtom(1));
......
......@@ -6,13 +6,17 @@ import com.example.qrquest.MainActivity;
import com.example.qrquest.atom.Atom;
import com.example.qrquest.atom.QRAtom;
public class Checker extends Action {
public class AbsoluteChecker extends Action {
/*
* Check for any atom of given class (QR, speech, Item), if first value found correspond to given WaitedValue open SuccessNodeID, else open FailNodeID
* and remove atom.
*/
private String WaitedValue;
private int SuccessNodeID, FailNodeID;
private Class AtomType;
public Checker(MainActivity mainActivity, int nodeID, String WaitedValue, Class AtomType, int SuccessNodeID, int FailNodeID) {
public AbsoluteChecker(MainActivity mainActivity, int nodeID, String WaitedValue, Class AtomType, int SuccessNodeID, int FailNodeID) {
super(mainActivity, nodeID);
this.WaitedValue = WaitedValue;
this.AtomType = AtomType;
......@@ -25,9 +29,10 @@ public class Checker extends Action {
super.Do();
for (Atom a: mainActivity.Atoms
) {
Log.d("Debug2","Checker action reads " + a.GetStringValue());
if(a.getClass() == AtomType)
{
Log.d("Debug2","AbsoluteChecker action reads " + a.GetStringValue());
if(a.GetStringValue().equals(WaitedValue)) mainActivity.OpenNodes.add(mainActivity.GetNode(SuccessNodeID));
else mainActivity.OpenNodes.add(mainActivity.GetNode(FailNodeID));
mainActivity.Atoms.remove(a);
......
......@@ -28,9 +28,42 @@
</required_atoms>
<action_list>
<TTSReading>Au revoir</TTSReading>
<ClearNodes></ClearNodes>
<TTSReading>Show me what you got</TTSReading>
<CaptureQR></CaptureQR>
<AbsoluteChecker>
<waited_value>36271</waited_value>
<atom_type>QRAtom</atom_type>
<success_node_id>50</success_node_id>
<fail_node_id>51</fail_node_id>
</AbsoluteChecker>
<RemoveNode>24</RemoveNode>
</action_list>
</node>
<node>
<id>50</id>
<required_atoms>
</required_atoms>
<action_list>
<TTSReading>Bravooooo</TTSReading>
<RemoveNode>50</RemoveNode>
</action_list>
</node>
<node>
<id>51</id>
<required_atoms>
</required_atoms>
<action_list>
<TTSReading>C'est non</TTSReading>
<AddNode>24</AddNode>
<RemoveNode>51</RemoveNode>
</action_list>
</node>
</liste>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<liste>
<node>
<id>1</id>
<required_atoms>
</required_atoms>
<action_list>
<TTSReading>Bonjour</TTSReading>
<ClearNodes> </ClearNodes>
<atom>
<type>ItemAtom</type>
<content>12</content>
</atom>
<AddNode>24</AddNode>
</action_list>
</node>
<node>
<id>24</id>
<required_atoms>
<atom>
<type>ItemAtom</type>
<content>12</content>
</atom>
</required_atoms>
<action_list>
<TTSReading>Au revoir</TTSReading>
<ClearNodes></ClearNodes>
</action_list>
</node>
</liste>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment