Search in sources :

Example 6 with Operator

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);
    }
}
Also used : Operator(nars.operator.Operator) Events(nars.io.events.Events) Plugin(nars.plugin.Plugin)

Example 7 with Operator

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);
}
Also used : Operator(nars.operator.Operator) NativeOperator(nars.io.Symbols.NativeOperator) Symbols.getOperator(nars.io.Symbols.getOperator) NativeOperator(nars.io.Symbols.NativeOperator) ArrayList(java.util.ArrayList) Operation(nars.operator.Operation)

Example 8 with Operator

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;
}
Also used : Operator(nars.operator.Operator) NativeOperator(nars.io.Symbols.NativeOperator) Symbols.getOperator(nars.io.Symbols.getOperator) NativeOperator(nars.io.Symbols.NativeOperator) Term(nars.language.Term)

Example 9 with Operator

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);
    }
}
Also used : Operator(nars.operator.Operator) NativeOperator(nars.io.Symbols.NativeOperator) Symbols.getOperator(nars.io.Symbols.getOperator) Variable(nars.language.Variable)

Example 10 with Operator

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;
}
Also used : NullOperator(nars.operator.NullOperator) Operator(nars.operator.Operator) NullOperator(nars.operator.NullOperator)

Aggregations

Operator (nars.operator.Operator)10 Operation (nars.operator.Operation)4 NativeOperator (nars.io.Symbols.NativeOperator)3 Symbols.getOperator (nars.io.Symbols.getOperator)3 Task (nars.entity.Task)2 Term (nars.language.Term)2 FunctionOperator (nars.operator.FunctionOperator)2 Memory (nars.storage.Memory)2 FileInputStream (java.io.FileInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1 BudgetValue (nars.entity.BudgetValue)1 Sentence (nars.entity.Sentence)1 Stamp (nars.entity.Stamp)1 TruthValue (nars.entity.TruthValue)1 EventEmitter (nars.io.events.EventEmitter)1 EventObserver (nars.io.events.EventEmitter.EventObserver)1 Events (nars.io.events.Events)1 TaskDerive (nars.io.events.Events.TaskDerive)1 Conjunction (nars.language.Conjunction)1