Beispiel #1
0
 protected List<Rule> createRuleList(PMML pmmlDocument) {
   List<Rule> ruleList = new ArrayList<Rule>();
   for (Object obj : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
     if (obj instanceof Scorecard) {
       Scorecard scorecard = (Scorecard) obj;
       Characteristics characteristics = getCharacteristicsFromScorecard(scorecard);
       createInitialRule(ruleList, scorecard);
       for (org.dmg.pmml.pmml_4_1.descr.Characteristic c : characteristics.getCharacteristics()) {
         int attributePosition = 0;
         for (org.dmg.pmml.pmml_4_1.descr.Attribute scoreAttribute : c.getAttributes()) {
           String name =
               formRuleName(
                   pmmlDocument, scorecard.getModelName().replaceAll(" ", ""), c, scoreAttribute);
           Rule rule = new Rule(name, 99, 1);
           String desc =
               ScorecardPMMLUtils.getExtensionValue(scoreAttribute.getExtensions(), "description");
           if (desc != null) {
             rule.setDescription(desc);
           }
           attributePosition++;
           populateLHS(rule, pmmlDocument, scorecard, c, scoreAttribute);
           populateRHS(rule, pmmlDocument, scorecard, c, scoreAttribute, attributePosition);
           ruleList.add(rule);
         }
       }
       createSummationRules(ruleList, scorecard);
     }
   }
   return ruleList;
 }
 private static Marshaller initContext(PMML pmml) throws JAXBException {
   JAXBContext pContext = JAXBContext.newInstance(pmml.getClass().getPackage().getName());
   Marshaller marshaller = pContext.createMarshaller();
   marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
   marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
   return marshaller;
 }
Beispiel #3
0
  protected void addDeclaredTypes(PMML pmml, Package aPackage) {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("\ndeclare DroolsScorecard\nend\n\n");

    for (Object obj : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
      if (obj instanceof Scorecard) {
        Scorecard scorecard = (Scorecard) obj;
        stringBuilder
            .append("declare ")
            .append(scorecard.getModelName().replaceAll(" ", ""))
            .append(" extends DroolsScorecard\n");

        addDeclaredTypeContents(pmml, stringBuilder, scorecard);

        stringBuilder.append("end\n");
      }
    }
    aPackage.addDeclaredType(stringBuilder.toString());
  }
Beispiel #4
0
  public String emitDRL(PMML pmml) {
    List<Rule> ruleList = createRuleList(pmml);
    String pkgName =
        ScorecardPMMLUtils.getExtensionValue(
            pmml.getHeader().getExtensions(), PMMLExtensionNames.SCORECARD_PACKAGE);
    org.drools.template.model.Package aPackage = new org.drools.template.model.Package(pkgName);

    DRLOutput drlOutput = new DRLOutput();
    for (Rule rule : ruleList) {
      aPackage.addRule(rule);
    }

    addDeclaredTypes(pmml, aPackage);

    addImports(pmml, aPackage);

    addGlobals(pmml, aPackage);

    internalEmitDRL(pmml, ruleList, aPackage);

    aPackage.renderDRL(drlOutput);
    String drl = drlOutput.getDRL();
    return drl;
  }
