Menu

confused with Rulechain in the source code

2018-09-03
2022-04-01
  • supermanheng21

    supermanheng21 - 2018-09-03
       public void apply(List<Node> acuList, RuleContext ctx, Language language) {
            ruleChain.apply(acuList, ctx, language);
            for (RuleSet ruleSet : ruleSets) {
                if (ruleSet.applies(ctx.getSourceCodeFile())) {
                    ruleSet.apply(acuList, ctx);
                }
            }
        }
    

    what is the difference between rulechian.apply() and ruleSet.apply(), why to use rulechain here?

     

    Last edit: supermanheng21 2018-09-03
  • supermanheng21

    supermanheng21 - 2018-09-03

    another question is why the src code gets RuleSets twice? why not use the first RuleSets? thank you very much

     
    • Juan Sotuyo

      Juan Sotuyo - 2018-09-03

      The RuleChain improves performance of rule application by allowing several
      rules to run on a single trasversal of the AST.

      Unless your rule depends on the order in which the nodes are visited
      (shouldn't!) it will make no difference for the rule, but will improve
      runtime performance.

      another question is why the src code gets RuleSets twice? why not use the
      first RuleSets? thank you very much

      I have no idea at what point of the source code you are referring to.

      On Mon, Sep 3, 2018 at 6:32 AM supermanheng21 supermanheng21@users.sourceforge.net wrote:

      another question is why the src code gets RuleSets twice? why not use the
      first RuleSets? thank you very much


      confused with Rulechain in the source code


      Sent from sourceforge.net because you indicated interest in <
      https://sourceforge.net/p/pmd/discussion/188192/>

      To unsubscribe from further messages, please visit <
      https://sourceforge.net/auth/subscriptions/>

       
  • supermanheng21

    supermanheng21 - 2018-09-04

    the first rulesets is created in pmd-core/net/sourceforge/pmd/PMD.doPMD():

    RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration, new ResourceLoader());
            RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(configuration.getRuleSets(), ruleSetFactory);
            if (ruleSets == null) {
                return 0;
            }
    

    the second rulesets is created in pmd-core/net/sourceforge/pmd/processor/AbstractPMDProcessor.java

    public void processFiles(RuleSetFactory ruleSetFactory, List<DataSource> files, RuleContext ctx,
                List<Renderer> renderers) {
            RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport());
            configuration.getAnalysisCache().checkValidity(rs, configuration.getClassLoader());
            SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
    
            for (DataSource dataSource : files) {
                String niceFileName = filenameFrom(dataSource);
    
                runAnalysis(new PmdRunnable(dataSource, niceFileName, renderers, ctx, rs, processor));
            }
    
            // render base report first - general errors
            renderReports(renderers, ctx.getReport());
    
            // then add analysis results per file
            collectReports(renderers);
        }
    

    why not just use the first ruleset all the time?

     
    • Juan Sotuyo

      Juan Sotuyo - 2018-09-04

      Oh, that's because RuleSet / Rules are not required to be thread-safe, and
      PMD can work on multiple threads. So PMD provides each thread with it's own
      set of rule instances it can work with without locking / worrying.

      On Tue, Sep 4, 2018 at 10:38 AM supermanheng21 supermanheng21@users.sourceforge.net wrote:

      the first rulesets is created in pmd-core/net/sourceforge/pmd/PMD.doPMD():

      ~~~
      RuleSetFactory ruleSetFactory =
      RulesetsFactoryUtils.getRulesetFactory(configuration, new ResourceLoader());
      RuleSets ruleSets =
      RulesetsFactoryUtils.getRuleSetsWithBenchmark(configuration.getRuleSets(),
      ruleSetFactory);
      if (ruleSets == null) {
      return 0;
      }
      ~~~

      the second rulesets is created in
      pmd-core/net/sourceforge/pmd/processor/AbstractPMDProcessor.java

      ~~~
      public void processFiles(RuleSetFactory ruleSetFactory, List<datasource>
      files, RuleContext ctx,
      List<renderer> renderers) {
      RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport());
      configuration.getAnalysisCache().checkValidity(rs,
      configuration.getClassLoader());
      SourceCodeProcessor processor = new
      SourceCodeProcessor(configuration);</renderer></datasource>

          for (DataSource dataSource : files) {
              String niceFileName = filenameFrom(dataSource);
      
              runAnalysis(new PmdRunnable(dataSource, niceFileName,
      

      renderers, ctx, rs, processor));
      }

          // render base report first - general errors
          renderReports(renderers, ctx.getReport());
      
          // then add analysis results per file
          collectReports(renderers);
      }
      

      ~~~
      why not just use the first ruleset all the time?


      confused with Rulechain in the source code


      Sent from sourceforge.net because you indicated interest in <
      https://sourceforge.net/p/pmd/discussion/188192/>

      To unsubscribe from further messages, please visit <
      https://sourceforge.net/auth/subscriptions/>

       
      • supermanheng21

        supermanheng21 - 2018-09-05
        public void processFiles(RuleSetFactory ruleSetFactory, List<DataSource> files, RuleContext ctx,
                    List<Renderer> renderers) {
                **RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport());**
                configuration.getAnalysisCache().checkValidity(rs, configuration.getClassLoader());
                SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
        
                for (DataSource dataSource : files) {
                    String niceFileName = filenameFrom(dataSource);
        
                    **runAnalysis(new PmdRunnable(dataSource, niceFileName, renderers, ctx, rs, processor));**
                }
        
                // render base report first - general errors
                renderReports(renderers, ctx.getReport());
        
                // then add analysis results per file
                collectReports(renderers);
            }
        

        I think this line "RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport());" doesn't create thread-local rulesets, instead, it creates new rulesets from the ruleset .xml files(which is identical to the first rulesets), but this line "runAnalysis(new PmdRunnable(dataSource, niceFileName, renderers, ctx, rs, processor))" creates thread-local rulesets from rs. if we don't create rs from .xml file, instead we use the first ruleset to create the thread-local ruleset, we will save some time. is this right?

         

        Last edit: supermanheng21 2018-09-05
        • Juan Sotuyo

          Juan Sotuyo - 2018-09-05

          You may be on to something... it's quite some time since I last checked
          that code but it's plausible.

          I'm currently reworking ruleset parsing as part of
          https://github.com/pmd/pmd/issues/724. I'll make sure to look into this as
          part of that.

          Thanks for your insight.

          On Tue, Sep 4, 2018 at 11:14 PM supermanheng21 supermanheng21@users.sourceforge.net wrote:

          ~~~
          public void processFiles(RuleSetFactory ruleSetFactory, List<datasource>
          files, RuleContext ctx,
          List<renderer> renderers) {
          RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport());
          configuration.getAnalysisCache().checkValidity(rs,
          configuration.getClassLoader());
          SourceCodeProcessor processor = new
          SourceCodeProcessor(configuration);</renderer></datasource>

              for (DataSource dataSource : files) {
                  String niceFileName = filenameFrom(dataSource);
          
                  **runAnalysis(new PmdRunnable(dataSource, niceFileName,
          

          renderers, ctx, rs, processor));**
          }

              // render base report first - general errors
              renderReports(renderers, ctx.getReport());
          
              // then add analysis results per file
              collectReports(renderers);
          }
          

          ~~~
          I think this line "RuleSets rs = createRuleSets(ruleSetFactory,
          ctx.getReport());" doesn't create thread-local rulesets, it creates new
          rulesets from the ruleset .xml files(which is identical to the first
          rulesets), but this line "runAnalysis(new PmdRunnable(dataSource,
          niceFileName, renderers, ctx, rs, processor))" create thread-local rulesets
          from rs. if we don't create rs from .xml file, instead we use the first
          ruleset to create the thread-local ruleset, we will save some time. is
          this right?


          confused with Rulechain in the source code


          Sent from sourceforge.net because you indicated interest in <
          https://sourceforge.net/p/pmd/discussion/188192/>

          To unsubscribe from further messages, please visit <
          https://sourceforge.net/auth/subscriptions/>

           
          • supermanheng21

            supermanheng21 - 2018-09-05

            thank you for your reply.

             
          • supermanheng21

            supermanheng21 - 2018-09-05

            thank you for your reply.

             
  • g sdf

    g sdf - 2022-04-01

    There is little documentation for these. Other than typeResolution, which
    is the maximum mature and all document is focused on consuming kind facts to install apk.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.