Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Types | Public Member Functions | Protected Attributes
nsRDFConInstanceTestNode Class Reference

Rule network node that tests if a resource is an RDF container, or uses multi-attributes to ``contain'' other elements. More...

#include <nsRDFConInstanceTestNode.h>

Inheritance diagram for nsRDFConInstanceTestNode:
Inheritance graph
[legend]
Collaboration diagram for nsRDFConInstanceTestNode:
Collaboration graph
[legend]

List of all members.

Classes

class  Element

Public Types

enum  Test { eFalse, eTrue, eDontCare }

Public Member Functions

 nsRDFConInstanceTestNode (InnerNode *aParent, nsConflictSet &aConflictSet, nsIRDFDataSource *aDataSource, const nsResourceSet &aMembershipProperties, PRInt32 aContainerVariable, Test aContainer, Test aEmpty)
virtual nsresult FilterInstantiations (InstantiationSet &aInstantiations, void *aClosure) const
 Given a set of instantiations, filter out any that are inconsistent with the test node's test, and append variable-to-value assignments and memory element support for those which do pass the test node's test.
virtual nsresult GetAncestorVariables (VariableSet &aVariables) const
 Retrieve the set of variables that are introduced by this node and any of its ancestors.
virtual PRBool CanPropagate (nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, Instantiation &aInitialBindings) const
 Determine wether the node can propagate an assertion with the specified source, property, and target.
virtual void Retract (nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, nsTemplateMatchSet &aFirings, nsTemplateMatchSet &aRetractions) const
 
  

InnerNodeGetParent () const
 Retrieve the test node's parent.
virtual nsresult Propagate (const InstantiationSet &aInstantiations, void *aClosure)
 Calls FilterInstantiations() on the instantiation set, and if the resulting set isn't empty, propagates the new set down to each of the test node's children.
virtual nsresult Constrain (InstantiationSet &aInstantiations, void *aClosure)
 Calls FilterInstantiations() on the instantiation set, and if the resulting set isn't empty, propagates the new set up to the test node's parent.
virtual PRBool HasAncestor (const ReteNode *aNode) const
 Determine if this node has another node as its direct ancestor.
nsresult AddChild (ReteNode *aNode)
 Add another node as a child of this node.
nsresult RemoveAllChildren ()
 Remove all the children of this node.

Protected Attributes

nsConflictSetmConflictSet
nsCOMPtr< nsIRDFDataSourcemDataSource
const nsResourceSetmMembershipProperties
PRInt32 mContainerVariable
Test mContainer
Test mEmpty
InnerNodemParent
ReteNodeSet mKids

Detailed Description

Rule network node that tests if a resource is an RDF container, or uses multi-attributes to ``contain'' other elements.

Definition at line 54 of file nsRDFConInstanceTestNode.h.


Member Enumeration Documentation

Enumerator:
eFalse 
eTrue 
eDontCare 

Definition at line 57 of file nsRDFConInstanceTestNode.h.


Constructor & Destructor Documentation

nsRDFConInstanceTestNode::nsRDFConInstanceTestNode ( InnerNode aParent,
nsConflictSet aConflictSet,
nsIRDFDataSource aDataSource,
const nsResourceSet aMembershipProperties,
PRInt32  aContainerVariable,
Test  aContainer,
Test  aEmpty 
)

Definition at line 65 of file nsRDFConInstanceTestNode.cpp.

    : nsRDFTestNode(aParent),
      mConflictSet(aConflictSet),
      mDataSource(aDataSource),
      mMembershipProperties(aMembershipProperties),
      mContainerVariable(aContainerVariable),
      mContainer(aContainer),
      mEmpty(aEmpty)
{
#ifdef PR_LOGGING
    if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
        nsCAutoString props;

        nsResourceSet::ConstIterator last = aMembershipProperties.Last();
        nsResourceSet::ConstIterator first = aMembershipProperties.First();
        nsResourceSet::ConstIterator iter;

        for (iter = first; iter != last; ++iter) {
            if (iter != first)
                props += " ";

            const char* str;
            iter->GetValueConst(&str);

            props += str;
        }

        PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
               ("nsRDFConInstanceTestNode[%p]: parent=%p member-props=(%s) container-var=%d container=%s empty=%s",
                this,
                aParent,
                props.get(),
                mContainerVariable,
                TestToString(aContainer),
                TestToString(aEmpty)));
    }