Beispiel #5
0
 private void addImports(PMML pmml, Package aPackage) {
   String importsFromDelimitedString =
       ScorecardPMMLUtils.getExtensionValue(
           pmml.getHeader().getExtensions(), PMMLExtensionNames.SCORECARD_IMPORTS);
   if (!(importsFromDelimitedString == null || importsFromDelimitedString.isEmpty())) {
     for (String importStatement : importsFromDelimitedString.split(",")) {
       Import imp = new Import();
       imp.setClassName(importStatement);
       aPackage.addImport(imp);
     }
   }
   Import defaultScorecardImport = new Import();
   defaultScorecardImport.setClassName("org.drools.scorecards.DroolsScorecard");
   aPackage.addImport(defaultScorecardImport);
   defaultScorecardImport = new Import();
   defaultScorecardImport.setClassName("org.drools.scorecards.PartialScore");
   aPackage.addImport(defaultScorecardImport);
   defaultScorecardImport = new Import();
   defaultScorecardImport.setClassName("org.drools.scorecards.InitialScore");
   aPackage.addImport(defaultScorecardImport);
   defaultScorecardImport = new Import();
   defaultScorecardImport.setClassName("org.drools.scorecards.BaselineScore");
   aPackage.addImport(defaultScorecardImport);
 }
  public static PMML generateSimpleNeuralNetwork(
      String modelName,
      String[] inputfieldNames,
      String[] outputfieldNames,
      double[] inputMeans,
      double[] inputStds,
      double[] outputMeans,
      double[] outputStds,
      int hiddenSize,
      double[] weights) {

    int counter = 0;
    int wtsIndex = 0;
    PMML pmml = new PMML();
    pmml.setVersion("4.0");

    Header header = new Header();
    Application app = new Application();
    app.setName("Drools PMML Generator");
    app.setVersion("0.01 Alpha");
    header.setApplication(app);

    header.setCopyright("BSD");

    header.setDescription(" Smart Vent Model ");

    Timestamp ts = new Timestamp();
    ts.getContent().add(new java.util.Date().toString());
    header.setTimestamp(ts);

    pmml.setHeader(header);

    DataDictionary dic = new DataDictionary();
    dic.setNumberOfFields(BigInteger.valueOf(inputfieldNames.length + outputfieldNames.length));
    for (String ifld : inputfieldNames) {
      DataField dataField = new DataField();
      dataField.setName(ifld);
      dataField.setDataType(DATATYPE.DOUBLE);
      dataField.setDisplayName(ifld);
      dataField.setOptype(OPTYPE.CONTINUOUS);
      dic.getDataFields().add(dataField);
    }
    for (String ofld : outputfieldNames) {
      DataField dataField = new DataField();
      dataField.setName(ofld);
      dataField.setDataType(DATATYPE.DOUBLE);
      dataField.setDisplayName(ofld);
      dataField.setOptype(OPTYPE.CONTINUOUS);
      dic.getDataFields().add(dataField);
    }

    pmml.setDataDictionary(dic);

    NeuralNetwork nnet = new NeuralNetwork();
    nnet.setActivationFunction(ACTIVATIONFUNCTION.LOGISTIC);
    nnet.setFunctionName(MININGFUNCTION.REGRESSION);
    nnet.setNormalizationMethod(NNNORMALIZATIONMETHOD.NONE);
    nnet.setModelName(modelName);

    MiningSchema miningSchema = new MiningSchema();
    for (String ifld : inputfieldNames) {
      MiningField mfld = new MiningField();
      mfld.setName(ifld);
      mfld.setOptype(OPTYPE.CONTINUOUS);
      mfld.setUsageType(FIELDUSAGETYPE.ACTIVE);
      miningSchema.getMiningFields().add(mfld);
    }
    for (String ofld : outputfieldNames) {
      MiningField mfld = new MiningField();
      mfld.setName(ofld);
      mfld.setOptype(OPTYPE.CONTINUOUS);
      mfld.setUsageType(FIELDUSAGETYPE.PREDICTED);
      miningSchema.getMiningFields().add(mfld);
    }

    nnet.getExtensionsAndNeuralLayersAndNeuralInputs().add(miningSchema);

    Output outputs = new Output();
    for (String ofld : outputfieldNames) {
      OutputField outFld = new OutputField();
      outFld.setName("Out_" + ofld);
      outFld.setTargetField(ofld);
      outputs.getOutputFields().add(outFld);
    }

    nnet.getExtensionsAndNeuralLayersAndNeuralInputs().add(outputs);

    NeuralInputs nins = new NeuralInputs();
    nins.setNumberOfInputs(BigInteger.valueOf(inputfieldNames.length));

    for (int j = 0; j < inputfieldNames.length; j++) {
      String ifld = inputfieldNames[j];
      NeuralInput nin = new NeuralInput();
      nin.setId("" + counter++);
      DerivedField der = new DerivedField();
      der.setDataType(DATATYPE.DOUBLE);
      der.setOptype(OPTYPE.CONTINUOUS);
      NormContinuous nc = new NormContinuous();
      nc.setField(ifld);
      nc.setOutliers(OUTLIERTREATMENTMETHOD.AS_IS);
      LinearNorm lin1 = new LinearNorm();
      lin1.setOrig(0);
      lin1.setNorm(-inputMeans[j] / inputStds[j]);
      nc.getLinearNorms().add(lin1);
      LinearNorm lin2 = new LinearNorm();
      lin2.setOrig(inputMeans[j]);
      lin2.setNorm(0);
      nc.getLinearNorms().add(lin2);
      der.setNormContinuous(nc);
      nin.setDerivedField(der);
      nins.getNeuralInputs().add(nin);
    }

    nnet.getExtensionsAndNeuralLayersAndNeuralInputs().add(nins);

    NeuralLayer hidden = new NeuralLayer();
    hidden.setNumberOfNeurons(BigInteger.valueOf(hiddenSize));

    for (int j = 0; j < hiddenSize; j++) {
      Neuron n = new Neuron();
      n.setId("" + counter++);
      n.setBias(weights[wtsIndex++]);
      for (int k = 0; k < inputfieldNames.length; k++) {
        Synapse con = new Synapse();
        con.setFrom("" + k);
        con.setWeight(weights[wtsIndex++]);
        n.getCons().add(con);
      }
      hidden.getNeurons().add(n);
    }

    nnet.getExtensionsAndNeuralLayersAndNeuralInputs().add(hidden);

    NeuralLayer outer = new NeuralLayer();
    outer.setActivationFunction(ACTIVATIONFUNCTION.IDENTITY);
    outer.setNumberOfNeurons(BigInteger.valueOf(outputfieldNames.length));

    for (int j = 0; j < outputfieldNames.length; j++) {
      Neuron n = new Neuron();
      n.setId("" + counter++);
      n.setBias(weights[wtsIndex++]);
      for (int k = 0; k < hiddenSize; k++) {
        Synapse con = new Synapse();
        con.setFrom("" + (k + inputfieldNames.length));
        con.setWeight(weights[wtsIndex++]);
        n.getCons().add(con);
      }
      outer.getNeurons().add(n);
    }

    nnet.getExtensionsAndNeuralLayersAndNeuralInputs().add(outer);

    NeuralOutputs finalOuts = new NeuralOutputs();
    finalOuts.setNumberOfOutputs(BigInteger.valueOf(outputfieldNames.length));
    for (int j = 0; j < outputfieldNames.length; j++) {
      NeuralOutput output = new NeuralOutput();
      output.setOutputNeuron("" + (j + inputfieldNames.length + hiddenSize));
      DerivedField der = new DerivedField();
      der.setDataType(DATATYPE.DOUBLE);
      der.setOptype(OPTYPE.CONTINUOUS);
      NormContinuous nc = new NormContinuous();
      nc.setField(outputfieldNames[j]);
      nc.setOutliers(OUTLIERTREATMENTMETHOD.AS_IS);
      LinearNorm lin1 = new LinearNorm();
      lin1.setOrig(0);
      lin1.setNorm(-outputMeans[j] / outputStds[j]);
      nc.getLinearNorms().add(lin1);
      LinearNorm lin2 = new LinearNorm();
      lin2.setOrig(outputMeans[j]);
      lin2.setNorm(0);
      nc.getLinearNorms().add(lin2);
      der.setNormContinuous(nc);
      output.setDerivedField(der);
      finalOuts.getNeuralOutputs().add(output);
    }

    nnet.getExtensionsAndNeuralLayersAndNeuralInputs().add(finalOuts);

    pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(nnet);

    return pmml;
  }