michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #ifndef nsXULTemplateQueryProcessorRDF_h__ michael@0: #define nsXULTemplateQueryProcessorRDF_h__ michael@0: michael@0: #include "nsIRDFContainer.h" michael@0: #include "nsIRDFContainerUtils.h" michael@0: #include "nsIRDFDataSource.h" michael@0: #include "nsIRDFObserver.h" michael@0: #include "nsIRDFService.h" michael@0: #include "nsIXULTemplateBuilder.h" michael@0: #include "nsIXULTemplateQueryProcessor.h" michael@0: #include "nsCollationCID.h" michael@0: michael@0: #include "nsResourceSet.h" michael@0: #include "nsRuleNetwork.h" michael@0: #include "nsRDFQuery.h" michael@0: #include "nsRDFBinding.h" michael@0: #include "nsXULTemplateResultSetRDF.h" michael@0: #include "nsCOMArray.h" michael@0: #include "nsString.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsRefPtrHashtable.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include "prlog.h" michael@0: #ifdef PR_LOGGING michael@0: extern PRLogModuleInfo* gXULTemplateLog; michael@0: #endif michael@0: michael@0: class nsIContent; michael@0: class nsIRDFCompositeDataSource; michael@0: class nsXULTemplateResultRDF; michael@0: michael@0: /** michael@0: * An object that generates results from a query on an RDF graph michael@0: */ michael@0: class nsXULTemplateQueryProcessorRDF MOZ_FINAL : public nsIXULTemplateQueryProcessor, michael@0: public nsIRDFObserver michael@0: { michael@0: public: michael@0: typedef nsTArray > ResultArray; michael@0: michael@0: nsXULTemplateQueryProcessorRDF(); michael@0: michael@0: ~nsXULTemplateQueryProcessorRDF(); michael@0: michael@0: nsresult InitGlobals(); michael@0: michael@0: // nsISupports interface michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsXULTemplateQueryProcessorRDF, michael@0: nsIXULTemplateQueryProcessor) michael@0: michael@0: // nsIXULTemplateQueryProcessor interface michael@0: NS_DECL_NSIXULTEMPLATEQUERYPROCESSOR michael@0: michael@0: // nsIRDFObserver interface michael@0: NS_DECL_NSIRDFOBSERVER michael@0: michael@0: /* michael@0: * Propagate all changes through the rule network when an assertion is michael@0: * added to the graph, adding any new results. michael@0: */ michael@0: nsresult michael@0: Propagate(nsIRDFResource* aSource, michael@0: nsIRDFResource* aProperty, michael@0: nsIRDFNode* aTarget); michael@0: michael@0: /* michael@0: * Retract all changes through the rule network when an assertion is michael@0: * removed from the graph, removing any results that no longer match. michael@0: */ michael@0: nsresult michael@0: Retract(nsIRDFResource* aSource, michael@0: nsIRDFResource* aProperty, michael@0: nsIRDFNode* aTarget); michael@0: michael@0: /* michael@0: * Synchronize results when the graph changes, updating their bindings. michael@0: */ michael@0: nsresult michael@0: SynchronizeAll(nsIRDFResource* aSource, michael@0: nsIRDFResource* aProperty, michael@0: nsIRDFNode* aOldTarget, michael@0: nsIRDFNode* aNewTarget); michael@0: michael@0: /* michael@0: * Return true if a resource is a container michael@0: */ michael@0: nsresult michael@0: CheckContainer(nsIRDFResource* aTargetResource, michael@0: bool* aIsContainer); michael@0: michael@0: /* michael@0: * Check if a resource does not have any children michael@0: */ michael@0: nsresult michael@0: CheckEmpty(nsIRDFResource* aTargetResource, michael@0: bool* aIsEmpty); michael@0: michael@0: /** michael@0: * Check if a resource is a separator michael@0: */ michael@0: nsresult michael@0: CheckIsSeparator(nsIRDFResource* aResource, bool* aIsSeparator); michael@0: michael@0: /* michael@0: * Compute the containment properties which are additional arcs which michael@0: * indicate that a node is a container, in additional to the RDF container michael@0: * tests. The computed list is stored in mContainmentProperties michael@0: */ michael@0: nsresult michael@0: ComputeContainmentProperties(nsIDOMNode* aRootNode); michael@0: michael@0: /** michael@0: * Compile a query that uses the extended template syntax. The last michael@0: * compiled node of the query is returned as aLastNode. This node will michael@0: * have been added to mAllTests which owns the node. michael@0: */ michael@0: nsresult michael@0: CompileExtendedQuery(nsRDFQuery* aQuery, michael@0: nsIContent* aConditions, michael@0: TestNode** aLastNode); michael@0: michael@0: /** michael@0: * Compile a single query child and return the compiled node in aResult. michael@0: * This node will have been added to mAllTests which owns the node and michael@0: * set as a child of aParentNode. michael@0: */ michael@0: virtual nsresult michael@0: CompileQueryChild(nsIAtom* aTag, michael@0: nsRDFQuery* aQuery, michael@0: nsIContent* aConditions, michael@0: TestNode* aParentNode, michael@0: TestNode** aResult); michael@0: michael@0: /** michael@0: * Parse the value of a property test assertion for a condition or a simple michael@0: * rule based on the parseType attribute into the appropriate literal type. michael@0: */ michael@0: nsresult ParseLiteral(const nsString& aParseType, michael@0: const nsString& aValue, michael@0: nsIRDFNode** aResult); michael@0: michael@0: /** michael@0: * Compile a condition and return the compiled node in aResult. michael@0: * This node will have been added to mAllTests which owns the node and michael@0: * set as a child of aParentNode. michael@0: */ michael@0: nsresult michael@0: CompileTripleCondition(nsRDFQuery* aQuery, michael@0: nsIContent* aCondition, michael@0: TestNode* aParentNode, michael@0: TestNode** aResult); michael@0: michael@0: /** michael@0: * Compile a condition and return the compiled node in aResult. michael@0: * This node will have been added to mAllTests which owns the node and michael@0: * set as a child of aParentNode. michael@0: */ michael@0: nsresult michael@0: CompileMemberCondition(nsRDFQuery* aQuery, michael@0: nsIContent* aCondition, michael@0: TestNode* aParentNode, michael@0: TestNode** aResult); michael@0: michael@0: /** michael@0: * Add the default rules shared by all simple queries. This creates michael@0: * the content start node followed by a member test. The member TestNode michael@0: * is returned in aChildNode. Both nodes will have been added to mAllTests michael@0: * which owns the nodes. michael@0: */ michael@0: nsresult michael@0: AddDefaultSimpleRules(nsRDFQuery* aQuery, michael@0: TestNode** aChildNode); michael@0: michael@0: /** michael@0: * Compile a query that's specified using the simple template michael@0: * syntax. Each TestNode is created in a chain, the last compiled node michael@0: * is returned as aLastNode. All nodes will have been added to mAllTests michael@0: * which owns the nodes. michael@0: */ michael@0: nsresult michael@0: CompileSimpleQuery(nsRDFQuery* aQuery, michael@0: nsIContent* aQueryElement, michael@0: TestNode** aLastNode); michael@0: michael@0: RDFBindingSet* michael@0: GetBindingsForRule(nsIDOMNode* aRule); michael@0: michael@0: /* michael@0: * Indicate that a result is dependant on a particular resource. When an michael@0: * assertion is added to or removed from the graph involving that michael@0: * resource, that result must be recalculated. michael@0: */ michael@0: void michael@0: AddBindingDependency(nsXULTemplateResultRDF* aResult, michael@0: nsIRDFResource* aResource); michael@0: michael@0: /** michael@0: * Remove a dependency a result has on a particular resource. michael@0: */ michael@0: void michael@0: RemoveBindingDependency(nsXULTemplateResultRDF* aResult, michael@0: nsIRDFResource* aResource); michael@0: michael@0: /** michael@0: * A memory element is a hash of an RDF triple. One exists for each triple michael@0: * that was involved in generating a result. This function adds this to a michael@0: * map, keyed by memory element, when the value is a list of results that michael@0: * depend on that memory element. When an RDF triple is removed from the michael@0: * datasource, RetractElement is called, and this map is examined to michael@0: * determine which results are no longer valid. michael@0: */ michael@0: nsresult michael@0: AddMemoryElements(const Instantiation& aInst, michael@0: nsXULTemplateResultRDF* aResult); michael@0: michael@0: /** michael@0: * Remove the memory elements associated with a result when the result is michael@0: * no longer being used. michael@0: */ michael@0: nsresult michael@0: RemoveMemoryElements(const Instantiation& aInst, michael@0: nsXULTemplateResultRDF* aResult); michael@0: michael@0: /** michael@0: * Remove the results associated with a memory element since the michael@0: * RDF triple the memory element is a hash of has been removed. michael@0: */ michael@0: void RetractElement(const MemoryElement& aMemoryElement); michael@0: michael@0: /** michael@0: * Return the index of a result's resource in its RDF container michael@0: */ michael@0: int32_t michael@0: GetContainerIndexOf(nsIXULTemplateResult* aResult); michael@0: michael@0: /** michael@0: * Given a result and a predicate to sort on, get the target value of michael@0: * the triple to use for sorting. The sort predicate is the predicate michael@0: * with '?sort=true' appended. michael@0: */ michael@0: nsresult michael@0: GetSortValue(nsIXULTemplateResult* aResult, michael@0: nsIRDFResource* aPredicate, michael@0: nsIRDFResource* aSortPredicate, michael@0: nsISupports** aResultNode); michael@0: michael@0: nsIRDFDataSource* GetDataSource() { return mDB; } michael@0: michael@0: nsIXULTemplateBuilder* GetBuilder() { return mBuilder; } michael@0: michael@0: nsResourceSet& ContainmentProperties() { return mContainmentProperties; } michael@0: michael@0: #ifdef PR_LOGGING michael@0: nsresult michael@0: Log(const char* aOperation, michael@0: nsIRDFResource* aSource, michael@0: nsIRDFResource* aProperty, michael@0: nsIRDFNode* aTarget); michael@0: michael@0: #define LOG(_op, _src, _prop, _targ) \ michael@0: Log(_op, _src, _prop, _targ) michael@0: michael@0: #else michael@0: #define LOG(_op, _src, _prop, _targ) michael@0: #endif michael@0: michael@0: protected: michael@0: // We are an observer of the composite datasource. The cycle is michael@0: // broken when the document is destroyed. michael@0: nsCOMPtr mDB; michael@0: michael@0: // weak reference to the builder, cleared when the document is destroyed michael@0: nsIXULTemplateBuilder* mBuilder; michael@0: michael@0: // true if the query processor has been initialized michael@0: bool mQueryProcessorRDFInited; michael@0: michael@0: // true if results have been generated. Once set, bindings can no longer michael@0: // be added. If they were, the binding value arrays for results that have michael@0: // already been generated would be the wrong size michael@0: bool mGenerationStarted; michael@0: michael@0: // nesting level for RDF batch notifications michael@0: int32_t mUpdateBatchNest; michael@0: michael@0: // containment properties that are checked to determine if a resource is michael@0: // a container michael@0: nsResourceSet mContainmentProperties; michael@0: michael@0: // the end node of the default simple node hierarchy michael@0: TestNode* mSimpleRuleMemberTest; michael@0: michael@0: // the reference variable michael@0: nsCOMPtr mRefVariable; michael@0: michael@0: // the last ref that was calculated, used for simple rules michael@0: nsCOMPtr mLastRef; michael@0: michael@0: /** michael@0: * A map between nsIRDFNodes that form the left-hand side (the subject) of michael@0: * a and an array of nsIXULTemplateResults. When a new assertion michael@0: * is added to the graph involving a particular rdf node, it is looked up michael@0: * in this binding map. If it exists, the corresponding results must then michael@0: * be synchronized. michael@0: */ michael@0: nsClassHashtable mBindingDependencies; michael@0: michael@0: /** michael@0: * A map between memory elements and an array of nsIXULTemplateResults. michael@0: * When a triple is unasserted from the graph, the corresponding results michael@0: * no longer match so they must be removed. michael@0: */ michael@0: nsClassHashtable > mMemoryElementToResultMap; michael@0: michael@0: // map of the rules to the bindings for those rules. michael@0: // XXXndeakin this might be better just as an array since there is usually michael@0: // ten or fewer rules michael@0: nsRefPtrHashtable mRuleToBindingsMap; michael@0: michael@0: /** michael@0: * The queries michael@0: */ michael@0: nsTArray > mQueries; michael@0: michael@0: /** michael@0: * All of the RDF tests in the rule network, which are checked when a new michael@0: * assertion is added to the graph. This is a subset of mAllTests, which michael@0: * also includes non-RDF tests. michael@0: */ michael@0: ReteNodeSet mRDFTests; michael@0: michael@0: /** michael@0: * All of the tests in the rule network, owned by this list michael@0: */ michael@0: ReteNodeSet mAllTests; michael@0: michael@0: // pseudo-constants michael@0: static nsrefcnt gRefCnt; michael@0: michael@0: public: michael@0: static nsIRDFService* gRDFService; michael@0: static nsIRDFContainerUtils* gRDFContainerUtils; michael@0: static nsIRDFResource* kNC_BookmarkSeparator; michael@0: static nsIRDFResource* kRDF_type; michael@0: }; michael@0: michael@0: #endif // nsXULTemplateQueryProcessorRDF_h__