Search in sources :

Example 1 with NativeOperator

use of nars.io.Symbols.NativeOperator in project opennars by opennars.

the class Equivalence method make.

public static Equivalence make(Term subject, Term predicate, int temporalOrder) {
    // to be extended to check if subject is Conjunction
    if (invalidStatement(subject, predicate) && temporalOrder != TemporalRules.ORDER_FORWARD && temporalOrder != TemporalRules.ORDER_CONCURRENT) {
        return null;
    }
    if ((subject instanceof Implication) || (subject instanceof Equivalence) || (predicate instanceof Implication) || (predicate instanceof Equivalence) || (subject instanceof Interval) || (predicate instanceof Interval)) {
        return null;
    }
    if ((temporalOrder == TemporalRules.ORDER_BACKWARD) || ((subject.compareTo(predicate) > 0) && (temporalOrder != TemporalRules.ORDER_FORWARD))) {
        Term interm = subject;
        subject = predicate;
        predicate = interm;
    }
    NativeOperator copula;
    switch(temporalOrder) {
        case TemporalRules.ORDER_BACKWARD:
            temporalOrder = TemporalRules.ORDER_FORWARD;
        // TODO determine if this missing break is intended
        case TemporalRules.ORDER_FORWARD:
            copula = NativeOperator.EQUIVALENCE_AFTER;
            break;
        case TemporalRules.ORDER_CONCURRENT:
            copula = NativeOperator.EQUIVALENCE_WHEN;
            break;
        default:
            copula = NativeOperator.EQUIVALENCE;
    }
    Term[] t;
    if (temporalOrder == TemporalRules.ORDER_FORWARD)
        t = new Term[] { subject, predicate };
    else
        t = Term.toSortedSetArray(subject, predicate);
    if (t.length != 2)
        return null;
    return new Equivalence(t, temporalOrder);
}
Also used : NativeOperator(nars.io.Symbols.NativeOperator)

Example 2 with NativeOperator

use of nars.io.Symbols.NativeOperator in project opennars by opennars.

the class PrintSymbols method main.

public static void main(String[] args) {
    int relations = 0;
    int innates = 0;
    int symbols = 0;
    System.out.println("string" + "\t\t" + "rel?" + "\t\t" + "innate?" + "\t\t" + "opener?" + "\t\t" + "closer?");
    for (NativeOperator i : Symbols.NativeOperator.values()) {
        System.out.println(i.symbol + "\t\t" + i.relation + "\t\t" + i.isNative + "\t\t" + i.opener + "\t\t" + i.closer);
        if (i.relation)
            relations++;
        if (i.isNative)
            innates++;
        symbols++;
    }
    System.out.println("symbols=" + symbols + ", relations=" + relations + ", innates=" + innates);
}
Also used : NativeOperator(nars.io.Symbols.NativeOperator)

Example 3 with NativeOperator

use of nars.io.Symbols.NativeOperator 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 4 with NativeOperator

use of nars.io.Symbols.NativeOperator 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)

Aggregations

NativeOperator (nars.io.Symbols.NativeOperator)4 Symbols.getOperator (nars.io.Symbols.getOperator)2 Operator (nars.operator.Operator)2 ArrayList (java.util.ArrayList)1 Term (nars.language.Term)1 Operation (nars.operator.Operation)1