#endif
}

Here is the call graph for this function:


Member Function Documentation

nsresult InnerNode::AddChild ( ReteNode aNode) [inline, inherited]

Add another node as a child of this node.

Parameters:
aNodethe node to add.
Returns:
NS_OK if no errors occur.

Definition at line 973 of file nsRuleNetwork.h.

{ return mKids.Add(aNode); }

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsRDFConInstanceTestNode::CanPropagate ( nsIRDFResource aSource,
nsIRDFResource aProperty,
nsIRDFNode aTarget,
Instantiation aInitialBindings 
) const [virtual]

Determine wether the node can propagate an assertion with the specified source, property, and target.

If the assertion can be propagated, aInitialBindings will be initialized with appropriate variable-to-value assignments to allow the rule network to start a constrain and propagate search from this node in the network.

Returns:
PR_TRUE if the node can propagate the specified assertion.

Implements nsRDFTestNode.

Definition at line 265 of file nsRDFConInstanceTestNode.cpp.

{
    nsresult rv;

    PRBool canpropagate = PR_FALSE;

    nsCOMPtr<nsIRDFContainerUtils> rdfc
        = do_GetService("@mozilla.org/rdf/container-utils;1");

    if (! rdfc)
        return NS_ERROR_FAILURE;

    // We can certainly propagate ordinal properties
    rv = rdfc->IsOrdinalProperty(aProperty, &canpropagate);
    if (NS_FAILED(rv)) return PR_FALSE;

    if (! canpropagate) {
        canpropagate = mMembershipProperties.Contains(aProperty);
    }

#ifdef PR_LOGGING
    if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
        const char* source;
        aSource->GetValueConst(&source);

        const char* property;
        aProperty->GetValueConst(&property);

        nsAutoString target;
        nsXULContentUtils::GetTextForNode(aTarget, target);

        PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
               ("nsRDFConInstanceTestNode[%p]: CanPropagate([%s]==[%s]=>[%s]) => %s",
                this, source, property, NS_ConvertUCS2toUTF8(target).get(),
                canpropagate ? "true" : "false"));
    }
#endif

    if (canpropagate) {
        aInitialBindings.AddAssignment(mContainerVariable, Value(aSource));
        return PR_TRUE;
    }

    return PR_FALSE;
}

Here is the call graph for this function:

nsresult TestNode::Constrain ( InstantiationSet aInstantiations,
void aClosure 
) [virtual, inherited]

Calls FilterInstantiations() on the instantiation set, and if the resulting set isn't empty, propagates the new set up to the test node's parent.

Implements InnerNode.

Definition at line 1065 of file nsRuleNetwork.cpp.

{
    nsresult rv;

    PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
           ("TestNode[%p]: Constrain() begin", this));

    rv = FilterInstantiations(aInstantiations, aClosure);
    if (NS_FAILED(rv)) return rv;

    if (! aInstantiations.Empty()) {
        // if we still have instantiations, then ride 'em on up to the
        // parent to narrow them.

        PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
               ("TestNode[%p]: Constrain() passing to parent %p", this, mParent));

        rv = mParent->Constrain(aInstantiations, aClosure);
    }
    else {
        PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
               ("TestNode[%p]: Constrain() failed", this));

        rv = NS_OK;
    }

    PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
           ("TestNode[%p]: Constrain() end", this));

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsRDFConInstanceTestNode::FilterInstantiations ( InstantiationSet aInstantiations,
void aClosure 
) const [virtual]

Given a set of instantiations, filter out any that are inconsistent with the test node's test, and append variable-to-value assignments and memory element support for those which do pass the test node's test.

Parameters:
aInstantiationsthe set of instantiations to be filtered
aClosureapplication-specific data that is to be passed through the network.
Returns:
NS_OK if no errors occurred.

Implements TestNode.

Definition at line 111 of file nsRDFConInstanceTestNode.cpp.

