Skip to content

Commit

Permalink
added line numbers to skill model
Browse files Browse the repository at this point in the history
this enhances debugging as it is now much easier to find out where
skills are generated from
  • Loading branch information
Orbiter committed Nov 4, 2019
1 parent b3fe5cb commit aa3188e
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 63 deletions.
5 changes: 3 additions & 2 deletions src/ai/susi/mind/SusiAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,16 @@ public SusiAction(JSONObject json) throws SusiActionException {
* @param answers
* @return the action
*/
public static JSONObject answerAction(SusiLanguage language, String... answers) {
public static JSONObject answerAction(int line, SusiLanguage language, String... answers) {
JSONArray phrases = new JSONArray();
for (String answer: answers) phrases.put(answer.trim());
JSONObject json = new JSONObject()
JSONObject json = new JSONObject(true)
.put("type", RenderType.answer.name())
.put("select", SelectionType.random.name())
.put("phrases", phrases);
assert language != SusiLanguage.unknown;
if (language != SusiLanguage.unknown) json.put("language", language.name());
if (line > 0) json.put("line", line);
return json;
}

Expand Down
1 change: 1 addition & 0 deletions src/ai/susi/mind/SusiArgument.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Random;
import java.util.regex.Matcher;

import org.eclipse.jetty.util.log.Log;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down
5 changes: 3 additions & 2 deletions src/ai/susi/mind/SusiInference.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ public SusiInference(JSONObject json) {
this.json = json;
}

public SusiInference(String expression, Type type) {
public SusiInference(String expression, Type type, int line) {
assert expression != null;
this.json = new JSONObject(true);
this.json.put("type", type.name());
this.json.put("expression", expression);
this.json.put("line", line);
}

public SusiInference(JSONObject definition, Type type) {
Expand Down Expand Up @@ -161,7 +162,7 @@ public JSONObject getDefinition() {
final long delay = date.getTime() - start;

// create planned action
JSONObject actionj = SusiAction.answerAction(flow.getLanguage(), reflection);
JSONObject actionj = SusiAction.answerAction(0, flow.getLanguage(), reflection);
try {
SusiAction planned_utterance = new SusiAction(actionj);
SusiThought planned_thought = flow.applyAction(planned_utterance); // this also instantiates the answer in the planned_utterance
Expand Down
12 changes: 6 additions & 6 deletions src/ai/susi/mind/SusiIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ public SusiIntent(
boolean prior,
int depth,
SusiSkill.ID skillid) throws SusiActionException {
assert skillid != null;
assert utterances != null : "utterances is null in " + skillid.getPath();
assert utterances.size() > 0 : "utterance size is 0 in " + skillid.getPath();
assert skillid != null;
if (utterances == null) throw new SusiActionException("utterances is null in " + skillid.getPath());
if (utterances.size() == 0) throw new SusiActionException("utterances size is 0 in " + skillid.getPath());

this.utterances = new ArrayList<>();
for (SusiUtterance u: utterances) this.utterances.add(u);
this.actions = new ArrayList<>();
Expand Down Expand Up @@ -439,7 +439,7 @@ public static JSONObject answerIntent(
if (condition != null && condition.length() > 0) {
JSONArray c = new JSONArray();
intent.put("process", c);
c.put(new SusiInference(condition, SusiInference.Type.memory).getJSON());
c.put(new SusiInference(condition, SusiInference.Type.memory, 0).getJSON());
}

// quality control
Expand All @@ -456,7 +456,7 @@ public static JSONObject answerIntent(
// write actions
JSONArray a = new JSONArray();
intent.put("actions", a);
a.put(SusiAction.answerAction(language, answers));
a.put(SusiAction.answerAction(0, language, answers));
return intent;
}

Expand Down
6 changes: 3 additions & 3 deletions src/ai/susi/mind/SusiProcedures.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SusiProcedures extends LinkedHashMap<Pattern, BiFunction<SusiArgume
public SusiProcedures() {
super();
}

/**
* Deduction is the application of an intent on perception and a world model.
* In this method the mappings from the intent set is applied to the perception q and previous
Expand Down Expand Up @@ -65,11 +65,11 @@ public SusiThought deduce(SusiArgument flow, String q) {
// we silently ignore these exceptions as they are normal and acceptable during thinking
}
}

// no success: produce an empty thought
return new SusiThought();
}

/**
* Inspiration is the application of an intent on a minimum perception. In this method the mappings
* from the intent set is applied only to the perception q.
Expand Down
57 changes: 32 additions & 25 deletions src/ai/susi/mind/SusiSkill.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,16 @@ public SusiSkill(
}

// check utterances
List<SusiUtterance> utterances = phrasesFromWildcard(skillid.getPath(), acceptWildcardIntent, block.utterance, prior);
if (utterances == null) continue intentloop;
for (SusiUtterance u: utterances) {
if (u.isCatchallPhrase()) {
DAO.log("WARNING: skipping skill / wildcard not allowed here: " + skillid.getPath());
continue intentloop;
List<SusiUtterance> utterances = phrasesFromWildcard(skillid.getPath(), acceptWildcardIntent, block.utterance, prior, block.lineNumber);
if (utterances == null || utterances.size() == 0) continue intentloop;

// the following applies only to on-skills!
if (!acceptWildcardIntent) {
for (SusiUtterance u: utterances) {
if (u.isCatchallPhrase()) {
DAO.log("WARNING: skipping skill / wildcard not allowed here: " + skillid.getPath());
continue intentloop;
}
}
}

Expand All @@ -277,9 +281,11 @@ public SusiSkill(
List<SusiIntent> fileIntents = new ArrayList<>();
fileIntents.add(new SusiIntent(utterances, prior, 0, skillid));
StringBuilder bang_bag = new StringBuilder();
int lineNr = 0;
readloop: while (lineNr < block.model.size()) {
String line = block.model.get(lineNr++);
int blockNr = 0;
readloop: while (blockNr < block.model.size()) {
SusiSkillFile.Line numberedLine = block.model.get(blockNr++);
String line = numberedLine.line;
int lineNumber = numberedLine.number;

// parse bang types and answer lines
int thenpos;
Expand All @@ -299,11 +305,11 @@ public SusiSkill(
} else if (head.equals("implication")) {
fileIntents.forEach(intent -> intent.setImplication(tail));
} else if (head.equals("first")) {
fileIntents.forEach(intent -> intent.addInference(new SusiInference("FIRST", SusiInference.Type.flow)));
fileIntents.forEach(intent -> intent.addInference(new SusiInference("FIRST", SusiInference.Type.flow, lineNumber)));
} else if (head.equals("rest")) {
fileIntents.forEach(intent -> intent.addInference(new SusiInference("REST", SusiInference.Type.flow)));
fileIntents.forEach(intent -> intent.addInference(new SusiInference("REST", SusiInference.Type.flow, lineNumber)));
} else if (head.equals("plan")) {
fileIntents.forEach(intent -> intent.addInference(new SusiInference("PLAN " + param + ":" + tail, SusiInference.Type.flow)));
fileIntents.forEach(intent -> intent.addInference(new SusiInference("PLAN " + param + ":" + tail, SusiInference.Type.flow, lineNumber)));
} else {
// start multi-line bang
bang_type = head;
Expand All @@ -326,10 +332,10 @@ public SusiSkill(
// create a javascript intent

// javascript process
SusiInference inference = new SusiInference(bang_bag.toString(), Type.javascript);
SusiInference inference = new SusiInference(bang_bag.toString(), Type.javascript, lineNumber);

// answers; must contain $!$
SusiAction action = new SusiAction(SusiAction.answerAction(this.id.language(), bang_answers.split("\\|")));
SusiAction action = new SusiAction(SusiAction.answerAction(lineNumber, this.id.language(), bang_answers.split("\\|")));

fileIntents.forEach(intent -> {
intent.addInference(inference);
Expand Down Expand Up @@ -376,7 +382,7 @@ public SusiSkill(

// answers; must contain names from the console result array
if (bang_answers.length() > 0) try {
SusiAction action = new SusiAction(SusiAction.answerAction(this.id.language(), bang_answers.split("\\|")));
SusiAction action = new SusiAction(SusiAction.answerAction(lineNumber, this.id.language(), bang_answers.split("\\|")));
fileIntents.forEach(intent -> intent.addAction(action));
} catch (SusiActionException e) {
DAO.severe(e.getMessage());
Expand Down Expand Up @@ -406,12 +412,12 @@ public SusiSkill(
String[] answers = ifsubstring.split("\\|");
fileIntents.forEach(intent -> {
try {
intent.addAction(new SusiAction(SusiAction.answerAction(skillid.language(), answers)));
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), answers)));
} catch (SusiActionException e) {
e.printStackTrace();
}
});
fileIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory)));
fileIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory, lineNumber)));
continue readloop;
}
} else {
Expand All @@ -425,24 +431,24 @@ public SusiSkill(
String[] answers = ifsubstring.split("\\|");
fileIntents.forEach(intent -> {
try {
intent.addAction(new SusiAction(SusiAction.answerAction(skillid.language(), answers)));
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), answers)));
} catch (SusiActionException e) {
e.printStackTrace();
}
});
fileIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory)));
fileIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory, lineNumber)));
}
String elsesubstring = line.substring(elsepos + 1).trim();
if (elsesubstring.length() > 0) {
String[] elseanswers = elsesubstring.split("\\|");
clonedIntents.forEach(intent -> {
try {
intent.addAction(new SusiAction(SusiAction.answerAction(skillid.language(), elseanswers)));
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), elseanswers)));
} catch (SusiActionException e) {
e.printStackTrace();
}
});
clonedIntents.forEach(intent -> intent.addInference(new SusiInference("NOT " + condition, SusiInference.Type.memory)));
clonedIntents.forEach(intent -> intent.addInference(new SusiInference("NOT " + condition, SusiInference.Type.memory, lineNumber)));
// attach the clones intents to the list of intents
clonedIntents.forEach(intent -> fileIntents.add(intent));
}
Expand All @@ -452,7 +458,7 @@ public SusiSkill(
String[] answers = line.split("\\|");
fileIntents.forEach(intent -> {
try {
intent.addAction(new SusiAction(SusiAction.answerAction(skillid.language(), answers)));
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), answers)));
} catch (SusiActionException e) {
e.printStackTrace();
}
Expand All @@ -475,10 +481,10 @@ public SusiSkill(
* @param prior
* @return a list of compiled utterances
*/
private static List<SusiUtterance> phrasesFromWildcard(String skillidname, boolean acceptWildcardIntent, String utterances_declaration, boolean prior) {
private static List<SusiUtterance> phrasesFromWildcard(String skillidname, boolean acceptWildcardIntent, String utterances_declaration, boolean prior, int line) {
List<SusiUtterance> utterances = new ArrayList<>();
for (String u: utterances_declaration.split("\\|")) {
SusiUtterance utterance = new SusiUtterance(u.trim(), prior);
SusiUtterance utterance = new SusiUtterance(u.trim(), prior, line);
if (!acceptWildcardIntent && utterance.isCatchallPhrase()) {
DAO.log("WARNING: skipping skill / wildcard not allowed here: " + skillidname);
continue;
Expand Down Expand Up @@ -510,7 +516,7 @@ private static void extendParentWithAnswer(List<SusiIntent> preceding_intents, S
int parent_id = parent_intent.hashCode();

// - set an invisible assignment in parent for linking variable to ID
parent_intent.addInferences(new SusiInference("SET ", SusiInference.Type.memory));
parent_intent.addInferences(new SusiInference("SET ", SusiInference.Type.memory, 0));

// - add a check rule in child so a child fires only if linking ID is set correctly

Expand Down Expand Up @@ -686,6 +692,7 @@ public static void main(String[] args) {
File conf = FileSystems.getDefault().getPath("conf").toFile();
//File skillFile = new File(conf, "os_skills/test/en/alarm.txt");
File skillFile = new File(conf, "../../susi_skill_data/models/general/Communication/en/Hotword_Changer.txt");

//File model = new File(DAO.model_watch_dir, "general");
//File skill = SusiSkill.getSkillFileInModel(model, "Westworld");
System.out.println(skillFile);
Expand Down
Loading

0 comments on commit aa3188e

Please sign in to comment.