5.3 Computed Facts

Computed facts are used when you want to calculate the value for a fact. Although computed facts are not jobs, they use the same JDL syntax.

To create a new computed fact outside of the Orchestrate Development Client, you can create a file with a .cfact extension with the JDL to compute the fact value.

After the new computed fact is created, you deploy it using the same procedures required for jobs (using either the zosadmin command line tool or the Orchestrate Development Client).

The following example shows a computed fact that returns the number of active job instances for a specific job for the current job instance. This fact can be used in an accept or start constraint to limit how many jobs a user can run in the system. The constraint is added to the job policy in which to have the limit. In this example, the start constraint uses this fact to limit the number of active jobs for a user to one:

"""
    <constraint type="start" >
        <lt fact="cfact.activejobs"
            value="1"
            reason="You are only allowed to have 1 job running at a time" />
     </constraint>

Change JOB_TO_CHECK to define which job is to be limited.
"""
JOB_TO_CHECK="quickie"

class activejobs(ComputedFact):

   def compute(self):

          j = self.getContext()
          if j == None:
               # This means computed Fact is executed in a non running

               # job context.  e.g., the ZOC fact browser
               print "no job instance"
               return 0
          else:
               # Computed fact is executing in a job context
               user = j.getFact("user.id")
               activejobs = self.getMatrix().getActiveJobs()
               count = 0
               for j in activejobs:
                    jobname = j.getFact("job.id")

                    # Don't include queued in count !
                    state = j.getFact("jobinstance.state.string")
                    if jobname == JOB_TO_CHECK \
                              and j.getFact("user.id") == user \
                              and (state == "Running" or state == "Starting"):
                         count+=1

               jobid = j.getFact("jobinstance.id")
               print "jobid=%s count=%d" % (jobid,count)
               return count

For another computed fact example, see activejobs.cfact (located in the examples/activejobs.cfact directory).