{
    nsresult rv;

    nsCOMPtr<nsIRDFContainerUtils> rdfc
        = do_GetService("@mozilla.org/rdf/container-utils;1");

    if (! rdfc)
        return NS_ERROR_FAILURE;

    InstantiationSet::Iterator last = aInstantiations.Last();
    for (InstantiationSet::Iterator inst = aInstantiations.First(); inst != last; ++inst) {
        Value value;
        if (! inst->mAssignments.GetAssignmentFor(mContainerVariable, &value)) {
            NS_ERROR("can't do unbounded container testing");
            return NS_ERROR_UNEXPECTED;
        }

#ifdef PR_LOGGING
        if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
            const char* container;
            VALUE_TO_IRDFRESOURCE(value)->GetValueConst(&container);

            PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
                   ("nsRDFConInstanceTestNode[%p]::FilterInstantiations() container=[%s]",
                    this, container));
        }
#endif

        nsCOMPtr<nsIRDFContainer> rdfcontainer;

        PRBool isRDFContainer;
        rv = rdfc->IsContainer(mDataSource, VALUE_TO_IRDFRESOURCE(value), &isRDFContainer);
        if (NS_FAILED(rv)) return rv;

        if (mEmpty != eDontCare || mContainer != eDontCare) {
            Test empty = eDontCare;
            Test container = eDontCare;

            if (isRDFContainer) {
                // It's an RDF container. Use the container utilities
                // to deduce what's in it.
                container = eTrue;

                // XXX should cache the factory
                rdfcontainer = do_CreateInstance("@mozilla.org/rdf/container;1", &rv);
                if (NS_FAILED(rv)) return rv;

                rv = rdfcontainer->Init(mDataSource, VALUE_TO_IRDFRESOURCE(value));
                if (NS_FAILED(rv)) return rv;

                PRInt32 count;
                rv = rdfcontainer->GetCount(&count);
                if (NS_FAILED(rv)) return rv;

                empty = (count == 0) ? eTrue : eFalse;
            } else {
                empty = eTrue;
                container = eFalse;

                // First do the simple check of finding some outward
                // arcs; mMembershipProperties should be short, so this can
                // save us time from dealing with an iterator later on
                for (nsResourceSet::ConstIterator property = mMembershipProperties.First();
                     property != mMembershipProperties.Last();
                     ++property) {
                    nsCOMPtr<nsIRDFNode> target;
                    rv = mDataSource->GetTarget(VALUE_TO_IRDFRESOURCE(value), *property, PR_TRUE, getter_AddRefs(target));
                    if (NS_FAILED(rv)) return rv;

                    if (target != nsnull) {
                        // bingo. we found one.
                        empty = eFalse;
                        container = eTrue;
                        break;
                    }
                }

                // if we still don't think its a container, but we
                // want to know for sure whether it is or not, we need
                // to check ArcLabelsOut for potential container arcs.
                if (container == eFalse && mContainer != eDontCare) {
                    nsCOMPtr<nsISimpleEnumerator> arcsout;
                    rv = mDataSource->ArcLabelsOut(VALUE_TO_IRDFRESOURCE(value), getter_AddRefs(arcsout));
                    if (NS_FAILED(rv)) return rv;

                    while (1) {
                        PRBool hasmore;
                        rv = arcsout->HasMoreElements(&hasmore);
                        if (NS_FAILED(rv)) return rv;

                        if (! hasmore)
                            break;

                        nsCOMPtr<nsISupports> isupports;
                        rv = arcsout->GetNext(getter_AddRefs(isupports));
                        if (NS_FAILED(rv)) return rv;

                        nsCOMPtr<nsIRDFResource> property = do_QueryInterface(isupports);
                        NS_ASSERTION(property != nsnull, "not a property");
                        if (! property)
                            return NS_ERROR_UNEXPECTED;

                        if (mMembershipProperties.Contains(property)) {
                            container = eTrue;
                            break;
                        }
                    }
                }
            }

            PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
                   ("    empty => %s",
                    (empty == mEmpty) ? "consistent" : "inconsistent"));

            PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
                   ("    container => %s",
                    (container == mContainer) ? "consistent" : "inconsistent"));

            if (((mEmpty == empty) && (mContainer == container)) ||
                ((mEmpty == eDontCare) && (mContainer == container)) ||
                ((mContainer == eDontCare) && (mEmpty == empty)))
            {
                Element* element =
                    nsRDFConInstanceTestNode::Element::Create(mConflictSet.GetPool(),
                                                              VALUE_TO_IRDFRESOURCE(value),
                                                              container, empty);

                if (! element)
                    return NS_ERROR_OUT_OF_MEMORY;

                inst->AddSupportingElement(element);
            }
            else {
                aInstantiations.Erase(inst--);
            }
        }
    }

    return NS_OK;
}

