Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 556

db4o-doc-8.0-1.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Basics Operations &amp; Concepts|Querying|SODA Query" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>SODA Evaluations</title>
        <script type="text/javascript">/* <![CDATA[ */
window.onload = function(){
	var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf';
	ZeroClipboard.setMoviePath(pathToFlash);
			
	function bindToClipBord(element,content){
		var clip = new ZeroClipboard.Client();
		clip.setText(content);
		clip.glue(element);
	};
		
	if(location.protocol==='file:'){
		$('.copylink-marker').remove();
	} else{
			$('.copylink-marker').each(function(){
				var text = $(this).parent().parent().children('.prettyprint').html();
				$(this).hover(function(){
					bindToClipBord(this,text);
				},
				function(){});
			});	
	}		
	prettyPrint();	
};
                /* ]]> */</script>
        <link href="../../../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" />
        <script src="../../../Resources/Code/prettify.js">
        </script>
        <script src="../../../Resources/Code/lang-vb.js">
        </script>
        <script src="../../../Resources/Code/jquery.min.js">
        </script>
        <script src="../../../Resources/Code/ZeroClipboard.js">
        </script>
        <script src="../../../SkinSupport/MadCapAll.js" type="text/javascript">
        </script>
    </head>
    <body>
        <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../index_CSH.html#basics/querying/soda/soda_evaluations.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../basics.htm">Basics Operations &amp; Concepts</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../querying.htm">Querying</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../soda_query.htm">SODA Query</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Simple Evaluation</span>
        </div>
        <p>
            <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">');
				document.write("Direct Link");
			document.write('</a>');/*]]>*/</script>
        </p>
        <p>
        </p>
        <h1>SODA Evaluations</h1>
        <p>Sometimes the capabilities of regular SODA-queries is not enough. In such cases you can add evaluations to the SODA-query. A evaluation is a piece of code which runs against objects.</p>
        <p>To use a evaluation, you need to pass an instance of the <span class="PrimaryEvaluation">IEvaluation</span>-interface as a constrain. db4o will call the match-method of that interface. Implement the match-method of the <span class="PrimaryEvaluation">IEvaluation</span>-interface. In the match-method you can get the candidate-object and the object-container. Compare the object and when it matches, pass true to the include-method. Otherwise pass false.</p>
        <p>While SODA evaluations are extremely powerful they are also slow. In order to run the evaluation the objects need to be instantiated from the database and then processed by the evaluator. This means that you should use evaluations only when there's no other possibility.</p>
        <h2><a name="SimpleEvaluation"></a>Simple Evaluation</h2>
        <p>Here's an example for a simple evaluation. This evaluation filters pilots by the age and picks only pilots with an odd-number as age.</p>
        <p>First we need to create the evaluation-class:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">class OnlyOddAge : IEvaluation
{
    public void Evaluate(ICandidate candidate)
    {
        Pilot pilot = (Pilot)candidate.GetObject();
        candidate.Include(pilot.Age % 2 != 0);
    }
}
</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.cs: Simple evaluation which includes only odd aged pilots
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Class OnlyOddAge
    Implements IEvaluation
    Public Sub Evaluate(ByVal candidate As ICandidate) _
        Implements IEvaluation.Evaluate

        Dim pilot As Pilot = DirectCast(candidate.GetObject(), Pilot)
        candidate.Include(pilot.Age Mod 2 &lt;&gt; 0)
    End Sub
End Class
</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.vb: Simple evaluation which includes only odd aged pilots
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>After that, you can use the evaluation in the SODA-query. An evaluation is added as a regular constrain.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Constrain(new OnlyOddAge());

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.cs: Simple evaluation
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Constrain(New OnlyOddAge())

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.vb: Simple evaluation
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <h2><a name="EvaluationOnField"></a>Evaluation on Field</h2>
        <p>It's also possible to use the evaluation on a certain field. For this you descend into the field on which the evaluation should be applied. After that, specify the evaluation as a constrain on that field.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Car));
query.Descend("pilot").Constrain(new OnlyOddAge());

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.cs: Evaluation on field
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim query As IQuery = container.Query()
query.Constrain(GetType(Car))
query.Descend("pilot").Constrain(New OnlyOddAge())

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.vb: Evaluation on field
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <h2><a name="kanchor19"></a><a name="RegexEvaluation"></a>Regex on Fields</h2>
        <p>Evaluation also allow you to add very specific additional query capabilities. On of the most useful ones is regular expressions. First create a regular expression evaluation:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">class RegexConstrain : IEvaluation
{
    private readonly Regex pattern;

    public RegexConstrain(String pattern)
    {
        this.pattern = new Regex(pattern);
    }

    public void Evaluate(ICandidate candidate)
    {
        string stringValue = (string)candidate.GetObject();
        candidate.Include(pattern.IsMatch(stringValue));
    }
}</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.cs: Regex Evaluator
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Class RegexConstrain
    Implements IEvaluation
    Private ReadOnly pattern As Regex

    Public Sub New(ByVal pattern As [String])
        Me.pattern = New Regex(pattern)
    End Sub

    Public Sub Evaluate(ByVal candidate As ICandidate) _
        Implements IEvaluation.Evaluate
        Dim stringValue As String = DirectCast(candidate.GetObject(), String)
        candidate.Include(pattern.IsMatch(stringValue))
    End Sub
End Class</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.vb: Regex Evaluator
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>After that you can use it on any string field:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("name").Constrain(new RegexConstrain("J.*nn.*"));

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.cs: Regex-evaluation on a field
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("name").Constrain(New RegexConstrain("J.*nn.*"))

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaEvaluationExamples.vb: Regex-evaluation on a field
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p style="font-weight: bold;">&#160;</p>
        <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>