use of nars.language.SetInt in project opennars by opennars.
the class Count method function.
@Override
protected Term function(Memory memory, Term[] x) {
if (x.length != 1) {
throw new RuntimeException(requireMessage);
}
Term content = x[0];
if (!(content instanceof SetExt) && !(content instanceof SetInt)) {
throw new RuntimeException(requireMessage);
}
int n = ((CompoundTerm) content).size();
return Term.get(n);
}
use of nars.language.SetInt in project opennars by opennars.
the class CompositionalRules method decomposeCompound.
/**
* {<(S|P) ==> M>, <P ==> M>} |- <S ==> M>
*
* @param implication The implication term to be decomposed
* @param componentCommon The part of the implication to be removed
* @param term1 The other term in the contentInd
* @param index The location of the shared term: 0 for subject, 1 for
* predicate
* @param compoundTask Whether the implication comes from the task
* @param nal Reference to the memory
*/
private static void decomposeCompound(CompoundTerm compound, Term component, Term term1, int index, boolean compoundTask, int order, DerivationContext nal) {
if ((compound instanceof Statement) || (compound instanceof ImageExt) || (compound instanceof ImageInt)) {
return;
}
Term term2 = reduceComponents(compound, component, nal.mem());
if (term2 == null) {
return;
}
long delta = 0;
while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) term2).term[0];
delta += interval.time;
term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
Statement oldContent = (Statement) task.getTerm();
TruthValue v1, v2;
if (compoundTask) {
v1 = sentence.truth;
v2 = belief.truth;
} else {
v1 = belief.truth;
v2 = sentence.truth;
}
TruthValue truth = null;
Term content;
if (index == 0) {
content = Statement.make(oldContent, term1, term2, order);
if (content == null) {
return;
}
if (oldContent instanceof Inheritance) {
if (compound instanceof IntersectionExt) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof IntersectionInt) {
truth = reduceDisjunction(v1, v2);
} else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
truth = reduceConjunction(v1, v2);
} else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
truth = reduceDisjunction(v1, v2);
} else if (compound instanceof DifferenceExt) {
if (compound.term[0].equals(component)) {
truth = reduceDisjunction(v2, v1);
} else {
truth = reduceConjunctionNeg(v1, v2);
}
}
} else if (oldContent instanceof Implication) {
if (compound instanceof Conjunction) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof Disjunction) {
truth = reduceDisjunction(v1, v2);
}
}
} else {
content = Statement.make(oldContent, term2, term1, order);
if (content == null) {
return;
}
if (oldContent instanceof Inheritance) {
if (compound instanceof IntersectionInt) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof IntersectionExt) {
truth = reduceDisjunction(v1, v2);
} else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
truth = reduceConjunction(v1, v2);
} else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
truth = reduceDisjunction(v1, v2);
} else if (compound instanceof DifferenceInt) {
if (compound.term[1].equals(component)) {
truth = reduceDisjunction(v2, v1);
} else {
truth = reduceConjunctionNeg(v1, v2);
}
}
} else if (oldContent instanceof Implication) {
if (compound instanceof Disjunction) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof Conjunction) {
truth = reduceDisjunction(v1, v2);
}
}
}
if (truth != null) {
BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
if (delta != 0) {
long baseTime = task.sentence.getOccurenceTime();
if (baseTime != Stamp.ETERNAL) {
baseTime += delta;
nal.getTheNewStamp().setOccurrenceTime(baseTime);
}
}
// (allow overlap), a form of detachment
nal.doublePremiseTask(content, truth, budget, false, true);
}
}
use of nars.language.SetInt in project opennars by opennars.
the class RuleTables method compoundAndStatement.
/**
* Inference between a compound term and a statement
*
* @param compound The compound term
* @param index The location of the current term in the compound
* @param statement The statement
* @param side The location of the current term in the statement
* @param beliefTerm The content of the belief
* @param nal Reference to the memory
*/
private static void compoundAndStatement(CompoundTerm compound, short index, Statement statement, short side, Term beliefTerm, DerivationContext nal) {
if (index >= compound.term.length) {
return;
}
Term component = compound.term[index];
Task task = nal.getCurrentTask();
if (component.getClass() == statement.getClass()) {
if ((compound instanceof Conjunction) && (nal.getCurrentBelief() != null)) {
Conjunction conj = (Conjunction) compound;
Term[] u = new Term[] { compound, statement };
if (Variables.unify(VAR_DEPENDENT, component, statement, u)) {
compound = (Conjunction) u[0];
statement = (Statement) u[1];
if (// only allow dep var elimination
conj.isSpatial || compound.getTemporalOrder() != TemporalRules.ORDER_FORWARD || index == 0) {
// for (&/ on first component!!
SyllogisticRules.elimiVarDep(compound, component, statement.equals(beliefTerm), nal);
}
} else if (task.sentence.isJudgment()) {
// && !compound.containsTerm(component)) {
CompositionalRules.introVarInner(statement, (Statement) component, compound, nal);
}
}
} else {
if (task.sentence.isJudgment()) {
if (statement instanceof Inheritance) {
StructuralRules.structuralCompose1(compound, index, statement, nal);
if (!(compound instanceof SetExt || compound instanceof SetInt || compound instanceof Negation)) {
StructuralRules.structuralCompose2(compound, index, statement, side, nal);
}
// {A --> B, A @ (A&C)} |- (A&C) --> (B&C)
} else if ((statement instanceof Similarity) && !(compound instanceof Conjunction)) {
StructuralRules.structuralCompose2(compound, index, statement, side, nal);
}
// {A <-> B, A @ (A&C)} |- (A&C) <-> (B&C)
}
}
}
use of nars.language.SetInt in project opennars by opennars.
the class StructuralRules method transformSetRelation.
/* -------------------- set transform -------------------- */
/**
* {<S --> {P}>} |- <S <-> {P}>
*
* @param compound The set compound
* @param statement The premise
* @param side The location of the indicated term in the premise
* @param nal Reference to the memory
*/
static void transformSetRelation(CompoundTerm compound, Statement statement, short side, DerivationContext nal) {
if (compound.size() > 1) {
return;
}
if (statement instanceof Inheritance) {
if (((compound instanceof SetExt) && (side == 0)) || ((compound instanceof SetInt) && (side == 1))) {
return;
}
}
Term sub = statement.getSubject();
Term pre = statement.getPredicate();
Statement content;
if (statement instanceof Inheritance) {
content = Similarity.make(sub, pre);
} else {
if (((compound instanceof SetExt) && (side == 0)) || ((compound instanceof SetInt) && (side == 1))) {
content = Inheritance.make(pre, sub);
} else {
content = Inheritance.make(sub, pre);
}
}
if (content == null) {
return;
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
TruthValue truth = sentence.truth;
BudgetValue budget;
if (sentence.isJudgment()) {
budget = BudgetFunctions.compoundForward(truth, content, nal);
} else {
budget = BudgetFunctions.compoundBackward(content, nal);
}
nal.singlePremiseTask(content, truth, budget);
}
use of nars.language.SetInt in project opennars by opennars.
the class StructuralRules method structuralDecompose1.
/**
* {<(S|T) --> P>, S@(S|T)} |- <S --> P> {<S --> (P&T)>, P@(P&T)} |- <S --> P>
*
* @param compound The compound term
* @param index The location of the indicated term in the compound
* @param statement The premise
* @param nal Reference to the memory
*/
static void structuralDecompose1(CompoundTerm compound, short index, Statement statement, DerivationContext nal) {
if (index >= compound.term.length) {
return;
}
Term component = compound.term[index];
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
int order = sentence.getTemporalOrder();
TruthValue truth = sentence.truth;
if (truth == null) {
return;
}
final float reliance = Parameters.reliance;
TruthValue truthDed = TruthFunctions.deduction(truth, reliance);
TruthValue truthNDed = TruthFunctions.negation(TruthFunctions.deduction(truth, reliance));
Term subj = statement.getSubject();
Term pred = statement.getPredicate();
if (compound.equals(subj)) {
if (compound instanceof IntersectionInt) {
structuralStatement(component, pred, order, truthDed, nal);
} else if ((compound instanceof SetExt) && (compound.size() > 1)) {
Term[] t1 = new Term[] { component };
structuralStatement(new SetExt(t1), pred, order, truthDed, nal);
} else if (compound instanceof DifferenceInt) {
if (index == 0) {
structuralStatement(component, pred, order, truthDed, nal);
} else {
structuralStatement(component, pred, order, truthNDed, nal);
}
}
} else if (compound.equals(pred)) {
if (compound instanceof IntersectionExt) {
structuralStatement(subj, component, order, truthDed, nal);
} else if ((compound instanceof SetInt) && (compound.size() > 1)) {
structuralStatement(subj, new SetInt(component), order, truthDed, nal);
} else if (compound instanceof DifferenceExt) {
if (index == 0) {
structuralStatement(subj, component, order, truthDed, nal);
} else {
structuralStatement(subj, component, order, truthNDed, nal);
}
}
}
}
Aggregations