Here is the call graph for this function:

Retrieve the set of variables that are introduced by this node and any of its ancestors.

To correctly implement this method, a node must add any variables that it introduces to the variable set, and then recursively call GetAncestorVariables() on its parent (or parents).

Parameters:
aVariablesThe variable set to which the callee will add its variables, and its ancestors variables.
Returns:
NS_OK if no errors occur.

Reimplemented from TestNode.

Definition at line 254 of file nsRDFConInstanceTestNode.cpp.

{
    nsresult rv;

    rv = aVariables.Add(mContainerVariable);
    if (NS_FAILED(rv)) return rv;

    return TestNode::GetAncestorVariables(aVariables);
}

Here is the call graph for this function:

InnerNode* TestNode::GetParent ( ) const [inline, inherited]

Retrieve the test node's parent.

Returns:
the test node's parent

Definition at line 1065 of file nsRuleNetwork.h.

{ return mParent; }

Here is the caller graph for this function:

PRBool TestNode::HasAncestor ( const ReteNode aNode) const [virtual, inherited]

Determine if this node has another node as its direct ancestor.

Parameters:
aNodethe node to look for.
Returns:
PR_TRUE if aNode is a direct ancestor of this node, PR_FALSE otherwise.

Implements InnerNode.

Definition at line 1106 of file nsRuleNetwork.cpp.

{
    return aNode == this ? PR_TRUE : mParent->HasAncestor(aNode);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult TestNode::Propagate ( const InstantiationSet aInstantiations,
void aClosure 
) [virtual, inherited]

Calls FilterInstantiations() on the instantiation set, and if the resulting set isn't empty, propagates the new set down to each of the test node's children.

Implements ReteNode.

Definition at line 1036 of file nsRuleNetwork.cpp.

{
    nsresult rv;

    PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
           ("TestNode[%p]: Propagate() begin", this));

    InstantiationSet instantiations = aInstantiations;
    rv = FilterInstantiations(instantiations, aClosure);
    if (NS_FAILED(rv)) return rv;

    if (! instantiations.Empty()) {
        ReteNodeSet::Iterator last = mKids.Last();
        for (ReteNodeSet::Iterator kid = mKids.First(); kid != last; ++kid) {
            PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
                   ("TestNode[%p]: Propagate() passing to child %p", this, kid.operator->()));

            kid->Propagate(instantiations, aClosure);
        }
    }

    PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
           ("TestNode[%p]: Propagate() end", this));

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult InnerNode::RemoveAllChildren ( ) [inline, inherited]

Remove all the children of this node.

Returns:
NS_OK if no errors occur.

Definition at line 979 of file nsRuleNetwork.h.

{ return mKids.Clear(); }

Here is the call graph for this function:

Here is the caller graph for this function:

void nsRDFConInstanceTestNode::Retract ( nsIRDFResource aSource,
nsIRDFResource aProperty,
nsIRDFNode aTarget,
nsTemplateMatchSet aFirings,
nsTemplateMatchSet aRetractions 
) const [virtual]

  

Implements nsRDFTestNode.

Definition at line 315 of file nsRDFConInstanceTestNode.cpp.

{
    // XXXwaterson oof. complicated. figure this out.
    if (0) {
        mConflictSet.Remove(Element(aSource, mContainer, mEmpty), aFirings, aRetractions);
    }
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 142 of file nsRDFConInstanceTestNode.h.

Definition at line 146 of file nsRDFConInstanceTestNode.h.

Definition at line 145 of file nsRDFConInstanceTestNode.h.

Definition at line 143 of file nsRDFConInstanceTestNode.h.

Definition at line 147 of file nsRDFConInstanceTestNode.h.

ReteNodeSet InnerNode::mKids [protected, inherited]

Definition at line 982 of file nsRuleNetwork.h.

Definition at line 144 of file nsRDFConInstanceTestNode.h.

InnerNode* TestNode::mParent [protected, inherited]

Definition at line 1100 of file nsRuleNetwork.h.


The documentation for this class was generated from the following files: