Search in sources :

Example 1 with TreeNode

use of org.encog.ml.tree.TreeNode in project opennars by opennars.

the class MathExpression method getTerm.

public static Term getTerm(TreeNode node) {
    CharSequence name = node instanceof ProgramNode ? ("\"" + Texts.escape(((ProgramNode) node).getName()) + '\"') : node.getClass().getSimpleName();
    List<TreeNode> children = node.getChildNodes();
    ExpressionValue[] data = null;
    ProgramNode p = (ProgramNode) node;
    data = p.getData();
    if ((children == null) || (children.isEmpty())) {
        if ((data == null) || (data.length == 0) || (p.isVariable())) {
            if (p.isVariable()) {
                long idx = data[0].toIntValue();
                String varname = p.getOwner().getVariables().getVariableName((int) idx);
                return Term.get(varname);
            }
            return Term.get(name);
        } else
            return getTerms(data);
    }
    if ((data != null) && (data.length > 0))
        return Inheritance.make(new Product(getTerms(children), getTerms(data)), Term.get(name));
    else
        return Inheritance.make(getTerms(children), Term.get(name));
}
Also used : ProgramNode(org.encog.ml.prg.ProgramNode) ExpressionValue(org.encog.ml.prg.expvalue.ExpressionValue) TreeNode(org.encog.ml.tree.TreeNode) Product(nars.language.Product)

Example 2 with TreeNode

use of org.encog.ml.tree.TreeNode in project opennars by opennars.

the class MathExpression method getTerms.

public static Term getTerms(List<TreeNode> children) {
    if (children.size() == 1)
        return getTerm(children.get(0));
    Term[] c = new Term[children.size()];
    int j = 0;
    for (TreeNode t : children) {
        c[j++] = getTerm(t);
    }
    return new Product(c);
}
Also used : TreeNode(org.encog.ml.tree.TreeNode) Product(nars.language.Product) Term(nars.language.Term)

Aggregations

Product (nars.language.Product)2 TreeNode (org.encog.ml.tree.TreeNode)2 Term (nars.language.Term)1 ProgramNode (org.encog.ml.prg.ProgramNode)1 ExpressionValue (org.encog.ml.prg.expvalue.ExpressionValue)1