use of nars.operator.Operator in project opennars by opennars.
the class NAR method removePlugin.
public void removePlugin(PluginState ps) {
if (plugins.remove(ps)) {
Plugin p = ps.plugin;
if (p instanceof Operator) {
memory.removeOperator((Operator) p);
}
// TODO sensory channels can be plugins
ps.setEnabled(false);
emit(Events.PluginsChange.class, null, p);
}
}
use of nars.operator.Operator in project opennars by opennars.
the class Narsese method parseTerm.
/* ---------- react String into term ---------- */
/**
* Top-level method that react a Term in general, which may recursively call
* itself.
* <p>
* There are 5 valid cases: 1. (Op, A1, ..., An) is a CompoundTerm if Op is
* a built-in getOperator 2. {A1, ..., An} is an SetExt; 3. [A1, ..., An] is an
* SetInt; 4. <T1 Re T2> is a Statement (including higher-order Statement);
* 5. otherwise it is a simple term.
*
* @param s0 the String to be parsed
* @param memory Reference to the memory
* @return the Term generated from the String
*/
public Term parseTerm(String s) throws InvalidInputException {
s = s.trim();
if (s.length() == 0)
return null;
int index = s.length() - 1;
char first = s.charAt(0);
char last = s.charAt(index);
NativeOperator opener = getOpener(first);
if (opener != null) {
switch(opener) {
case COMPOUND_TERM_OPENER:
if (last == COMPOUND_TERM_CLOSER.ch) {
return parseCompoundTerm(s.substring(1, index));
} else {
throw new InvalidInputException("missing CompoundTerm closer");
}
case SET_EXT_OPENER:
if (last == SET_EXT_CLOSER.ch) {
return SetExt.make(parseArguments(s.substring(1, index) + ARGUMENT_SEPARATOR));
} else {
throw new InvalidInputException("missing ExtensionSet closer");
}
case SET_INT_OPENER:
if (last == SET_INT_CLOSER.ch) {
return SetInt.make(parseArguments(s.substring(1, index) + ARGUMENT_SEPARATOR));
} else {
throw new InvalidInputException("missing IntensionSet closer");
}
case STATEMENT_OPENER:
if (last == STATEMENT_CLOSER.ch) {
return parseStatement(s.substring(1, index));
} else {
throw new InvalidInputException("missing Statement closer");
}
}
} else if (Parameters.FUNCTIONAL_OPERATIONAL_FORMAT) {
// parse functional operation:
// function()
// function(a)
// function(a,b)
// test for existence of matching parentheses at beginning at index!=0
int pOpen = s.indexOf('(');
int pClose = s.lastIndexOf(')');
if ((pOpen != -1) && (pClose != -1) && (pClose == s.length() - 1)) {
String operatorString = Operator.addPrefixIfMissing(s.substring(0, pOpen));
Operator operator = memory.getOperator(operatorString);
if (operator == null) {
// ???
throw new InvalidInputException("Unknown operator: " + operatorString);
}
String argString = s.substring(pOpen + 1, pClose + 1);
Term[] a;
if (argString.length() > 1) {
ArrayList<Term> args = parseArguments(argString);
a = args.toArray(new Term[args.size()]);
} else {
// void "()" arguments, default to (SELF)
a = Operation.SELF_TERM_ARRAY;
}
Operation o = Operation.make(operator, a, true);
return o;
}
}
// if no opener, parse the term
return parseAtomicTerm(s);
}
use of nars.operator.Operator in project opennars by opennars.
the class Narsese method parseCompoundTerm.
/**
* Parse a String to create a CompoundTerm.
*
* @return the Term generated from the String
* @param s0 The String to be parsed
* @throws nars.io.StringParser.InvalidInputException the String cannot be
* parsed into a Term
*/
private Term parseCompoundTerm(final String s0) throws InvalidInputException {
String s = s0.trim();
if (s.isEmpty()) {
throw new InvalidInputException("Empty compound term: " + s);
}
int firstSeparator = s.indexOf(ARGUMENT_SEPARATOR);
if (firstSeparator == -1) {
throw new InvalidInputException("Invalid compound term (missing ARGUMENT_SEPARATOR): " + s);
}
String op = (firstSeparator < 0) ? s : s.substring(0, firstSeparator).trim();
NativeOperator oNative = getOperator(op);
Operator oRegistered = memory.getOperator(op);
if ((oRegistered == null) && (oNative == null)) {
throw new InvalidInputException("Unknown operator: " + op);
}
ArrayList<Term> arg = (firstSeparator < 0) ? new ArrayList<>(0) : parseArguments(s.substring(firstSeparator + 1) + ARGUMENT_SEPARATOR);
Term[] argA = arg.toArray(new Term[arg.size()]);
Term t;
if (oNative != null) {
t = Terms.term(oNative, argA);
} else if (oRegistered != null) {
t = make(oRegistered, argA, true);
} else {
throw new InvalidInputException("Invalid compound term");
}
return t;
}
use of nars.operator.Operator in project opennars by opennars.
the class Narsese method parseAtomicTerm.
// private static void showWarning(String message) {
// new TemporaryFrame( message + "\n( the faulty line has been kept in the addInput window )",
// 40000, TemporaryFrame.WARNING );
// }
/**
* Parse a Term that has no internal structure.
* <p>
* The Term can be a constant or a variable.
*
* @param s0 the String to be parsed
* @throws nars.io.StringParser.InvalidInputException the String cannot be
* parsed into a Term
* @return the Term generated from the String
*/
private Term parseAtomicTerm(String s0) throws InvalidInputException {
String s = s0.trim();
if (s.length() == 0) {
throw new InvalidInputException("missing term");
}
Operator op = memory.getOperator(s0);
if (op != null) {
return op;
}
if (s.contains(" ")) {
// invalid characters in a name
throw new InvalidInputException("invalid term");
}
char c = s.charAt(0);
if (c == Symbols.INTERVAL_PREFIX) {
return Interval.interval(s);
}
if (containVar(s) && !s.equals("#")) {
return new Variable(s);
} else {
return Term.get(s);
}
}
use of nars.operator.Operator in project opennars by opennars.
the class Register method execute.
/**
* To register a new operator
* @param args Arguments, a Statement followed by an optional tense
* @param memory The memory in which the operation is executed
* @return Immediate results as Tasks
*/
@Override
protected ArrayList<Task> execute(Operation operation, Term[] args, Memory memory) {
Operator op = new NullOperator(args[1].toString());
// add error checking
memory.addOperator(op);
return null;
}
Aggregations