Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] [NP-2802] Make predicates ContextAware #4386

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ FOAM_FILES([
{ name: "foam/dao/SequenceNumberDAO" },
{ name: "foam/dao/SequenceNumberDAOTest" },
{ name: "foam/dao/ContextualizingDAO" },
{ name: "foam/dao/PredicateContextualizingDAO" },
{ name: "foam/dao/VersionNoDAO" },
{ name: "foam/dao/sync/SyncRecord" },
{ name: "foam/dao/SyncDAO" },
Expand Down
13 changes: 13 additions & 0 deletions src/foam/dao/EasyDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ foam.CLASS({
'foam.dao.ClientDAO',
'foam.dao.CompoundDAODecorator',
'foam.dao.ContextualizingDAO',
'foam.dao.PredicateContextualizingDAO',
'foam.dao.DeDupDAO',
'foam.dao.InterceptedDAO',
'foam.dao.GUIDDAO',
Expand Down Expand Up @@ -290,6 +291,12 @@ foam.CLASS({
build();
}

if ( getContextualizePredicates() ) {
delegate = new foam.dao.PredicateContextualizingDAO.Builder(getX()).
setDelegate(delegate).
build();
}

if ( getOrder() != null && getOrder().length > 0 ) {
// TODO: CompositeDAO or thenBy
for ( foam.mlang.order.Comparator comp : getOrder() )
Expand Down Expand Up @@ -503,6 +510,12 @@ foam.CLASS({
name: 'contextualize',
value: false
},
{
documentation: 'Contextualize passed predicates with the context passed to select().',
class: 'Boolean',
name: 'contextualizePredicates',
value: false
},
{
class: 'Boolean',
name: 'ruler',
Expand Down
37 changes: 37 additions & 0 deletions src/foam/dao/PredicateContextualizingDAO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
foam.CLASS({
KernelDeimos marked this conversation as resolved.
Show resolved Hide resolved
package: 'foam.dao',
name: 'PredicateContextualizingDAO',
extends: 'foam.dao.ProxyDAO',

javaImports: [
'foam.core.ContextAware',
'foam.core.X'
],

axioms: [
{
name: 'javaExtras',
buildJavaClass: function(cls) {
cls.extras.push(`
public PredicateContextualizingDAO(X x, DAO delegate) {
setX(x);
setDelegate(delegate);
}
`);
}
}
],

methods: [
{
name: 'select_',
javaCode: `
if ( predicate != null && predicate instanceof ContextAware ) {
((ContextAware) predicate).setX(x);
}
return getDelegate().select_(x, sink, skip, limit, order, predicate);
`
}
],

});
58 changes: 58 additions & 0 deletions src/foam/mlang/mlang.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ foam.CLASS({
extends: 'foam.mlang.predicate.AbstractPredicate',
abstract: true,

javaImports: [
'foam.core.ContextAware'
],

documentation: 'Abstract Unary (single-argument) Predicate base-class.',

properties: [
Expand Down Expand Up @@ -582,6 +586,20 @@ foam.CLASS({
javaCode: `
getArg1().authorize(x);
`
},
{
name: 'setX',
args: [
{
name: 'x',
type: 'Context'
}
],
javaCode: `
x_ = x;
if ( getArg1() != null && getArg1() instanceof ContextAware )
((ContextAware) getArg1()).setX(x);
`
}
]
});
Expand Down Expand Up @@ -694,6 +712,10 @@ foam.CLASS({
extends: 'foam.mlang.predicate.AbstractPredicate',
abstract: true,

javaImports: [
'foam.core.ContextAware'
],

documentation: 'Abstract Binary (two-argument) Predicate base-class.',

properties: [
Expand Down Expand Up @@ -764,6 +786,22 @@ getArg2().prepareStatement(stmt);`
getArg1().authorize(x);
getArg2().authorize(x);
`
},
{
name: 'setX',
args: [
{
name: 'x',
type: 'Context'
}
],
javaCode: `
x_ = x;
if ( getArg1() != null && getArg1() instanceof ContextAware )
((ContextAware) getArg1()).setX(x);
if ( getArg2() != null && getArg2() instanceof ContextAware )
((ContextAware) getArg2()).setX(x);
`
}
]
});
Expand All @@ -775,6 +813,10 @@ foam.CLASS({
extends: 'foam.mlang.predicate.AbstractPredicate',
abstract: true,

javaImports: [
'foam.core.ContextAware'
],

documentation: 'Abstract n-ary (many-argument) Predicate base-class.',

properties: [
Expand Down Expand Up @@ -836,6 +878,22 @@ foam.CLASS({
predicate.authorize(x);
}
`
},
{
name: 'setX',
args: [
{
name: 'x',
type: 'Context'
}
],
javaCode: `
x_ = x;
for ( Object obj : getArgs() ) {
if ( obj != null && obj instanceof ContextAware )
((ContextAware) obj).setX(x);
}
`
}
]
});
Expand Down
1 change: 1 addition & 0 deletions tools/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ var classes = [
'foam.dao.index.PersistedIndexTest',
'foam.dao.SequenceNumberDAO',
'foam.dao.SequenceNumberDAOTest',
'foam.dao.PredicateContextualizingDAO',
'foam.dao.PipelinePMDAO',
'foam.dao.PMDAO',
'foam.nanos.pm.PMFactory',
Expand Down