use of nars.language.Product 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));
}
use of nars.language.Product in project opennars by opennars.
the class MathExpression method getTerms.
public static Term getTerms(ExpressionValue[] data) {
if (data.length == 1)
return getTerm(data[0]);
Term[] c = new Term[data.length];
int j = 0;
for (ExpressionValue t : data) {
c[j++] = getTerm(t);
}
return new Product(c);
}
use of nars.language.Product in project opennars by opennars.
the class InternalExperience method toTerm.
public static Term toTerm(final Sentence s, final Memory mem) {
String opName;
switch(s.punctuation) {
case Symbols.JUDGMENT_MARK:
opName = "^believe";
if (!AllowWantBelieve) {
return null;
}
break;
case Symbols.GOAL_MARK:
opName = "^want";
if (!AllowWantBelieve) {
return null;
}
break;
case Symbols.QUESTION_MARK:
opName = "^wonder";
break;
case Symbols.QUEST_MARK:
opName = "^evaluate";
break;
default:
return null;
}
Term opTerm = mem.getOperator(opName);
Term[] arg = new Term[s.truth == null ? 2 : 3];
arg[0] = Term.SELF;
arg[1] = s.getTerm();
if (s.truth != null) {
arg[2] = s.projection(mem.time(), mem.time()).truth.toWordTerm();
}
// Operation.make ?
Term operation = Inheritance.make(new Product(arg), opTerm);
if (operation == null) {
throw new RuntimeException("Unable to create Inheritance: " + opTerm + ", " + Arrays.toString(arg));
}
return operation;
}
use of nars.language.Product in project opennars by opennars.
the class StructuralRules method transformSubjectPI.
/**
* Equivalent transformation between products and images when the subject is
* a compound {<(*, S, M) --> P>, S@(*, S, M)} |- <S --> (/, P, _, M)> {<S
* --> (/, P, _, M)>, P@(/, P, _, M)} |- <(*, S, M) --> P> {<S --> (/, P, _,
* M)>, M@(/, P, _, M)} |- <M --> (/, P, S, _)>
*
* @param subject The subject term
* @param predicate The predicate term
* @param nal Reference to the memory
*/
private static void transformSubjectPI(short index, CompoundTerm subject, Term predicate, DerivationContext nal) {
TruthValue truth = nal.getCurrentTask().sentence.truth;
BudgetValue budget;
Inheritance inheritance;
Term newSubj, newPred;
if (subject instanceof Product) {
Product product = (Product) subject;
short i = index;
/*for (short i = 0; i < product.size(); i++)*/
{
newSubj = product.term[i];
newPred = ImageExt.make(product, predicate, i);
if (!(newSubj instanceof Interval)) {
// no intervals as subjects
inheritance = Inheritance.make(newSubj, newPred);
if (inheritance != null) {
if (truth == null) {
budget = BudgetFunctions.compoundBackward(inheritance, nal);
} else {
budget = BudgetFunctions.compoundForward(truth, inheritance, nal);
}
nal.singlePremiseTask(inheritance, truth, budget);
}
}
}
} else if (subject instanceof ImageInt) {
ImageInt image = (ImageInt) subject;
int relationIndex = image.relationIndex;
for (short i = 0; i < image.size(); i++) {
if (i == relationIndex) {
newSubj = image.term[relationIndex];
newPred = Product.make(image, predicate, relationIndex);
} else {
newSubj = ImageInt.make(image, predicate, i);
newPred = image.term[i];
}
inheritance = Inheritance.make(newSubj, newPred);
if (inheritance != null) {
if (truth == null) {
budget = BudgetFunctions.compoundBackward(inheritance, nal);
} else {
budget = BudgetFunctions.compoundForward(truth, inheritance, nal);
}
nal.singlePremiseTask(inheritance, truth, budget);
}
}
}
}
use of nars.language.Product in project opennars by opennars.
the class StructuralRules method seqToImage.
public static void seqToImage(Conjunction conj, int index, DerivationContext nal) {
// extensional
int side = 0;
short[] indices = new short[] { (short) side, (short) index };
Product subject = Product.make(conj.term);
Term predicate = Term.SEQ_TEMPORAL;
if (conj.isSpatial) {
predicate = Term.SEQ_SPATIAL;
}
Inheritance inh = Inheritance.make(subject, predicate);
StructuralRules.transformProductImage(inh, inh, indices, nal);
}
Aggregations